Niemand die me kan helpen?
Ik heb nu een quick-n-dirty methode waarmee het functioneel wel werkt, maar er zitten nog schoonheidsfoutjes in welke ik er niet uit krijg. Zie mijn init.lua, pas als netwerk actief is wordt het 2e script gestart (lamp.lua). Als deze 1x gestart wordt, werkt alleen "srv:listen(43333,function(conn)" gedeelte, en niet de "gpio.trig(pinSwitch, 'both', onChange)". Als ik in ESPlorer de lamp.lua nog een keer start, werkt het wel.
Daarom in de init.lua wordt lamp.lua 2x gestart. Echter kreeg ik een PANIC omdat ik niet voor een tweede keer een TCP server mag starten (vandaar de check "if srv==nil then srv=net.createServer(net.TCP) end")
Het werkt nu.... maar ik wil dat de dim-waarde (0-31) bewaard wordt. Stel met domotica wordt de lamp op 25 (80%) gezet, dan wil ik dat als de lamp met de schakelaar uitgezet wordt, en daarna weer aangezet wordt, dat de lamp dezelfde dim-waarde gebruikt. Echter lijkt het wel of het twee verschillende waardes zijn, want de CUR welke in het
srv gedeelte wordt ingesteld "local CUR = (LAMP_target/33)" is niet bekend in de onChange functie
init.lua
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
| wifi.setmode(wifi.STATION)
wifi.sta.config("DivePassion","hebikniet")
tmr.alarm(0, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(0)
print(wifi.sta.getip())
dofile("lamp.lua")
tmr.delay(5000000)
dofile("lamp.lua")
end
end) |
lamp.lua
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
| -- output
pinLamp=3
pinSwitch=4
pwm.setup(pinLamp, 1000, 005)
pwm.start(pinLamp)
-- input/schakelaar
gpio.mode(pinSwitch, gpio.INT)
gpio.trig(pinSwitch, 'both', onChange)
-- variabelen
local switch = false
local dim = false
local MIN = 0
local MAX = 31
local CUR = 0
local LAMP_current=0
local LAMP_target=0
local Fadetime=2500
local Stepcounter=0
local PosStepcounter=0
local DimTimer=0
pwm.setduty(pinLamp, LAMP_current)
-- start code bediening via schakelaar
function onChange ()
print('CUR:'..CUR)
-- schakelaar ingedrukt
if gpio.read(pinSwitch) == 1 then
switch = true
tmr.alarm(1, 300, 1, function()
switch = false
if dim then
if (CUR < MAX) then
CUR = CUR+1
LAMP_target = CUR * 33
LAMP_current = LAMP_target
pwm.setduty(pinLamp, LAMP_target)
print(LAMP_target..' ('..CUR..')')
end
else
if (CUR > MIN) then
CUR = CUR-1
LAMP_target = CUR * 33
LAMP_current = LAMP_target
pwm.setduty(pinLamp, LAMP_target)
print(LAMP_target..' ('..CUR..')')
end
end
end)
end
-- schakelaar los
if gpio.read(pinSwitch) == 0 then
tmr.stop(1)
-- schakelaar korter dan 0,5s ingedrukt
if switch then
switch = false; -- reset switch status
if LAMP_target == 0 then
if CUR == 0 then CUR = 16 end
LAMP_target = CUR * 33
LAMP_current = CUR * 33
pwm.setduty(pinLamp, LAMP_target)
else
LAMP_target = 0
LAMP_current = 0
pwm.setduty(pinLamp, LAMP_target)
end
print('schakelaar'..LAMP_target)
else
-- bepalen of de volgende keer omhoog of omlaag gedimt moet worden
if dim then
dim = false
else
dim = true
end
end
end
end
-- einde code bediening via schakelaar
-- start server input via domoticz
if srv==nil then srv=net.createServer(net.TCP) end
srv:listen(43333,function(conn)
conn:on("receive",function(conn,payload)
print("Input:"..payload)
if string.find(payload,"LAMP") then
LAMP_target = tonumber(string.sub(payload, 13) )
print(LAMP_target)
Stepcounter=(LAMP_target)-(LAMP_current)
if (Stepcounter) < 0 then
PosStepcounter=(Stepcounter)*-1
else PosStepcounter=(Stepcounter)
end
if (PosStepcounter) == 0 then
PosStepcounter=(PosStepcounter)+1
else PosStepcounter=(PosStepcounter)
end
DimTimer=(Fadetime)/(PosStepcounter)
if (DimTimer) == 0 then
DimTimer=(DimTimer)+1
else DimTimer=(DimTimer)
end
local CUR = (LAMP_target/33)
print ('>'..(CUR))
print (Fadetime)
print (Stepcounter)
print (PosStepcounter)
print (DimTimer)
print (LAMP_current)
print (LAMP_target)
tmr.alarm(2, (DimTimer), 1, function()
if LAMP_current < LAMP_target then
LAMP_current = (LAMP_current + 1)
pwm.setduty(pinLamp, LAMP_current)
elseif LAMP_current > LAMP_target then
LAMP_current = (LAMP_current - 1)
pwm.setduty(pinLamp, LAMP_current)
elseif LAMP_current == LAMP_target then tmr.stop(2)
end
end)
end
end)
end) |
Edit:
Gevonden
"gpio.trig(pinSwitch, 'both', onChange)" moet
na de function onChange() komen.
En alle local's moeten eruit