Both Azure IoT Hub Device example and Azure IoT Device Provisioning Service enrollment example, written in Python. It supports both D2C and C2D communication.
Check out the blog post behind this sample code for more background information.
Both examples are exposing the same logic.
This code expects an Environment variable named 'IOTHUB_DEVICE_CONNECTION_STRING' to be present. Otherwise the app will fail to authenticate. See the blog post for instructions.
This code expects Environment variables named "PROVISIONING_HOST", "PROVISIONING_IDSCOPE", "PROVISIONING_REGISTRATION_ID", and "PROVISIONING_SYMMETRIC_KEY".
On startup, the device will ask for any Device twin information. It will receive both the desired and reported properties.
You can send an interval (in seconds) to 'SetTelemetryInterval'. No JSON format neede, junst ths integer.
Just send some string to the device. It will show the received text in the console output.
Send a Device twin desired property change. It will be shown in the console output.
The device will respond with a reported property update.
This code is based on thiese resources:
- https://github.com/Azure-Samples/azure-iot-samples-python/tree/master/iot-hub/Quickstarts/simulated-device-2
- https://github.com/Azure/azure-iot-sdk-python/blob/main/azure-iot-device/samples/sync-samples/receive_message.py
- https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-python-twin-getstarted#create-the-device-app
Check out the details there for more indept information.
if packages are missing, like Azure, try:
pip install azure.iot.device
Check the 'from' lines in the code for package names.