Still struggling with mqtt timings and Lua
Posted: Mon Jan 15, 2018 3:23 am
I have an ESP8266 successfully connecting to wifi, mqtt, monitorng sensors and submitting publish messages, but realised I needed to trap connection errors espcially for field testing where a higher latency link will be in use.
Firstly I had this code which runs without problem but if the connection to mqtt fails it just disappears off to deep sleep.
So I replaced it with this which is now supposed to trap any connection error, but the problem I get is that the error message is displayed even when the connection is successful.
The output on the ESPlorer is as below:
It seems to me that execution is non-linear as the call to sleep function is supposed to after the the other functions have completed (indeed with a slight delay). The connection to the broker is successful and I see the published messages, but the connect function still runs the error callback.
Can anyone offer any suggestions?
Firstly I had this code which runs without problem but if the connection to mqtt fails it just disappears off to deep sleep.
Code: Select all
function module.start()
-- set up MQTT Client
m = mqtt.Client(config.ID, config.KALIVE, config.USER, config.PASW)
-- configure last will and testament keepalive should be at least twice sleep time
m:lwt(config.HEAD .. config.ID .. subtopic, config.LWT, 0, 0)
-- connect to broker
m:connect(config.HOST, config.PORT, 0, 0, function(con)
print ("Connected to Broker")
get-data()
end)
tmr.alarm(6, 300, tmr.ALARM_SINGLE, call_dsleep) -- call sleep module to shutdown after delay to allow for mqtt send to complete
end
So I replaced it with this which is now supposed to trap any connection error, but the problem I get is that the error message is displayed even when the connection is successful.
Code: Select all
local function is_connected()
print ("Connected to Broker")
get_data()
end
function module.start() -- let's roll
m = mqtt.Client(config.ID, config.KALIVE, config.USER, config.PASW) -- set up MQTT Client
m:lwt(config.HEAD .. config.ID .. subtopic, config.LWT, 0, 0) -- configure lwt - keep alive should be at least twice sleep time
m:connect(config.MQTTHOST, config.PORT, 0, 0, is_connected,
function(client, reason) print("Broker connection failed: "..reason)
end)
tmr.alarm(6, 500, tmr.ALARM_SINGLE, call_dsleep) -- complete, shutdown after delay to allow for mqtt send to complete
end
The output on the ESPlorer is as below:
Code: Select all
Connecting to IoT-Net
> IP unavailable, Waiting...0
IP unavailable, Waiting...1
====================================
ESP8266 mode is: 1
Node ID is: 9769087
MAC address is: 5e:cf:7f:95:10:7f
IP is: 192.168.50.195
====================================
Connected to Broker
/Temperature 23.3
/Humidity 38.5
All done, going to sleep for 30 seconds
Broker connection failed: -5
It seems to me that execution is non-linear as the call to sleep function is supposed to after the the other functions have completed (indeed with a slight delay). The connection to the broker is successful and I see the published messages, but the connect function still runs the error callback.
Can anyone offer any suggestions?