Connecting NodeMCU to Microsoft Azure IoT Hub - Part 2

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

At ThingForward, we keep examining different features of cloud services for different use cases of IoT applications. In the first part of our series, we started to use Microsoft Azure Cloud Services like IoT Hub. Now it’s time to test direct messaging and data storage on the cloud.

If you haven’t read the first in the series yet, click here to check it out!

The credits of this blogpost go to: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-store-data-in-azure-table-storage
The original code: https://github.com/Azure-Samples/iot-hub-feather-huzzah-client-app
The repository needed for blogpost: https://github.com/emirez/iot-azure-bme280.git

Today we will be going further with our previous setup, NodeMCUv2 with BME280 sensor, and we will cover:

  • Using the IoT Hub that we created on Azure
  • Sending the sensor data to the cloud
  • Invoking methods on the device by using cloud
  • Storing the sensor data on cloud

Image 1: Today’s Setup

Image 1: Today's Setup

Direct Methods to Device

Microsoft Azure IoT Hub tool has its own features such as direct methods and direct messaging. We covered direct messaging in our first blogpost, and today we’ll be testing direct methods.

Basically, this feature allows you to invoke methods on your device. Please navigate yourself to IoT Hub > Explorers > IoT Devices and click on the device.
After that, choose Direct Method. Now you will select your device ID and call the method name you have already programmed.

In our code, stop and start methods were already there and ready to be invoked. Calling stop method with an additional message looks like this. Please remember that you can only send messages in JSON format. Click on Invoke Method on top.

Image 2: Calling Stop Method

Image 2: Calling Stop Method

Here is the console output:

Try to invoke method stop.

Stop sending temperature and humidity data.

By invoking the stop method, the MCU stopped sending the data. Now let's test start method in order to wake our MCU up:

Image 3: Calling Start Method

Image 3: Calling Start Method

Here is the console output:

Try to invoke method start.

Start sending temperature and humidity data.

Sending message: {"deviceId":"Feather HUZZAH ESP8266 WiFi","messageId":2503,"temperature":19.23,"humidity":47}.

IoTHubClient accepted the message for delivery.

You can always add new methods in your code in order to invoke them. To create new method behaviours, please open the iotHubClient.ino file under your project tree and edit following part:

if (strcmp(methodName, "start") == 0)

 {
    start();
 }

 else if (strcmp(methodName, "stop") == 0)

 {
    stop();
 }

else

 {
    Serial.printf("No method %s found.\r\n", methodName);
    responseMessage = notFound;
    result = 404;
 }

Azure Data Storage

Another powerful service of Microsoft Azure is its data storage tool. In order to start, create a new source and follow Storage > Storage Account.

Image 4: Data Storage Service

Image 4: Data Storage Service

Give it a name, select a plan, give your closest location and save it.

Image 5: Data Storage Service Settings

Image 5: Data Storage Service Settings

We need to select the storage unit as an endpoint from IoT Hub for reaching the sensor data. To do so, please navigate to IoT Hub > Messaging > Endpoints and click on add.

Image 6: IoT Hub Integration

Image 6: IoT Hub Integration

Give it a name, change endpoint type, select it as a storage and click on OK.

Image 7: IoT Hub Integration

Image 7: IoT Hub Integration

The last step of integration is adding a route. To do so, navigate to Messaging > Routes and click on add.

Image 8: IoT Hub Route

Image 8: IoT Hub Route

Give it a name, choose data source, endpoint and click on save.

Image 9: IoT Hub Route Settings

Image 9: IoT Hub Route Settings

In order to see the messages in storage, open your data storage service on Azure and navigate to your container and choose files . To do so, please follow the directions in the image:

Image 10: Data Storage Service and Sensor Data
Image 11: Data Storage Service and Sensor Data
Image 12: Data Storage Service and Sensor Data

Images 10-12: Data Storage Service and Sensor Data

Mission accomplished! In the last image, you can see the sensor data saved on data storage service.

Conclusion

That's it! Now you can easily store your data on the cloud and invoke methods via cloud messages. There are plenty of plans and storage types that you can customise according to your needs. This concludes the second part of Microsoft Azure IoT services. With the upcoming posts about other cloud services, we will examine different providers' various services with a view to increasing the efficiency of IoT applications.

Stay tuned!

Eren

Follow ThingForward on Twitter, Facebook and Linkedin!