# Next create a device using the device template, and select Connect to get the device connection details. # Add the connection details to your secrets.py file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id # 'key' - the devices primary key # # The adafruit-circuitpython-azureiot library depends on the following libraries: # # From the Adafruit CircuitPython Bundle (https://github.com/adafruit/Adafruit_CircuitPython_Bundle): # * adafruit-circuitpython-minimqtt # * adafruit-circuitpython-requests from adafruit_azureiot import IoTCentralDevice # Create an IoT Hub device client and connect device = IoTCentralDevice(socket, esp, secrets["id_scope"], secrets["device_id"], secrets["key"]) print("Connecting to Azure IoT Central...") # Connect to IoT Central device.connect() print("Connected to Azure IoT Central!") message_counter = 60 while True: try: # Send telemetry every minute # You can see the values in the devices dashboard if message_counter >= 60:
# 'key' - the devices primary key # # The adafruit-circuitpython-azureiot library depends on the following libraries: # # From the Adafruit CircuitPython Bundle (https://github.com/adafruit/Adafruit_CircuitPython_Bundle): # * adafruit-circuitpython-minimqtt # * adafruit-circuitpython-requests # pylint: disable=wrong-import-position from adafruit_azureiot import IoTCentralDevice from adafruit_azureiot.iot_mqtt import IoTResponse # pylint: enable=wrong-import-position # Create an IoT Hub device client and connect device = IoTCentralDevice( socket, esp, secrets["id_scope"], secrets["device_id"], secrets["key"] ) # Subscribe to commands # Commands can be sent from the devices Dashboard in IoT Central, assuming # the device template and view has been set up with the commands # Command handlers need to return a response to show if the command was handled # successfully or not, returning an HTTP status code and message def command_executed(command_name: str, payload) -> IoTResponse: print("Command", command_name, "executed with payload", str(payload)) # return a status code and message to indicate if the command was handled correctly return IoTResponse(200, "OK") # Subscribe to the command execute event device.on_command_executed = command_executed
# Next create a device using the device template, and select Connect to get the device connection details. # Add the connection details to your secrets.py file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id # 'key' - the devices primary key # # The adafruit-circuitpython-azureiot library depends on the following libraries: # # From the Adafruit CircuitPython Bundle (https://github.com/adafruit/Adafruit_CircuitPython_Bundle): # * adafruit-circuitpython-minimqtt # * adafruit-circuitpython-requests from adafruit_azureiot import IoTCentralDevice # Create an IoT Hub device client and connect device = IoTCentralDevice(socket, esp, secrets["id_scope"], secrets["device_id"], secrets["key"]) # Subscribe to property changes # Properties can be updated either in code, or by adding a form to the view # in the device template, and setting the value on the dashboard for the device def property_changed(property_name, property_value, version): print("Property", property_name, "updated to", str(property_value), "version", str(version)) # Subscribe to the property changed event device.on_property_changed = property_changed print("Connecting to Azure IoT Central...")