Connecting NodeMCU to Microsoft Azure IoT Hub - Part 1

Welcome to the first part of our Microsoft Azure Cloud Services blogpost series!

At ThingForward, we are combining Embedded Development and Cloud Services in order to increase the productivity of IoT applications. Synchronizing the smart devices via cloud services/tools opens the doors of efficient connectivity between "the things" and cloud for real life implementations of IoT Today we will be connecting our NodeMCU to the Microsoft's Cloud service called Azure.

By following this blogpost, you will be able to set up your device with any sensor you want, and you will be able to connect it to the Azure services on cloud. After that, several features of Azure will be tested.

The credits of this blogpost go to:

The original code:

This blogpost captures:

  • Using NodeMCUv2 with BME280 Sensor
  • Creating IoT Hub on Azure
  • Registering our device on IoT Hub
  • Sending the sensor data to the cloud
  • Cloud to device messaging

Here is our basic setup for today, don't forget to connect your BME280 sensor to I2C ports.

Image 1: Today’s Setup

Image 1: Today's Setup

For today's task, you will need:

  • NodeMCUv2
  • BME280 Sensor
  • Arduino IDE
  • Microsoft Azure Subscription
  • Optional: Groove Shield

Let's proceed to and create a hub.

  • Click on Create a Source > Internet of Things > IoT Hub
  • Specify the hub behaviour as follows:

Image 2: Azure IoT Hub

Image 2: Azure IoT Hub

Creating a hub can take up to 5 minutes. Now you can refill your coffee.

The Azure IoT Hub is ready!

Now, it is time to register our IoT device, NodeMCUv2. To do so;

  • Open your Azure Hub.
  • Click on IoT Devices under Device Manager.
  • Add a new device
  • Give it a name, check the box and save.

Image 3: Registering The Device

Image 3: Registering the Device

Click on the created device and open it.

Image 4: IoT Devices

Image 4: IoT Devices

Save the Primary Key:

Image 5: Devices Key

Image 5: Devices Key

After that, let’s clone the repository:

$ git clone

Open your Arduino IDE and plug your NodeMCU to your computer. After that, install these libraries from Arduino Library Manager:

  • AzureIoTHub
  • AzureIoTUtility
  • AzureIoTProtocol_MQTT
  • ArduinoJson
  • DHT sensor library
  • Adafruit Unified Sensor

After that, update your SSID Name, Password and Primary Key under your credentials.ino file from lines 28, 30, 32. Upload the code and open your serial monitor.

connected with belkin.36ef, channel 1
dhcp client start...
Connected to wifi belkin.36ef.
Fetched NTP epoch time is: 28812.
IoT Hub SDK for C, version 1.1.29
Sending message: {"deviceId":"Feather HUZZAH ESP8266 WiFi","messageId":1,"temperature":19.44,"humidity":42}.
IoTHubClient accepted the message for delivery.
pm open,type:2 0

Cloud to Device Messaging

This is a feature of Azure IoT Hub that lets you send messages to your device. You have two options to do so:

1) via Azure Portal on IoT Hub

▪ Open Device Management > Device > Message to Device ▪ Select your node and write your message. ▪ Click on Send Message and look at your serial monitor.

Image 6: Azure IoT Devices

Image 6: Azure IoT Devices

Image 6: Azure Messaging Tool

Image 7: Azure Messaging Tool

Image 8: Sending a Message to the Device

Image 8: Sending a Message to the Device

On the console, here is the output:

IoTHubClient accepted the message for delivery.
Receive C2D message: Hello Device!
Message sent to Azure IoT Hub

By giving the key and word, add this message to the hash and you can use it for further needs.

2) via Command Line Tool

First install the tool:

$ npm install -g iothub-explorer

Find your general key and device ID.

Image 9: Device Settings and Keys

Image 9: Device Settings and Keys

Now you can monitor these cloud-to-device messages by typing:

$ iothub-explorer monitor-events <device-id> --login "<IoTHubConnectionString>"

Here is the output:

==== From: 'TFNode' at '2018-05-04T08:18:51.812Z' ====
 "deviceId": "Feather HUZZAH ESP8266 WiFi",
 "messageId": 166,
 "temperature": 18.99,
 "humidity": 49
---- application properties ----
 "temperatureAlert": "false"

Now try to send cloud-to-device messages by command:

$ iothub-explorer login "<IoTHubConnectionString>"
$ iothub-explorer send <device-id> <message>
Message sent with id: 50d3c745-45b9-46aa-a148-680928dfda46

Here is the console output:

Receive C2D message: HelloDevice.
Message sent to Azure IoT Hub


IoT Hub is not the only feature that brings cloud synchronisation to Azure, but it’s a good start to set up your hardware and library environment.

This concludes the first part of Microsoft Azure IoT services. With the upcoming blogposts about Microsoft Azure and other cloud services, we will extend the usage of these tools by storing the data on cloud, visualising the real time sensor data and more. Stay tuned!


Follow ThingForward on Twitter, Facebook and Linkedin!