Raspberry Pi

Nextion Raspberry Pi UI

Posted on Updated on

A quick project to connect a Nextion HMI to Raspberry Pi and get some text displayed. (Or whatever goes through your mind)

nextion_hmi_320x240

I am using a Chinese version (I am in China!) of 2.8″ 320×240 device.

  • The Nextion project

I made a simple screen with 4 text field to receive data form the RPI.

The text field are named: t0, t1, t2, t3.

nextion_project

Here is the project file (download and rename it as rpi_hmi.HMI)

downlod rpi_hmi.HMI

pi3_gpio

Connect + 5 VDC to pin 2

Connect GND to pin 6

Connect Rx to pin 8

Connect Tx to pin 10

  • To send data to the screen I am using my favorite application: Node-Red.

From Node-Red it is possible to run commands and send them to the serial port.

Before that we should make sure that the serial port is available.

If RPI 2, read this post.

If RPI 3, read this post.

In PRI 3 , to use ttyAMA0 it is necessary to stop the bluetooth.

To send data to the Nextion, we should format them like:

t0.txt=, 0x22, text to send, 0x22, 0xFF, 0xFF, 0xFF

Here is the Node-Red flow.

[{"id":"244f48cf.81b92","type":"inject","z":"7a00aa0e.a9b7d4","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":128.5,"y":136,"wires":[["e0180d8.3c9047"]]},{"id":"e0180d8.3c9047","type":"function","z":"7a00aa0e.a9b7d4","name":"","func":"s_object = \"t0.txt=\";\ns_text = \"Hello\";\nvar arr = [];\nfor (var i = 0, l = s_object.length; i < l; i ++) {\n    var ascii = s_object.charCodeAt(i);\n    arr.push(ascii);\n}\narr.push(0x22);\nfor (var i = 0, l = s_text.length; i < l; i ++) {\n    var ascii = s_text.charCodeAt(i);\n    arr.push(ascii);\n}\narr.push(0x22);\narr.push(0xFF);\narr.push(0xFF);\narr.push(0xFF);\nmsg.payload = new Buffer(arr);\nreturn msg;","outputs":1,"noerr":0,"x":288.5,"y":135,"wires":[["858c79ee.44437"]]},{"id":"858c79ee.44437","type":"serial out","z":"7a00aa0e.a9b7d4","name":"","serial":"2466d4ba.b7759c","x":502.5,"y":134,"wires":[]},{"id":"2466d4ba.b7759c","type":"serial-port","z":"7a00aa0e.a9b7d4","serialport":"/dev/ttyAMA0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"100","bin":"bin","out":"time","addchar":false}]

Another flow to display the CPU temperature

[{"id":"244f48cf.81b92","type":"inject","z":"7a00aa0e.a9b7d4","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":128.5,"y":136,"wires":[["e0180d8.3c9047","3c8c3ccb.ea114c","ef39e89d.a084d"]]},{"id":"e0180d8.3c9047","type":"function","z":"7a00aa0e.a9b7d4","name":"","func":"s_object = \"t0.txt=\";\ns_text = \"Hello World\";\nvar arr = [];\nfor (var i = 0, l = s_object.length; i < l; i ++) {\n    var ascii = s_object.charCodeAt(i);\n    arr.push(ascii);\n}\narr.push(0x22);\nfor (var i = 0, l = s_text.length; i < l; i ++) {\n    var ascii = s_text.charCodeAt(i);\n    arr.push(ascii);\n}\narr.push(0x22);\narr.push(0xFF);\narr.push(0xFF);\narr.push(0xFF);\nmsg.payload = new Buffer(arr);\nreturn msg;","outputs":1,"noerr":0,"x":288.5,"y":135,"wires":[["858c79ee.44437"]]},{"id":"858c79ee.44437","type":"serial out","z":"7a00aa0e.a9b7d4","name":"","serial":"2466d4ba.b7759c","x":636.5,"y":138,"wires":[]},{"id":"8d36ba49.d16358","type":"serial in","z":"7a00aa0e.a9b7d4","name":"","serial":"2466d4ba.b7759c","x":112.5,"y":414,"wires":[["9057e978.8b3c"]]},{"id":"9057e978.8b3c","type":"debug","z":"7a00aa0e.a9b7d4","name":"","active":true,"console":"false","complete":"false","x":398.5,"y":417,"wires":[]},{"id":"3c8c3ccb.ea114c","type":"function","z":"7a00aa0e.a9b7d4","name":"","func":"s_object = \"t1.txt=\";\ns_text = \"Line 2\";\nvar arr = [];\nfor (var i = 0, l = s_object.length; i < l; i ++) {\n    var ascii = s_object.charCodeAt(i);\n    arr.push(ascii);\n}\narr.push(0x22);\nfor (var i = 0, l = s_text.length; i < l; i ++) {\n    var ascii = s_text.charCodeAt(i);\n    arr.push(ascii);\n}\narr.push(0x22);\narr.push(0xFF);\narr.push(0xFF);\narr.push(0xFF);\nmsg.payload = new Buffer(arr);\nreturn msg;","outputs":1,"noerr":0,"x":289,"y":199,"wires":[["858c79ee.44437"]]},{"id":"ef39e89d.a084d","type":"exec","z":"7a00aa0e.a9b7d4","command":"vcgencmd measure_temp","addpay":false,"append":"","useSpawn":false,"timer":"","name":"measure temp","x":347,"y":295,"wires":[["b985e249.4f6ff8"],[],[]]},{"id":"b985e249.4f6ff8","type":"function","z":"7a00aa0e.a9b7d4","name":"","func":"s_object = \"t2.txt=\";\ns_text = msg.payload;\nvar arr = [];\nfor (var i = 0, l = s_object.length; i < l; i ++) {\n    var ascii = s_object.charCodeAt(i);\n    arr.push(ascii);\n}\narr.push(0x22);\nfor (var i = 0, l = s_text.length; i < l; i ++) {\n    var ascii = s_text.charCodeAt(i);\n    arr.push(ascii);\n}\narr.push(0x22);\narr.push(0xFF);\narr.push(0xFF);\narr.push(0xFF);\nmsg.payload = new Buffer(arr);\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":286,"wires":[["858c79ee.44437"]]},{"id":"2466d4ba.b7759c","type":"serial-port","z":"7a00aa0e.a9b7d4","serialport":"/dev/ttyAMA0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"500","bin":"bin","out":"time","addchar":false}]

Enjoy.