Adafruit IO is an internet of things platform (designed by Adafruit!) to help connect your project to the internet. You're going to send the Pico's CPU temperature to Adafruit IO and display it on a dashboard page. You'll add a toggle block to turn on or off the Pico's LED from the internet.
Secrets File Setup for Adafruit IO
To connect to Adafruit IO, you will need to start by modifying your secrets file to include your Adafruit IO credentials.
- If you do not already have a secrets file, visit this page and come back here when you've created a secrets file
Navigate to io.adafruit.com and click My Key to obtain your Adafruit IO username and Active Key. Add these credentials into the secrets file, which will now look something like the following:
# This file is where you keep secret settings, passwords, and tokens! # If you put them in the code you risk committing that info or sharing it secrets = { 'ssid' : 'home ssid', 'password' : 'my password', 'timezone' : "America/New_York", # http://worldtimeapi.org/timezones 'aio_username': 'MY_ADAFRUIT_IO_USERNAME', 'aio_key': 'MY_ADAFRUIT_IO_PASSWORD' }
Create Adafruit IO Feeds
Next, set up two new Adafruit IO feeds - temperature
and led
. The temperature feed will store the value of the Pico's CPU temperature and the LED feed will store the state of a toggle switch block on an Adafruit IO dashboard.
- If you do not know how to set up a feed on Adafruit IO, follow this page and come back when you've created the two feeds listed above.
Create an Adafruit IO Dashboard
Navigate to the Adafruit IO Dashboards page and create a new dashboard named Pico RP2040.
- If you do not know how to create a dashboard on Adafruit IO, follow this page and come back when you've created a new dashboard.
Click the cog in the upper right hand corner of the dashboard to bring up the Dashboard Settings.
From the Dashboard settings, click Create New Block
Add a toggle block to turn the Pico's LED on or off.
- From the Create a New Block screen, select the Toggle Block.
- Connect the led feed to the block by ticking the checkbox.
- In the final screen, click Create Block.
Next, create a gauge block to display the Pico's CPU temperature.
- Create a new gauge block.
- Connect the temperature feed to the block by ticking the checkbox.
- Under Block Settings, you may change the block's look and feel.
- When you've finished customizing the block, click Create Block.
Once you've finished creating both blocks, your dashboard should look something like the following image:
# SPDX-FileCopyrightText: Brent Rubell for Adafruit Industries # SPDX-License-Identifier: MIT import time from microcontroller import cpu import board import busio from digitalio import DigitalInOut from adafruit_esp32spi import adafruit_esp32spi from adafruit_esp32spi import adafruit_esp32spi_wifimanager import adafruit_esp32spi.adafruit_esp32spi_socket as socket import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT ### WiFi ### # Get wifi details and more from a secrets.py file try: from secrets import secrets except ImportError: print("WiFi secrets are kept in secrets.py, please add them there!") raise # Raspberry Pi RP2040 esp32_cs = DigitalInOut(board.GP13) esp32_ready = DigitalInOut(board.GP14) esp32_reset = DigitalInOut(board.GP15) spi = busio.SPI(board.GP10, board.GP11, board.GP12) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets) # Configure the RP2040 Pico LED Pin as an output led_pin = DigitalInOut(board.LED) led_pin.switch_to_output() # Define callback functions which will be called when certain events happen. # pylint: disable=unused-argument def connected(client): # Connected function will be called when the client is connected to Adafruit IO. print("Connected to Adafruit IO! ") def subscribe(client, userdata, topic, granted_qos): # This method is called when the client subscribes to a new feed. print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) # pylint: disable=unused-argument def disconnected(client): # Disconnected function will be called when the client disconnects. print("Disconnected from Adafruit IO!") def on_led_msg(client, topic, message): # Method called whenever user/feeds/led has a new value print("New message on topic {0}: {1} ".format(topic, message)) if message == "ON": led_pin.value = True elif message == "OFF": led_pin.value = False else: print("Unexpected message on LED feed.") # Connect to WiFi print("Connecting to WiFi...") wifi.connect() print("Connected!") # Initialize MQTT interface with the esp interface MQTT.set_socket(socket, esp) # Initialize a new MQTT Client object mqtt_client = MQTT.MQTT( broker="io.adafruit.com", port=1883, username=secrets["aio_username"], password=secrets["aio_key"], ) # Initialize an Adafruit IO MQTT Client io = IO_MQTT(mqtt_client) # Connect the callback methods defined above to Adafruit IO io.on_connect = connected io.on_disconnect = disconnected io.on_subscribe = subscribe # Set up a callback for the led feed io.add_feed_callback("led", on_led_msg) # Connect to Adafruit IO print("Connecting to Adafruit IO...") io.connect() # Subscribe to all messages on the led feed io.subscribe("led") prv_refresh_time = 0.0 while True: # Poll for incoming messages try: io.loop() except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) wifi.reset() wifi.connect() io.reconnect() continue # Send a new temperature reading to IO every 30 seconds if (time.monotonic() - prv_refresh_time) > 30: # take the cpu's temperature cpu_temp = cpu.temperature # truncate to two decimal points cpu_temp = str(cpu_temp)[:5] print("CPU temperature is %s degrees C" % cpu_temp) # publish it to io print("Publishing %s to temperature feed..." % cpu_temp) io.publish("temperature", cpu_temp) print("Published!") prv_refresh_time = time.monotonic()
Once all the files are copied from your computer to the Pico, you should have the following files on your CIRCUITPY drive.
Connecting to WiFi... Connected! Connecting to Adafruit IO... Connected to Adafruit IO! Subscribed to brubell/f/led with QOS level 0 CPU temperature is 24.32 degrees C Publishing 24.32 to temperature feed... Published!
Navigate to your Adafruit IO Dashboard and you should see a new value in the gauge block. Every 30 seconds, the Pico reads its internal CPU temperature and sends it to Adafruit IO.
Toggle the switch block to turn the Pico's LED on or off: