Use CircuitPython with IoT Central!

Published Apr 02 2020 03:51 PM 2,231 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.   

%3CLINGO-SUB%20id%3D%22lingo-sub-1276986%22%20slang%3D%22en-US%22%3EUse%20CircuitPython%20with%20IoT%20Central!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1276986%22%20slang%3D%22en-US%22%3E%3CP%20data-unlink%3D%22true%22%3E%3CSTRONG%3EUPDATE%20as%20of%205%2F1%2F2020%3A%3C%2FSTRONG%3E%26nbsp%3BThis%20library%20has%20now%20been%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fadafruit%2FAdafruit_CircuitPython_AzureIoT%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Econtributed%20to%20Adafruit%3C%2FA%3E--%20we%20encourage%20you%20to%20check%20it%20out%20here%20and%2C%20if%20this%20is%20something%20you'd%20like%20to%20see%20official%20Azure%20IoT%20support%20for%2C%20please%20raise%20it%20as%20a%20feature%20request%20on%20our%20%3CA%20href%3D%22https%3A%2F%2Ffeedback.azure.com%2Fforums%2F321918-azure-iot%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EUserVoice%20page%3C%2FA%3E%20and%2For%20provide%20feedback%20on%20%3CA%20href%3D%22https%3A%2F%2Fforms.office.com%2FPages%2FResponsePage.aspx%3Fid%3Dv4j5cvGGr0GRqy180BHbRyKaGeYFaYdNly2bhngkmi5URUhKRFFSUVZWM1ZPNklYNEtOUThOT1dSTS4u%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Ethis%20brief%20form%3C%2FA%3E.%26nbsp%3B%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-center%22%20data-unlink%3D%22true%22%3E------------------------------------------------------------%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20data-contrast%3D%22auto%22%3EAs%20a%20Hackathon%20exploration%2C%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3Ewe%20adapted%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%26nbsp%3Ban%20IoT%20Central%20Python%20library%20to%20c%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3Ereate%20an%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Felhorton%2FCircuitpython-AzureIoT%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%20data-contrast%3D%22none%22%3EMQTT-based%20CircuitPython%20library%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%26nbsp%3Bfor%20Azure%20IoT%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E.%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fcircuitpython.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%3CSPAN%20data-contrast%3D%22none%22%3ECircuitPython%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%2C%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3EAdafruit%E2%80%99s%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3Estripped%20down%20version%20of%20Python%20for%20embedded%20development%2C%20is%20easy%20to%20use%20and%20significantly%20lowers%20the%20barrier%20of%20entry%20for%20building%20cool%20IoT%20solutions.%20This%20library%20has%20been%20tested%20with%20the%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.adafruit.com%2Fproduct%2F4116%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EPyPortal%3C%2FA%3E%20device%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%26nbsp%3B(which%20has%20a%20broad%20set%20of%20capabilities%2C%20including%20a%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3ETFT%20touchscreen%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%26nbsp%3Bdisplay)%20and%20a%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fcircuitpython.org%2Fboard%2Fpybadge%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%3CSPAN%20data-contrast%3D%22none%22%3EPyBadge%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%E2%80%94both%20of%20these%20devices%20from%20Adafruit%20are%20able%20to%20connect%20via%20an%20ESP32%20serving%20as%20a%20wi%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E-%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3Efi%20coprocessor.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A259%7D%22%20data-wac-het%3D%221%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22ElenaHorton_0-1585867245650.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F181759iF410A853CDDDD93C%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22ElenaHorton_0-1585867245650.png%22%20alt%3D%22ElenaHorton_0-1585867245650.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335551550%26quot%3B%3A2%2C%26quot%3B335551620%26quot%3B%3A2%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A259%7D%22%20data-wac-het%3D%221%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20data-contrast%3D%22auto%22%3EAs%26nbsp%3BCircuitPython%26nbsp%3Bhas%20been%20gaining%20popularity%20in%20the%20education%20and%20maker%20spaces%2C%20we%20hope%20this%20library%20offers%20a%20solid%20starting%20point%20for%20anyone%20wanting%20to%20play%20around%20with%20Azure%20IoT%E2%80%99s%20offerings!%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%26nbsp%3BTo%20quickly%20illustrate%20how%26nbsp%3Beasy%26nbsp%3Bit%20can%20be%20to%20work%20with%26nbsp%3BCircuitPython%2C%20here%E2%80%99s%20a%20look%20at%20the%20main%20application%20code%20in%20the%20sample%3A%3C%2FSPAN%3E%26nbsp%3B%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A259%7D%22%20data-wac-het%3D%221%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%23%20Set%20up%20wifi%20connection%0Aspi%20%3D%20busio.SPI(board.SCK%2C%20board.MOSI%2C%20board.MISO)%0Aconnection%20%3D%20Connection()%0Aconnection.connect(spi%2C%20True)%20%0A%0A%23%20Get%20info%20for%20your%20specific%20device%20configuration%0Aid_scope%20%3D%20secrets%5B'id_scope'%5D%0Adevice_id%20%3D%20secrets%5B'device_id'%5D%0Aprimary_key%20%3D%20secrets%5B'key'%5D%20%0A%0A%23%20create%20your%20ESP32%20wifi%20enabled%20device%2C%20pass%20in%20connection%20%26amp%3B%20wifi%20setup%20%0Amy_device%20%3D%20Device(id_scope%2C%20primary_key%2C%20device_id%2C%20IOTConnectType.IOTC_CONNECT_SYMM_KEY%2C%20socket)%0A%0Amy_device.connect()%0Amy_device.on(%22ConnectionStatus%22%2C%20onconnect)%0Amy_device.on(%22MessageSent%22%2C%20onmessagesent)%0Amy_device.on(%22Command%22%2C%20oncommand)%20%23%20write%20command%20handlers%20in%20the%20oncommand%20function%0Amy_device.on(%22SettingsUpdated%22%2C%20onsettingsupdated)%0A%0Awhile%20my_device.isConnected()%3A%0A%20%20%20%20my_device.doNext()%20%23%20do%20the%20async%20work%20needed%20to%20be%20done%20for%20MQTT%0A%0A%20%20%20%20%23%20Do%20whatever%0A%0A%20%20%20%20%23%20For%20example%2C%20a%20sample%20of%20sending%20simulated%20telemetry%0A%20%20%20%20temp%20%3D%2032.0%20%2B%20random.uniform(-20.0%2C%2020.0)%0A%20%20%20%20state%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22TestTelemetry%22%3A%20random.randint(0%2C%201024)%2C%0A%20%20%20%20%20%20%20%20%22Temperature%22%3A%20temp%0A%20%20%20%20%7D%0A%20%20%20%20my_device.sendState(json.dumps(state))%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20data-contrast%3D%22auto%22%3EWhile%20this%20is%20in%20its%20early%20stages%2C%20we%20encourage%20you%20to%20check%20it%20out%20and%20provide%20feedback%20on%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Felhorton%2FCircuitpython-AzureIoT%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%20data-contrast%3D%22none%22%3Ethe%20Github%20repo%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-contrast%3D%22auto%22%3E.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3EWe%20also%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3Eexperimented%20with%20an%20MQTT-based%26nbsp%3BMicroPython%26nbsp%3Bsample%2C%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3Ewhich%20you%20can%20check%20out%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Finternet-of-things%2Fmicro-python-on-esp32-to-send-telemetry-to-azure-iot-hub-with%2Fba-p%2F1236022%22%20target%3D%22_blank%22%3E%3CSPAN%20data-contrast%3D%22none%22%3E%26nbsp%3Bin%20this%20post%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-contrast%3D%22auto%22%3E.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A259%7D%22%20data-wac-het%3D%221%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EI%3C%2FSTRONG%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%3CSTRONG%3Ef%20you%E2%80%99re%20interested%3C%2FSTRONG%3E%20in%20seeing%20more%20of%26nbsp%3BCircuitPython%26nbsp%3Bor%26nbsp%3BMicroPython%26nbsp%3Bin%20Azure%20IoT%2C%20%3CSTRONG%3Elet%20us%20know%3C%2FSTRONG%3E%20by%20filling%20out%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22auto%22%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fforms.office.com%2FPages%2FResponsePage.aspx%3Fid%3Dv4j5cvGGr0GRqy180BHbRyKaGeYFaYdNly2bhngkmi5URUhKRFFSUVZWM1ZPNklYNEtOUThOT1dSTS4u%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%20data-contrast%3D%22none%22%3Ethis%20quick%20form%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-contrast%3D%22auto%22%3E.%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A259%7D%22%20data-wac-het%3D%221%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1276986%22%20slang%3D%22en-US%22%3E%3CP%3ECheck%20out%20a%20new%20experimental%20library%20and%20sample%20for%20easily%20connecting%20Adafruit%20devices%20to%20Azure%20IoT%20with%20CircuitPython%20and%20MQTT.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Version history
Last update:
‎May 06 2020 09:45 PM
Updated by: