As the title says... Chat on...

User avatar
By biobier
#10864 Hi,

can some one please give me a hint why this script crash the ESP?
It does not do
tmr.alarm(1, 1000, 1, function() if finished == 0 then sendTemp() end end)

Code: Select allprint("Heap0: "..node.heap())
local humi=9999
local h
local temp=9999
local t
local finished=0
PIN = 6 --  data pin, GPIO12

function ReadDHT22()
     print("Heap before DHT22: "..node.heap())
     dht22 = require("dht22_min")
     dht22.read(PIN)
     t = dht22.getTemperature()
     h = dht22.getHumidity()
     if t == nil then         
          print("t is nill! Error Reading!")         
          humi=9999
          temp=9999
     else
          temp=(t/10)
          if h ~= nil then   
               humi=(h/10)
               print("Humidity:    "..humi.."%")
          else         
               print("h is nill! Error Reading!")   
          end
          print("Temperature: "..temp.." deg C")
     end
     dht22 = nil
     package.loaded["dht22_min"]=nil
     h = nil
     t = nil
     print("Heap after DHT22: "..node.heap())
end

function sendTemp()
     print("Heap before sendTemp"..node.heap())   
     if temp ~=9999 then
          m:connect("192.168.0.1", 1883, 0, function(conn)
               print("connected")
                    m:publish("sensors/test/temp",temp,0,0, function(conn)
                         print("sent temp")
                         finished=finished+1
                         temp = nil
                         --tmr.stop(1)
                    end)
                m:close()
          end)
     else
          print("bad temp data!")         
          finished=finished+1
          tmr.stop(0)
     end
     print("Heap after sendTemp"..node.heap())   
end

function sendHumi()
     if humi ~=9999 then
          m:connect("192.168.0.1", 1883, 0, function(conn)
               print("connected")
                    m:publish("sensors/test/humi",humi,0,0, function(conn)
                         print("sent humi")
                         finished=finished+1 
                         humi = nil
                         --tmr.stop(2)
                    end)
                --end)
                m:close()
          end)
     else
          print("bad humi data!")
          finished=finished+1
          tmr.stop(1)
     end
end

m = mqtt.Client(wifi.sta.getmac(), 10)--, "user", "password")
---m:lwt("/lwt", wifi.sta.getmac(), 0, 0)

m:on("offline", function(con)
     print ("offline!") 
     if finished == 2 then
          print ("Clean up!")
          m = nil
          finished = nil
          print ("going to sleep a bit...")
          --node.dsleep(6000000)
     else
          print ("Not ready to sleep yet!") 
     end
     --print("Heap after offline: "..node.heap())
end)

print("Heap1: "..node.heap())

if wifi.sta.status() == 5 then
     print("status ok")
     ReadDHT22()
     --sendTemp()
     --sendHumi()
     tmr.alarm(1, 1000, 1, function() if finished == 0 then sendTemp() end end )     
     tmr.alarm(2, 1000, 1, function() if finished == 1 then sendHumi() end end )         
else
     print("WIFI Status not 5!")
end
print("Heap end: "..node.heap())


Out put is:
NodeMCU 0.9.5 build 20150213 powered by Lua 5.1.4
lua: cannot open init.lua
> dofile('dht2mqtt3.lua')
Heap0: 11776
Heap1: 8664
status ok
Heap before DHT22: 8608
Temperature: 19.7 deg C
Heap after DHT22: 5864
Heap end: 5880
> c_ÇÏRSöJSúfJSúfêá

NodeMCU 0.9.5 build 20150213 powered by Lua 5.1.4
lua: cannot open init.lua