MQTT - How to detect communication loss

flexy

#1

Hello,

I do MQTT communication between a FLEXY 205 (Firmware Version 13.0s0) and Azure IoT HUB.

I am looking for a way to detect that there is a problem with the communication (Internet or MQTT) to not lose any transfer.

When I have a communication problem, I will save the data in a file and I will retransfer it later when the communication is back.

Do you have any idea of the information I need to use to make the change based on the state of the communication.

Thank you
Christian


#2

Hi Christian,

Are you worried about communication loss just between the devices or communication with the internet? Also are you using ethernet/wifi/3G/4G?

-Tim


#4

Hi Tim,

I want to detect the possible losses of communication between Flexy and Azure Iot HUB as well as Internet losses.

I have several devices and I use all types of communication (ethernet / wifi / 3G / 4G).

If I use MQTT “PUBLISH” with QoS = 1, is it possible to read the answer to make sure the communication was successful?

Thank you
Christian


#5

Hi Christian,

I found an answer to the Internet loss question, but unfortunately you’ll need to have a Talk2M pro account.

I’m still working on checking the connection between a flexy and a device
-Tim


#6

I think this should help you check connection between your device and the flexy. It checks to see if the device is responding to pings and will send out a message if there is no response.

Let me know if this solution works for you.

-Tim


#7

Hello Tim,

It does not not really what I need :wink:

Here are the instructions that I use to publish with QoS = 1.

QoS% = 1
RMFV% = 0
MQTT "publish", Topic $, $ MessageSend $, QoS%, RMFV%

image

To be able to manage the communications, I would need to know if the server did its acknowledgment (PUBACK) for my PUBLISH before making another PUBLISH. In this way, I could know if the communication is good.

My RealtimeLog:

But, I do not know how to check if the PUBACK is received before doing another PUBLISH.

Thanks again
Christian


#8

Hi Christian,

Sorry about that, I couldn’t quite find an MQTT solution at the time, so I was trying to give another solution that might work in the short term. Could you do a while loop to make it wait until it sees the string received PUBACK to send another Publish?

-Tim


#9

Hi Tim,

Yes, this idea interests me :grinning:

Thanks
Christian


#10

Hi Christian,

I heard from Simon that he is helping you work on some code. Can you let me know if the code he sent works for you?

-Tim


#11

Hi! Could you share this code with me?
Thanks!


#12

Hi Tim,

I’m going to test this week and I’ll get back to you with the result.

Thank you
Christian


#13

T1:

ConnStatus% = MQTT "STATUS"

IF ConnStatus% = 5 THEN

counter% = counter% +1

SETSYS PRG,"RESUMENEXT",1

MQTT "PUBLISH", "/topic/flexy", "Hello From Flexy " + SerNum$, 0,0

PRINT "Message published to the MQTT broker " + STR$ counter%

ErrorReturned = GETSYS PRG,"LSTERR"

Print ErrorReturned

IF ErrorReturned=28 THEN

Print "ERROR PUBLISH"

ENDIF

ELSE

PRINT "Error : Flexy not connected"

ENDIF

END


#15

Hi,

After several tests, here is the code I am currently using:

== Cyclic Section ==
@MQTTConnection()

// Your MQTT Publish here if (MQTTComOK% = 1)

== Init Section ==
MQTTComOK% = 0
@InitMQTTConnection()

== User Section ==
FUNCTION InitMQTTConnection()
  FirstConnection% = 1
  MQTT "close"

  LastEtatWan% = @WANState%()  //use in @MQTTConnection()

  IF LastEtatWan% = 1 THEN
    PRINT "WAN UP"
  ELSE
    PRINT "WAN DOWN"
  ENDIF
ENDFN

FUNCTION WANState%()
  SETSYS INF, "LOAD"
  $IP$ = GETSYS  INF, "VPNIP"
  IF($IP$ = "0.0.0.0") THEN
    $WANState% = 0
  ELSE
    $WANState% = 1
  ENDIF
ENDFN

FUNCTION MQTTConnection()
  $EtatWAN% = @WANState%()

  IF (($EtatWAN% = 1) And (FirstConnection% = 1)) Then
      @openMQTT_Azure()
      ONMQTTSTATUS '@MQTT_StatusChange(MQTT("status"))'
      MQTT "Connect"  
      FirstConnection% = 0
  ENDIF

  IF (($EtatWAN% = 1) And (LastEtatWan% = 0)) Then
      PRINT "WAN is now up"
      LastEtatWan% = 1
  ENDIF

  IF (($EtatWAN% = 0) And (LastEtatWan% = 1)) Then
      PRINT "WAN is now down"
      LastEtatWan% = 0
  ENDIF
ENDFN

FUNCTION MQTT_StatusChange($status%)
  IF $status% = 5 Then
    MQTTComOK% = 1
    PRINT "MQTT connected: "; $status%
  ELSE
    MQTTComOK% = 0
    PRINT "MQTT disconnected:"; $status%
  ENDIF
ENDFN

FUNCTION openMQTT_Azure()
  IoTHub$ = "Your Hub"
  DeviceID$ = "Your Device"
  SASToken$ = "Your SASToken"
  QoS% = 1 //Quality of Service Values
  RMFV% = 0 //Retained Message Feature Values 
  MQTT "open", DeviceID$, IoTHub$+".azure-devices.net"
  MQTT "setparam", "port", "8883"
  MQTT "setparam", "keepalive", "30"
  MQTT "setparam", "username", IoTHub$+".azure-devices.net"+"/"+DeviceID$+"/api-version=2017-11-11"
  MQTT "setparam", "password", SASToken$
  MQTT "setparam", "CAFile", "/usr/CA_Azure/BaltimoreCyberTrustRoot.pem"
  MQTT "setparam", "CleanSession", "1"
  MQTT "setparam", "ProtocolVersion", "3.1.1"
  MQTT "setparam", "TLSVersion", "tlsv1.2"
  MQTT "setparam", "log", "1"
  MQTT "setparam", "MaxInFlight", "50"
  PRINT 'MQTT:"open and setparam" with Azure'
ENDFN

With QoS = 1, my publish data is stored for 50 transactions.


Mqtt-
#16

Hi Christian,

Did you still have any questions for this, or are you all set with MQTT?

-Tim


#17

@Tim_hms,

It’s good for the moment.
I hope in a futur firmware can use the instruction MQTT “status” to follow the status of the MQTT connection. Problem also found by Simon.

Thanks again
Christian