After a few days break wherein work got in the way of interests (not a good thing) I got up early this morning, determined to do some work on my ESP-01 boards while I have a clear weekend ahead of me. I checked for updates for MQTT and there are none – which is fine – that indicates that nothing has gone wrong this week.
On a whim I decided to go check the LUA site. I already new that the latest MQTT had been imported from conversations with Minh who’s been doing the MQTT work but I was delighted to see signs that also some more work had been done on integration with Eclipse.
Every single time I’ve tried to install the nodemcu LUA code into Eclipse there has been a problem – something not found, some path missing, some issue taking hours to resolve.
Yet this morning I downloaded the latest ZIP file (updated yesterday) and imported it into Eclipse. CLEAN – COMPILE – FLASH… not a hiccup – it worked perfectly. Not only that but when I tried powering Lua up – it came up straight away without any hint of an issue. This is new for me as I’ve always had some trouble or other. Magic.
Though it is possible to debug Lua code using the simple terminal you can install into the Eclipse environment, I did develop my own serial terminal and then subsequently discovered ESPLorer.
I powered up ESPlorer and checked the available RAM on my newly installed Lua installation on an ESP-01 (I’m still waiting for my new white board to turn up from China with all the pins available – meanwhile I have ESP-01, ESP-03 and ESP-12 boards to play with).
A quick node.heap() call showed that we’re starting off with 22,144 bytes of working RAM which is a good improvement on the earlier versions especially as MQTT is now incorporated.
First things first, I told the unit about my router and then my MQTT broker which regular readers will know is sitting on a Synology DiskStation and has been sitting there working flawlessly for weeks now despite my attempts to bombard it to death with messages.
I noted when using wifi.sta.getip() that the unit remembered my router from previous experiments (I wish someone would produce a nice graphical memory map to show where these various elements are stored and how to tinker with them). So that was easy.
I followed this example – obviously filling in the bits that apply to my installation..
— 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
— 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)
— you can call m:connect again
(again ignore the IP address and port – I filled in my own) – I ran this and… connected no problem. All seemed well. I had subscribed to “test” rather than “/topic” and had commented out the publish for now.
I tried publishing “test” to see what would happen – nothing. But then I guessed as the last line was “m:close()” I figured it reasonable that the ESP-01 would not be listening and so called the connect line again – after all – there is a comment in there “you can call m:connect again”.
The result (passwords have been changed to protect the innocent):
> m = mqtt.Client("peteluatestid", 120, "xxxxxx", "yyyyyyy")
NodeMCU 0.9.5 build 20150123 powered by Lua 5.1.4
lua: cannot open init.lua
Yes that’s a crash. Oh dear, this is not the first time I’ve seen Lua just die. I can handle error messages but this tendency to just simply reboot…
Meanwhile, my experiments with programming in C, using MQTT directly have, thanks to Minh Tuan’s constant vigilance in fixing things, produced rock-solid results. Indeed I’ve been away since last weekend and I turned off a perfectly working MQTT temperature sensor in order to give Lua another go. I thought the one-wire library in Lua might make the transition to a Dallas DS18B20 a little easier but I’m not going to start another round of failed experiments while Lua so easily crashes.
Incidentally, I had several attempts with this Lua installation – rebooting several times along the way. When I finally managed to get the whole thing to work and in commenting out the CLOSE – as you need the connection open to receive anything, I noted my wonderful 22k of RAM had dropped down to 17.2K. I really do wish they’d put a little more RAM on that chip.
Having checked that the compilation had included all the modules (app/include/user_config.h) which as far as I can tell the default combination SHOULD do… I thought I’d try the DS18B20 module – the idea being to send an MQTT request – and get an MQTT message back with the temperature in it.
I thought I would avoid experiment and merely check the temperature first. I duly attached a working SD18B20 unit and pull up resistor to GPIO2 (that’s input 4 according to the table).
The code looked simple enough..
t = nil
ds18b20 = nil
stdin:1: module ‘ds18b20’ not found:
no field package.preload[‘ds18b20’]
no file ‘ds18b20.lua’
no file ‘ds18b20.lc’
Thoughts? Am I missing the point here?