Azure IoT And Raspberry PI - Send Sensor Data To Azure Using Python

Introduction

 
This article demonstrates how to send data from Raspberry Pi to Microsoft Azure. Microsoft provides a suite of IoT services, which you can use to create any kind of very large application.
 
About Microsoft Azure
  • Microsoft Azure is a cloud computing service created by Microsoft.
  • Azure cloud services to help your organization meet your business challenges.
  • it supports many different programming languages, tools, and frameworks.
  • Azure provides 600 different types of services.

Azure IoT Hub

  • Azure IoT hub is a managed IoT service which is hosted in the cloud
  • It lets you connect, monitor, and manage billions of IoT assets
  • This cloud-to-device connectivity means that you can receive data from your devices.
Step 1
  • First, Open the Raspberry Pi terminal. 
  • Next, we have to install some Azure IoT Package, so run some command to the Raspberry Pi and also install the Python package.
Install the below packages:
  1. sudo pip3 install azure-iot-device  
  2. sudo pip3 install azure-iot-hub  
  3. sudo pip3 install azure-iothub-service-client  
  4. sudo pip3 install azure-iothub-device-client  
This above package command requires this particular application. 
 
Step 2 - Create IoT Hub
  • Next, go to Azure portal " https://portal.azure.com ".
  • Create a resource >> IoT Hub.
Azure IoT And Raspberry PI - Send Sensor Data To Azure Using Python
 
   It is a dedicated service which is provided by Microsoft and it can be directly configured to your Raspberry Pi.
  • Next, create one IoT Hub Service and fill in the necessary details, give some name at recognizable into your IoT Hub.
  • Let us give the name as raviraw299. It is available and click on next-create.
Azure IoT And Raspberry PI - Send Sensor Data To Azure Using Python
 
Here you can decide how you want to your IoT Hub to be,  so there are  three categories provided -  S, B, F
  1. S    -   Standard (S1, S2 & S3)
  2. B    -   Basic  (B1, B2 & B3)
  3. F    -   Free (F1).
B1, B2 & B3 are very basic and the below protocol is not available here.
 
Azure IoT And Raspberry PI - Send Sensor Data To Azure Using Python
  • Click on Review+ Create button. Now this step to create the Azure IoT Hub. After creating Azure IoT Hub you have space where you can perform experimental Azure.
  • Next, create a device where you can actually receive some data from the hardware.
Step 3 - Create an IoT Device 
  • Go to the IoT Device and click on new, and give the device ID (mypi).
Three types of authentication algorithms are there.
  1.   Symmetric 
  2.   X.509 Self Signed
  3.   X.509 CA Signed.
Azure IoT And Raspberry PI - Send Sensor Data To Azure Using Python
  •  We choose the symmetric algorithm because it uses a long text string performing the authentication, and click on save.
  • Click on the device  and then you will go to all the properties related to the device. You will see something called the primary connection string and copy the connection string.
Azure IoT And Raspberry PI - Send Sensor Data To Azure Using Python
 
Step 4 
 
Create a new DHT 11 sensor sample program, the name of the program is simulateddeivce.py.
 
Source Code
  1. import random  
  2. import time  
  3.   
  4. from azure.iot.device import IoTHubDeviceClient, Message  
  5.   
  6. CONNECTION_STRING = "Connection_String Here"  
  7.   
  8. TEMPERATURE = 20.0  
  9. HUMIDITY = 60  
  10. MSG_TXT = '{{"temperature": {temperature},"humidity": {humidity}}}'  
  11.   
  12. def iothub_client_init():  
  13.     client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)  
  14.     return client  
  15.   
  16. def iothub_client_telemetry_sample_run():  
  17.   
  18.     try:  
  19.         client = iothub_client_init()  
  20.         print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )  
  21.         while True:  
  22.             
  23.             temperature = TEMPERATURE + (random.random() * 15)  
  24.             humidity = HUMIDITY + (random.random() * 20)  
  25.             msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity)  
  26.             message = Message(msg_txt_formatted)  
  27.   
  28.             if temperature > 30:  
  29.               message.custom_properties["temperatureAlert"] = "true"  
  30.             else:  
  31.               message.custom_properties["temperatureAlert"] = "false"  
  32.   
  33.             print"Sending message: {}".format(message) )  
  34.             client.send_message(message)  
  35.             print ( "Message successfully sent" )  
  36.             time.sleep(3)  
  37.   
  38.     except KeyboardInterrupt:  
  39.         print ( "IoTHubClient sample stopped" )  
  40.   
  41. if __name__ == '__main__':  
  42.     print ( "IoT Hub Quickstart #1 - Simulated device" )  
  43.     print ( "Press Ctrl-C to exit" )  
  44.     iothub_client_telemetry_sample_run()  
Now before you run the Python program create an azure shell (CLI). When you open it for the first time it will ask you to create storage.
 
See below to add the extension:
  1. az extension add --name azure-cli-iot-ext  
See below to start device monitor to check incoming DHT 11 data,
  1. az iot hub monitor-events --hub-name raviraw299 --device-id mypi  
  • hub-name    =   YourIoTHubName
  • device-id      =   MyPythonD
Step 5
 
Run the Python program simulateddevice.py
 
Output
 
Azure IoT And Raspberry PI - Send Sensor Data To Azure Using Python 
 
This program sends data every 10 seconds, let me just run this program. Exactly the same message is sent from Raspberry to Azure command-line interface (CLI). You can see there is no latency at all.
 
This is the most basic starting point of starting Azure IoT Hub.
 

Summary

 
In this article, you learned how to send data from Raspberry Pi to Microsoft Azure
 
If you have any questions/ feedback/ issues, please write in the comment box.