I have doing this simple code to count led impulses from my electric power grid counter.
The problem is that the esp reboot in the end.
If i remove the last line of code, run it and send "m:connect("rpiserver",1883,0,1)" in the LuaLoader everything runs ok.
Any ideas?
What i'm doing wrong?
local pin = 1
local min_pw_ms = 250
local upload_rate = 20
local pulse_detected = 0
local timestamp = 0
local counter = 0
local watts = 0
local deviceID = "8"
gpio.mode(pin, gpio.INT)
m=mqtt.Client("electnode1",60,"","")
m:on("connect",function(conn)
print("connection "..node.heap())
tmr.alarm(0, upload_rate * 1000, 1, maintask);
tmr.alarm(1, min_pw_ms, 1, pulsetask);
print("...done")
end )
m:on("offline", function(conn)
print("disconnect from broker...")
tmr.stop(0);
tmr.stop(1);
print(node.heap())
print("...done")
end)
m:on("message",function(conn, topic, data) print("msg received:"..topic.." : "..data) end )
function pin1up(level)
pulse_detected = 1
tmr.wdclr()
end
function maintask()
tmr.wdclr()
print("Counter is:"..counter)
watts = counter*60
print("Watts is:"..watts)
print("Uploading to server...")
local pl="{ \"idx\": "..deviceID..", \"svalue1\": \""..watts.."\"}";
m:publish("/domoticz/in",pl,0,0)
tmr.wdclr()
counter=0;
print("...done")
end
function pulsetask()
tmr.wdclr()
timestamp = timestamp + 1
if pulse_detected == 1 then
counter = counter + 1
pulse_detected = 0
end
end
gpio.trig(pin, "up", pin1up)
m:connect("rpiserver",1883,0,1)