Connecting ewon flexy 202 to serverless cluster

Hello @fahmimff

I took the basic script example from this page to get started and added some configurations, please follow the instructions.

  1. Ensure you are using the latest Ewon Flexy 202 firmware available.

  2. Download and upload it to your device the Let’s Encrypt CA certificate file that must be used with the script. Link to download: https://letsencrypt.org/certs/isrgrootx1.pem

  3. Edit Cluster URL, Username, and Password from the script below copy it in your Ewon Flexy BASIC IDE (Init Section), and save it.

CLS
SETSYS INF, "LOAD"
SerNum$ = GETSYS INF, "SERNUM"
SendTagValues% = 0

//###################################
//########   CONFIG   ###############
//###################################

MQTTBrokerURL$ = "TYPE_YOUR_HIVEMQ_CLOUD_CLUSTER_URL"
MQTTPort$ = "8883"
TopicToPublishOn$ = "/topic/flexy/" + SerNum$ + "/data"
TopicToSubscribe$ = "/topic/flexy/" + SerNum$ + "/command"
MsgToPublish$ = "Hello From Flexy " + SerNum$

//<--Uncomment the below line if you want to send your Tag values in a json format-->
//SendTagValues% = 1

//To update Tags, you can send/publish the json {"tagname1":12.3,"tagname2":4.56}

//###################################
//######## END CONFIG ###############
//###################################


//START SCRIPT
Last_ConnStatus% = 0
//Configure MQTT Connection parameters

CONNECTMQTT:
MQTT "OPEN", SerNum$ , MQTTBrokerURL$
MQTT "SETPARAM", "PORT", MQTTPort$ 
MQTT "SETPARAM", "KEEPALIVE", "60"
MQTT "SETPARAM", "USERNAME", "TYPE_YOUR_HIVEMQ_CLOUD_USERNAME"
MQTT "SETPARAM", "PASSWORD", "TYPE_YOUR_HIVEMQ_CLOUD_PASSWORD"
MQTT "SETPARAM", "TLSVERSION", "tlsv1.2" //Firmware 14.8 Required
MQTT "SETPARAM", "PROTOCOLVERSION", "3.1.1"
MQTT "SETPARAM", "CAFILE", "/usr/isrgrootx1.pem"
MQTT "SETPARAM", "WILLTOPIC", "/willtopic"
MQTT "SETPARAM", "WILLPAYLOAD", "Flexy " + SerNum$ + " is disconnected"
MQTT "SUBSCRIBE",TopicToSubscribe$,1
//Launch the MQTT process in the background
SETSYS PRG,"RESUMENEXT",1  //Continue in case of error at MQTT "CONNECT"
MQTT "CONNECT"
//If an error is raised --> Log a message
ErrorReturned% = GETSYS PRG,"LSTERR"
IF ErrorReturned% = 28 THEN @Log("[MQTT SCRIPT] WAN interface not yet ready")
SETSYS PRG,"RESUMENEXT",0
//When receiving a message from Broker, "GOTO MQTTRECEIVEMSG"
ONMQTT "GOTO MqttRx"
ONTIMER 1, "GOTO SENDDATA"
TSET 1,5 //publish every 5 seconds
END

SENDDATA:
//Read MQTT Connection Status (5 = Connected, other values = Not connected)
ConnStatus% = MQTT "STATUS"
IF Last_ConnStatus% <> ConnStatus% THEN
  IF ConnStatus% = 5 THEN //Connection is back online
    @Log("[MQTT SCRIPT] Flexy connected to Broker")
  ELSE
    @Log("[MQTT SCRIPT] Flexy disconnected from Broker")
  ENDIF
  Last_ConnStatus% = ConnStatus%
ENDIF
//IF Connected --> Publish messages
IF ConnStatus% = 5 THEN //If connected --> Publish

  IF SendTagValues% = 1 THEN
    NB% = GETSYS PRG,"NBTAGS"
    MsgToPublish$ = "["
    FOR i% = 0 TO NB%-1
      SETSYS Tag, "load",-i%
      TagName$ = GETSYS Tag, "Name"
      TagValue$ = GETSYS Tag, "TagValue"
      IF i% = 0 THEN
        MsgToPublish$ = MsgToPublish$ + '{"tag":"' + TagName$ + '","value":"' + TagValue$ + '","time":"' + @GetTime$() + '"}'
      ELSE  
        MsgToPublish$ = MsgToPublish$ + ',{"tag":"' + TagName$ + '","value":"' + TagValue$ + '","time":"' + @GetTime$() + '"}'
      ENDIF
    NEXT i%
    MsgToPublish$ = MsgToPublish$ + "]"
  ENDIF

  MQTT "PUBLISH",  TopicToPublishOn$ , MsgToPublish$, 0,0
  PRINT "[MQTT SCRIPT] Message '" + MsgToPublish$ + "' sent on topic : " +  TopicToPublishOn$
ELSE //If not connected --> Save message in file
  @Log("[MQTT SCRIPT] Flexy not connected")
ENDIF
END

MqttRx:
//Executed when receiving messages from Broker
//Example : {"TestMQTT":0,"string1":"test string","ana1":12.3} to update Tags

   MessageQty%=Mqtt "READ"  //Return the number of pending messages
   IF (MessageQty%>0) Then
      MsgTopic$= MQTT "MSGTOPIC"
      MsgData$ = MQTT "MSGDATA"
      Print "Subscribe Message Received:" +MsgTopic$
      Print "Message:" +MsgData$
      posJson% = 1
      
ReadJsonNext:
      JsonPart$ = @SplitString$(MsgData$,posJson%, ",")
      //PRINT JsonPart$
      posJson% = posJson%+1
      IF JsonPart$ = "" THEN GOTO ReadJsonEnd //no more data to read

      JsonPart$ = JsonPart$ + "}" // to help to detect first and middle elements end

      Pos1stQuote% = INSTR 1,JsonPart$, '"'
      Pos1stQuote% = Pos1stQuote%+1
      Pos2stQuote% = INSTR Pos1stQuote%,JsonPart$, '"'
      TagNameToWrite$ = JsonPart$(Pos1stQuote% TO Pos2stQuote%-1)
   
      Pos1stColumn% = INSTR 1,JsonPart$, ':'
      Pos1stColumn% = Pos1stColumn%+1
      Pos2stBraket% = INSTR Pos1stColumn%,JsonPart$, '}'
      TagValueToWrite$ = JsonPart$(Pos1stColumn% TO Pos2stBraket%-1)
   
      //PRINT TagNameToWrite$ + " - " + TagValueToWrite$
      IF TagValueToWrite$ >"" THEN
        //Print "valid value"
        SETSYS PRG,"RESUMENEXT",1
        SETSYS Tag, "load", TagNameToWrite$
        LastError%= GETSYS PRG,"LSTERR"
        IF LastError% = -4 THEN SETSYS PRG,"RESUMENEXT",0 : GOTO ReadJsonNext
        myType$ = GETSYS TAG, "Type"
        If myType$ = "6" THEN
           TagValueToWrite$ = JsonPart$(Pos1stColumn%+1 TO Pos2stBraket%-2)  //Trim off quote marks
           //PRINT TagValueToWrite$
           SETIO TagNameToWrite$, TagValueToWrite$
        ELSE
          SETIO TagNameToWrite$, VAL TagValueToWrite$
        ENDIF
        SETSYS PRG,"RESUMENEXT",0
      ENDIF
      GOTO ReadJsonNext
ReadJsonEnd:   
      //PRINT MsgData$ + " (Topic: " + MsgTopic$ + ")"
      GOTO MQTTRx
   ENDIF
END


FUNCTION SplitString$ ($StringToParse$,$Pos%, $char$)

  $e% = 1
  $loopnbr% = 0
  $NextItem:
  $f% = INSTR $e% , $StringToParse$ , $char$
  //PRINT STR$ $f%
  //LAST ELEMENT
  IF $f% = 0 THEN 
      $B$ = $StringToParse$( $e% TO LEN $StringToParse$)
      IF $Pos% > $loopnbr% + 1 THEN $B$ = ""
      GOTO $EndOfLine       
  ENDIF
  
  $loopnbr% = $loopnbr% + 1
  $B$ = $StringToParse$( $e% TO $f%-1)
  IF $Pos% = $loopnbr% THEN GOTO $EndOfLine
  $e% = $f% + 1 //REM Init for next loop/line
  
  GOTO $NextItem
  $EndOfLine:
  $SplitString$ = $B$
ENDFN

FUNCTION Log($Msg$)
  LOGEVENT  $Msg$ ,100
  PRINT $Msg$
ENDFN

FUNCTION GetTime$()
  $a$ = Time$
  $GetTime$ = $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

Kind regards,
Diego from HiveMQ Team