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:
- sudo pip3 install azure-iot-device
- sudo pip3 install azure-iot-hub
- sudo pip3 install azure-iothub-service-client
- 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.
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.
Here you can decide how you want to your IoT Hub to be, so there are three categories provided - S, B, F
- S - Standard (S1, S2 & S3)
- B - Basic (B1, B2 & B3)
- F - Free (F1).
B1, B2 & B3 are very basic and the below protocol is not available here.
- 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.
- Symmetric
- X.509 Self Signed
- X.509 CA Signed.
- 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.
Step 4
Create a new DHT 11 sensor sample program, the name of the program is simulateddeivce.py.
Source Code
- import random
- import time
-
- from azure.iot.device import IoTHubDeviceClient, Message
-
- CONNECTION_STRING = "Connection_String Here"
-
- TEMPERATURE = 20.0
- HUMIDITY = 60
- MSG_TXT = '{{"temperature": {temperature},"humidity": {humidity}}}'
-
- def iothub_client_init():
- client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
- return client
-
- def iothub_client_telemetry_sample_run():
-
- try:
- client = iothub_client_init()
- print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )
- while True:
-
- temperature = TEMPERATURE + (random.random() * 15)
- humidity = HUMIDITY + (random.random() * 20)
- msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity)
- message = Message(msg_txt_formatted)
-
- if temperature > 30:
- message.custom_properties["temperatureAlert"] = "true"
- else:
- message.custom_properties["temperatureAlert"] = "false"
-
- print( "Sending message: {}".format(message) )
- client.send_message(message)
- print ( "Message successfully sent" )
- time.sleep(3)
-
- except KeyboardInterrupt:
- print ( "IoTHubClient sample stopped" )
-
- if __name__ == '__main__':
- print ( "IoT Hub Quickstart #1 - Simulated device" )
- print ( "Press Ctrl-C to exit" )
- 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:
- az extension add --name azure-cli-iot-ext
See below to start device monitor to check incoming DHT 11 data,
- 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
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.