Azure IOT cloud

Any idea for data send to Microsoft azure IOT using eWON Flexy.

Hello @admin-ashley

Thanks for reply

If is it passable Data send to Azure IOT Hub using http Request.

@udayakumar

We can absolutely do a post! Below is an example using a BASIC script – is this what you were after?

PUTHTTP u$+":"+p$+"@"+s$, "/tags.php", "TagName1="+STR$ Tag@", "", "Failed"

The above example would send the tag data TagName1 to server s$ with username u$ and password p$ in the URI of /tags.php. You can call the value of the tag by using its name followed by an @ sign. Upon failure, the error string “Failed” will be presented. To use HTTPS, simply precede the server’s address with “https://”

To find more information on PUTHTTP requests, check out page 90 of our Programming Reference Guide, linked below.

https://developer.websupport.ewon.biz/system/files_force/RG-006-0-EN-(Programming%20Reference%20Guide).pdf

Please let me know if the above solution answers your question, or if you have any further questions.

Best regards,
Ashley

1 Like

Hello ashley

Thanks for reply.

I try to send a data to Microsoft Azure IOT HUB using REQUESTHTTPX command in ewon basic script. But it doesn’t work.

Do You have any sample ewon Basic script code using REQUESTHTTPX command to send a data to Microsoft Azure IOT HUB.

@udayakumar

No problem! A REQUESTHTTPX script would be similar to the PUTHTTP example above, but we now recommend using the PUTHTTP script over the REQUESTHTTPX script since it now supports HTTPS. An example of REQUESTHTTPX is below:

REQUESTHTTPX u$+":"+p$+"@"+s$, "/tags.php", "TagName1="+STR$ Tag@", "", "Failed"

Please let me know if this helps!

Ashley

1 Like

@admin-ashley

Thanks for continuous reply

Do you have any eWON basic script for send a data to Microsoft Azure Iot cloud using REQUESTHTTPX command.

Thanks & regards,
udayakumar

@udayakumar

My team and I were able to successfully send data to the Azure IOT Hub. We discovered that the REQUESTHTTPX command was breaking up the Authorization header into two segments, and we were able to get it to send correctly using a URL encoder/decoder (I used the one here).

A. Generating a SAS Token

First, you will want to generate a SAS Token for your device. You can generate a SAS token using the Device Explorer tool we used previously (I was also able to successfully send data to a device I created using both Device Explorer and iothub-explorer using Node.js).

The below example is from a SAS key I generated using device explorer for a device I created called “flexy”.

  1. In Device Explorer, click on the Management tab.
  2. Select the device you want to generate a SAS Token for.
  3. Click on SAS Token, and a SASTokenForm will pop up.
  4. Make sure the correct device is selected, set TTL (time to live in days), then click on Generate.

The form will generate a SAS Token, but you only want to copy from the second “SharedAccessSignature” to the end (see below, I’ve hidden the private keys).

image

B. Encoding the SAS Token

  1. Navigate to a URL Encoder/Decoder like the one here.
  2. Paste the SAS Token into the textarea and click “Encode” – it should return the encoded form of the SAS token that you can then include in your Flexy’s script.

C. Put together the REQUESTHTTPX Script

Again, I placed the below script into the Flexy’s Init Section so that it runs as soon as I start the scripting engine.

url$ = "https://YOURIOTHUBURL.azure-devices.net/devices/YOURDEVICE/messages/events?api-version=2016-02-03"
method$ = "POST"
saskey$ = "INSERT YOUR SAS TOKEN FROM STEP B HERE"
header$ = "Authorization=" + saskey$ + "&Content-Type=application/json"
REQUESTHTTPX url$, method$, header$, '{ "testtag": "testvalue" }'

Below is a snip from the data monitoring tag in Device Explorer. Under the Event Hub Data, you can see the two messages that my Flexy sent.

image

Reusable Function

Below is a function you can use that takes a tag name as a parameter.

FUNCTION postToAzure($Tagname$)
    url$ = "https://YOURIOTHUBURL.azure-devices.net/devices/YOURDEVICE/messages/events?api-version=2016-02-03"
    method$ = "POST"
    Tagvalue% = GETIO $Tagname$
    sasKey$ = "YOUR SAS TOKEN FROM STEP B"
    Tagvalue_String$ = STR$ Tagvalue%
    DataToSend$ = '{"' + $Tagname$ + '":' + Tagvalue_String$ + '}'
    header$ = "Authorization=" + saskey$ + "&Content-Type=application/json"
    REQUESTHTTPX url$, method$, header$, DataToSend$
    PRINT "Value of " + $Tagname$ + " sent to Azure IoT Hub"
ENDFN  

I set up an ONCHANGE command to trigger the function, using the example tag name “Tank Level”.

Calling the Function
ONCHANGE 'TankLevel', '@postToAzure("TankLevel")'

I was able to change the value of my tag TankLevel and see the messages coming through using the data monitoring feature of Device Explorer (see below).
image

As you can see, you can customize this function to work with any tag value that you want to monitor in your Azure IOT Hub. Additionally, if you have more than one Flexy, just customize the url$ and sasKey$ values to reflect the device you want to communicate with.

I hope this provides the direction you were looking for. Please let us know if we can help with anything else!

Best,
Ashley

3 Likes

@ashley_hms

Thank you very much.

I tested. It is work. thank a lot ashley.

thanks & regards,
udayakumar.

@udayakumar

My pleasure! Have a great rest of your week and don’t hesitate to reach out with any other questions.

Best,
Ashley

@ashley_hms

No further Question. thanks

Hello support

That URL, where do I generate it within azure?
I already generated the SAS token.

In this particular URL:

https://YOURIOTHUBURL.azure-devices.net/devices/YOURDEVICE/messages/events?api-version=2016-02-03

It just uses your IOT Hub URL and your device name. However, that may be outdated. Please see the latest API documentation for the Azure IOT Hub here:

https://docs.microsoft.com/en-us/rest/api/iothub/iothubresource

Hello kyle

Thanks for the information, apparently they didn’t update the url, I found one for tag updates, I don’t know if it will be stored in the cloud, please confirm me
PATCH https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/IotHubs/{resourceName}?api-version=2018-04-01

Yes, that will update existing IoT Hub tags.