Post your best Lua script examples here

User avatar
By saytinh
#17343
tuanpm wrote:UPDATE: 2015-Jan-17: while waiting NodeMcu published, you can try to play with it here: https://github.com/tuanpmt/nodemcu-firmware
UPDATE: 2015-Jan-18: New source/api more reliable: https://github.com/nodemcu/nodemcu-firmware/tree/mqtt

Hello,
Here are the results of my attempt to add mqtt stack to net module of nodemcu:
Port from Native MQTT client for ESP8266:
viewtopic.php?f=6&t=1031

Firmware:
bin.zip


Code for test:

Code: Select all-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid", 120, "user", "password")

-- setup Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0)

m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)

-- on publish message receive event
m:on("message", function(conn, topic, data)
  print(topic .. ":" )
  if data ~= nil then
    print(data)
  end
end)

-- for secure: m:connect("192.168.11.118", 1880, 1)
m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end)

-- subscribe topic with qos = 0
m:subscribe("/topic",0, function(conn) print("subscribe success") end)

-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, function(conn) print("sent") end)

m:close();
-- you can call m:connect again


Example application
Code: Select allm = mqtt.Client(wifi.sta.getmac(), 120, "user", "password")
m:lwt("/lwt", wifi.sta.getmac(), 0, 0)

m:on("offline", function(con)
     print ("reconnecting...")
     print(node.heap())
     tmr.alarm(1, 10000, 0, function()
          m:connect("192.168.11.102", 1880, 0)
     end)
end)

-- on publish message receive event
m:on("message", function(conn, topic, data)
  print(topic .. ":" )
  if data ~= nil then
    print(data)
  end
end)

tmr.alarm(0, 1000, 1, function()
 if wifi.sta.status() == 5 then
     tmr.stop(0)
     m:connect("192.168.11.102", 1880, 0, function(conn)
          print("connected")
          m:subscribe("/topic",0, function(conn)
               m:publish("/topic","hello",0,0, function(conn) print("sent") end)
          end)
     end)
 end
end)

Regards


Hi tuanpm. Thank so much for your contribution and I need your help now.

My ESP8266-01 module was still running normal before but now it's always restart continuously after I did tried the test as your guide. I used LuaUploader tool as following:

1. First saving init.lua to the module like that:
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config ( "mySSID" , "myPass" ) 
print(wifi.sta.getip())
dofile ( "main.lua")


2. Second saving main.lua to the module. The code as the same as your Example Application.

After that the module continuous restart. I just get the respond like that:
Code: Select allö,3 8ÿD@
cÀâgFàȈ÷¨Hø
NodeMCU 0.9.5 build 20150318  powered by Lua 5.1.4
nil


I also tried re-fash by the nodemcu flash tool but it can not flash now. Is that possible my module is broken?

Please let me know if you have any advice. Thanks
User avatar
By SRG
#19451 Whatever i do, i have

dofile("init.lua")
not enough memory

whereas my program is very short (read through a loop a distance from a sensor - hr-sr04) and publish that value to mqtt (less than 100 lines, including comments).
I have a nodemcu esp8266, with latest firmware (april 2015).

Is it possible to make such a program with LUA on nodemcu or is the available memory too short ?
User avatar
By LEDAero
#19924
SRG wrote:Whatever i do, i have

dofile("init.lua")
not enough memory

whereas my program is very short (read through a loop a distance from a sensor - hr-sr04) and publish that value to mqtt (less than 100 lines, including comments).
I have a nodemcu esp8266, with latest firmware (april 2015).

Is it possible to make such a program with LUA on nodemcu or is the available memory too short ?


If you get 'not enough memory' errors on a simple prog, it usually indicates something is running away inside.

Split your code and do regular heap dumps to see if you are consuming resources.

Without my ESP (extra sensory perception) on, I can't see your code, so debugging it remotely is nearly impossible...
User avatar
By Daniel Castro
#27090 Hi,

i need some help,
I have a problem publishing topics. I can only publish immediately the connection established.
I f i try to publish a few seconds lather, the message don't come to the broker. If i try to connect again then the firmware crashes saying that the connection already exists and reboot.

This is my publish function when the connection is already established:

function pub_online()
MQTT:publish("/"..CLI_ID.."/status/ONLINE",tmr.now(),2,0,function (conn)
print ("Publish ONLINE...")
LAST_PUBLISH = tmr.now()
end)
end

I have tried whit QoS 0, 1 and 2 but never arrived to the broker.

Regards,
DCP