Coder Social home page Coder Social logo

flexy205-azure's Introduction

Ewon Flexy to Microsoft Azure IoT Hub via MQTT

Introduction

About this document

This document describes how to connect an Ewon Flexy device to Microsoft Azure IoT Hub. This multi-step process includes:

  • Configuring Azure IoT Hub
  • Configuring an Ewon Flexy
  • Testing the MQTT connection with Device Explorer

Step 1: Prerequisites

You should have the following items ready before beginning the process:

  1. Ewon Flexy device, minimum FW 14.0s0
  2. Microsoft Azure Portal account: https://azure.microsoft.com/en-us/features/azure-portal/
  3. Ewon Flexy device (purchase one here: https://www.ewon.biz/contact/find-distributor)
  4. DigiCert Baltimore Certificate: https://github.com/vivekmano/flexy205-azure/blob/master/BaltimoreCyberTrustRoot.pem
  5. Microsoft DeviceExplorer: https://github.com/Azure/azure-iot-sdk-csharp/releases/download/2019-1-4/SetupDeviceExplorer.msi

Step 2: Create an Azure IoT Hub and collect Azure IoT Hub connectivity parameters

  1. Log in to the Azure Portal with your account, go to IoT Hub Go to IoT Hub

  2. Create a new resource of type “IoT Hub” Add New IoT Hub

  3. Create the IoT Hub

    1. Choose Free Trial
    2. Assign a Resource Group, or create a new one
    3. Assign your region
    4. Create an IoT Hub name and WRITE IT DOWN!
    5. Click Review and Create

    IoT Hub Options

  4. Click "Create" to finalize and confirm!

Next, go to your IoT Hub

  1. Under the "Explorers" sub-section, click on IoT Device Click IoT Device

  2. Click New

  3. Create your Device and:

    1. Enter in a device name (e.g. MyFlexy205-SAS, REMEMBER THIS!)
    2. Choose "SAS Token"
    3. Click Save

    Create Device

  4. Your device has been created!

Confirmation device created

Next, we're going to collect the Azure IoT connectivity parameters

  1. Under “Settings"
    1. Click Shared Access Policies
    2. Click iothubowner
    3. Copy your “Connection String - primary key” by clicking the Copy to clipboard icon Getting the Connection String
    • Your string should look something like: HostName=FlexyStepByStepGuide.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=blahblahblahblahblahblah=

That’s all we need from Azure! Let’s generate your SAS Token.

  1. Open up Device Explorer and paste your Connection String, click Update, then click Generate SAS. Generate SAS Token
  • The three key pieces of information we need to keep handy are:
    • Device Name (also known as DeviceId)
    • Host Name (also known as IoTHubName)
    • SAS Token (what we just generated in Device Explorer)

Step 3: Set up Ewon Flexy for MQTT communication with Azure IoT Hub

For this section, we are going to follow the recommendations given by Microsoft here: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#using-the-mqtt-protocol-directly-as-a-device

  1. Load the DigiCert Baltimore PEM certificate on to your Flexy via FTP into the /usr folder
  2. Log in to your Flexy device and ensure that you have at least one (1) tag created.
  3. Navigate to the BASIC IDE by clicking on Setup -> BASIC IDE and select the Init Section 2020-02-20 20_17_59-Window (Extra Small)
  4. Copy the code below in Appendix A into the Init Section. Find your connection string from above and input the following information:
    • DeviceID (what we named our device, I used MyFlexy205-SAS)
    • IoTHubName
    • SASToken
  5. Click File -> Save and then run the script (Run -> Run)
    • In the console window you should be able to see PUBLISH. If not, check your steps again.
  6. Open Device Explorer, click on the Data tab, and click Monitor

Congratulations! You are now sharing data with Azure IoT Hub.

Appendix A - BASIC Script for Flexy

For the latest scripts, please check the following links:

DeviceId$="MyFlexy205-SAS" //put in your own DeviceId here within the quotes
IotHubName$ ="FlexyStepByStepGuide" //put in your own IoT Hub Name here within the quotes
SASToken$="" //put in your SAS Token here within the quotes, usually starts with SharedAccessSignature
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)

MQTT "Open",DeviceId$,IotHubName$ + ".azure-devices.net"
Mqtt "SetParam","Port","8883"
MQTT "setparam", "log", "1"
MQTT "setparam", "keepalive", "20"
MQTT "setparam", "TLSVERSION", "tlsv1.2"
MQTT "setparam", "PROTOCOLVERSION", "3.1.1"
MQTT "setparam", "cafile","/usr/BaltimoreCyberTrustRoot.pem"
Mqtt "SetParam","Username",IotHubName$+ ".azure-devices.net/"+DeviceId$+"/api-version=2018-06-30"
Mqtt "SetParam","Password",SASToken$
Mqtt "Connect"

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% 
    TSET 2,Changepushtime%
    Ontimer 2, "goto MqttPublishChangedValue"
    
    Tset 1,Fullpushtime%
    Ontimer 1,"goto MqttPublishAllValue"   
    
    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: 
testtag@ = testtag@+1
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$, 1, 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$, 1, 0
   Else
     Print "Not connected"
   Endif
End

flexy205-azure's People

Contributors

vivekmano avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

hikariwin gomzy04

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.