Switch to desktop version  

Ewon TechForum
Providing technical solutions to technical requests



BASIC live data publishing with MQTT all tags
ToonW
Junior Member
#1
Hi, 

I've been using the ewon script from the ewon repository to send live data over MQTT i was wondering since we have two interval times. One for when things changed and the other one pushes all the data every xx seconds. Is it possible to send all the data over only when there is a change in values (also from the the values that didn't changed) ? Because now i received by every tagg null except for the one who has changed. This is quite annoying for the database having lots of empty cells. And how should i implement this in the script ? Also can't see any code in the script to change this only the interval times?

Thanks!
Reply

simon
eWON Support
#2
Hello,

Yes sure it is possible and it is fairly easy ;-)

If you use one of my scripts :

Just call the MQTT_ComputeJsonAllTags into the MQTT_ComputeJsonChangeTags function.

FUNCTION MQTT_ComputeJsonChangeTags$()
$MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags()
ENDFN
Reply

ToonW
Junior Member
#3
(07-12-2022, 12:50 PM)simon Wrote: Hello,

Yes sure it is possible and it is fairly easy ;-)

If you use one of my scripts :

Just call the MQTT_ComputeJsonAllTags into the MQTT_ComputeJsonChangeTags function.

FUNCTION MQTT_ComputeJsonChangeTags$()
    $MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags()
ENDFN

When i do that i get the error message that: label/function is not found(27).
Insert it at the end of the function ? Or am i wrong with this ? 

FUNCTION MQTT_ComputeJsonChangeTags$()

  $counter% = 0
  //Compute JSON
  $json$ = '{'
  $NB% = GETSYS PRG,"NBTAGS"
  FOR $r% = 1 TO $NB%
    IF a($r%,2) = 1 THEN
      a($r%,2) = 0
      $negIndex% = a($r%,1)
      SETSYS Tag, "LOAD", $negIndex%
      $Tagname$= GETSYS TAG, "NAME"
      $TagValue$ = GETSYS TAG, "TAGVALUE"
      $json$ = $json$ + '"' + $Tagname$ + '":"'+ $TagValue$ + '",'
      $counter% = $counter% +1
    ENDIF
  NEXT $r%
 
  $json$ = $json$ +    '"time": "' + @MQTT_GetJsonTime$() + '"'
  $json$ = $json$ +    '}'
  IF $counter% <> 0 THEN
    $MQTT_ComputeJsonChangeTags$ = $json$
  ELSE
    $MQTT_ComputeJsonChangeTags$ = ""
  ENDIF
  $MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags()
ENDFN
Reply

ToonW
Junior Member
#4
(07-12-2022, 01:57 PM)ToonW Wrote:
(07-12-2022, 12:50 PM)simon Wrote: Hello,

Yes sure it is possible and it is fairly easy ;-)

If you use one of my scripts :

Just call the MQTT_ComputeJsonAllTags into the MQTT_ComputeJsonChangeTags function.

FUNCTION MQTT_ComputeJsonChangeTags$()
    $MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags()
ENDFN

When i do that i get the error message that: label/function is not found(27).
Insert it at the end of the function ? Or am i wrong with this ? 

FUNCTION MQTT_ComputeJsonChangeTags$()

  $counter% = 0
  //Compute JSON
  $json$ = '{'
  $NB% = GETSYS PRG,"NBTAGS"
  FOR $r% = 1 TO $NB%
    IF a($r%,2) = 1 THEN
      a($r%,2) = 0
      $negIndex% = a($r%,1)
      SETSYS Tag, "LOAD", $negIndex%
      $Tagname$= GETSYS TAG, "NAME"
      $TagValue$ = GETSYS TAG, "TAGVALUE"
      $json$ = $json$ + '"' + $Tagname$ + '":"'+ $TagValue$ + '",'
      $counter% = $counter% +1
    ENDIF
  NEXT $r%
 
  $json$ = $json$ +    '"time": "' + @MQTT_GetJsonTime$() + '"'
  $json$ = $json$ +    '}'
  IF $counter% <> 0 THEN
    $MQTT_ComputeJsonChangeTags$ = $json$
  ELSE
    $MQTT_ComputeJsonChangeTags$ = ""
  ENDIF
  $MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags()
ENDFN

Weird but it looks like it's working? Still getting the error label/function is not found(27).
Reply

simon
eWON Support
#5
Toon,

No idea where it comes from. I need to see the whole script to maybe see...
You can comment/remove the rest of the code and only keep $MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags()
Reply

ToonW
Junior Member
#6
(07-12-2022, 03:42 PM)simon Wrote: Toon,

No idea where it comes from.  I need to see the whole script to maybe see...
You can comment/remove the rest of the code and only keep $MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags()

Okay i changed the code to your suggestion,
Still getting the error. Please take a look at my code (if you have time).
Code:
Rem --- eWON start section: Cyclic Section
eWON_cyclic_section:
Rem --- eWON user (start)
Rem --- eWON user (end)
End
Rem --- eWON end section: Cyclic Section
Rem --- eWON start section: Init Section
eWON_init_section:
Rem --- eWON user (start)


//-->CONFIGURATION
CONF_AWSMQTTBROKERURL$="xxxxx.amazonaws.com"//COMMENT:AWS MQTT Broker URL (See start.sh) ->Ex: xxxx-ats.iot.eu-west-3.amazonaws.com
CONF_ClientID$ = "xxxx"//COMMENT:AWS IOT Client ID (See start.sh)->Ex: sdk-nodejs-93514xxxx...
CONF_ThingName$ = "Vopak_Buffer32"//COMMENT:Thing Name choosen when creating the Thing/Device in Aws IoT (See start.sh)
CONF_DeviceTopic$ =  "topic_1"//COMMENT:AWS IOT Device Topic used for sending message (See permission on Device policy. See requirents) -> Ex : topic_1 ->topic_1
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$ = "30"//COMMENT:Time interval to push all values -> time in seconds ->60
CONF_ChangePushTime$ = "5"//COMMENT:Time interval to push all values that changed -> time in seconds ->5
//-->END CONFIGURATION

CONF_FullPushTime% = VAL CONF_FullPushTime$
CONF_ChangePushTime% = VAL CONF_ChangePushTime$
MsgDBPath$ = "/usr/mqttmsg_db.txt"
MaxMsgBufferFileSize% = 5000000 //max size of file used to save msg in bytes
CLS
Last_ConnStatus% = -1
FullPushLastTimeSec% = 0
ChangePushLastTimeSec% = 0
@LOG("Generate CA Certificate in usr directory.")
@Generate_Aws_CA_Certificate()
@LOG("Initalize MQTT, connecting to Aws IoT broker")

MQTT "OPEN",CONF_ClientID$, CONF_AWSMQTTBROKERURL$
MQTT "SETPARAM", "Port","8883"
MQTT "SETPARAM", "log", "1"
MQTT "SETPARAM", "keepalive", "20"
MQTT "SETPARAM", "cafile","/usr/AWS_CA.crt"
MQTT "SETPARAM", "certfile","/usr/"+CONF_ThingName$+".cert.pem"
MQTT "SETPARAM", "keyfile","/usr/"+CONF_ThingName$+".private.key"
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$, 1, 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$, 1,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
//Compute the Json string with the Tags that have changed.
FUNCTION MQTT_ComputeJsonChangeTags$()
  $MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags()
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"
     
      IF $GroupA$ = "1" And CONF_GroupA$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
      IF $GroupB$ = "1" And CONF_GroupB$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
      IF $GroupC$ = "1" And CONF_GroupC$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
      IF $GroupD$ = "1" And CONF_GroupD$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
   
  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 Aws IoT SCRIPT] " + $Msg$ ,100
  PRINT "[MQTT SCRIPT] " + $Msg$
ENDFN

FUNCTION Generate_Aws_CA_Certificate()
//WRITE OLD CA CERT In case of Old instances
CACRT$ =        "-----BEGIN CERTIFICATE-----" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"rqXRfboQnoZsG4q5WTP468SQvvG5" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"-----END CERTIFICATE-----"
OPEN "file:/usr/AWS_CA_old.crt" FOR BINARY OUTPUT AS 1
PUT 1, CACRT$
CLOSE 1
//WRITE CA CERT
CACRT$ =        "-----BEGIN CERTIFICATE-----" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"MIID7zCCAtegAwIBAgIBADANBgkqhkiG9w0BAQsFADCBmDELMAkGA1UEBhMCVVMx" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"EDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxJTAjBgNVBAoT" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"HFN0YXJmaWVsZCBUZWNobm9sb2dpZXMsIEluYy4xOzA5BgNVBAMTMlN0YXJmaWVs" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"ZCBTZXJ2aWNlcyBSb290IENlcnRpZmljYXRlIEF1dGhvcml0eSAtIEcyMB4XDTA5" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"MDkwMTAwMDAwMFoXDTM3MTIzMTIzNTk1OVowgZgxCzAJBgNVBAYTAlVTMRAwDgYD" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"VQQIEwdBcml6b25hMRMwEQYDVQQHEwpTY290dHNkYWxlMSUwIwYDVQQKExxTdGFy" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"ZmllbGQgVGVjaG5vbG9naWVzLCBJbmMuMTswOQYDVQQDEzJTdGFyZmllbGQgU2Vy" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"dmljZXMgUm9vdCBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkgLSBHMjCCASIwDQYJKoZI" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"hvcNAQEBBQADggEPADCCAQoCggEBANUMOsQq+U7i9b4Zl1+OiFOxHz/Lz58gE20p" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"OsgPfTz3a3Y4Y9k2YKibXlwAgLIvWX/2h/klQ4bnaRtSmpDhcePYLQ1Ob/bISdm2" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"8xpWriu2dBTrz/sm4xq6HZYuajtYlIlHVv8loJNwU4PahHQUw2eeBGg6345AWh1K" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"Ts9DkTvnVtYAcMtS7nt9rjrnvDH5RfbCYM8TWQIrgMw0R9+53pBlbQLPLJGmpufe" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"hRhJfGZOozptqbXuNC66DQO4M99H67FrjSXZm86B0UVGMpZwh94CDklDhbZsc7tk" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"6mFBrMnUVN+HL8cisibMn1lUaJ/8viovxFUcdUBgF4UCVTmLfwUCAwEAAaNCMEAw" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"DwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAQYwHQYDVR0OBBYEFJxfAN+q" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"AdcwKziIorhtSpzyEZGDMA0GCSqGSIb3DQEBCwUAA4IBAQBLNqaEd2ndOxmfZyMI" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"bw5hyf2E3F/YNoHN2BtBLZ9g3ccaaNnRbobhiCPPE95Dz+I0swSdHynVv/heyNXB" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"ve6SbzJ08pGCL72CQnqtKrcgfU28elUSwhXqvfdqlS5sdJ/PHLTyxQGjhdByPq1z" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"qwubdQxtRbeOlKyWN7Wg0I8VRw7j6IPdj/3vQQF3zCepYoUz8jcI73HPdwbeyBkd" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"iEDPfUYd/x7H4c7/I9vG+o1VTqkC50cRRj70/b17KSa7qWFiNyi2LSr2EIZkyXCn" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"0q23KXB56jzaYyWf/Wi3MOxw+3WKt21gZ7IeyLnp2KhvAotnDU0mV3HaIPzBSlCN" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"sSi6" + CHR$(13) + CHR$(10)
CACRT$ = CACRT$ +"-----END CERTIFICATE-----"
OPEN "file:/usr/AWS_CA.crt" FOR BINARY OUTPUT AS 1
PUT 1, CACRT$
CLOSE 1
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
 
  IF TimeSecTemp% - ChangePushLastTimeSec% >= CONF_ChangePushTime% THEN
    ChangePushLastTimeSec% = TimeSecTemp%
    Json$ = @MQTT_ComputeJsonChangeTags$()
    IF Json$ <> "" THEN
      @MQTT_Publish(json$)
      PRINT "[MQTT SCRIPT] Some Tags have changed - Publish"
    ELSE
      PRINT "[MQTT SCRIPT] No Tag changes detected! -> Don't publish"
    ENDIF 
  ENDIF
 
END
Rem --- eWON user (end)
End
Rem --- eWON end section: Init Section
Reply

simon
eWON Support
#7
My mistake. A "$" was missing
$MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags$()
Reply

Technical
Junior Member
#8
(07-12-2022, 10:50 PM)simon Wrote: My mistake. A "$" was missing
$MQTT_ComputeJsonChangeTags$ = @MQTT_ComputeJsonAllTags$()

Hi Simon, could I send by MQTT the values of the tags like int, real, etc.. or they must to be in Json.

I mean, I want to send from EWON a message by MQTT with this structure:

    - Tag1: 5.01   (this tag for example is a real)
    - Tag2: True   (this tag is a bool)

and for now my script send the message all values like a json, all is a string, like this:
 
    - "Tag1":"5.01"   
    - "Tag2":"True"

They are considered like an string although they are a real and bool too.
Reply

simon
eWON Support
#9
I think it is a Json syntax issue.
Here we choose to surround all values with a double quote "".
This makes that the system interpret them as strings.

Here are the code lines doing that :
IF $GroupA$ = "1" And CONF_GroupA$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
IF $GroupB$ = "1" And CONF_GroupB$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
IF $GroupC$ = "1" And CONF_GroupC$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
IF $GroupD$ = "1" And CONF_GroupD$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'

So the idea would be to remove the double quotes when it is a int, float...
Here is an example :


IF $GroupA$ = "1" And CONF_GroupA$= "1" THEN $json$ = $json$ + @CompteJsonValue$($Tagname$,$TagValue$)
...


FUNCTION CompteJsonValue$($Fct_TagName$, $Fct_TagValue$)
SETSYS TAG, "LOAD", $Fct_TagName$
$TAG_Format$ = GETSYS TAG, "TYPE"
IF $TAG_Format$ = "6" THEN //Is string
$CompteJsonValue$= '"' + $Fct_TagName$ + '":"' + $Fct_TagValue$ + '",'
ELSE
$CompteJsonValue$= '"' + $Fct_TagName$ + '":' + $Fct_TagValue$ + ','
ENDIF
ENDFN
Reply

Technical
Junior Member
#10
(04-01-2024, 04:45 PM)simon Wrote: I think it is a Json syntax issue.
Here we choose to surround all values with a double quote "".
This makes that the system interpret them as strings.

Here are the code lines doing that :
    IF $GroupA$ = "1" And CONF_GroupA$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
      IF $GroupB$ = "1" And CONF_GroupB$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
      IF $GroupC$ = "1" And CONF_GroupC$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'
      IF $GroupD$ = "1" And CONF_GroupD$= "1" THEN $json$ = $json$ + '"' + $Tagname$ + '":"' + $TagValue$ + '",'

So the idea would be to remove the double quotes when it is a int, float...
Here is an example :


IF $GroupA$ = "1" And CONF_GroupA$= "1" THEN $json$ = $json$ + @CompteJsonValue$($Tagname$,$TagValue$)
...


FUNCTION CompteJsonValue$($Fct_TagName$, $Fct_TagValue$)
  SETSYS TAG, "LOAD", $Fct_TagName$
  $TAG_Format$ = GETSYS TAG, "TYPE"
  IF $TAG_Format$ = "6" THEN //Is string
  $CompteJsonValue$= '"' + $Fct_TagName$ + '":"' + $Fct_TagValue$ + '",'
  ELSE
  $CompteJsonValue$= '"' + $Fct_TagName$ + '":' + $Fct_TagValue$ + ','
  ENDIF
ENDFN


OK Simon thank you!

If I make an error in the basic ide with autorun enabled, can I lose connection to the vpn? If the ewon can't connect, doesn't the script stop?

I made a change in the script and the EWON no longer connects to the VPN, what can I do in this situation? The ewon is in another company in a different country... So I need to recover this connection if possible without having to contact the client.

Thanks again.
Reply

simon
eWON Support
#11
Hi,

Sorry for the late reply
It depends which error. If it cause a reboot, yes you could loose it indeed.
If the Ewon does not come back because of this, then somebody should do a reset level 1 of the Ewon to restore default settings for tags, ioserver AND Basic.
https://forum.hms-networks.com/t/perform...eset/11072
Reply

Technical
Junior Member
#12
(12-01-2024, 08:22 PM)simon Wrote: Hi,

Sorry for the late reply
It depends which error. If it cause a reboot, yes you could loose it indeed.
If the Ewon does not come back because of this, then somebody should do a reset level 1 of the Ewon to restore default settings for tags, ioserver AND Basic.
https://forum.hms-networks.com/t/perform...eset/11072
So with this lvl 1 reset the VPN could be back normally, right?
Reply

simon
eWON Support
#13
Yes. If there is well a continuous reboot because of the script...
Reply




Possibly Related Threads…
11-08-2025, 02:51 PM
Last Post: Anton
18-07-2025, 07:22 AM
Last Post: LuisEnrique
  AWS IOT MQTT support Started by remi.reppert
1 Replies - 277 Views
01-07-2025, 08:44 AM
Last Post: simon
18-06-2025, 04:57 PM
Last Post: mickaa
05-06-2025, 12:23 PM
Last Post: simon
15-05-2025, 05:57 PM
Last Post: LuisEnrique
20-03-2025, 12:55 PM
Last Post: driesdebouver
24-10-2024, 01:52 PM
Last Post: anas.rhouati
11-07-2024, 12:12 PM
Last Post: Mithun
10-07-2024, 09:06 PM
Last Post: dmned



Users browsing this thread:
1 Guest(s)



Theme © Ewon 2019 - Forum software by © MyBB - Cookie policy