Dear friends,
I wish to implement a sensor network using MQTT over wi-fi with a local broker. Each node has a unique id, which I am using as topic itself. I am using wemos to implement the node. Now I want the mqtt client which acquires the data from the node to know whether the node is online or offline. I know that this can be done using LastWill feature but an unable to use the mqttconnect options. The template for connecting to the broker is the one written by Erel:
B4R code for node:
But this does not seem to work as a sudden disconnection of the node does not trigger the broker to send the LastWill to publish anything!
I must be making a mistake somewhere.
I wish to implement a sensor network using MQTT over wi-fi with a local broker. Each node has a unique id, which I am using as topic itself. I am using wemos to implement the node. Now I want the mqtt client which acquires the data from the node to know whether the node is online or offline. I know that this can be done using LastWill feature but an unable to use the mqttconnect options. The template for connecting to the broker is the one written by Erel:
B4R code for node:
B4X:
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
uid=esp.UniqueID
If wifi.Connect2("***","********") Then
client.ConnectIP(serverIp,serverPort)
Log("Waiting for connection.")
Log("My ip: ", wifi.LocalIp)
Else
Log("Failed to connect to Wifi.")
End If
mqtt.Initialize(client.Stream, serverIp, serverPort, uid, "Mqtt_MessageArrived", "Mqtt_Disconnected")
timer1.Initialize("timer1_Tick",1000)
Connect(0)
End Sub
Sub Connect(unused As Byte)
Dim mo As MqttConnectOptions 'code added by me
mo.Initialize("", "") 'code added by me
mo.SetLastWill("DEVICE", "OFFLINE", 0, False) 'code added by me
mqtt.Connect2(mo) 'code added by me
If mqtt.Connect = False Then
Log("trying to connect again")
CallSubPlus("Connect", 1000, 0)
Return
End If
Log("Connected to broker")
mqtt.Subscribe("ESP", 0)
...
End Sub
But this does not seem to work as a sudden disconnection of the node does not trigger the broker to send the LastWill to publish anything!
I must be making a mistake somewhere.