This commit is contained in:
OniriCorpe 2024-09-01 06:16:42 +02:00
commit f4e079fb21
14 changed files with 315 additions and 0 deletions

127
koloretsua/code.py Normal file
View file

@ -0,0 +1,127 @@
# somewhat based on https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/blob/main/examples/native_networking/minimqtt_adafruitio_native_networking.py
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import board
import json
from math import log
import neopixel
import os
import socketpool
import ssl
import time
import wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
# config stuff
BROKER_HOST = os.getenv("BROKER_HOST")
BROKER_PORT = os.getenv("BROKER_PORT")
BROKER_ACCOUNT = os.getenv("BROKER_ACCOUNT")
BROKER_PASSWORD = os.getenv("BROKER_PASSWORD")
BROKER_TOPIC = os.getenv("BROKER_TOPIC")
LED_STRIP_NUMBER = os.getenv("LED_STRIP_NUMBER")
strip = neopixel.NeoPixel(board.IO16, LED_STRIP_NUMBER)
global last_color
last_color = (0, 0, 0)
global strip_on
strip_on = 0
print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID"))
wifi.radio.connect(
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
)
print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID"))
def color_strip(color):
color = tuple(list(color)) # convert string tuple to tuple tuple
strip.fill(color)
# remember the color
global last_color
last_color = color
# remember the time when the strip was enabled
global strip_on
strip_on = time.monotonic()
def fade_color(color):
if color == (0, 0, 0):
return (0, 0, 0)
faded = tuple()
for i in range(len(color)):
if color[i] < 5:
faded_color = 0
elif color[i] > 0:
faded_color = color[i] / log(color[i])
faded = faded + (faded_color,)
return faded
# Define callback methods which are called when events occur
def connected(client, userdata, flags, rc):
print("Connected to MQTT Broker!")
def disconnected(client, userdata, rc):
print("Disconnected from MQTT Broker!")
def subscribe(client, userdata, topic, granted_qos):
print(f"Subscribed to {topic} with QOS level {granted_qos}")
def unsubscribe(client, userdata, topic, pid):
print(f"Unsubscribed from {topic} with PID {pid}")
def on_message(client, topic, message):
print(f"New message by {client} on topic {topic}: {message}")
message = json.loads(message)
if ("color" in message) and (message["color"]):
color_strip(message["color"])
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
ssl_context = ssl.create_default_context()
# Set up a MiniMQTT Client
client = MQTT.MQTT(
broker=BROKER_HOST,
port=BROKER_PORT,
username=BROKER_ACCOUNT,
password=BROKER_PASSWORD,
socket_pool=pool,
ssl_context=ssl_context,
)
# Setup the callback methods above
client.on_connect = connected
client.on_disconnect = disconnected
client.on_subscribe = subscribe
client.on_unsubscribe = unsubscribe
client.on_message = on_message
# Connect the client to the MQTT broker.
print("Connecting to MQTT broker...")
client.connect()
# Subscribe to the configured topic
client.subscribe(f"{BROKER_TOPIC}/visits", 0)
# Start a blocking message loop...
# NOTE: NO code below this loop will execute
while True:
client.loop(timeout=1)
if time.monotonic() - strip_on < 5:
# turn off the led strip after ~5 seconds
continue
strip.fill(last_color)
last_color = fade_color(last_color)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
koloretsua/lib/neopixel.mpy Normal file

Binary file not shown.

View file

@ -0,0 +1,10 @@
CIRCUITPY_WIFI_SSID = "WIFI_SSID"
CIRCUITPY_WIFI_PASSWORD = "WIFI_PASSWORD"
BROKER_HOST = "broker.example.com" # string, IP or domain-name of your mqtt broker
BROKER_PORT = 1883 # int, port of your mqtt broker
BROKER_ACCOUNT = "account-name" # string
BROKER_PASSWORD = "account-password" # string
BROKER_TOPIC = "bisitariak" # string
LED_STRIP_NUMBER = 8 # int, number of LED on the strip