Yes adding the line
return msg
did help.
Alas it isn't exactly working.
When I click the inject
the screen gets filled with junk.
But at least it is a start.
Yes adding the line
return msg
did help.
Alas it isn't exactly working.
When I click the inject
the screen gets filled with junk.
But at least it is a start.
with and without quotes around the boolean
But nothing on (My) display... possibly just a node incompatibility??
I'll bow now - as I don't have the LCD hardware.
Best of luck with it.
At least you get something... if with same node, then that at least means it IS trying to talk to the display... well yours at least.
Ok, the stuff I have that works 99.9% of the time.
[{"id":"71348421.a60544","type":"inject","z":"eff5d6e6.236338","name":"Start script","repeat":"","crontab":"","once":true,"onceDelay":"1","topic":"","payload":"true","payloadType":"bool","x":390,"y":270,"wires":[["73b9b439.c214c4","fcafcc00.26f288"]]},{"id":"fcafcc00.26f288","type":"exec","z":"eff5d6e6.236338","command":"export DISPLAY=:0 && lxterminal -e python3 /home/pi/s/display_control.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Buttons and LCD","x":670,"y":270,"wires":[[],[],[]]}]
That is needed to start the script.
[{"id":"76101fc8.e92f3","type":"inject","z":"eff5d6e6.236338","name":"Stop script","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Stop","payloadType":"str","x":580,"y":540,"wires":[["13e26331.967f8d"]]},{"id":"13e26331.967f8d","type":"mqtt out","z":"eff5d6e6.236338","name":"","topic":"alarm_clock/ctl","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"9e83409.d55cfc","x":1010,"y":540,"wires":[]},{"id":"9e83409.d55cfc","type":"mqtt-broker","name":"Local Host","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
That is to stop the script. Not really needed, but it is nice to have.
This is the script:
# 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.clearScreen()
myLCD.setFastBacklight(255,255,255)
# myLCD.Display()
if (msg.payload.decode("utf-8") == 'BKL-OFF'):
print("Backlight Off")
#myLCD.clearScreen()
myLCD.setFastBacklight(0,0,0)
# myLCD.Display()
# 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()
if (msg.payload.decode("UTF-8") == 'MARK'):
print("")
print("-----------")
print("")
# Now send message back saying "Ready for next"
client.publish("alarm_clock/next","OK")
########################
# Text part
if (msg.topic == "alarm_clock/text"):
txt = msg.payload.decode("utf-8")
print ("=============")
print (txt)
client.publish("alarm_clock/received",txt)
try:
text = txt.split("@")
print ("Text to display is ",text[0])
print ("Raw co-ordinates are ",text[1])
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])
client.publish("alarm_clock/next","OK")
except:
#client.publish("alarm_clock/oops",text[0])
client.publish("alarm_clock/oops",text)
client.publish("alarm_clock/next","OK")
# pass
# myLCD.print(text[0])
# myLCD.print("X")
# code here maybe so if this happens
# a message is sent back to NR saying "Message not received"?
####
## inits
#myLCD = qwiic_serlcd.QwiicSerlcd()
##mqtt_broker = "192.168.0.99"
#mqtt_broker = "127.0.0.1"
#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()
# inits
myLCD = qwiic_serlcd.QwiicSerlcd()
myLCD.disableSystemMessages()
#mqtt_broker = "192.168.0.99"
mqtt_broker = "127.0.0.1"
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)
You will have to edit the MQTT
topics etc.
And this is a test script to make sure it is running.
[{"id":"713f5a7.db70024","type":"inject","z":"eff5d6e6.236338","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Hello@1,1","payloadType":"str","x":380,"y":670,"wires":[["cfbb25ac.223b68"]]},{"id":"178d1f98.c09dc8","type":"inject","z":"eff5d6e6.236338","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Hello@3,1","payloadType":"str","x":380,"y":710,"wires":[["cfbb25ac.223b68"]]},{"id":"207d3466.52e3fc","type":"inject","z":"eff5d6e6.236338","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"blah@3,0","payloadType":"str","x":380,"y":750,"wires":[["cfbb25ac.223b68"]]},{"id":"cfbb25ac.223b68","type":"mqtt out","z":"eff5d6e6.236338","name":"","topic":"alarm_clock/text","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"9e83409.d55cfc","x":640,"y":670,"wires":[]},{"id":"9e83409.d55cfc","type":"mqtt-broker","name":"Local Host","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
Thanks Dave. Silly me forgetting to add the return msg;
at the end.
So the script you put ... somewhere you like.
Load the flows.
The first one has an exec
node.
Set the path to the python script.
(Oh, note: python3)
The script will also need editing.
You need to set the MQTT topics (etc).
How I used it I kept it inside its own little world so as to keep the higher level topics a bit clearer.
Then you will have to edit the MQTT nodes so they are talking on the same topic
You will also need the python library.pimorini
Maybe the qwiic_serlcd
library.
I'm trying to find the link - just so you know why I didn't/haven't posted it here now.
I see this as my first issue... I use mosquito on the RPi, so I will have to do some Googling to find the correct way to import, whatever it needs.
import paho.mqtt.client as mqtt
That is more for the script.
Sorry. It was done a fair while ago.
And someone else did most of the leg work.
You will still need the standard mosquitto you have on the Pi.
And please delete all those old lines in the script which aren't actually being used.
If I am trying something I remark out the block with the #
and put in the new block.
So if it all dies a nasty death, I can step back.
I see I forgot to delete a whole block of init
code.
Not hurting, but isn't doing anything by being there and probably better if it is not there now.
Oh the qwiic library....
sparkfun-qwiic-i2c
is the full name.
That had me running around in circles for days.
Turns out I just need the paho anyhow... as it is the client, doh!
Just have to redo them both with pip3
Alas a lot has happened here since I did that. Maybe a year or so.
If you have a script that works, maybe I would suggest you build on that; as you have a better understanding of what is going on.
Mine is ok and it does work and allows a lot of control. But I don't know if it will meet all your needs.
There is also extra code there for debugging stuff which you may not want/need.
Yes, probably best... as it appears on top of everything else, you have a RGB backlit LCD, mine is poor old white on blue so lots of commands to cut out.
No problems.
But it may be handy to look at and maybe get something useful.
All the best.
I still want to figure out these nodes... but now also having something to work on for running python scripts is good as well for future stuffs.
I think it was written for a specific chip set.
But I can't be sure.
I think I lost a lot of time trying to get it working and finally gave up and wrote that script I showed you. (Well, ok: I got someone else to write the basics and I tweaked it to fit my needs)
This should be set to json, then you can see it in the json editor.
Also msg, msgs, pos and center should be surrounded by " " eg. "msg"
And "false" should be false
Could you paste the message for me?
Mine works a bit better and I would be interested if I can get it working via the node - just as a curiosity.
Specific LCD module - SparkFun Qwiic Serial LCDs - I don't have that, mine is old skool
Oh well... back to the node testing