Last active
July 21, 2023 02:42
-
-
Save vitorio/ced24738a2bf183be1680b5232d91f7d to your computer and use it in GitHub Desktop.
CO2 monitor using Pimoroni Wireless Plasma Kit + Adafruit SCD40, MicroPython 1.20.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/plasma_stick/co2.py | |
# https://docs.micropython.org/en/latest/library/rp2.html#rp2.bootsel_button | |
# https://github.com/pimoroni/phew/blob/v0.0.3/phew/__init__.py | |
import time | |
import plasma | |
from plasma import plasma_stick | |
from machine import Pin | |
import breakout_scd41 as scd | |
from pimoroni_i2c import PimoroniI2C | |
import rp2 | |
import network | |
import random | |
wlan = network.WLAN(network.AP_IF) | |
NUM_LEDS = 50 | |
YELLOW_START = 50 | |
YELLOW_END = 70 | |
GREEN_START = 95 | |
GREEN_END = 145 | |
RED_START = 345 | |
RED_END = 375 | |
HUE_START = YELLOW_START | |
HUE_END = YELLOW_END | |
SPEED = 0.3 # bigger = faster (harder, stronger) | |
VALUE = 0.8 | |
LAST_TIME = time.ticks_ms() | |
def hex_to_rgb(hex): | |
# converts a hex colour code into RGB | |
h = hex.lstrip('#') | |
r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4)) | |
return r, g, b | |
# set up the Pico W's onboard LED | |
pico_led = Pin('LED', Pin.OUT) | |
# set up the WS2812 / NeoPixel™ LEDs | |
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma_stick.DAT, color_order=plasma.COLOR_ORDER_RGB) | |
# start updating the LED strip | |
led_strip.start() | |
# set up I2C | |
i2c = PimoroniI2C(plasma_stick.SDA, plasma_stick.SCL) | |
# set up SCD41 breakout | |
scd.init(i2c) | |
scd.start() | |
co2 = 600 | |
distance = 0.0 | |
direction = SPEED | |
while True: | |
if scd.ready(): | |
co2, temperature, humidity = scd.measure() | |
wlan.active(False) | |
wlan.config(essid=f"CO2: {str(int(co2))}", password=str(random.randrange(10000000, 99999999))) | |
wlan.active(True) | |
for i in range(NUM_LEDS): | |
if co2 < 600: | |
HUE_START = GREEN_START | |
HUE_END = GREEN_END | |
elif co2 >= 800: | |
HUE_START = RED_START | |
HUE_END = RED_END | |
else: | |
HUE_START = YELLOW_START | |
HUE_END = YELLOW_END | |
# generate a triangle wave that moves up and down the LEDs | |
j = max(0, 1 - abs(distance - i) / (NUM_LEDS / 3)) | |
hue = HUE_START + j * (HUE_END - HUE_START) | |
if time.ticks_ms() - LAST_TIME > 350: | |
if rp2.bootsel_button(): | |
VALUE = VALUE - 0.1 | |
if VALUE <= 0.1: | |
VALUE = 0.8 | |
LAST_TIME = time.ticks_ms() | |
led_strip.set_hsv(i, hue / 360, 1.0, VALUE) | |
# reverse direction at the end of colour segment to avoid an abrupt change | |
distance += direction | |
if distance > NUM_LEDS: | |
direction = - SPEED | |
if distance < 0: | |
direction = SPEED | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment