Use CircuitPython with IoT Central!
Published Apr 02 2020 03:51 PM 2,905 Views
Microsoft

UPDATE as of 5/1/2020: This library has now been contributed to Adafruit-- we encourage you to check it out here and, if this is something you'd like to see official Azure IoT support for, please raise it as a feature request on our UserVoice page and/or provide feedback on this brief form

 

------------------------------------------------------------

 

As a Hackathon exploration, we adapted an IoT Central Python library to create an MQTT-based CircuitPython library for Azure IoTCircuitPythonAdafruit’s stripped down version of Python for embedded development, is easy to use and significantly lowers the barrier of entry for building cool IoT solutions. This library has been tested with the PyPortal device (which has a broad set of capabilities, including a TFT touchscreen display) and a PyBadge—both of these devices from Adafruit are able to connect via an ESP32 serving as a wi-fi coprocessor.  

 

ElenaHorton_0-1585867245650.png

 

 

As CircuitPython has been gaining popularity in the education and maker spaces, we hope this library offers a solid starting point for anyone wanting to play around with Azure IoT’s offerings! To quickly illustrate how easy it can be to work with CircuitPython, here’s a look at the main application code in the sample:  

 

 

 

# Set up wifi connection
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
connection = Connection()
connection.connect(spi, True) 

# Get info for your specific device configuration
id_scope = secrets['id_scope']
device_id = secrets['device_id']
primary_key = secrets['key'] 

# create your ESP32 wifi enabled device, pass in connection & wifi setup 
my_device = Device(id_scope, primary_key, device_id, IOTConnectType.IOTC_CONNECT_SYMM_KEY, socket)

my_device.connect()
my_device.on("ConnectionStatus", onconnect)
my_device.on("MessageSent", onmessagesent)
my_device.on("Command", oncommand) # write command handlers in the oncommand function
my_device.on("SettingsUpdated", onsettingsupdated)

while my_device.isConnected():
    my_device.doNext() # do the async work needed to be done for MQTT

    # Do whatever

    # For example, a sample of sending simulated telemetry
    temp = 32.0 + random.uniform(-20.0, 20.0)
    state = {
        "TestTelemetry": random.randint(0, 1024),
        "Temperature": temp
    }
    my_device.sendState(json.dumps(state))

 

 

 

While this is in its early stages, we encourage you to check it out and provide feedback on the Github repoWe also experimented with an MQTT-based MicroPython sample, which you can check out in this post 

 

If you’re interested in seeing more of CircuitPython or MicroPython in Azure IoT, let us know by filling out this quick form.   

Version history
Last update:
‎May 06 2020 09:45 PM
Updated by: