Using Client script to connect your device Azure IOT Hub

This post is to understand the Node.js code used to connect to Azure IOT Hub…the complete code can be found This post is just to explain in detail for beginners to understand this quickly.

Navigate to this link…

Now we will understand the code in the integrated console window as highlighted below…

So let’s understand each piece of it…

Firstly we need to understand about controlling the GPIO pins of the Raspberry PI…for high performance C language should be preferred language used but most developers are more comfortable with JS/Node…node.js is used.

So in order to read the pins, we should be using package require(‘wiring-pi’);

Next we will be requiring Azure IOT Device Client SDK’s to create a client for interaction with the Raspberry PI and to send messages


Based on the protocol being used, we need to import the respective package..

The following table provides the high-level recommendations for your choice of protocol:

ProtocolWhen you should choose this protocol
MQTT over WebSocket
Use on all devices that do not require to connect multiple devices (each with its own per-device credentials) over the same TLS connection.
AMQP over WebSocket
Use on field and cloud gateways to take advantage of connection multiplexing across devices.
HTTPSUse for devices that cannot support other protocols.

MQTT and AMQP are binary protocols, which result in more compact payloads than HTTPS, this sample uses MQTT Protocol for communication, hence below package is used


For bme280-sensor, a Node.js I2C module for the Bosch BME280 Humidity, Barometric Pressure, Temperature Sensor, used below


Regarding the connection string, you can follow my previous post on getting this from Azure Portal, however there is also an alternative to use SDK’s

There is a function in azure-iot-device npm (IoT Hub Device SDK for Node.js) to generate device connection string:

import { ConnectionString as DeviceConnectionString } from "azure-iot-device";
const deviceConnectionString = DeviceConnectionString.createWithSharedAccessKey(hostName, device.deviceId, device.authentication.SymmetricKey.primaryKey);

You could also refer to full code here to see how Azure IoT Toolkit generates the device connection string.

The next part is the custom logic on sending messages based on the temperature sensor to Azure IOT Hub and switching on the LED based conditions which is completely based on the needs…

Hope this helps…




Connecting Raspberry Pi using Azure IOT Online Simulator


This is my first post in the Azure IOT series….and really excited to share this content.

Have you ever tried a lot to get some hands-on experience connecting devices to Azure IOT Hub. Don’t worry…Microsoft IOT Team had made it so easy so that we can connect to a real device like Raspberry Pi without actually having any physical hardware with you.

This is all possible with Open Sourced Raspberry Pi Online Simulator…with the below link..

Once you open this…you should see the below screen….all you need to enter your IOT Device connection string…

If you check in the above image, you could see that connectionString is highlighted which you need to get from Azure Portal. So lets see how you can get this from Azure portal…

Step 1: Create a free Azure account or if you already have one proceed to next step

Step 2: Create Azure IOT Hub from Azure portal

From the Azure homepage, select the + Create a resource button, and then enter IoT Hub in the Search the Marketplace field

In the results, click Create.

Step 3: Once the deployment is succeeded and your Azure IOT Hub got created, you need to create a device to which you will be connected by navigating as below. Select IOT Devices under Explorers available in the Side pane as highlighted below

Step 4:

Click on New Device as highlighted below

Step 5: Enter Device ID and click on save as higlighted below

Step 6: Now Open your device by click on IOT Devices…and grab the primary connection string..make sure Enable connection to IOT Hub is enabled as below.

Step 7: Now go back to Raspberry PI Simulator and enter this connection string at the place shown in the first image.

Step 8: And finally boom…you will see that the message sent to Azure…and the bulb was switched on.

In the next post, will briefly describe on the Node.js code used and also on the Device SDK’s for Azure IOT so that we can gain more insight.

If you have any further questions on this….please post it here or you can reach out to Community Support on this tool at the below link…

Hope this helps….

Thank you.




Sharing thoughts and challenges on Power Platform & Azure along with respective solutions put together...

SharePains by Microsoft MVP Pieter Veenstra

Microsoft 365, Power Platform, SharePoint, Teams, Azure and Dynamics

D365 Demystified

A closer look at Microsoft Dynamics 365.

Vicky Rodgers - Microsoft Dynamics 365

Everything Microsoft Dynamics 365 for Customer Engagement

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

Microsoft Dynamics 365 UK MCT

All about Microsoft Dynamics 365

High Voltages

Arduino | ESP | Raspberry pi | IoT

Mihail's space

Just another site

Arun Potti's MS CRM blog

Microsoft Dynamics CRM

Sander van de Velde

Microsoft MVP Azure | IoT Platform Architect | Speaker about IoT | Let me add some value

Power Platform Blog

Author : Dharanidharan Balasubramaniam

That API Guy

Making the world a better place one PowerApp/Flow at a time!

Dynamics Back Page

Tips, Tricks, and General Musings on Dynamics 365 (CRM), the Power Platform, Power Apps, Power Automate, Power BI and related technologies

Azure Integration services for Dynamics 365

Azure Integration services for Dynamics 365 Unified Operations, Logic Apps, Azure Functions, REST

Power Maverick

Incredible journey into #Dynamics365, #CDS, #PowerApps #Azure and all good stuff that #Microsoft releases

Unleashing CRM

My Technical Thoughts about Microsoft Dynamics CRM- Unleashed

Srikanth Alluri

My experiences in & around Power Platform + Azure

Debajit's Power Apps & Dynamics 365 Blog

All about Power Apps & Dynamics

Rajeev Pentyala - Microsoft Power Platform

Sharing my knowledge on Power Platform, Dynamics 365, Azure & .Net Stack

Bansal Blogs - Dynamics 365, PowerApps, Microsoft Flows, Power BI

Programming demands a lot more than simple mastery in tools and technologies

%d bloggers like this: