Transmit Sensor Data using Gateway Device
In this article
In this quickstart, you send telemetry from a physical Raspberry Pi device through Azure IoT Hub to a back-end application for processing. IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the cloud for storage or processing. This quickstart uses a pre-written Python application to send the telemetry. Before you run this application, you create an IoT hub and register a device with the hub.
Prerequisites
An Azure account with an active subscription. Create one for free.
Python 3.7+. For other versions of Python supported, see Azure IoT Device Features.
Port 8883 open in your firewall. The device sample in this quickstart uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see Connecting to IoT Hub (MQTT).
Install Python Library for Azure IoT Device
Install azure.iot.device using the following command in the terminal. Click Terminal > New Terminal in VS Code and run the following command.
Use of python3
-m option with pip
command ensures that the correct version of pip
is used for package installation. In the above command pip
version corresponding to the default python3 will be used.
Download and organise sample code
Download the sample Python project in Raspberry Pi.
Right click the downloaded archive (.zip) and select Extract here
Rename the extracted folder azure-iot-samples-python-master to azure-iot-samples-python and move it to the Documents folder in Raspberry Pi.
Open azure-iot-samples-python folder in VS Code and navigate to azure-iot-samples-python/iot-hub/Quickstarts/simulated-device/SimulatedDeviceSync.py.
Replace the value of variable,
CONNECTION_STRING
, with the primary connection string copied from IoT Hub Setup process inSimulatedDeviceSync.py
.
Run
You will see the following output in the terminal.
You can monitor Monitor D2C (device-to-cloud) messages in VS code. A sample output is shown below.
Last updated