Electron Interfacing to Azure IoT Hub

Hello,

I am looking for examples, documentation to get Particle Electron connected with Azure IoT Hub.

Please help.

1 Like

Have you tried reading some of these?
https://community.particle.io/search?&q=Azure

1 Like

Hello @Moors7

I did refer that before and used it to create my trial application to send an event to Azure.
I have done following.

  1. Created Azure demo account
  2. Created Service Bus “mtest1” and copied the connection string
  3. Created Eventhub “testevent” in “mtest1”
  4. Created webhook using the above connection string from Step 2 as below
{
    "eventName": "testevent",
    "url": "https://mtest1.servicebus.windows.net/testevent/messages",
    "requestType": "POST",
	"json": {
		"str1": "{{data1}}",
		"published_at": "{{SPARK_PUBLISHED_AT}}",
		"coreid": "{{SPARK_CORE_ID}}"
	},
    "azure_sas_token": {
        "key_name": "RootManageSharedAccessKey",
        "key": "_Key_From_Connection_String_"
    },
	"nodefaults": true,
    "mydevices": true
}
  1. Created test firmware with a Particle Publish command as
Particle.publish("testevent","{\"data1\": " + String(11)+ "}",60,PRIVATE);
  1. When the Particle Publish is executed I can see the events in “https://dashboard.particle.io/user/logs” as below
  2. However when I see the azure event hub dashboard I see the failed request with error as per following image

Is there anything I am missing here?
Will there be any difference if I am from India timezone. As while creating the servicebus I am not getting India timezone to select. I have selected it as Central US time zone

Please help me for this.

Thanks
Mahesh

Hello,

Can someone help me on this please?

Thanks
Mahesh

You could try adding double quotes around your values

int value = 11;
Particle.publish("testevent", String::format("{\"data1\":\"%d\"}", value), PRIVATE);

Thanks for your help.

I could get the mistake, when I was copying the key from the connection string,I had taken the key but for the last character “=”. :pensive: my bad!
Now I am able to get a message in Event hub as per below image-

Regards
Mahesh

1 Like

Hi @ScruffR,

can you give me the final hint? I am sending several data via iot hub to powerbi, trying to form a json:
{
“event”: “db”,
“data”: “{ “ts”:0.000000, “lat”:51.526293, “lng”:15.408672, “alt”:0, “spd”:0.259280, “sat”:0.000000, “dop”:536876440 }”,
“published_at”: “2017-01-25T13:49:09.756Z”,
“coreid”: “550047000e51323432393339”
}

Works fine but arrives as “one” string in PowerBI and not as distinguished values.
I tried custom format and escaped values a.s.o. but cannot find a way that Azure streaming analysis separates the data correctly. Any example available how to setup the string correctly to end up with values in PowerBI?

This is what I entered to debug:
SELECT
*,
ts as time,
lat as latitude,
lng as longitude,
alt as altitude,
spd as speed,
sat as stellites,
dop as hdop,
published_at as ptime,
coreid as trackerid
INTO
PowerBI
FROM
UTMtracker

Reading through the post it seemed to me, you did it before and have fighted the walls!

I must admit I haven’t used PowerBI, so I’ll be no help about its capabilities to parse JSON

Hi ScruffR,

I got a tip and it is as silly as I thought. My problems were caused by a missing blank before the escape characters. After I added the blank and pushed the stream through analytics with select * all data were forwarded as json and each parameter was accessible in i.e.PowerBI.

String payload = String::format( "{ \"ts\":%lu, \"lat\":%f, \"lng\":%f, \"alt\":%f, \"spd\":%f, \"sat\":%f, \"dop\":%i }", gps.time.value(),gps.location.lat(),gps.location.lng(),gps.altitude.meters(),gps.speed.kmph(),gps.satellites.value(),gps.hdop.value());

So thanks for supporting me even when you couldn’t give me a detail directly.

1 Like

@copterview If your willing please do share your application and how your using it with Power BI since there are very few examples of how others are using this combo. My setup is still in the works also.

@RWB Indeed - hope this helps:

How to transmit data from elektron to PowerBI directly

My company wants a simple asset tracker that stores data in a database and visualizes them in PowerBI. To avoid a LAMP setup with API I used Microsoft's iot hub to transmit the data from several elektrons to AZURE

Parts

Particle asset tracker
Active GPS antenna

How to transmit data

  • include tinygps++

String payload = String::format( "{ "ts":%lu, "lat":%f, "lng":%f, "alt":%f, "spd":%f, "sat":%f, "dop":%i }",gps.time.value(),gps.location.lat(),gps.location.lng(),gps.altitude.meters(),gps.speed.kmph(),gps.satellites.value(),gps.hdop.value());
Particle.publish("db", payload, PRIVATE);

iot hub integration

Make sure you add blanks before every escape character when formatting the string.

Particles iot hub will not transmit json as a string unless you add the fields as custom json under advanced settings. For the case above it looks like:

{
"gpstime": "{{ts}}",
"latitude": "{{lat}}",
"longitude": "{{lng}}",
"altitude": "{{alt}}",
"speed": "{{spd}}",
"satellites": "{{sat}}",
"hdop": "{{dop}}",
"source": "{{PARTICLE_DEVICE_ID}}",
"published_at": "{{PARTICLE_PUBLISHED_AT}}"
}

So far that is the same as described here. Make sure to setup the iot hub on AZURE first and have the credentials and name of the hub available.

Azure iot hub - stream analysis

You cannot make a mistake when setting up an iot hub. Simply click and wait. Name of iot integration must comply with iot hub name indeed. iot hub receives data but to convert, store or forward them to PowerBI you want to add a stream analysis. When added to your AZURE define an stream analysis input, yes, iot hub and an output PowerBI. At that point AZURE asks you to authorize your PowerBI account.
To push the data through the analysis without changes simple add

SELECT
*
INTO
PowerBI
FROM
UTMtrack

PowerBI setup

In PowerBI, there is nothing to do. When stream analysis is started, the stream automatically shows up in PowerBI sources because you have authorized in your stream analysis

Now you can use you elektron data in PowerBi:

3 Likes

Very nice! Better than I expected!

I’m sure this will help others who journey down this road.

Thanks!

Hello,

I am trying to do something very similar, but I am having trouble getting anything besides a single line of text into IoT Hub. I am using an Electron 2G and Azure tools (IoT Hub, Streaming Analytics, and SQL Server). I followed this tutorial (https://docs.particle.io/tutorials/integrations/azure-iot-hub/) successfully and I was even able to get data flowing into a SQL Server database. Both the cli and the actual device firmware work for me in sending this simple data (see below).

Now I want to send more than just one column of data. Based on reading these posts (Webhook custom templates not working as expected and How to pass unescaped JSON to Azure IoTHub?) it seems the way to do this is to add additional JSON in the field when you set up the webhook integration.

Everything looks correct from the point of sending the formatted JSON and checking the Console, but when I check the input data coming into IoT hub, I get empty strings.

Webhook setup (I have tried this with and without the “json” tag):

Firmware to send JSON strings:

String json = String::format
("{ \"gps_time\": \"%d-%d-%dT%d:%d:%dZ\"\
, \"latitude\": \"%s\"\
, \"longitude\": \"%s\"\
, \"altitude\": \"%d\"\
, \"uncertainty\": \"%d\"\
, \"speed\": \"%d\"\
, \"direction\": \"%d\"\
, \"vertical_accuracy\": \"%d\"\
, \"sensor_used\": \"%d\"\
, \"satellites_used\": \"%d\"\
, \"antenna_status\": \"%d\"\
, \"jamming_status\": \"%d\"}",
    loc.year,
    loc.month,
    loc.day,
    loc.hour,
    loc.minute,
    loc.second,
    loc.lat,
    loc.lng,
    loc.altitude,
    loc.uncertainty,
    loc.speed,
    loc.direction,
    loc.vertical_acc,
    loc.sensor_used,
    loc.sv_used,
    loc.antenna_status,
    loc.jamming_status,
    loc.count
);

Verifying that the message is going out from the Particle platform:

Now I sample some data from the IoT input to check it:

but when I run this input into the query using “Test” I get:

  • duplicated data
  • blank strings
  • no default data (device_id, etc.) even though I did NOT specify “nodefaults = true”

Are there any other ways to troubleshoot the point in the network between the Particle Console and the Azure IoT hub? I noticed one tutorial that mentions using RequestBin, but I’m not sure if this would help me (https://docs.particle.io/guide/tools-and-features/webhooks/#debugging-with-requestbin)

Silly question, are you creating a Webhook or an ‘Azure IOT Hub’ under Integrations on the console?

I am using the Azure IoT Hub integration. Sorry if I misspoke and called it a webhook.

Lolz just checking. When I was first trying to get this to work it was seeing my multiple values as one single entry. It turned out to be a formatting thing so I would check that very carefully as well as all your variable names matching the ones specified in the JSON definition, nothing is leaping at me although you do perhaps have a lot of extra \ in your JSON string, compare it with the one above…

Oh and tools - Device Explorer shows you whats coming into your hub but into an app on your desktop.

What is Device Explorer? Is it something different than iothub-explorer?

I tried iothub-explorer and I could never get it to work. I set it to listen to my hub and I never saw any messages pass through, even though messages were going from Particle to Azure just fine.