PiSupply / PiJuice

Resources for PiJuice HAT for Raspberry Pi - use your Pi Anywhere
https://uk.pi-supply.com/collections/pijuice/products/pijuice-portable-power-raspberry-pi
GNU General Public License v3.0
435 stars 104 forks source link

PiJuice System Events: 'System Start' and 'System Stop' #871

Open Stevo52 opened 2 years ago

Stevo52 commented 2 years ago

I have been trying to work out how to use the two 'new' System Events that show in the GUI in Ver 1-6

I have experimented a bit and wrote a simple code that turns the Pijuice I/O ports on for a second, then off. Running this code manually from Thonny IDE when the Pi 4B! 8G + PiJuice + Blinkt! + ButtonShim + a couple of Relay Plates is powered up. Works fine, just lighting up a couple of LEDS at this stage.

But if I try to use that code as User Function 1, assigned to 'System Start' Event (with System Task Enabled) doesn't seem to work when I power the system up via a switch on the SW1 on the PiJuice. The IO ports don't change state at all. I tried setting the IO ports manually to boot up aqs active works, but the code doesn't turn the ports off.

I think that the System Start event occurs very early in the boot process, well before the code file is available on the Pi I think? If so, how can that event be used programatically?

This issue: https://github.com/PiSupply/PiJuice/issues/612#issuecomment-750965633 makes reference to those 'new' events but I haven't been able to find any documentation or reference to help me troublehoot. Can anyone point me to such documents if they available? @tvoverbeek was also referenced in the same item, are you able to help with this?

Thanks in advance, for any assistance. In the meantime I'll look in to other ways of doing what I need.

tvoverbeek commented 2 years ago

The function specified for the 'System Start' event is called by the startup code of the pijuice service just before the service (pijuice_sys.py) starts its polling loop. See lines 395-396 in https://github.com/PiSupply/PiJuice/blob/master/Software/Source/src/pijuice_sys.py

The function specified for the 'System Stop' event is called when the pijuice service is shutting down. Note that shutdown/halt is then already in progress. So do not call shutdown from this user function. See lines 329-330 in https://github.com/PiSupply/PiJuice/blob/master/Software/Source/src/pijuice_sys.py.

Both functions are called with configData as argument.

Stevo52 commented 2 years ago

Thanks @tvoverbeek I had already found reference to the System Start event and also the use of configData in calling the nominated file for the event. As I understand it, the details in configData argument come from settings in the CLI/GUI for that event and the available settings in the GUI were sufficient to run a program at that point? Or do I have to make changes to the pijuice_sys.py to allow it to work?

I still can't seem to work the timing out and why the function doesn't call the test file I created? The same file works fine as a crontab job at reboot, or if I run the command manually once the Pi is fully booted up. What am I missing? Is there any documentation around that event available? And is the System Start event any sooner in the boot process than the crontab activity?

The System Stop event isn't really required at this stage as I can monitor the presence/absence of Pi/PiJuice VSys, to do any shutdown actions externally, should I need to.

What I am trying to do is activate a latching relay as soon as possible after turning the PiJuice and the rest of the system is on, so that I can start pre-heating of some sensors via a relay and an external power supply. That would allow a scheduled start evey 15 minutes or so to take some readings once the sensors are stabilised. I'd prefer to use code in the device instead of relying on an another device to turn the heaters on. But the sensor functions are not the issue here, it is more the timing sequence that is required.

It just seems curious to me, that there is a System Start Event at all, if it won't run a program from the event via the available settings at that System Start point? What was the intention of that event? Has it been used by others? Are there examples of it's use available?

tvoverbeek commented 2 years ago

The user function has to be an executable script owned by a non-privileged user and the non-privileged user has to belong to the pijuice group. Specifying USER_FUNC1 as python3 myfunction.py does not work. myfunction.ppy has to an executable script. First script line should be #!/usr/bin/python3 for a python script (or #!/usr/bin/bash for a bash shell script). Suggest to add some logging to file at the start of the script. Then you know if the script gets called at all. If I use https://github.com/PiSupply/PiJuice/blob/master/Software/Test/user_func1.py as USER_FUNC1 assigned to the start event then I get a nice log in user_func1.log with the configuration.

Stevo52 commented 2 years ago

Thanks for the clarifications @tvoverbeek . The additional information was very helpful towards resolving the issue. The script I was trying to run, complied with all those requirements you mentioned but still wouldn't work from the System Start event. I added the logging routine to the start of the script and still no go.. :-(

So I stepped backwards, created a simple logging script, based on the example you provided and then used that as a user funtion triggered from System Start, and that worked.. so I then added the simple script to set IO1 as Digital Output, high (1), a 2 sec sleep then setting IO1 Low (0) and that still worked... so I achieved what I was trying to do via the System Start Event.. Then I went back to my original script and it still wouldn't work. My guess is the file was corrupted somehow, even though it looked OK in Thonny. I now understand the use of those System Events a lot better, but maybe some detailed documentation/user reference is needed?

But in the meantime, I actually went in a different direction, sensing the low to high transition of Vsys or 3.3V, generating the required startup pulse to latch the latching relay. This gave me the earliest possible 'Turn On' pulse and ensures there is sufficient time for the sensor heating element to get up to temperature and allow an accurate read on the first loop of my sensor monitoring code which was the whole aim.. I am now able to trigger the Pi remotely, activate the high voltage heating elements and then take a series of reads to establish and average, then shut down the Pi and the sensors interface (the heating elements go to an idle, low temperature mode). The next read (manual, or scheduled remote activation, or local timed activation in the event of loss of comm's) sets the process off again. The new design avoids the use of the System Stop event also and the system is now working as I had hoped for with the design.