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…




Author: Pavan Mani Deep Y

Technology geek who loves sharing my leanings, quick tips and new features on Dynamics 365 & related tools, technologies. An Azure IOT Enthusiast....

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


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: