Scott Hanselman

Connecting my Particle Photon Internet of Things device to the Azure IoT Hub

December 20, '16 Comments [25] Posted in Azure | Hardware
Sponsored By

Particle Photon connected to the cloudMy vacation continues. Yesterday I had shoulder surgery (adhesive capsulitis release) so today I'm messing around with Azure IoT Hub. I had some devices on my desk - some of which I had never really gotten around to exploring - and I thought I'd see if I could accomplish something.

I've got a Particle Photon here, as well as a Tessel 2, a LattePanda, Funduino, and Onion Omega. A few days ago I was able to get the Onion Omega to show my blood sugar on a small OLED screen, which was cool. Tonight I'm going to try to hook the Particle Photon up to the Azure IoT hub for monitoring.

The Photon is a tiny little device with Wi-Fi built-in. It's super easy to setup and it has a cloud-based IDE with tons of examples written in C and Node.js for you to use. Particle Photon also has a node.js based command line. From there you can list out your Photons, see their available functions, and even call functions over the internet! A hacker's delight, to be sure.

Here's a standard "blink an LED" Hello world on a Photon. This one creates a cloud function called "led" and binds it to the "ledToggle" method. Those cloud methods take a string, so there's no enum for the on/off command.

int led1 = D0;
int led2 = D7;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Spark.function("led",ledToggle);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}

void loop() {
}

int ledToggle(String command) {
if (command=="on") {
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
return 1;
}
else if (command=="off") {
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
return 0;
}
else {
return -1;
}
}

From the command line I can use the Particle command line interface (CLI) to enumerate my devices:

C:\Users\scott>particle list
hansel_photon [390039000647xxxxxxxxxxx] (Photon) is online
Functions:
int led(String args)

See how it doesn't just enumerate devices, but also cloud methods that hang off devices? LOVE THIS.

I can get a secret API Key from the Particle Photon's cloud based Console. Then using my Device ID and auth token I can call the method...with an HTTP request! How much easier could this be?

C:\Users\scott\>curl https://api.particle.io/v1/devices/390039000647xxxxxxxxx/led -d access_token=31fa2e6f --insecure -d arg="on"
{
"id": "390039000647xxxxxxxxx",
"last_app": "",
"connected": true,
"return_value": 1
}

At this moment the LED on the Particle Photon turns on. I'm going to change the code a little and add some telemetry using the Particle's online code editor.

Editing Particle Photon Code online

They've got a great online code editor, but I could also edit and compile the code locally:

C:\Users\scott\Desktop>particle compile photon webconnected.ino

Compiling code for photon

Including:
webconnected.ino
attempting to compile firmware
downloading binary from: /v1/binaries/5858b74667ddf87fb2a2df8f
saving to: photon_firmware_1482209089877.bin
Memory use:
text data bss dec hex filename
6156 12 1488 7656 1de8
Compile succeeded.
Saved firmware to: C:\Users\scott\Desktop\photon_firmware_1482209089877.bin

I'll change the code to announce an "Event" when I turn on the LED.

if (command=="on") {
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);

String data = "Amazing! Some Data would be here! The light is on.";
Particle.publish("ledBlinked", data);

return 1;
}

I can head back over to the http://console.particle.io and see these events live on the web:

Particle Photon's have great online charts

Particle also supports integration with Google Cloud and Azure IoT Hub. Azure IoT Hub allows you to manage billions of devices and all their many billions of events. I just have a few, but we all have to start somewhere. ;)

I created a free Azure IoT Hub in my Azure Account...

Azure IoT Hub has charts and graphs built in

And made a shared access policy for my Particle Devices.

Be sure to set all the Access Policy Permissions you need

Then I told Particle about Azure in their Integrations system.

Particle has Azure IoT Hub integration built in

The Azure IoT SDKS on GitHub at https://github.com/Azure/azure-iot-sdks/releases have both a Windows-based Azure IoT Explorer and a command-line one called IoT Hub Explorer.

I logged in to the IoT Hub Explorer using the connection string from the Azure Portal:

iothub-explorer login "HostName=HanselIoT.azure-devices.net;SharedAccessKeyName=particle-iot-hub;SharedAccessKey=rdWUVMXs="

Then I'll run "iothub-explorer monitor-events" passing in the device ID and the connection string for the shared access policy. Monitor-events is cool because it'll hang and just output the events as they're flowing through the whole system.

IoTHub-Explorer monitor-events command line

So I'm able to call methods on the Particle using their cloud, and monitor events from within Azure IoT Hub. I can explore diagnostics data and query huge amounts of device-to-cloud data that would potentially flow in from my hardware devices.

The IoT Hub Limits are very generous for free/hobbyist users as we learn to develop. I haven't paid anything yet. However, it can scale to thousands of messages a second per unit! That means millions of messages a second if you need it.

I can definitely see how the the value an IoT Hub solution like this would add up quickly after you've got more than one device. Text files don't really scale. Even if I just IoT'ed up my house, it would be nice to have all that data flowing into a single hub I could manage and query securely.


Sponsor: Big thanks to Telerik! They recently published a comprehensive whitepaper on The State of C#, discussing the history of C#, what’s new in C# 7 and whether C# is the top tech to know. Check it out!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Tuesday, 20 December 2016 12:46:26 UTC
Very nice vacation :) I'm anxious to test it!! Tks a lot!
Wednesday, 21 December 2016 04:54:40 UTC
Great!
Wednesday, 21 December 2016 13:07:42 UTC
Its a great post. I am really pleased to used it. Thanks a lot for share this post.
Wednesday, 21 December 2016 15:46:17 UTC
Thanks Scott! You are appreciated very much by the programming community. Thank you for all you do. :-)
Kerry Jenkins
Friday, 23 December 2016 09:03:07 UTC
Hello! Excellent and decent post. I found this much informative, as to what I was exactly searching for. Thanks for such post and please keep on sharing with us.... Showbox
Monday, 26 December 2016 18:14:27 UTC
this post is very usefull! Thanks
Thursday, 29 December 2016 07:26:59 UTC
Hello! Excellent and decent post. I found this much informative, as to what I was exactly searching for. Thanks for such post and please keep on sharing with us.... Showbox
Thursday, 29 December 2016 18:50:24 UTC
Hello Scott! I appreciate, cause I found just what I used to be looking for. You have ended my four day lengthy hunt! God Bless you man. Have a nice day. I'd like to share you with Cartercoupons Gearbest Coupon and Cartercoupons Apparelnbags Promo Code as return.
Friday, 30 December 2016 08:20:01 UTC
Thanks scott for sharing this information with us. you may also check our website page : pirelli tyres .
Rosy James
Friday, 30 December 2016 17:35:08 UTC
Such an awesome idea Scott, thank you for sharing it with us!

באתרינו תוכלו להזמין נערות ליווי באזור המרכז והסביבה של ת"א-יפו.

Wednesday, 04 January 2017 14:45:09 UTC
Great idea Scott - thanks for sharing :)

בבלוג שלנו תמצאו מדריכים וטיפים לדגומא כיצד לדבר מלוכלך במיטה בעת סקס ועוד חדשות וכתבות שונות.
Wednesday, 04 January 2017 15:33:35 UTC
Thats a great idea Scott. Thanks for putting the time into producing this article and then sharing for us all to digest! Great!
Thursday, 05 January 2017 01:16:40 UTC
tank you
Thursday, 05 January 2017 09:48:05 UTC
tank you
Thursday, 05 January 2017 11:30:55 UTC
Hope you have a speedy recovery from your op.
Matt Newboult
Thursday, 05 January 2017 17:07:36 UTC
Speedy recovery Scott my man!

More info can be found here.
Monday, 09 January 2017 19:35:16 UTC
Good read, thank you Scott for sharing with us.

להזמנת נערות ליוי ומין לחצו כאן כעת.
Friday, 13 January 2017 07:24:00 UTC
Excellent post, thanks for sharing Scott!!!
http://blog10.info
Luis
Sunday, 15 January 2017 22:58:02 UTC
thinks for sharing this project
Monday, 16 January 2017 00:48:42 UTC
very nice
Monday, 16 January 2017 00:49:18 UTC
den ser godt ud :)
Monday, 16 January 2017 00:50:37 UTC
Excellent post, Thankyou
Monday, 16 January 2017 00:51:35 UTC
Good read, thank you very gut
Monday, 16 January 2017 10:26:11 UTC
nice article found it very useful.
Tuesday, 17 January 2017 15:17:24 UTC
this article so important, a content very good
Name
E-mail (will show your gravatar icon)
Home page
 
Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, li, ol, pre, strike, strong, sub, super, u, ul) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.
Live Comment Preview

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.