Left here for archival purposes.

User avatar
By maw
#42142 Hello gentlemen,
What am I doing wrong? See following lua code. It works, but only for few minutes. I tested various firmware versions also various branches, but results are approximately the same. Nodemcu hangs after few minutes. It should work as an external thermometer but becouse of stability it is unusable. :cry:

file: main.lua
Code: Select allwifi.setmode(wifi.STATION)
wifi.setphymode(wifi.PHYMODE_N)
wifi.sta.config('WIFI_SSID', 'wifi_passwd')
wifi.sta.connect()
wifi.sta.setip({ip='192.168.2.254',netmask='255.255.255.0',gateway='192.168.2.1'})
function loop()
 if wifi.sta.status() == 5 then
  m = mqtt.Client('nodemcu',120,'mqtt_user','mqtt_password')
  m:connect('192.168.2.2', 1883, 0, 1, function(conn)
   tmr.stop(0)
   local status, temp, humi, temp_dec, humi_dec = dht.read(4)
   local temperature = string.format("%d.%03d", math.floor(temp), temp_dec)
   local humidity = string.format("%d.%03d", math.floor(humi), humi_dec)
   local a = {}
   a["temperature"] = temperature;
   a["humidity"] = humidity;
   m:publish("/nodemcu/dht22", cjson.encode(a) ,0,0, function(conn)
    m:close()
    wifi.sta.disconnect()
    node.dsleep(60000000)
   end)
  end)
 end
end
tmr.alarm(0, 500, 1, function() loop() end)


file: init.lua
Code: Select alldofile('main.lua')
collectgarbage()


And this is the output from serial console (my latest attempt,it is not so much helpful):
Code: Select allNodeMCU custom build by frightanic.com
    branch: master
    commit: c8037568571edb5c568c2f8231e4f8ce0683b883
    SSL: false
    modules: file,gpio,mqtt,net,node,tmr,uart,wifi
 build     built on: 2016-02-29 17:42
 powered by Lua 5.1.4 on SDK 1.4.0
> 0�~?�4�!� [�$��2: �� �� O�:��>�� �R
 
NodeMCU custom build by frightanic.com
    branch: master
    commit: c8037568571edb5c568c2f8231e4f8ce0683b883
    SSL: false
    modules: file,gpio,mqtt,net,node,tmr,uart,wifi
 build     built on: 2016-02-29 17:42
 powered by Lua 5.1.4 on SDK 1.4.0
> 0�~?�4�!� [�$��2: �� �� O�:��>�� �R
 
NodeMCU custom build by frightanic.com
    branch: master
    commit: c8037568571edb5c568c2f8231e4f8ce0683b883
    SSL: false
    modules: file,gpio,mqtt,net,node,tmr,uart,wifi
 build     built on: 2016-02-29 17:42
 powered by Lua 5.1.4 on SDK 1.4.0
> 0�~?�4�