Ewon TechForum
Providing technical solutions to technical requests


MQTT- Easy connection to Microsoft AZURE IOT
Waterlot Offline
Junior Member
#1
Hi guys, 

Here is a Flexy application (basic script + config webpage) to connect your Flexy to the Microsoft AZURE IOT platform  through MQTT.

This application allows you to :
  • Configure the Azure connection using a simple webpage (azureiot.shtm).
    .png   Configurationpageazureiot.PNG (Size: 20,6 KB / Downloads: 318)


  • Send multiple value tags from Flexy to Microsoft AZURE IOT on a Time based and/or when it changes.

  • Select the group of Tag to send
Here is a document that describes how to setup this application into your Flexy :  

.pdf   HMS-eWON-Flexy-integration-to-AZURE-Iot.pdf (Size: 556,97 KB / Downloads: 541)

Here are the files needed  : 

.zip   AZUREIOT.zip (Size: 4,69 KB / Downloads: 262)



Bye

Gautier


NOTE [Simon] : Since this app uses SAS token to authenticate to the cloud and these tokens seem to be limited to 365 days, I have developed a script which uses self-signed certificates (valid for 10 years)  rather than the SAS tokens.  See https://techforum.ewon.biz/thread-786.html
NOTE UPDATE [Simon] : Somebody has found an easy way to create SAS tokens with a longer validity on https://techforum.ewon.biz/thread-786-po...ml#pid3868 (Thanks again to him! :-) ). So both methods (SAS or Certificate) can be used without problem with the Flexy! Enjoy!
Reply

Richard Offline
Ewon Distributor
#2
Thanks for this example. i get stuck when you ask to download the following SDK from Github: "
The next step is to create the authentication token.
Get your account authentication token ( Shared access> iothubowner>primary key token) and store it somewhere:
Then, download this tool:
https://github.com/Azure/azure-iot-sdk-csharp/releases
"
Right after that you say to install it, but it is not an installer. I found out it had something to do with Visual studio. But also in Visual studio I do not know what to do with it..?
Then you speak o the first window (of something called Device Explorer Twin) where I should put the primary key but I do not know where to open this window?
Could you guide me a little bit more please?
Thanks,
Richard
Reply

simon Offline
eWON Support
#3
Hi Richard,

If you scroll down the Microsoft page, you can find a MSI installer : https://github.com/Azure/azure-iot-sdk-c...plorer.msi
Reply

Richard Offline
Ewon Distributor
#4
Thanks Simon!, this was really hard to find on those pages!
now I can proceed.
Thanks,
Richard
Reply

Richard Offline
Ewon Distributor
#5
I was able to copy the "Connection string-primary key" into the "Device Explorer Twin", update it and generate the SAS key.
However; when I go to the Management tab there is no device. Also when I press refresh it is not there..
When I press Crate it makes a device but when I try to click on the SAS button I can not selct a Device ID nor Device keys nor TTL and program crashes after that....
What do I do wrong?

I managed to Create it when I checked a box For auto generate ID.
Is it correct that the SAS key is everything beginning with SharedAccessSignature untill the end?
I managed to fill in the shtm file on xxx.xxx.xxx.xxx/usr/azureiot.shtm with the SAS, change push time on 2 and full push time on 20 and select all tags.
However when I run the basic script I see the following syntax error? see attachment
on line 73 Changepushtime


Attached Files
.png   syntax error.PNG (Size: 31,94 KB / Downloads: 71)
Thanks,
Richard
Reply

Richard Offline
Ewon Distributor
#6
Another question: when it works; how should I visualize the data within Azure?
Thanks,
Richard
Reply

simon Offline
eWON Support
#7
This is a tricky question... and to be honest we faced the same issue when testing... We did not manage to view data in the dashboards.
The only way we found out is to use the same software as used for generating the SAS key...

If you find the trick, let us know
Reply

Richard Offline
Ewon Distributor
#8
(16-04-2018, 04:47 PM)Simon Wrote: This is a tricky question... and to be honest we faced the same issue when testing...  We did not manage to view data in the dashboards.
The only way we found out is to use the same software as used for generating the SAS key...

If you find the trick, let us know

Hi Simon,
I also do not get it to work with the Twin tool. Could you please explain it more so I can solve these problems and get something working here as the example suggests?
Thanks,
Richard
Reply

rene.divossen Offline
Junior Member
#9
(28-03-2018, 01:05 PM)Waterlot Wrote: Hi guys, 

Here is a Flexy application (basic script + config webpage) to connect your Flexy to the Microsoft AZURE IOT platform  through MQTT.

This application allows you to :
  • Configure the Azure connection using a simple webpage (azureiot.shtm).

  • Send multiple value tags from Flexy to Microsoft AZURE IOT on a Time based and/or when it changes.

  • Select the group of Tag to send
Here is a document that describes how to setup this application into your Flexy :  

Here are the files needed  : 


Bye

Gautier

Hello Gautier, hello Ewon users,

thanks for providing this script.  We are using it on an Ewon Flexy 205 but so far cannot go productive because the script will not work anymore after restarting the Ewon.
We have checked the Autostart option and the script is marked as "running" after a restart, but no messages are send and no logging is displayed.  If we manually stop and start the script again, everything is working again.  We think upon startup the script is not working because it is already starting to run before the WAN / VPN is ready.

We have already tried to wait a bit at the beginning of the script to allow for the Ewon to initialize:

TSET 1, 5
Label1:
IF NOT TGET 1 THEN GOTO LABEL1

This did not change anything.


We then increased the waiting time from 5 to 60 sec and got really strange behaviour.  The Flexy failed to connect to the VPN and could not be reached by eCatcher anymore.

When we changed the waiting time to 30 sec, the connection to the VPN worked again but was delayed for an estimated 30 seconds.  For us it looks like the Gateway IP was only set after the waiting time expired.  The script did not autorun properly with this waiting time either.

If you know how to make the script work properly and automatically after a restart, your help would be greatly appreciated!

Kind regards,
René
Reply

simon Offline
eWON Support
#10
Hi,

Here is an update of the script of Gauthier that fixes two issues : Script now works when rebooting and the DeviceID was not well managed

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)
CLS

REM Sending multiple value tags from Flexy to AZURE IOT Platform
REM By: Gautier Waterlot
REM Date: March, 21th, 2017

REM Connection with the platform from a web page.
REM Definition of the push-out period
REM Configuration of files with the parameters of the connection and the option to send
REM Sending  tags value if the group of tags is sent.(the value of group must be 1)
REM Connection with the platform from a configuration file

DeviceId$=""
IotHub$ =""
SASToken$=""
Changepushtime% = 2 // Change Push Time
Fullpushtime% = 20// Full Push Time
configfile$="Azureiot_parameters.txt"// Name of the configuration file
GROUPA% = 1
GROUPB% = 1
GROUPC% = 1
GROUPD% = 1
NB%= GETSYS PRG,"NBTAGS"
DIM a(NB%,2)

OPEN "file:/usr/"+configfile$ FOR BINARY INPUT AS 1
A$=""
COUNT% =1
TEST:
 FOR i%= 1 To 200
 test$=GET 1,1
 
   IF test$=CHR$ (10) THEN
     GOTO "FICH"
   ELSE
     A$=A$+test$
   ENDIF
    IF test$="" THEN
   PRINT " IOT_PARAMETERS NOT DETECTED"
   ENDIF
 NEXT i%
END
//cut the file
FICH:
 IF COUNT%=1 THEN
 DeviceId$ = A$
 COUNT%=COUNT%+1
 A$=""
 GOTO "TEST"
 ENDIF
 IF COUNT%=2 THEN
 IotHub$ =A$
 A$=""
 COUNT% = COUNT%+1
 GOTO "TEST"
 ENDIF
 IF COUNT%=3 THEN
 SASToken$ =A$
 A$=""
 COUNT% = COUNT%+1
 GOTO "TEST"
 ENDIF
   IF COUNT%= 4 THEN
 Changepushtime%= Val A$
 A$=""
 COUNT% = COUNT%+1
 GOTO "TEST"
 ENDIF
    IF COUNT%= 5 THEN
Fullpushtime%= Val A$
 A$=""
 COUNT% = COUNT%+1
 GOTO "TEST"
 ENDIF
    IF COUNT%= 6 THEN
 GROUPA%= Val A$
 A$=""
 COUNT% = COUNT%+1
 GOTO "TEST"
 ENDIF
      IF COUNT%= 7 THEN
 GROUPB%= Val A$
 A$=""
 COUNT% = COUNT%+1
 GOTO "TEST"
 ENDIF
     IF COUNT%= 8 THEN
 GROUPC%= Val A$
 A$=""
 COUNT% = COUNT%+1
 GOTO "TEST"
 ENDIF
 IF COUNT% = 9 THEN
   GROUPD%= Val A$
 CLOSE 1
 GOTO "MqttCONNECT"
 ENDIF
END


MqttCONNECT:

TSET 2,Changepushtime%
Ontimer 2, "goto MqttPublishChangedValue"
   
Tset 1,Fullpushtime%
Ontimer 1,"goto MqttPublishAllValue"  

MQTT "Open",DeviceId$,IotHub$+".azure-devices.net"
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/BaltimoreCyberTrustRoot.pem"
Mqtt "SetParam","Username",IotHub$+".azure-devices.net"+"/"+DeviceId$+"/api-version=2016-11-14"
Mqtt "SetParam","Password",SASToken$
Mqtt "Connect"
ONMQTT "GOTO MqttRx"
MQTT "SUBSCRIBE","/test",0
ONMQTTSTATUS "GOTO IsConnected"
End
//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
IsConnected:
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 GROUPA%= 1  THEN
         Onchange -i%, "a("+ STR$ k%+",2)= 1" // 1 si la valeur change
       ENDIF
       IF GroupB$ = "1" And GROUPB%= 1 THEN
        Onchange -i%, "a("+ STR$ k%+",2)= 1"
       ENDIF
       IF GroupC$ = "1" And GROUPC%= 1 THEN
        Onchange -i%, "a("+ STR$ k%+",2)= 1"
       ENDIF
       IF GroupD$ = "1" And GROUPD%= 1THEN
        Onchange -i%, "a("+ STR$ k%+",2)= 1"
       ENDIF
   Next i%

   
   Goto "MqttPublishAllValue"
END
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
//Publish just the changed tags
MqttPublishChangedValue:
counter = 0
json$ =         '{'
FOR r% = 1 TO NB%
 IF a( r%,2) = 1 THEN
   a(r%,2) = 0
   negIndex% = a(r%,1)
   SETSYS Tag, "LOAD", negIndex%
   name$= GETSYS Tag, "name"
   json$ = json$ + '"' + name$+ '":"'+STR$ GETIO name$ + '",'
   counter= counter+1
 ENDIF
NEXT r%
json$ = json$ +    '"time": "'+@GetTime$()+'"'
json$ = json$ +    '}'
IF counter > 0 THEN
 MQTT "PUBLISH","devices/"+DeviceID$+"/messages/events/",json$, 0, 0
 PRINT "Changes detected -> Publish"
 
ELSE
 PRINT "No change detected!"
ENDIF
END
   
//publish all tags
MqttPublishAllValue:
json$ =         '{'
   FOR i% = 0 TO NB% -1
       SETSYS Tag, "load",-i%
       i$= GETSYS TAG,"Name"
       GroupA$= GETSYS TAG,"IVGROUPA"
       GroupB$= GETSYS TAG,"IVGROUPB"
       GroupC$= GETSYS TAG,"IVGROUPC"
       GroupD$= GETSYS TAG,"IVGROUPD"
       IF  GroupA$ = "1" And GROUPA%= 1  THEN
       json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",'
       ENDIF
       IF GroupB$ = "1" And GROUPB%= 1 THEN
       json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",'
       ENDIF
       IF GroupC$ = "1" And GROUPC%= 1 THEN
       json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",'
       ENDIF
       IF GroupD$ = "1" And GROUPD%= 1THEN
       json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",'
       ENDIF
   Next i%    
   json$ = json$ +    '"time": "'+@GetTime$()+'"'
   json$ = json$ +         '}'
   
  STATUS% = MQTT("STATUS")
 
  //Is Connected
  If (STATUS% = 5) Then
    Print "PUBLISH: " + json$
    MQTT "PUBLISH","devices/"+DeviceID$+"/messages/events/",json$, 0, 0
  Else
    Print "Not connected (" + STR$ STATUS% + ")"
  Endif
End
//the use of page web
UseForm:
DeviceId$=deviceid!
IotHub$ =iothub!
SASToken$=sastoken!
Changepushtime% = Val changepushtime!
Fullpushtime% = Val fullpushtime!
GROUPA% = Val groupa!
GROUPB% = Val groupb!
GROUPC% = Val groupc!
GROUPD% = Val groupd!
OPEN "file:/usr/" + configfile$ FOR BINARY OUTPUT AS 1
PUT 1,DeviceId$ +CHR$(10)
PUT 1,IotHub$+ CHR$(10)
PUT 1,SASToken$+ CHR$(10)
PUT 1,changepushtime!+CHR$(10)
PUT 1,fullpushtime!+CHR$(10)
PUT 1,groupa!+ CHR$(10)
PUT 1,groupb!+ CHR$(10)
PUT 1,groupc!+ CHR$(10)
PUT 1,groupd!+ CHR$(10)
CLOSE 1
GOTO "MqttCONNECT"
END


Rem --- eWON user (end)
End
Rem --- eWON end section: Init Section
Reply

rene.divossen Offline
Junior Member
#11
(21-09-2018, 04:17 PM)rene.divossen Wrote: Hello Gautier, hello Ewon users,

thanks for providing this script.  We are using it on an Ewon Flexy 205 but so far cannot go productive because the script will not work anymore after restarting the Ewon.
We have checked the Autostart option and the script is marked as "running" after a restart, but no messages are send and no logging is displayed.  If we manually stop and start the script again, everything is working again.  We think upon startup the script is not working because it is already starting to run before the WAN / VPN is ready.

We have already tried to wait a bit at the beginning of the script to allow for the Ewon to initialize:

TSET 1, 5
Label1:
IF NOT TGET 1 THEN GOTO LABEL1

This did not change anything.


We then increased the waiting time from 5 to 60 sec and got really strange behaviour.  The Flexy failed to connect to the VPN and could not be reached by eCatcher anymore.

When we changed the waiting time to 30 sec, the connection to the VPN worked again but was delayed for an estimated 30 seconds.  For us it looks like the Gateway IP was only set after the waiting time expired.  The script did not autorun properly with this waiting time either.

If you know how to make the script work properly and automatically after a restart, your help would be greatly appreciated!

Kind regards,
René

Just before Simon posted the corrected version of the Script, we came up with a solution of our own.
The script is autostarting fine after reboot if the  following code is added at the top of the script:

CheckForWan:
WanIp$ = GETSYS PRG, "WANIP"
IF (WanIp$="0.0.0.0") THEN
  Print "Modem offline"
  TSET 1,10
  ONTIMER 1, "goto CheckForWan"
Else
  Print "Modem online, IP:"; WanIp$
  TSET 1,0
  GOTO "CheckForVpn"
Endif
End
CheckForVpn:
VpnIp$ = GETSYS PRG, "VPNIP"
IF (VpnIp$="0.0.0.0") THEN
  Print "VPN not established"
  TSET 2,10
  ONTIMER 2, "goto CheckForVpn"
Else
  Print "VPN established, IP:"; VpnIp$
  TSET 2,0
  GOTO "AzureUpload"
Endif
End

AzureUpload:
rem put the uploading script here



This will  make the upload script wait until a WAN and VPN Connection is available.

Regards,
René
Reply

Richard Offline
Ewon Distributor
#12
Hi,
Today I need to make this connection, I do not get an error message when I run the Basic script and fill in the correct credentials in the Html.
But I also do not see any other printed command than "No change detected!" but values are changing! I can see them change when I read the values.
I do not see any string send out, also not after the time set at full push time.
In the Microsoft Azure Storage explorer I see that there are no errors. But how/ where can I see which data is coming in in that tool?
I hope someone can point me in the right direction here?
thanks in advance
Richard
Thanks,
Richard
Reply

pduffy64 Offline
Junior Member
#13
Hello,

I am going through the HMS eWON Flexy integration to AZURE IOT document.  I have copied the files to the Flexy via FTP, I can access the flexy but the Device Explorer can not find the Flexy.  Is there something I need to turn on on the Flexy?

Thank you,

Patrick Duffy
Reply

Richard Offline
Ewon Distributor
#14
Hi Patrick
I alsof do not get data in twin Explorer but I do get data in the iot hub and are the data in Microsoft azure storage Explorer.
Gr Richard
Thanks,
Richard
Reply

pduffy64 Offline
Junior Member
#15
I have another issue. I am stepping through the code Simon posted on here. It seems to be reading the Azureiot_parameters.txt config file and it is looking for a LF CHR$(10) as a delimiter. It seems to find the first two and then does not find any others and the loop ends and the program ends. Any ideas?

Thank you,

Patrick Duffy
Reply



Possibly Related Threads…
27-03-2020, 03:04 PM
Last Post: simon
09-03-2020, 02:29 PM
Last Post: simon
06-03-2020, 06:39 PM
Last Post: simon
24-01-2020, 09:45 PM
Last Post: tedsch
  Flexy and Microsoft Power BI Started by simon
19 Replies - 8.689 Views
10-12-2019, 11:25 AM
Last Post: simon
02-12-2019, 11:25 PM
Last Post: simon
27-11-2019, 10:30 PM
Last Post: simon
  MQTT hangs Started by Tecmafra
1 Replies - 627 Views
26-07-2019, 10:12 AM
Last Post: SivakumarRn
23-07-2019, 05:31 PM
Last Post: simon
19-07-2019, 06:00 PM
Last Post: simon



Users browsing this thread:
1 Guest(s)



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