platform | device | language |
---|---|---|
debian, Linux, raspbian, Ubuntu, windows |
any |
Python |
About this document
This document describes how to run the iothub_client_sample.py Python sample application. This multi-step process includes:
- Configuring Azure IoT Hub
- Registering your IoT device
- Build and deploy Azure IoT SDK on device
You should have the following items ready before beginning the process:
- Computer with Git client installed and access to the azure-iot-sdks GitHub public repository.
- Prepare your development environment
- Setup your IoT hub
- Provision your device and get its credentials
- Make sure desktop is ready as per instructions given on Prepare your development environment.
-
Navigate to the folder python/device/samples in your local copy of the repository.
-
Open the file iothub_client_sample.py in a text editor.
-
Locate the following code in the file:
connectionString = "[device connection string]"
-
Replace
[device connection string]
with the connection string for your device. Save the changes. -
Run the sample application using the following command:
python iothub_client_sample.py
-
The sample application will send messages to your IoT hub. The iothub-explorer utility will display the messages as your IoT hub receives them.
If you receive the error "ImportError: dynamic module does not define module export function (PyInit_iothub_client)" you may be running the samples with a different version of python than the iothub_client was build with. Following the Prepare your development environment to ensure the library is built with the desired python version
There is a command line switch to test each of the AMQP, HTTP and MQTT protocols. It is also possible to simply change the transport in the sample.
```
python iothub_client_sample.py -p <mqtt|http|amqp>
```
Visual Studio Code provides an excellent environment to write and debug Python code: