This Knowledge article describes or shows you an alternative for what to do when Ewon Flexy connected to Azure IoT Hub stops sending data suddenly.
APPLICABLE PRODUCTS
- Ewon Flexy brand.
ISSUE / QUESTION
In this case, we have an Ewon Flexy 205 device with a Basic program connecting to Azure IoT Hub.
Data has stopped being sent several times now, and once we connect to Flexy via Talk2M, it suddenly reboots, and we can't check the previous logs to see what caused the disconnection. This happens at least once a week.
Note here that we don't use the Azure connector, so our main concern is to find out why this is happening and prevent it from happening again.
POSSIBLE CAUSES / ANSWER (Mandatory)
As we know, the BASIC provides a MQTT API, allowing one MQTT client. This client is asynchronous (based on events in background).
The first thing is to check how MQTT is publishing data. This call is asynchronous, it is non-blocking and is executed in the background. Time to connect may vary depending on the device usage, the broker, and the Internet connection. The QoS and retained messages feature rely on the broker capabilities. In this matter, make sure that it is supported. If unsure, "0" should be used for both.
If you use a QoS 1, please change it first and try using QoS 0.
In this case, we change this
MQTT "PUBLISH","devices/"+DeviceId$+"/messages/events/",json$, 1, 0
by
MQTT "PUBLISH","devices/"+DeviceId$+"/messages/events/",json$, 0, 0
Keep in mind that the Ewon itself reinitializes the MQTT connection when it is down.
You start the MQTT connection at the beginning and let it live. The Ewon handles the reconnections automatically.
Also, doing this, is bad.
TryConnect:
SETSYS PRG,"RESUMENEXT",1 //Continue in case of error at MQTT "CONNECT"
MQTT "Open",DeviceId$,IotHubName$ + ".azure-devices.net"
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/DigiCertGlobalRootG2.crt.pem"
MQTT "SetParam","Username",IotHubName$+ ".azure-devices.net/"+DeviceId$+"/api-version=2018-06-30"
MQTT "SetParam","Password",SASToken$
MQTT "Connect"
SETSYS PRG,"RESUMENEXT",0
ONMQTTSTATUS "GOTO HandleConnectionStatus"
End
HandleConnectionStatus:
STATUS% = MQTT("STATUS")
IF STATUS% = 5 THEN
Goto "IsConnected"
ELSE
PRINT "Connection failed. Retrying in 5 seconds…"
FOR i% = 1 TO 5000 ' Delay loop for approximately 5 seconds
NEXT i%
Goto "TryConnect"
ENDIF
We recommend the same as we do for our scripts. (https://tools.ewonsupport.biz/BASIC_Repository/index.html) :
MQTT "OPEN",CONFDeviceId$,CONFIotHubURL$
MQTT "SETPARAM","PORT","8883"
MQTT "SETPARAM", "LOG", "1"
MQTT "SETPARAM", "KEEPALIVE", "20"
MQTT "SETPARAM", "TLSVERSION", "tlsv1"
MQTT "SETPARAM", "PROTOCOLVERSION", "3.1.1"
MQTT "SETPARAM", "CAFILE","/usr/AzureCA.crt"
MQTT "SETPARAM","USERNAME",CONFIotHubURL$+"/"+CONFDeviceId$+"/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
ADDITIONAL INFO (Optional)
- Programming Reference Guide.