B4R Question How to set mqttconnectoptions?

rbghongade

Active Member
Licensed User
Longtime User
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:
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.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
How are you testing it?

Is there any device subscribed to the "DEVICE" topic?

Run mosquito in verbose mode (-v) and disconnect the ESP8266. It will take a few seconds for it to actually disconnect and the last will will be sent.

Logs:
1477987591: Sending PINGRESP to arduino
1477987613: Client arduino has exceeded timeout, disconnecting.
1477987613: Socket error on client arduino, disconnecting.
1477987613: Sending PUBLISH to pc (d0, q0, r0, m0, 'will', ... (4 bytes))
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
I am having a client running on same laptop as the broker subscribed to topic of last will.. I simply disconnected the esp8266 and was expecting to receive the payload"OFFLINE". But it is not happening. Shall try running broker in verbose mode.
Another problem I am facing is that the published messages are retained and after some considerable time the broker publishes all the retained messages and the message queue gets long. So the current data reaches very late. If you could update the publish function with an option to retain/not retain the message this problem may be sorted out.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
Thanks , verbose mode shows the expected response, I guess the timeout delay being more , I was expecting the lastwill response too early!
As to the second problem, what could be the reason that after some time the updating stalls?
 
Upvote 0
Top