PiJuice status to NodeRed

Hi All!
I'm working on a project with a Raspberry Pi with Node-Red. I would like to have the dashboard showing the status of the battery of the PiJuice-hat and the informations comming there. Does anyone know how to do that?

Is it possible to access that information from the Pi? If so then how?

The PiJuice GUI app uses a python library on the inside. You could either create a python script around that library and call it from an exec, or create a JavaScript solution from it. It accesses the hat over the I2C bus.

Thanks for the ideas, but I do not have any knowlegde about how to do that. I'm not familiar with python or JavaScript..

Like @afelix is saying:

Save that file somewhere on your raspberry and use node-red (inject node + exec node + debug node), specify the path of the file in the exec node, click the inject node and watch the magic happen.

...and untick the append payload option...

I've tryed that but the debug tells me that I do not have the permissions, so i'm not getting any usefull outputs..

Hi @bakman2, I have never used python in combination with Node-RED. But is it also possible to copy the content of the py file into e.g. the (python editor of the) node-red-contrib-python3-function node?

Hi Bart (@BartButenaers),
Based on this comment on the github issues of the repository this was forked from it should in theory work: https://github.com/arnauorriols/node-red-contrib-python-function/issues/3#issuecomment-406710989
However, I would personally not advise this approach, I’ve done so in the past in production to get web applications running. What this does is dependency injection at runtime, and unless you are quite familiar with all the underlying libraries, it brings risks for errors. On top of that, the initial library still comes with a big warning to not use it on production. The library you linked just appears to add the much needed python3 support to the mix. Much needed as Python 2 will be EOL in just under 4 months. But it is still unsure if this library is stable.

A better idea would be to write a small wrapper script around the PiJuice python file with command line parameters, then call it from either an exec node or a custom node wrapper.

Edit: I was answering this based on importing that script into a python-function node, rather than pasting. This script is using hardware level libraries to interact with the I2C bus on the raspberry pi. I have absolutely no clue if this node is capable in correctly running a python setup with those calls without a full debug run, and I don’t have the hat or similar hardware I can connect to an I2C bus.

1 Like

....extend the file with mqtt support. It's a long file, haven't looked deep enough, but somewhere & somehow the status is sent. Otherwise it would not be difficult to add mqtt and publish the battery status to a broker. Provided you know enough Python

That works too, definitely. Is normally my first suggestion so I can’t believe I didn’t think of it now. :joy: Thanks.

I actually read through the entire thing beforehand. What this thing does is it defines a complete interface on connecting to the thing. At the very bottom an outer class is defined that contains interfaces to connect to all parts, from the status of the battery, to controlling wake up/shut down, to the RTC status, and so on. The sending of the status is done by calling functions on this interfaces, and the status is returned. To work around it, a small wrapper around that outer class that will send occasionally over mqtt would work, or from a command infrastructure that when a message is published on a control topic it is subscribed to, it will publish status on another.

1 Like

This got a little to technical for me. I'm not very experience in coding. I'm mostly working with the physical products like battery-systems and automation with relays and so on. It is possible for you to help me out here?

So I just looked at this for fun (I do not have a pijuice, even if it looks tempting to get one, so I cannot properly test what I have done)

I wrote a small python script that can be called via the cmd line or from the exec node in NR. It will return requested data as mqtt messages that can be captured by NR. See the flow sample below. In this first version, just some status and power messages are treated.


  1. Expecting
  • running Python3
  • this script will run in the same pi as where the pijuice is installed
  1. Install the mosquitto broker (https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/)
    sudo apt update
    sudo apt install -y mosquitto
    sudo systemctl enable mosquitto.service

  2. Install the Paho mqtt client
    sudo pip3 install paho-mqtt

  3. Install PiJuice software (https://github.com/PiSupply/PiJuice)
    sudo apt-get install pijuice-gui
    reboot the pi

  4. I also had to install smbus separately
    sudo pip3 install smbus

  5. Now copy the attached files to your Pi, I have them myself in /home/pi. Change the file types (extensions) to .py
    pijuice4nr.txt (581 Bytes)
    pijuice.txt (58.2 KB)

  6. Create this simple flow in NR

[{"id":"c0d45a58.55b8f8","type":"mqtt in","z":"797a7c58.e82134","name":"","topic":"pijuice/#","qos":"2","datatype":"auto","broker":"75eba16c.094f9","x":970,"y":160,"wires":[["eee81c93.820e4"]]},{"id":"a7fe7e1b.d30a8","type":"debug","z":"797a7c58.e82134","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1180,"y":290,"wires":[]},{"id":"a6c3311e.75d59","type":"inject","z":"797a7c58.e82134","name":"Get data","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":970,"y":80,"wires":[["dee8d33a.10619"]]},{"id":"dee8d33a.10619","type":"exec","z":"797a7c58.e82134","command":"python3 /home/pi/pijuice4nr.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":1240,"y":80,"wires":[[],[],[]]},{"id":"aa7c3e9c.fb29e","type":"json","z":"797a7c58.e82134","name":"","property":"payload","action":"obj","pretty":false,"x":960,"y":290,"wires":[["a7fe7e1b.d30a8"]]},{"id":"eee81c93.820e4","type":"function","z":"797a7c58.e82134","name":"","func":"p = msg.payload;\nmsg.payload = p.replace(/'/g, '\"');\nreturn msg;","outputs":1,"noerr":0,"x":960,"y":230,"wires":[["aa7c3e9c.fb29e"]]},{"id":"75eba16c.094f9","type":"mqtt-broker","z":"","name":"","broker":"","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Now try it and see what you receive (obviously I get communication error since I do not have the pijuice)

1 Like

Actually, you can use this simpler code in the function node

msg.payload = msg.payload.replace(/'/g, '"');
return msg;