Skip to content

Instantly share code, notes, and snippets.

@idriszmy
Last active May 8, 2022
Embed
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
Items:
- Maker Pi Pico
https://my.cytron.io/p-maker-pi-pico
- ESP8266 ESP-01 WiFi Serial Transceiver Module
https://my.cytron.io/p-esp-01-wifi-serial-transceiver-module-esp8266
- NeoPixel 8x32 LED Panel
https://my.cytron.io/p-ws2812b-neopixel-8x32-led-panel-256-led
- USB Micro B Cable
https://my.cytron.io/p-usb-micro-b-cable
- 3D printing products
https://my.cytron.io/c-3d-modeling
Libraries required from bundle (https://circuitpython.org/libraries):
- adafruit_espatcontrol
- adafruit_minimqtt
- adafruit_io
- adafruit_requests.mpy
- neopixel.mpy
References:
- https://learn.adafruit.com/circuitpython-essentials/circuitpython-neopixel
- https://learn.adafruit.com/adafruit-io-basics-color
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 secrets.py file
try:
from secrets import secrets
except ImportError:
print("All secret keys are kept in secrets.py, please add them there!")
raise
# 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))
pixels.show()
time.sleep(1)
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
time.sleep(1)
# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")
MQTT.set_socket(socket, esp)
# Initialize a new MQTT Client object
mqtt_client = MQTT.MQTT(
broker = "io.adafruit.com",
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...")
io.connect()
# Subscribe to all messages on the color and bright feed
io.subscribe("color")
io.subscribe("bright")
pixels.brightness = 0.05
pixels.fill((255, 128, 0))
pixels.show()
time.sleep(1)
while True:
# Poll for incoming messages
try:
io.loop()
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
io.reconnect()
continue
# 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