LAN Connection between Flexy 205 and raspberry Pi (MQTT broker installed)

“Dear team, as a new member of this project, I am currently in the process of establishing a LAN connection between the Flexy 205 and a Raspberry Pi. The Raspberry Pi is running a broker. Could you please provide guidance on whether any additional configurations need to be implemented?” I am using the same code provided by Simon except that i am using my pi IP address and the port is 1883https://tools.ewonsupport.biz/mqtt/program.txt

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

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

MQTTBrokerURL$ = "RASPBERRY PI IP ADDRESS 192.168.. "
MQTTPort$ = “1883”
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”, “10”
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

I expect Simon’s code works. Have you tested it?

hey Kyle

They work fine. I just changed the Raspberry Pi config file to be
allow_anonymous true
It works.

Basically i cannot see values while subscribing to mosquitto broker which is connected to LAN network . But when I try to stop the broker from PI Ewon gives me a broker not connected.
What is the topic name I should be using for subscribing and publishing

Can you please help me with that.

Thanks in advance

BR

Aravind

The names are up to you. There isn’t a standard naming convention, but you can find some links with ideas here: mqtt - naming conventions for MTT topics? - Stack Overflow