The Adafruit_CircuitPython_AdafruitO module allows you to easily write CircuitPython code which can interact with Adafruit IO.
You'll need to install the Adafruit CircuitPython Adafruit IO and the Adafruit CircuitPython ADT7410 libraries on your PyPortal.
First make sure you are running the latest version of Adafruit CircuitPython for your board.
Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.
Once you have your CircuitPython libraries installed, let's get your PyPortal connected to Adafruit IO and the internet. To this, you'll create a settings file.
If you have not yet set up a settings.toml file in your CIRCUITPY
drive and connected to the internet using it, follow this guide and come back when you've successfully connected to the internet.
Next, you should add your Adafruit IO Username and Adafruit IO Key to the settings.toml file.
Your settings.toml file should look like this:
CIRCUITPY_WIFI_SSID="your-wifi-ssid" CIRCUITPY_WIFI_PASSWORD="your-wifi-password" ADAFRUIT_AIO_USERNAME="my_username" ADAFRUIT_AIO_KEY="my_key" timezone="America/New_York" # http://worldtimeapi.org/timezones
Change my_username
and my_key
to your Adafruit IO username and the secret key.
After you finish editing settings.toml, make sure to save the file (cmd/ctrl+s).
Code
Using a text-editor (we like Mu since it has a built-in serial REPL), copy the code below to your CircuitPython board,and save it as code.py.
Click the Download Project Bundle button below to download a zip containing all of the required project files and libraries. Unzip it and copy the files to the CIRCUITPY drive.
# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries # # SPDX-License-Identifier: MIT """ PyPortal IOT Data Logger for Adafruit IO Dependencies: * CircuitPython_ADT7410 https://github.com/adafruit/Adafruit_CircuitPython_ADT7410 * CircuitPython_AdafruitIO https://github.com/adafruit/Adafruit_CircuitPython_AdafruitIO """ from os import getenv import time import board import busio from digitalio import DigitalInOut from analogio import AnalogIn # ESP32 SPI from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager # Import NeoPixel Library import neopixel # Import Adafruit IO HTTP Client from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError # Import ADT7410 Library import adafruit_adt7410 # Timeout between sending data to Adafruit IO, in seconds IO_DELAY = 30 # Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml # (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) ssid = getenv("CIRCUITPY_WIFI_SSID") password = getenv("CIRCUITPY_WIFI_PASSWORD") aio_username = getenv("ADAFRUIT_AIO_USERNAME") aio_key = getenv("ADAFRUIT_AIO_KEY") if None in [ssid, password, aio_username, aio_key]: raise RuntimeError( "WiFi and Adafruit IO settings are kept in settings.toml, " "please add them there. The settings file must contain " "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " "'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum." ) # PyPortal ESP32 Setup esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) esp32_reset = DigitalInOut(board.ESP_RESET) spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) # Create an instance of the Adafruit IO HTTP client io = IO_HTTP(aio_username, aio_key, wifi) try: # Get the 'temperature' feed from Adafruit IO temperature_feed = io.get_feed('temperature') light_feed = io.get_feed('light') except AdafruitIO_RequestError: # If no 'temperature' feed exists, create one temperature_feed = io.create_new_feed('temperature') light_feed = io.create_new_feed('light') # Set up ADT7410 sensor i2c_bus = busio.I2C(board.SCL, board.SDA) adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48) adt.high_resolution = True # Set up an analog light sensor on the PyPortal adc = AnalogIn(board.LIGHT) while True: try: light_value = adc.value print('Light Level: ', light_value) temperature = adt.temperature print('Temperature: %0.2f C'%(temperature)) print('Sending to Adafruit IO...') io.send_data(light_feed['key'], light_value) io.send_data(temperature_feed['key'], temperature, precision=2) print('Sent to Adafruit IO!') except (ValueError, RuntimeError, ConnectionError, OSError) as e: print("Failed to get data, retrying\n", e) wifi.reset() continue print('Delaying {0} seconds...'.format(IO_DELAY)) time.sleep(IO_DELAY)
Before running the code, verify CIRCUITPY looks like the following.

From the Mu Editor, click the Serial button to open the REPL. You should see the REPL displaying the temperature and light values from the PyPortal's onboard sensors, and sending the data to Adafruit IO:
Light Level: 37376 Temperature: 31.60 C Sending to Adafruit IO... Sent to Adafruit IO! Delaying 30 seconds...
Open the Adafruit IO Dashboard you created earlier. Notice that the fill and values of the gauges change as values are sent from your PyPortal to Adafruit IO.
Then, leave the PyPortal running for a while and come back later to see new data appear on the line graph.
Change the IO_DELAY
value at the top of the program.
For example, if you wish to send data to Adafruit IO every minute, change the following line from:
IO_DELAY = 30
to
IO_DELAY= 60
Note: Setting this value lower than 30 seconds will likely cause Adafruit IO to throw a throttling error - you can only send 30 data points per minute (or 60 data points per minute with Adafruit IO Plus) to Adafruit IO.
By default, Adafruit IO supports six points of precision (decimal places). However, in the code, you are only sending two .
To increase the precision of the temperature values you're sending from 2 to 4, modify the following line from:
io.send_data(temperature_feed['key'], temperature, precision=2)
to:
io.send_data(temperature_feed['key'], temperature, precision=4)
For more examples and ideas - check out the Adafruit IO category on the Adafruit Learning System.
On the CircuitPython Adafruit IO library repository, you'll find we've included lots of examples for sending data, interacting with your CircuitPython board's digital inputs and outputs, feed/group/data interaction, and more.
Page last edited April 09, 2025
Text editor powered by tinymce.