In this example, you'll use the AHT20 temperature and humidity sensor to log temperature and humidity data to Microsoft Azure IoT Central.
Code the Azure IoT Central Test
Once you've finished setting up your Pico W with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT import time import os import json import busio import microcontroller import board import rtc import socketpool import wifi import adafruit_ntp import adafruit_ahtx0 from adafruit_azureiot import IoTCentralDevice # use Pico W's GP0 for SDA and GP1 for SCL i2c = busio.I2C(board.GP1, board.GP0) aht20 = adafruit_ahtx0.AHTx0(i2c) print("Connecting to WiFi...") wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) print("Connected to WiFi!") # ntp clock pool = socketpool.SocketPool(wifi.radio) ntp = adafruit_ntp.NTP(pool) rtc.RTC().datetime = ntp.datetime if time.localtime().tm_year < 2022: print("Setting System Time in UTC") rtc.RTC().datetime = ntp.datetime else: print("Year seems good, skipping set time.") # Create an IoT Hub device client and connect esp = None pool = socketpool.SocketPool(wifi.radio) device = IoTCentralDevice( pool, esp, os.getenv('id_scope'), os.getenv('device_id'), os.getenv('device_primary_key') ) print("Connecting to Azure IoT Central...") device.connect() print("Connected to Azure IoT Central!") # clock to count down to sending data to Azure azure_clock = 500 while True: try: # when the azure clock runs out if azure_clock > 500: # pack message message = {"Temperature": aht20.temperature, "Humidity": aht20.relative_humidity} print("sending json") device.send_telemetry(json.dumps(message)) print("data sent") # reset azure clock azure_clock = 0 else: azure_clock += 1 # ping azure device.loop() # if something disrupts the loop, reconnect # pylint: disable=broad-except # any errors, reset Pico W except Exception as e: print("Error:\n", str(e)) print("Resetting microcontroller in 10 seconds") time.sleep(10) microcontroller.reset() # delay time.sleep(1) print(azure_clock)
Upload the Code and Libraries to the Pico W
After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Pico W's CIRCUITPY drive.
- lib folder
- code.py
Your Pico W CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
Add Your settings.toml File
Remember to add your settings.toml file as described in the Create Your settings.toml File page earlier in the guide. You'll need to include your WIFI_SSID
and WIFI_PASSWORD
in the file. Additionally, you'll need your Azure IoT Central id_scope
, device_id
and device_primary_key
.
CIRCUITPY_WIFI_SSID = "your-ssid-here" CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here" id_scope = "your-id-scope-here" device_id = "your-device-id-here" device_primary_key = "your-device-primary-key-here"
You'll need to setup a Microsoft Azure account and create an Azure IoT Central application to properly use this example. Be sure to reference the getting started with Microsoft Azure and CircuitPython guide to follow all of the steps for this process successfully.
Run code.py
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
Every five minutes, the AHT20's temperature and humidity data will be packed into a JSON message and transmitted to your Azure IoT Central app. In the REPL, you'll see DEBUG
messages from adafruit_requests
and messages from the loop letting you know when the JSON message has been sent and the sensor readings from the AHT20.
# when the azure clock runs out if azure_clock > 500: # pack message message = {"Temperature": aht20.temperature, "Humidity": aht20.relative_humidity} print("sending json") device.send_telemetry(json.dumps(message)) print("data sent") # reset azure clock azure_clock = 0
Text editor powered by tinymce.