Um, yeah, but it isn’t “code” as such. I am using Node-Red to do it, so it is not quite that easy.
Basically these are the messages sent:
This is sent daily.
{"_msgid":"2a937d9.ae2ae82","payload":"01/Jun/21@6,3","topic":"alarm_clock/text","_event":"node:e8276ab2.052ef8"}
As is this:
{"_msgid":"2a937d9.ae2ae82","payload":"Tue@2,3","topic":"alarm_clock/text","_event":"node:e8276ab2.052ef8"}
And this is sent every second:
{"payload":"09:50:55@6,1","topic":"alarm_clock/text","_msgid":"9f9fa087.130dc","_event":"node:e8276ab2.052ef8"}
That is sent to a python script which is this:
# Imports
## This must be the first line.
from __future__ import print_function
import sys
import os
import time
import paho.mqtt.client as mqtt
import qwiic_serlcd
def on_connect(client, userdata, flags, rc):
print("Connected:", rc)
client.subscribe("alarm_clock/#", 0)
def on_subscribe(client, userdata, mid, granted_qos):
print ('Subscribed:', userdata, mid, granted_qos)
print ('We are here, waiting for commands...')
def on_message(client, userdata, msg):
global abort
if msg.payload.decode("utf-8") == 'Heartbeat':
client.publish("alarm_clock/response", "Script is alive")
if (msg.topic == "alarm_clock/ctl"):
print(msg.payload.decode("utf-8"))
if msg.payload.decode("utf-8") == 'Stop':
print ("Script terminates..")
client.publish("alarm_clock/response", "Script is stopping")
abort = True
## Now put other commands here.
if (msg.payload.decode("utf-8") == 'CLS'):
print("CLS")
myLCD.clearScreen() # clear the screen - this moves the cursor to the home position as well
### Also need contrasts adjustments in here.
# work needed on backlight stuff. Not touched for now
print("X")
x = msg.payload.decode("utf-8")
y = x.split(":")
if (y[0] == 'BKL-ADJ'):
print ("=============")
print (y[1])
colours = "("+y[1]
colours = y[1].split(",")
print("--------------")
print (colours)
print ("R ",colours[0])
print ("G ",colours[1])
print ("B ",colours[2])
myLCD.setFastBacklight(int(colours[0]),int(colours[1]),int(colours[2]))
if (msg.payload.decode("utf-8") == 'BKL-ON'):
print("Backlight On")
myLCD.setBacklight(255,255,255)
if (msg.payload.decode("utf-8") == 'BKL-OFF'):
print("Backlight Off")
myLCD.setBacklight(0,0,0)
# done
if (msg.payload.decode("UTF-8") == 'BLK-OFF'):
print("Blink Off")
myLCD.noBlink()
if (msg.payload.decode("UTF-8") == 'BLK-ON'):
print("Blink On")
myLCD.blink()
# done
if (msg.payload.decode("UTF-8") == 'C-ON'):
print("Cursor On")
myLCD.cursor()
if (msg.payload.decode("UTF-8") == 'C-OFF'):
print("Cursor Off")
myLCD.noCursor()
if (msg.payload.decode("UTF-8") == 'D-OFF'):
print("Display Off")
myLCD.noDisplay()
if (msg.payload.decode("UTF-8") == 'D-ON'):
print("Display On")
myLCD.Display()
########################
# Text part
if (msg.topic == "alarm_clock/text"):
txt = msg.payload.decode("utf-8")
print ("=============")
print (txt)
try:
text = txt.split("@")
print ("Text to display is ",text[1])
print ("Raw co-ordinates are ",text[0])
print ("=============")
cord = text[1].split(",")
print ("X ",cord[0])
print ("Y ",cord[1])
x = int(cord[0])
y = int(cord[1])
myLCD.setCursor(x,y)
myLCD.print(text[0])
except:
myLCD.print(text[0])
# inits
myLCD = qwiic_serlcd.QwiicSerlcd()
mqtt_broker = "192.168.0.99"
mqtt_port = 1883
abort = False
client = mqtt.Mosquitto()
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
resp = client.connect(mqtt_broker, mqtt_port, 60)
client.loop_start()
while abort == False:
time.sleep(1)
client.loop_stop()
client.disconnect()
del client
print ('Script has terminated')
exit(0)
and it should all work.
SHOULD being the operative word.
Did I miss anything you need?
Oh, and this is a good screen picture of what it should look like - all things being good.
With the time being updated every second.