MQTT Disconnect

Hello,
We are making some tests with an ewon flexy to push some data on an Azure iot hub.
The script used is “Azure IOT Hub/Central Live Data”.
It is working but after some time, we get unexpected disconnections.

Here is the real time logs :

Do you have any idea where it could come from?

Unexpected disconnections are fairly common with any internet communication. How often does this occur and does it reconnect quickly?

Do you see any corresponding logs in the Event Logs, like an internet disconnection?

Thank you for your answer.

It is happening systematically after 2 or 3 hours.
The problem is it never reconnects. On the Realtime Logs, the event “Disconnected (unexpected)” is the last one visible (it disconnected yesterday, 23/02/2023 17:21:47):


On the script, it keeps trying to publish tags but without success:

The internet connection seems fine and the picture is green :
chrome_fxl1weBlt7
On the event logs, there is the mqtt broker disconnection but I can’t see any internet disconnections:

If I stop the script, and start it again, it will be ok for next few hours, but I would like this to be automatic.
The disconnection is effectively not a big problem and seems common for any internet communication, but the problem is it keeps being disconnected while the script execution is not restarted.

Hi @Florian_J,

Can you share the script you’re using?

The best would be to make a backup of the Ewon and check the box “include support files” as this will give us the script, the settings, and the logs. It will automatically be hidden from public view.

The script is here (i have taken it from Ewon BASIC programs ):

Rem --- eWON start section: Init Section
eWON_init_section:
Rem --- eWON user (start)
 
//-->NAME : Azure IOT Hub/Central Live Data
//-->VERSION : 1.1
//-->DESCRIPTION
//This program allows the Flexy to push its instantaneous Tag values to Azure IOT Hub/IOT Central (including Text buffer in case of communication loss).
//Device Authentication is done through SAS Token/Symmetric Key.
//-->END DESCRIPTION
//
//-->REQUIREMENTS
//You will have first to generate the SAS token for your Flexy on <a target="_new" href="https://tools.ewonsupport.biz/AzureSASToken/">https://tools.ewonsupport.biz/AzureSASToken/</a>
//Before starting the script, make sure to 
//- Create at least one Tag and set them into one of the selected Tag Group(s) - See Program Parameters below.
//- Connect your Flexy to the Internet (Port 8883 must be open)
//Need help ? Check our <a target="_new" href="Scripts/video/Azure IOT Hub_Central Live Data.MP4">video tutorial </a>
//-->END REQUIREMENTS
//-->CONFIGURATION
CONF_DeviceID$ = "Flexy205-ClxDemo-1825023824"//COMMENT:Azure IOT Hub Device ID
CONF_SASToken$="SharedAccessSignature sr=xxxxxxxx"//COMMENT:Azure IOT Hub Device SAS Token (See above requirements)
CONF_GROUPA$ = "1"//COMMENT:Select Tags from group A -> 0 or 1->1
CONF_GROUPB$ = "1"//COMMENT:Select Tags from group B -> 0 or 1->1
CONF_GROUPC$ = "1"//COMMENT:Select Tags from group C -> 0 or 1->1
CONF_GROUPD$ = "1"//COMMENT:Select Tags from group D -> 0 or 1->1
CONF_FullPushTime$ = "2"//COMMENT:Time interval to push all values (in seconds) -> time in seconds ->60
CONF_DeviceTopic$ = "devices/"+CONF_DeviceID$+"/messages/events/"
Pos_Start_CONF_IotHubURL% = INSTR 1, CONF_SASToken$, "sr="
Pos_End_CONF_IotHubURL% = INSTR 1, CONF_SASToken$, "&"
CONF_IotHubURL$ = CONF_SASToken$(Pos_Start_CONF_IotHubURL% + 3 TO Pos_End_CONF_IotHubURL% -1)
CONF_FullPushTime% = VAL CONF_FullPushTime$
MsgDBPath$ = "/usr/mqttmsg_db.txt"
MaxMsgBufferFileSize% = 10000000 //max size of file used to save msg in bytes
CLS
Last_ConnStatus% = -1
FullPushLastTimeSec% = 0
ChangePushLastTimeSec% = 0

MQTT "OPEN",CONF_DeviceId$,CONF_IotHubURL$ 
MQTT "SETPARAM","PORT","8883"
MQTT "SETPARAM", "LOG", "1"
MQTT "SETPARAM", "KEEPALIVE", "20"
MQTT "SETPARAM", "TLSVERSION", "tlsv1.2"
MQTT "SETPARAM", "PROTOCOLVERSION", "3.1.1"
MQTT "SETPARAM", "CAFILE","/usr/BaltimoreCyberTrustRoot.pem"
MQTT "SETPARAM","USERNAME",CONF_IotHubURL$+"/"+CONF_DeviceId$+"/api-version=2016-11-14"
MQTT "SETPARAM","PASSWORD",CONF_SASToken$
SETSYS PRG,"RESUMENEXT",1  //Continue in case of error at MQTT "CONNECT"
MQTT "CONNECT"
ErrorReturned% = GETSYS PRG,"LSTERR"
IF ErrorReturned% = 28 THEN @Log("WAN interface not yet ready. MQTT Launched anyway...")
SETSYS PRG,"RESUMENEXT",0
//a = table with 2 columns : one with the negative indice of the tag and the second one with 1 if the values of the tag change or 0 otherwise
//Record the Tag ONCHANGE events into an array.
//Allows to post only values that have changed
NB%= GETSYS PRG,"NBTAGS"
DIM a(NB%,2)
FOR i% = 0 TO NB%-1
  k%=i%+1
  SETSYS Tag, "load",-i%
  a(k%,1)=-i%
  a(k%,2) = 0
  GroupA$= GETSYS TAG,"IVGROUPA"
  GroupB$= GETSYS TAG,"IVGROUPB"
  GroupC$= GETSYS TAG,"IVGROUPC"
  GroupD$= GETSYS TAG,"IVGROUPD"
  IF GroupA$ = "1" And CONF_GroupA$= "1" THEN Onchange -i%, "a("+ STR$ k%+",2)= 1"
  IF GroupB$ = "1" And CONF_GroupB$= "1" THEN Onchange -i%, "a("+ STR$ k%+",2)= 1"
  IF GroupC$ = "1" And CONF_GroupC$= "1" THEN Onchange -i%, "a("+ STR$ k%+",2)= 1"
  IF GroupD$ = "1" And CONF_GroupD$= "1" THEN Onchange -i%, "a("+ STR$ k%+",2)= 1"
NEXT i%
//Set and Start Timer
MQTTProcess_CycleTime% = 1
ONTIMER 4, "GOTO MqttProcess"
TSET 4, MQTTProcess_CycleTime%
END
//Publish a message. If not connected, save the message in a file
FUNCTION MQTT_Publish($json$)
  $ConnStatus% = MQTT "STATUS"
  IF $ConnStatus% = 5 THEN
    MQTT "PUBLISH",CONF_DeviceTopic$,$json$, 0, 0
    PRINT "[MQTT SCRIPT] Message "+ $json$(1 To 20) +"... sent to topic " + CONF_DeviceTopic$ 
  ELSE //If not connected --> Save message in file
    @MQTT_SaveMQTTEvent($json$)
  ENDIF
ENDFN
//Check if the Connection is recovered and 
//if yes, send the messages contained in the buffer file to broker
FUNCTION MQTT_CheckConnection()
  $ConnStatus% = MQTT "STATUS"
  IF Last_ConnStatus% <> $ConnStatus% THEN
    IF $ConnStatus% = 5 THEN //Connection is back online
      @Log("Ewon Flexy connected to Broker")
      @MQTT_SendSavedEvents() //Parse Saved Messages and publish them
    ELSE
      @Log("Ewon Flexy disconnected from Broker")
    ENDIF
    Last_ConnStatus% = $ConnStatus%
  ENDIF
ENDFN
//Save message in Txt file.
FUNCTION MQTT_SaveMQTTEvent($Msg$)
  $filesize% = FS "size", MsgDBPath$
  IF $filesize% < MaxMsgBufferFileSize% THEN
     OPEN "file:" + MsgDBPath$ FOR BINARY APPEND AS 1
     PUT 1, $Msg$ + CHR$(10)
     CLOSE 1
     @LOG("Flexy not connected - message saved")
  ELSE
     @LOG("Flexy not connected - message not saved, buffer file is full")
  ENDIF
ENDFN
// Save messages contained in the txt file to the broker.
FUNCTION MQTT_SendSavedEvents()
  $FileExist% = FS "isFile", MsgDBPath$
  IF $FileExist% = -1 THEN
    RETURN
  ENDIF
  $NbrMsg% = 0
  OPEN "file:" + MsgDBPath$ FOR BINARY INPUT AS 1
  $Line$ = ""
  
$ReadNext:
  IF EOF 1 THEN GOTO $ReadDone
  
  $CHAR$ = GET 1,1
  IF $CHAR$ = CHR$(10) THEN
    MQTT "PUBLISH", CONF_DeviceTopic$ , $Line$, 0,0
    $Line$ = ""
    $NbrMsg% = $NbrMsg% + 1
  ELSE
    $Line$ = $Line$ + $CHAR$
  ENDIF
  GOTO $ReadNext
$ReadDone:
  CLOSE 1
  @LOG(STR$ $NbrMsg% + " saved messages published.")
  ERASE MsgDBPath$
ENDFN
//Compute the right time format for AZURE
FUNCTION MQTT_GetJsonTime$()
  $a$ = Time$
  $MQTT_GetJsonTime$ = $a$(7 To 10) + "-" + $a$(4 To 5) + "-" + $a$(1 To 2) + " " + $a$(12 To 13)+":"+$a$(15 To 16)+":"+$a$(18 To 19)
ENDFN
//Build json with all Tags
FUNCTION MQTT_ComputeJsonAllTags$()
  
  $json$ =         '{'
  $NBTags% = GETSYS PRG,"NBTAGS"
  
  FOR $i% = 0 TO $NBTags% -1
      SETSYS TAG, "LOAD",-$i%
      $Tagname$= GETSYS TAG,"NAME"
      $GroupA$ = GETSYS TAG,"IVGROUPA"
      $GroupB$ = GETSYS TAG,"IVGROUPB"
      $GroupC$ = GETSYS TAG,"IVGROUPC"
      $GroupD$ = GETSYS TAG,"IVGROUPD"
      $TagValue$ = GETSYS TAG, "TAGVALUE"
      $Qual$ = GETSYS TAG, "TagQuality" //Qual$ = 65472 when it is ok.
      
      IF $GroupA$ = "1" And CONF_GroupA$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":["' + $TagValue$ + '", "' + $Qual$ +'"],'
      IF $GroupB$ = "1" And CONF_GroupB$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":["' + $TagValue$ + '", "' + $Qual$ +'"],'
      IF $GroupC$ = "1" And CONF_GroupC$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":["' + $TagValue$ + '", "' + $Qual$ +'"],'
      IF $GroupD$ = "1" And CONF_GroupD$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":["' + $TagValue$ + '", "' + $Qual$ +'"],'
     
  NEXT $i%    
  
  $json$ = $json$ +    '"time": "'+ @MQTT_GetJsonTime$() +'"'
  $json$ = $json$ +   '}'
  
  $MQTT_ComputeJsonAllTags$ = $json$ 
  
ENDFN
//Log in event logs and BASIC Console
FUNCTION Log($Msg$)
  LOGEVENT  "[MQTT Azure SCRIPT] " + $Msg$ ,100
  PRINT "[MQTT SCRIPT] " + $Msg$
ENDFN

//Publish just the changed tags
MQTTProcess:
  //Disable timer
  TSET 4,0
  @MQTT_CheckConnection()
  TSET 4, MQTTProcess_CycleTime%
  TimeSecTemp% = GETSYS PRG , "TIMESEC"
  
  IF TimeSecTemp% - FullPushLastTimeSec% >= CONF_FullPushTime% THEN
    FullPushLastTimeSec% = TimeSecTemp%
    @MQTT_Publish(@MQTT_ComputeJsonAllTags$)
    PRINT "[MQTT SCRIPT] All Tags published"
  ENDIF
  
END
Rem --- eWON user (end)
End
Rem --- eWON end section: Init Section

The backup of the ewon is here enclosed :
MOVED TO STAFF NOTE (373 KB)

...