How to make eWON devices communicate via MQTT

It is possible to use eWON devices as MQTT clients and therefore make them execute any commands when a message is published on a subscribed topic.

 


 

Procedure on Databoom

You can use Databoom MQTT broker for publishing and subscribing to topics. An MQTT Device Template is needed.

  • Add a new device by clicking Add device button at the top of devices list
  • Select the type Template Device and fill the rest of the device fields
  • Set the MQTT type in the template additional settings

  • Insert
    • Project
      • eg. eWon
    • Topic where publish data
      • eg. pub
    • Topic where it's possible to receive information from Databoom
      • eg. sub
  • By confirming device creation you'll have
    • Client ID
      • es. btr1-<user generated unique value>
    • Topic for publication
      • es. eWon/<user generated unique value>/pub
    • Topic for subscription
      • es. eWon/<user generated unique value>/sub
  • In all the three fields an <user generated unique value> must be set before using them in the following script

 


 

Procedure on eWON

Access eWOM management interface by typing its IP address in a browser. Authentication is required, if they've not been updated, default credentials are:

Username: adm  Password: adm

Go to Configuration -> Script Setup (In eWON firmware newer versions you can access script management from the side menu Setup -> BASIC IDE, ensure that Script Execution is on RUNNING so that the script is executed).

To enstablish connection between two eWONs, one device must integrate publication on a certain topic, while the other must integrate subscription to the same topic.

Following example script allows a device to write a tag value on the publication topic every 5 seconds, then the value is read by the other device and written to another tag.

 


 

The first part of the script, with the MQTT broker connection, is common to both cases. Values generated in Databoom in the previous stesps are used in the script:

  • ClientID$ is the identification code of the MQTT template with the <user generated unique value> set. In order for both clients to use the same topic, you need to set different client IDs
    • eg. btr1-publihser, btr1-subscriber
  • Username$ is the username to access Databoom
    • eg. databoomtester
  • Password$ is a token generated in Databoom in the section Settings -> Credentials 
    • eg. o3Lk/Y9ypyXyr3HxIR75pp8nG6nn9DzECFA1JVCdeNk=
  • TopicToPublishOn$ and TopicToSubscribe$  are the topics shown in the template device view with the same <user generated unique value> used in the CLIENT_ID set. To enstablish connection between two eWONs, one device must integrate publication on a certain topic, while the other must integrate subscription to the same topic.
    • eg. eWon/test/pub

 


 

Follows the publication script. In green are the parts to substitute with your own values.

<TAG_EWON>@ is the tag to publish on the topic. It must be referred with its real name

  • eg. pubVar@

 

//############### CONFIG ###############
MQTTBrokerURL$ = "mqtt.databoom.com"
MQTTPort$ = "1883"
ClientID$ = "btr1-publisher"
Username$ = "databoomtester"
Password$ = "o3Lk/Y9ypyXyr3HxIR75pp8nG6nn9DzECFA1JVCdeNk="
TopicToPublishOn$ = "eWon/test/pub"
//############### END CONFIG ###############
Last_ConnStatus% = 0
//Configure MQTT Connection parameters
Goto CONNECTMQTT

CONNECTMQTT
: MQTT "OPEN", ClientID$ , MQTTBrokerURL$ MQTT "SetParam", "Username", Username$ MQTT "SetParam", "Password", Password$ MQTT "SETPARAM", "PORT", MQTTPort$ MQTT "SETPARAM", "KEEPALIVE", "10" 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 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 SETSYS PRG,"RESUMENEXT",1 MsgToPublish$ = SFMT <TAG_EWON>@,20,0,"%f" MQTT "PUBLISH", TopicToPublishOn$ , MsgToPublish$, 0,0 PRINT "[MQTT SCRIPT] Message published to the MQTT broker" ErrorReturned = GETSYS PRG,"LSTERR" IF ErrorReturned=28 THEN //ERROR while publishing --> No connection --> Better to close and open MQTT "CLOSE" GOTO CONNECTMQTT ENDIF ELSE //If not connected --> Save message in file @Log("[MQTT SCRIPT] Flexy not connected") ENDIF END

Tag publication happens in the following step

MsgToPublish$ = SFMT <TAG_EWON>@,20,0,"%f"
MQTT "PUBLISH",  TopicToPublishOn$ , MsgToPublish$, 0,0

 


 

Follows the subscription script. In green are the parts to substitute with your own values.

<TAG_EWON>@ is the tag to which the published value is assigned. It must be referred with its real name

  • eg. rcvVar@
//############### CONFIG ###############
MQTTBrokerURL$ = "mqtt.databoom.com"
MQTTPort$ = "1883"
ClientID$ = "btr1-subscriber"
Username$ = "databoomtester"
Password$ = "o3Lk/Y9ypyXyr3HxIR75pp8nG6nn9DzECFA1JVCdeNk="
TopicToSubscribe$ = "eWon/test/pub"
//############### END CONFIG ###############
Last_ConnStatus% = 0
//Configure MQTT Connection parameters
Goto CONNECTMQTT

CONNECTMQTT:
MQTT "OPEN", ClientID$ , MQTTBrokerURL$
MQTT "SetParam", "Username", Username$
MQTT "SetParam", "Password", Password$
MQTT "SETPARAM", "PORT", MQTTPort$
MQTT "SETPARAM", "KEEPALIVE", "10" 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 ONTIMER 1, "GOTO SENDDATA" TSET 1,5 //publish every 5 seconds END
MQTT "SUBSCRIBE",TopicToSubscribe$,1 //Launch the MQTT process in the background
//When receiving a message from Broker, "GOTO MQTTRECEIVEMSG"
ONMQTT "GOTO MQTTRECEIVEMSG"
END

MQTTRECEIVEMSG
: //Executed when receiving messages from Broker MessageQty%=Mqtt "READ" //Return the number of pending messages IF (MessageQty%>0) Then MsgTopic$= MQTT "MSGTOPIC" MsgData$ = MQTT "MSGDATA" @Log("[MQTT SCRIPT] Message '"+ MsgData$ + "' received on topic " +MsgTopic$) <TAG_EWON>@ = FCNV MsgData$,20,0,"%f" GOTO MQTTRECEIVEMSG ENDIF END FUNCTION Log($Msg$) LOGEVENT $Msg$ ,100 PRINT $Msg$ ENDFN

Tag value reception happens in the following step

MsgData$ = MQTT "MSGDATA"
<TAG_EWON>@ = FCNV MsgData$,20,0,"%f"

 


 To enstablish connection between two eWONs, one device must integrate publication on a certain topic, while the other must integrate subscription to the same topic.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.