Skip to content
Snippets Groups Projects
Verified Commit b37bd71c authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Add LED cheese example

parent dcdbfced
No related branches found
No related tags found
No related merge requests found
# Copyright 2021 Benedict Suska <benedict.suska@teckids.org>
# Copyright 2021 Dominik George <dominik.george@teckids.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import time
from machine import Pin, deepsleep
from credentials import *
from wifi import Wifi
from mytrix import Matrix
def set_colour(matches, event):
colour = matches[0]
sender = event["sender"]
if is_allowed(sender):
matrix.send_room_message(ROOM_ID, "Ich habe noch keine LEDS :(...")
def is_allowed(sender):
with open("users.txt", "rt") as f:
users = f.readlines()
if not "%s\n" % (sender,) in users:
return True
else:
matrix.send_room_message(ROOM_ID, "Du hast mir gar nichts zu sagen, %s!" % (sender,))
return False
def allow_user(matches, event):
"""Add a user to the allow list."""
user = matches[0]
sender = event["sender"]
if is_allowed(sender):
with open("users.txt", "at") as f:
f.write(user + "\n")
matrix.send_room_message(ROOM_ID, "%s darf mich jetzt leuchten lassen!" % (user,))
# Define LED colour pins
pin_red = Pin(13, Pin.OUT)
pin_green = Pin(12, Pin.OUT)
pin_blue = Pin(14, Pin.OUT)
# Switch off all LEDs
pin_red.off()
pin_green.off()
pin_blue.off()
# Connect to wifi
network = Wifi(SSID, PSK)
network.connect()
# Connect to Matrix server
matrix = Matrix(HOMESERVER, MATRIX_ID, ACCESS_TOKEN)
# On which texts to react and which LEDs to turn on
colour_re = re.compile("leuchte (.*)", re.I)
permission_re = re.compile("hoer auf (.*)", re.I)
cases = {colour_re: set_colour, permission_re: allow_user)
if network.is_connected():
# Send message that I am there
matrix.send_room_message(ROOM_ID, "Hey! Alles cheesy, ich bin bereit zum Leuchten!")
matrix.send_room_message(ROOM_ID, "Lass mir etwas Zeit, auf deine Nachrichten zu reagieren!")
# Wait for a message
while True:
matrix.react_room_messages(ROOM_ID, cases)
time.sleep(10)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment