From 51d6fe369810d3c108cb85f560a873a10fd6ae12 Mon Sep 17 00:00:00 2001 From: Fisch Date: Thu, 1 Jul 2021 21:14:36 +0200 Subject: [PATCH] first implementation test, untested --- requirements.txt | 2 + src/controller.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.py | 15 ++++++++ 3 files changed, 114 insertions(+) create mode 100644 requirements.txt create mode 100644 src/controller.py create mode 100644 src/main.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..30b84da --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +paho-mqtt +adafruit-circuitpython-ht16k33 \ No newline at end of file diff --git a/src/controller.py b/src/controller.py new file mode 100644 index 0000000..4e42412 --- /dev/null +++ b/src/controller.py @@ -0,0 +1,97 @@ +import subprocess +import paho.mqtt.client as mqtt + +import time +import board +import busio +from adafruit_ht16k33 import segments + +__TOPIC_BRIGHTNESS__ = "brightness" +__TOPIC_SCROLL_INTERVAL__ = "scroll_interval" +__TOPIC_TEXT__ = "" + +class Controller: + connected = False + + def __init__(self, base_topic, mqtt_host, mqtt_port = 1883): + + self.scroll_interval = 0.25 + self.text = "" + self.poscount = 0 + + # Create the I2C interface. + self.i2c = busio.I2C(board.SCL, board.SDA) + + # Create the LED segment class. + # This creates a 14 segment 4 character display: + self.display = segments.Seg14x4(self.i2c) + + # Clear the display. + self.display.fill(0) + + # set brightness, range 0-1.0, 1.0 max brightness + self.display.brightness = 1.0 + + self.mqtt = mqtt.Client() + self.mqtt.on_connect = self.on_connect + self.mqtt.on_message = self.on_message + self.mqtt.on_disconnect = self.on_disconnect + self.mqtt.connect(mqtt_host, mqtt_port) + self.topic = base_topic + + + def on_disconnect(self, client, userdata, rc): + print("MQTT disconnected") + self.connected = False + + def on_message(self, client, userdata, message): + msg = str(message.payload.decode("utf-8")) + print("msg = " + msg) + + if message.topic.endswith(__TOPIC_BRIGHTNESS__ + "/set"): + val = float(msg) + if val >= 0.0 and val <=1.0: + self.display.brightness = val + print("changed brightness to ", self.display.brightness) + self.mqtt.publish(self.topic + "/" + __TOPIC_BRIGHTNESS__, self.display.brightness, 1 ) + else: + print("invalid brightness ", val) + + if message.topic.endswith(__TOPIC_SCROLL_INTERVAL__ + "/set"): + val = float(msg) + if val > 0.0: + self.scroll_interval = val + print("changed scroll_interval to ", self.scroll_interval) + self.mqtt.publish(self.topic + "/" + __TOPIC_SCROLL_INTERVAL__, self.scroll_interval, 1 ) + else: + print("error not >0.0: ", val) + + + + + if message.topic.endswith(__TOPIC_TEXT__ + "/set"): + self.text = msg + print("changed text to ", self.text) + self.mqtt.publish(self.topic + "/" + __TOPIC_TEXT__, self.text, 1 ) + + + + def on_connect(self, client, userdata, flags, rc): + print("Connected to MQTT Broker") + self.mqtt.subscribe(self.topic + "/#") + self.connected = True + + + def loop_forever(self): + run = True + + while run: + self.mqtt.loop() + + + self.display.print(self.text[poscount]) + poscount += 1 + poscount %= len(self.text) + time.sleep(self.scroll_interval) + + \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..17423ab --- /dev/null +++ b/src/main.py @@ -0,0 +1,15 @@ +#!/usr/bin/python3 +from controller import Controller +from argparse import ArgumentParser + +parser = ArgumentParser(description="MQTT Control of RPI.") +parser.add_argument(dest="host",type=str,help="Host name of the MQTT broker") +parser.add_argument(dest="topic",type=str,help="set the base topic") + + + + +if __name__ == "__main__": + args = parser.parse_args() + controller = Controller(args.topic,args.host) + controller.loop_forever()