How to get variables from dashboard into a Python3 script

#1

Hi all,

Long time reader of the forum and it has answered all my questions to date, thank you.

I'm looking for the best method to do this. There seems to be many routes I can go and I'm getting myself in a muddle trying to figure out the best way.

I have a Python3 script that show info on a screen using Pygame on a Raspberry Pi. It's written to the framebuffer so no desktop is required. There are many possible configurations of colours/text on the screen so I ported all the variables to a separate Python file to save me trawling through the main script each time I want to change something. This variables file is imported at the beginning of the main script. This all works perfectly and I would like to keep this part as is.

What I would like to do is create a dashboard that allows me to change the variables and then update the variables file.

There are about 40 different variables in the file. This is a sample:

SIGN1_ON_COLOR = (250, 20, 20)
SIGN1_OFF_COLOR = (21, 0, 0)
SIGN1_ON_TEXT_COLOR = (251, 251, 251)
SIGN1_OFF_TEXT_COLOR = (65, 65, 65)
SIGN1_ON_TEXT = "DOOR OPEN"
SIGN1_OFF_TEXT = "DOOR CLOSED"
SIGN1_FONT_SIZE = 150

SIGN2_ON_COLOR = (20, 20, 250)
SIGN2_OFF_COLOR = (10, 10, 30)
SIGN2_ON_TEXT_COLOR = (251, 251, 251)
SIGN2_OFF_TEXT_COLOR = (65, 65, 65)
SIGN2_ON_TEXT = "HEATING ON"
SIGN2_OFF_TEXT = "HEATING OFF"
SIGN2_FONT_SIZE = 150

In very simplistic terms, I imagine that I can use the text input feature on dashboard to get the info and then use a function node to format it and then the file node to write the file to the desired location. This will be a challenge for me to do but I am very willing to put the time in and figure it out.
Can someone please advise if this is the best way to do it? Is there another simpler way to do it that I haven't come across yet?

Advice would be very welcome.

Thanks in advance
Ant.

#2

Simpler way would be to not use node red to do this.

Python has probably the easiest webserver built in already. Create a page with some fields and pulldowns and let it read/write to a file.

#3

Here is a simple starter to get you going

[{"id":"50332236.35324c","type":"ui_button","z":"68636f36.4163c","name":"","group":"cc4b0bcb.855c78","order":3,"width":0,"height":0,"passthru":false,"label":"save","tooltip":"","color":"","bgcolor":"","icon":"","payload":"true","payloadType":"bool","topic":"save","x":120,"y":740,"wires":[["e59132eb.c64fb"]]},{"id":"e59132eb.c64fb","type":"function","z":"68636f36.4163c","name":"","func":"\nif (msg.topic !== \"save\") {\n    if (msg.payload.indexOf(\"rgb\") === 0) {\n        msg.payload = msg.payload.substr(3);\n    }\n    context[msg.topic] = msg.payload;\n}\nelse {\n    var s = \"\";\n    for (var p in context) {\n        if (context.hasOwnProperty(p) && typeof(context[p]) === \"string\") {\n            s += p + \"=\" + context[p] + \"\\n\";\n        }\n    }  \n    msg.payload = s;\n    return msg;\n}\nreturn null;","outputs":1,"noerr":0,"x":320,"y":640,"wires":[["72fdadb8.68d1f4"]]},{"id":"c6c5b2a.967e65","type":"ui_text_input","z":"68636f36.4163c","name":"","label":"SIGN1_ON_TEXT","tooltip":"","group":"cc4b0bcb.855c78","order":2,"width":0,"height":0,"passthru":true,"mode":"text","delay":300,"topic":"SIGN1_ON_TEXT","x":150,"y":620,"wires":[["e59132eb.c64fb"]]},{"id":"1f3eaaf3.1d15c5","type":"ui_colour_picker","z":"68636f36.4163c","name":"","label":"SIGN1_ON_COLOR","group":"cc4b0bcb.855c78","format":"rgb","outformat":"string","showSwatch":true,"showPicker":false,"showValue":false,"showHue":false,"showAlpha":false,"showLightness":true,"dynOutput":"false","order":1,"width":0,"height":0,"passthru":true,"topic":"SIGN1_ON_COLOR","x":140,"y":560,"wires":[["e59132eb.c64fb"]]},{"id":"72fdadb8.68d1f4","type":"debug","z":"68636f36.4163c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":360,"y":720,"wires":[]},{"id":"cc4b0bcb.855c78","type":"ui_group","z":"","name":"Default","tab":"d29ed0b8.c1899","disp":true,"width":"6","collapse":false},{"id":"d29ed0b8.c1899","type":"ui_tab","name":"Tab 1","icon":"dashboard","order":1}]
#4

Thank you both for your replies.
Using Python as a webserver was in my mind, but to be honest I really like working with Node-Red Dashboard and want to see if I can get that to work first.
And... Many thanks to @dceejay for the invaluable help. Speaking as the 'man of a thousand if's', you have saved me at least 40 of them had I tried to write this myself.
I'm now off to dissect your script to see how it works. It might seem like easy reading for many, but to me it is a mystery to be solved. This is how I learn.

Many many thanks again.
A.

#5

Hi again,

I have been working on the above flow and it's perfect for my needs, thanks again. On thing I can't figure out.
I can only select the edge of the colour picker. As a result I cannot select a grey colour (which I need for the project). I have enabled the slider which lets me get black or white, but still no grey. Any ideas most welcome.
If it's not possible then can the node use the 'stock' windows colour picker instead?
I have looked at the text node, which can be set to 'color picker' but this outputs in HEX. I would then have to convert the HEX to RGB. I believe this can be done but I do prefer the smaller colour picker.

Thanks
A.