Current Lua downloadable firmware will be posted here

User avatar
By madarax64
#16946 Hello all,
After reading up about MQTT, I decided to implement a small test with my ESP-01 module. I downloaded the Mosquito broker version 1.4.1 for Windows and got that running successfully. Next, I wrote a simple MQTT client program for the ESP itself, and another for my laptop. The test was supposed to be a simple ping-pong parlay: laptop publishes a message to the /ping topic which the ESP is subscribed to, and the ESP (on receiving a message from the /ping topic) publishes to the /pong topic which the laptop is subscribed to. The laptop logs the reception, waits for 30 seconds and sends another message on the /ping topic....
Àfter about five minutes, the MQTT client on the ESP goes offline for no apparent reason. This happened each time I ran the test. I then increased the delay between pings to 60 seconds, and now the MQTT client on the ESP goes offline at about 727 seconds consistently. I can still send commands to the ESP and receive replies after the disconnection, and heap size is usually around 16.5k when the disconnection happens.
I'd really appreciate any help with this. The broker also simply reports a socket error with the ESP client and subsequently disconnects it.
The lua code is given below:

Code: Select allwifi.start.connect()
tmr.delay(2000000)

function handle publish(conn,topic,data)
if data~=nil then
print(data)
m:publish ("/pong”,"hollerback",0,0,function(conn) print("Pong") end)
end
end

m=MQTT.Client("esp",120,”user","pass")
m:on("message”, handlepublish)
m:connect("192.168.1.168", 1883,0,function(conn) print("Connected") end)
m:subscribe("/ping",0,function(conn) print("Subscribe success") end)
m:on("offline", function(conn) print("Offline") end)

User avatar
By madarax64
#20524 Hello El42,
Actually I have found a solution, but I need to give you a little background first...
I switched over to using the ESP8266 Arduino port (using IDE 1.6.4) and tried the exact same thing and ended up with the exame same results...the ESP8266 would simply disconnect from the Mosquitto instance on my laptop after about 180 seconds, rarely ever longer than that. After I enabled WiFi diagnostics over Serial, I noticed that the WiFi stack (if that's the right word) on the chip seemed to disconnect and reconnect to my WiFi network frequently and for no apparent reason, and it seemed that these disconnections were the reason for the MQTT client's peculiar failure. Looking at my router's WiFi settings, the only thing that caught my eye was the fact that it was broadcasting in 802.11g+n mode. I changed the setting to 802.11g mode only, and that seems to have solved my problem. The MQTT client on both NodeMCU and the Arduino port haven't dropped a single time since.
The tl;dr is: Sorry its this reply is a bit long, but you can perhaps try with the Arduino port and see if you have the same problems. If you do, check your router's WiFi mode (is it also in g+n mode or pure g mode) and change it to pure g mode if applicable. This seemed to solve my problem.
Ps: Use Lmroy's PubSubClient library not the vanilla Arduino version. It is here: https://github.com/Imroy/pubsubclient

Please let me know how it goes.
Good luck!
User avatar
By El42
#20584 Hello madarax64,
I've a nodeMCU development board and no experience with Arduino IDE.
Could you give me any reference to install and use Arduino IDE into nodeMCU development board?

Thank you