I'm using a script to read 6 connected DS18b20 sensors.
I use the init.lua to execute the script.
-- MQTT SETUP --
MQTT_CLIENTID = "esp-8266-Boiler_room"
MQTT_BROKER = "mqttbroker"
MQTT_PORT = 1883
m = mqtt.Client(MQTT_CLIENTID, 60, "", "")
-- HTTP SETUP --
port = 80
srv=net.createServer(net.TCP)
print("loaded...")
t=require("ds18b20")
t.setup(1) --output to GPIO5
addrs=t.addrs()
function get_sensor_Data()
--- Get temps
print("Reading sensors...")
t1 = t.read(addrs[1],t.C)
t2 = t.read(addrs[2],t.C)
t3 = t.read(addrs[3],t.C)
t4 = t.read(addrs[4],t.C)
t5 = t.read(addrs[5],t.C)
t6 = t.read(addrs[6],t.C)
t7 = t.read(addrs[7],t.C)
t8 = t.read(addrs[8],t.C)
print(t1,t2,t3,t4,t5,t6,t7,t8)
end
function report_temps_tmr()
get_sensor_Data()
m:publish("House/Basement/Boiler_room/T1", t1, 0, 0, function(m) end)
m:publish("House/Basement/Boiler_room/T2", t2, 0, 0, function(m) end)
m:publish("House/Basement/Boiler_room/T3", t3, 0, 0, function(m) end)
m:publish("House/Basement/Boiler_room/T4", t4, 0, 0, function(m) end)
m:publish("House/Basement/Boiler_room/T5", t5, 0, 0, function(m) end)
m:publish("House/Basement/Boiler_room/T6", t6, 0, 0, function(m) end)
print("Sent MQTT Message")
end
m:on("connect", function(m)
-- Serial status message
print ("\n\n", MQTT_CLIENTID, " connected to MQTT broker ", MQTT_BROKER,
" on port ", MQTT_PORT, "\n\n")
-- Subscribe to the topic where the ESP8266 will get commands from
m:subscribe("House/Basement/Boiler_room/+/CMD", 0, function(m)
print("Subscribed to House/Basement/Boiler_room/+/CMD Topic")
end)
end)
m:on("offline", function(m)
print ("\n\nDisconnected from broker")
print("Heap: ", node.heap())
end)
-- execute first read to get rid of "85" value
for s,v in pairs(addrs) do
t.read(addrs[s],t.C)
end
m:connect(MQTT_BROKER ,MQTT_PORT, 0, 1)
tmr.alarm(0, 60000, tmr.ALARM_AUTO, function() report_temps_tmr() end)
srv:listen(port,function(conn)
--conn:on("receive",function(conn,payload)
--print(payload)
get_sensor_Data()
conn:send(
'HTTP/1.1 200 OK\nContent-Type: text/html\n\n' ..
t1..',\r\n' ..
t2..',\r\n' ..
t3..',\r\n' ..
t4..',\r\n' ..
t5..',\r\n' ..
t6..',\r\n' ..
t7..',\r\n' ..
t8,
print("Data sent")
)
--end)
conn:on("sent",function(conn) conn:close() end)
end)This script works without a problem so i decided to clean it up a bit and upgrade it to:
-- MQTT SETUP --
MQTT_CLIENTID = "esp-8266-Boiler_room"
MQTT_BROKER = "mqttbroker"
MQTT_PORT = 1883
m = mqtt.Client(MQTT_CLIENTID, 60, "", "")
-- HTTP SETUP --
port = 80
srv=net.createServer(net.TCP)
print("loaded...")
t=require("ds18b20")
t.setup(1) --output to GPIO5
addrs=t.addrs()
function get_sensor_Data_http()
--Get temps
print("Reading sensors...")
t1 = t.read(addrs[1],t.C)
t2 = t.read(addrs[2],t.C)
t3 = t.read(addrs[3],t.C)
t4 = t.read(addrs[4],t.C)
t5 = t.read(addrs[5],t.C)
t6 = t.read(addrs[6],t.C)
t7 = t.read(addrs[7],t.C)
t8 = t.read(addrs[8],t.C)
print(t1,t2,t3,t4,t5,t6,t7,t8)
end
function get_sensor_Data()
--- Get temps but only from connected sensors
print("Reading sensors...")
for s,v in pairs(addrs) do
print (t.read(addrs[s],t.C))
end
end
function report_temps_tmr_all()
for i=1,8,1 do
m:publish("House/Basement/Boiler_room/T"..i, t.read(addrs[i],t.C), 0, 0, function(m) end)
print("Sent MQTT Message T"..i)
end
print("Done")
end
function report_temps_tmr()
for s,v in pairs(addrs) do
m:publish("House/Basement/Boiler_room/T"..s, t.read(addrs[s],t.C), 0, 0, function(m) end)
print("Sent MQTT Message T"..s)
end
print("Done")
end
m:on("connect", function(m)
-- Serial status message
print ("\n\n", MQTT_CLIENTID, " connected to MQTT broker ", MQTT_BROKER,
" on port ", MQTT_PORT, "\n\n")
-- Subscribe to the topic where the ESP8266 will get commands from
m:subscribe("House/Basement/Boiler_room/+/CMD", 0, function(m)
print("Subscribed to House/Basement/Boiler_room/+/CMD Topic")
end)
end)
m:on("offline", function(m)
print ("\n\nDisconnected from broker")
print("Heap: ", node.heap())
end)
-- execute first read
for s,v in pairs(addrs) do
t.read(addrs[s],t.C)
end
m:connect(MQTT_BROKER ,MQTT_PORT, 0, 1)
tmr.alarm(0, 10000, tmr.ALARM_AUTO, function() report_temps_tmr() end)
srv:listen(port,function(conn)
--conn:on("receive",function(conn,payload)
--print(payload)
get_sensor_Data_http()
conn:send(
'HTTP/1.1 200 OK\nContent-Type: text/html\n\n' ..
t1..',\r\n' ..
t2..',\r\n' ..
t3..',\r\n' ..
t4..',\r\n' ..
t5..',\r\n' ..
t6..',\r\n' ..
t7..',\r\n' ..
t8,
print("Data sent")
)
--end)
conn:on("sent",function(conn) conn:close() end)
end)
But this script causes the ESP to reset when the >> report_temps_tmr()<< function is called.
For debuging purposes i tried to disable the timer and call the function manually via LUALoader , but the function works without a problem.
> report_temps_tmr_all()
Sent MQTT Message T1
Sent MQTT Message T2
Sent MQTT Message T3
Sent MQTT Message T4
Sent MQTT Message T5
Sent MQTT Message T6
Sent MQTT Message T7
Sent MQTT Message T8
Done
> report_temps_tmr()
Sent MQTT Message T1
Sent MQTT Message T2
Done
Even if i stop the timer and restart it it works flawlessly.
But if reset the esp, the moment the timer expires the esp reboots itself.
> node.restart()
> ĆĺÖÔô19@H
ôDŹ@HŕUCá
NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
loaded...
esp-8266-Boiler_room connected to MQTT broker mqttbroker on port 1883
Subscribed to House/Basement/Boiler_room/+/CMD Topic
***timer expired here***
/ĺÖÔô19@H
)ôDŹ0@HŕÔCá
NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4By adding Print statements i managed to pinpoint the moment the esp reboots:
function report_temps_tmr()
print("1")
for s,v in pairs(addrs) do
print("2")
m:publish("House/Basement/Boiler_room/T"..s, t.read(addrs[s],t.C), 0, 0, function(m) end)
print("Sent MQTT Message T"..s)
end
print("Done")
end
Esp prints out 1,2 and then it resets itself.
What is curious is the fact that this only happens with the timer. If i execute this script manually it works normally
Another curiosity is the fact that if i manually execute the script before the timer expires the first time everything works normally.
So i tried and added a call to this script >>get_sensor_Data()<< but it still resets:
Here is the log from LUALoader
> node.restart()
> ĆĺÖÔô19@H
ôDŹ@HŕUCá
NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
loaded...
esp-8266-Boiler_room connected to MQTT broker mqttbroker on port 1883
Subscribed to House/Basement/Boiler_room/+/CMD Topic
Reading sensors...
23
23
/ĺÖÔô19@H
)ôDŹ0@HŕÔCá
NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4Any ideas?