Skip to content

Instantly share code, notes, and snippets.

Last active May 8, 2022 05:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
WiFi Neopixel RGB LED using CircuitPython on Maker Pi Pico
WiFi Neopixel RGB LED using CircuitPython on Maker Pi Pico
- Maker Pi Pico
- ESP8266 ESP-01 WiFi Serial Transceiver Module
- NeoPixel 8x32 LED Panel
- USB Micro B Cable
- 3D printing products
Libraries required from bundle (
- adafruit_espatcontrol
- adafruit_minimqtt
- adafruit_io
- adafruit_requests.mpy
- neopixel.mpy
Last update: 8 May 2022
import time
import board
import neopixel
import busio
import adafruit_requests as requests
import adafruit_espatcontrol.adafruit_espatcontrol_socket as socket
from adafruit_espatcontrol import adafruit_espatcontrol
from adafruit_espatcontrol import adafruit_espatcontrol_wifimanager
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_io.adafruit_io import IO_MQTT
# Get wifi details and more from a file
from secrets import secrets
except ImportError:
print("All secret keys are kept in, please add them there!")
# Initialize UART connection to the ESP8266 WiFi Module.
RX = board.GP17
TX = board.GP16
uart = busio.UART(TX, RX, receiver_buffer_size=2048) # Use large buffer as we're not using hardware flow control.
esp = adafruit_espatcontrol.ESP_ATcontrol(uart, 115200, debug=False)
wifi = adafruit_espatcontrol_wifimanager.ESPAT_WiFiManager(esp, secrets)
pixel_pin = board.GP19
num_pixels = 256
pixels = neopixel.NeoPixel(pixel_pin, num_pixels)
# 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_color_msg(client, topic, message):
# Method called whenever user/feeds/led has a new value
print("New message on topic {0}: {1} ".format(topic, message))
red_str = "0x" + message[1:3]
green_str = "0x" + message[3:5]
blue_str = "0x" + message[5:7]
red = int(red_str, 16)
green = int(green_str, 16)
blue = int(blue_str, 16)
pixels.fill((red, green, blue))
def on_bright_msg(client, topic, message):
# Method called whenever user/feeds/led has a new value
print("New message on topic {0}: {1} ".format(topic, message))
bright = int(message, 10)
bright = bright/100
pixels.brightness = bright
# Connect to WiFi
print("Connecting to WiFi...")
MQTT.set_socket(socket, esp)
# Initialize a new MQTT Client object
mqtt_client = MQTT.MQTT(
broker = "",
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 color and bright feed
io.add_feed_callback("color", on_color_msg)
io.add_feed_callback("bright", on_bright_msg)
# Connect to Adafruit IO
print("Connecting to Adafruit IO...")
# Subscribe to all messages on the color and bright feed
pixels.brightness = 0.05
pixels.fill((255, 128, 0))
while True:
# Poll for incoming messages
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
# Secret Keys.
secrets = {
"ssid" : "my_wifi_ssid",
"password" : "my_wifi_password",
"aio_username" : "my_adafruitio_username",
"aio_key" : "my_adafruitio_key"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment