2021-07-01 19:14:36 +00:00
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 "
2021-07-01 19:47:21 +00:00
__TOPIC_TEXT__ = " text "
2021-07-01 19:14:36 +00:00
class Controller :
2021-07-01 19:50:35 +00:00
connected = False
display = None
2021-07-01 20:06:18 +00:00
last_scrollupdate = 0
2021-07-01 19:50:35 +00:00
def __init__ ( self , base_topic , mqtt_host , mqtt_port = 1883 ) :
self . scroll_interval = 0.25
2021-07-01 20:06:18 +00:00
self . text = " "
2021-07-01 19:50:35 +00:00
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:
2021-07-01 20:32:08 +00:00
self . display = segments . Seg14x4 ( self . i2c , address = 0x70 ) # find address with "sudo i2cdetect -y 1" . you need to install: sudo apt-get install -y python-smbus i2c-tools
2021-07-01 19:50:35 +00:00
# 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
self . display . print ( " BOOT " )
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 " ) :
2021-07-01 20:06:18 +00:00
self . poscount = 0 #start at beginning of new text
2021-07-01 19:50:35 +00:00
self . text = msg
2021-07-01 20:06:18 +00:00
if len ( self . text ) == 0 :
self . text = " "
2021-07-01 19:50:35 +00:00
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 :
2021-07-01 20:06:18 +00:00
self . mqtt . loop ( 0.1 ) #with block timeout
2021-07-01 19:50:35 +00:00
2021-07-01 20:06:18 +00:00
if self . display is not None and time . time ( ) > self . last_scrollupdate + self . scroll_interval :
2021-07-01 19:50:35 +00:00
self . display . print ( self . text [ self . poscount ] )
self . poscount + = 1
self . poscount % = len ( self . text )
2021-07-01 20:06:18 +00:00
self . last_scrollupdate = time . time ( )
2021-07-01 19:47:21 +00:00