# Trying to send text to LCD over i2c contrib

**URL:** <https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873>\
**Category:** General\
**Created:** [1 July 2021 03:14 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873 "2021-07-01T03:14:21Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 05:28 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/21 "2021-07-01T05:28:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [1 July 2021 05:29 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/22 "2021-07-01T05:29:36Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/51c29e53196b30751c299e160fee58a15bac058b.png)

with and without quotes around the boolean

But nothing on (My) display... possibly just a node incompatibility??

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [1 July 2021 05:29 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/23 "2021-07-01T05:29:47Z")

</div>

I'll bow now - as I don't have the LCD hardware.  
Best of luck with it.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [1 July 2021 05:33 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/24 "2021-07-01T05:33:08Z")

</div>

> [@Trying\_to\_learn](#):
>
> When I click the `inject` the screen gets filled with junk.

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 05:34 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/25 "2021-07-01T05:34:27Z")

</div>

Ok, the stuff I have that works 99.9% of the time.

```auto
[{"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.

```auto
[{"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:

```auto
# 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.

```auto
[{"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":""}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 05:35 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/26 "2021-07-01T05:35:23Z")

</div>

Thanks Dave. Silly me forgetting to add the `return msg;` at the end.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 05:39 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/27 "2021-07-01T05:39:25Z")

</div>

So the script you put ... somewhere you like.

Load the flows.

The first one has an `exec` node.

# You need to edit it!

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`

# OH YEAH!

You will also need the ~~`pimorini`~~ python library.  
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.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [1 July 2021 05:45 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/28 "2021-07-01T05:45:11Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 05:46 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/29 "2021-07-01T05:46:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 05:54 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/30 "2021-07-01T05:54:48Z")

</div>

Oh the qwiic library....

`sparkfun-qwiic-i2c` is the full name.

That had me running around in circles for days.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [1 July 2021 05:55 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/31 "2021-07-01T05:55:27Z")

</div>

> [@Gunner](#):
>
> so I will have to do some Googling to find the correct way to import, whatever it needs.

Turns out I just need the paho anyhow... as it is the **client** , doh!

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [1 July 2021 05:55 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/32 "2021-07-01T05:55:59Z")

</div>

Just have to redo them both with pip3 😛

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 05:57 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/33 "2021-07-01T05:57:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [1 July 2021 05:59 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/34 "2021-07-01T05:59:53Z")

</div>

> [@Trying\_to\_learn](#):
>
> If you have a script that works, **maybe** I would suggest you build on that

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 06:00 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/35 "2021-07-01T06:00:56Z")

</div>

No problems.

But it may be handy to look at and maybe get something useful.

All the best.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [1 July 2021 06:00 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/36 "2021-07-01T06:00:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 06:07 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/37 "2021-07-01T06:07:18Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 July 2021 06:07 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/38 "2021-07-01T06:07:24Z")

</div>

> [@Gunner](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13ca15cdc9e81073f52ce465b3f65f03ec40e568.png)
> 
> with actual text being...

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`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 July 2021 06:08 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/39 "2021-07-01T06:08:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [1 July 2021 06:09 UTC](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873/40 "2021-07-01T06:09:08Z")

</div>

> [@Trying\_to\_learn](#):
>
> I think it was written for a specific chip set.

Specific LCD module - SparkFun Qwiic Serial LCDs - I don't have that, mine is old skool

Oh well... back to the node testing 🤪

[Previous page](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873.md?page=1)

[Next page](https://discourse.nodered.org/t/trying-to-send-text-to-lcd-over-i2c-contrib/47873.md?page=3)
