DietPi icon indicating copy to clipboard operation
DietPi copied to clipboard

Uninterruptible Power Supply UPS HAT (B) for Raspberry Pi Please Develop A Script for Support

Open AdamF100001 opened this issue 2 years ago • 3 comments

Creating a feature request

Is your feature request related to a problem? Please describe:

  • ...

Describe the solution you'd like:

  • ...

Describe alternatives you've considered:

  • ...

Additional context

  • ...

I have purchased a Uninterruptible Power Supply UPS HAT (B) for Raspberry Pi from PiHut.

Please can you develop a script for Support?

https://thepihut.com/products/uninterruptible-power-supply-ups-hat-b-for-raspberry-pi

I have alraedy asked the Raspberry Pi Forums for advice here with no luck.

https://forums.raspberrypi.com/viewtopic.php?p=1973049#p1973049

Here is my original tweet about support for the UPS hat here.

https://twitter.com/DietPi_/status/1491961760273903619

Here is a YouTube video of the device specs and it running in Raspberry Pi OS if that helps for development.

https://www.youtube.com/watch?v=p86AbqgRaIo

20220330_123848 20220330_123911

Thank You :)

AdamF100001 avatar Mar 30 '22 12:03 AdamF100001

Many thanks for your request.

Did you already try it with the instructions given here?

  • I2C can be enabled via dietpi-config > Advanced Options.
  • Python 3 can be installed via dietpi-software or just apt install python3.
  • Then download an execute the given Python script. If this is working well, you can start it at boot via /var/lib/postboot.d/ or dietpi-autostart custom foreground script.

MichaIng avatar Mar 30 '22 13:03 MichaIng

Not yet still waiting for amazon to delivery the batteries for the UPS.

Which should be 5th - 11th April.

I will keep you posted.

AdamF100001 avatar Mar 30 '22 13:03 AdamF100001

Ah okay, makes sense. I should have known that manufacturers provide install instructions and software to use HATs on Raspberry Pi, to make it attractive for customers. So what I wrote about required API info and such is covered by this. Of course one can use the quite simple provided Python script as a basis to customise the way which/how info is provided.

MichaIng avatar Mar 30 '22 13:03 MichaIng

Many thanks for your request.

Did you already try it with the instructions given here?

  • I2C can be enabled via dietpi-config > Advanced Options.
  • Python 3 can be installed via dietpi-software or just apt install python3.
  • Then download an execute the given Python script. If this is working well, you can start it at boot via /var/lib/postboot.d/ or dietpi-autostart custom foreground script.

The example above didn't work with dietpi as it's a command for raspberry pi os and not dietpi.

AdamF100001 avatar Jan 18 '23 19:01 AdamF100001

as it's a command for raspberry pi os and not dietpi

What do you mean by that? There are no "commands for raspberry pi os" or "commands for dietpi", all commands/tools can be missing or installed on both, both is Debian and the same under the hood.

If it fails, please share the exact error message, so we can find out what is missing.

MichaIng avatar Jan 18 '23 20:01 MichaIng

None of the commands worked off the website.

https://www.waveshare.com/wiki/UPS_HAT_(B)

AdamF100001 avatar Jan 18 '23 20:01 AdamF100001

As requested by @MichaIng , you would need to share the error message and/or output of these commands. Otherwise we are not able to assist further. In general, things working on RPI OS will work same way on DietPi.

Joulinar avatar Jan 18 '23 20:01 Joulinar

Did you actually read what I posted above and what you quoted? Instead of raspi-config, call dietpi-config to enable I2C. Install Python 3 as instructed. All commands below "Run the example" (from your link) will then work. The only question is whether the last python3 INA219.py will work OOTB or needs adjustments.

EDIT: And yes, without an error message, we cannot help anyway.

MichaIng avatar Jan 18 '23 20:01 MichaIng

Then download an execute the given Python script. If this is working well, you can start it at boot via /var/lib/postboot.d/ or dietpi-autostart custom foreground script.

AdamF100001 avatar Jan 18 '23 20:01 AdamF100001

Where do I download the python script and how do I run it?

AdamF100001 avatar Jan 18 '23 20:01 AdamF100001

The commands below "Run the example" in the linked wiki.

MichaIng avatar Jan 18 '23 21:01 MichaIng

Here is the output of the python3 INA219.py

Load Voltage: 8.412 V Current: 0.045300 A Power: 0.380 W Percent: 100.0%

Is it working correctly?

AdamF100001 avatar Jan 18 '23 21:01 AdamF100001

Seems so, at least the values look consistent: Battery full, no charging. For a test whether it really works, you'd need to unplug the PSU.

MichaIng avatar Jan 18 '23 21:01 MichaIng

Yes! It works

Load Voltage:   8.292 V Current:       -0.134900 A Power:          1.118 W Percent:       95.5%

How do I can start it at boot via /var/lib/postboot.d/ or dietpi-autostart custom foreground script?

Will dietpi shutdown safely when battery is dead?

AdamF100001 avatar Jan 18 '23 21:01 AdamF100001

How do I can start it at boot via /var/lib/postboot.d/ or dietpi-autostart custom foreground script?

Simplest solution:

sed -i '1i\#!/usr/bin/env python3' INA219.py
mv INA219.py /var/lib/postboot.d/

This however blocks the console. You cannot enter any commands until hitting CTRL+C to cancel the script. The 2nd option:

sed -i '1i\#!/usr/bin/env python3' INA219.py
mv INA219.py /var/lib/dietpi/dietpi-autostart/custom.sh
dietpi-autostart 17

It is a bit hacky, since dietpi-autostart is meant to start a shell script. So we rename this Python script and add a shebang to be executed with Python. That way we do not need a wrapper script. This allows you to use the console, but the scripts output may interfere. In this case, the script can be stopped with this command:

systemctl stop dietpi-autostart_custom

SSH still works fine in both cases, of course. Those two use the local console (attached monitor or LCD) only.

Will dietpi shutdown safely when battery is dead?

In theory it is possible to trigger a shutdown via GPIO, but as far as I can see on the product page, this is not supported. What can be done is the following:

  • Since the script runs a loop already, parse the "Current" and "Percent" values.
  • If "Percent" is below e.g. 10% and "Current" negative (de-charging), print a warning and shutdown the system.
  • Could be also done nicely with a little timer, to shutdown in 60 seconds or so, not sure what is reasonable, also depends on how long the batteries hold.
  • Could be also done as dedicated script/service, which does not print the stats to screen.

MichaIng avatar Jan 18 '23 22:01 MichaIng

Hi Again

Please can you confirm that I have successfully added the UPS script to auto start?

root@DietPi:~# sed -i '1i\#!/usr/bin/env python3' INA219.py
sed: can't read INA219.py: No such file or directory
root@DietPi:~# mv INA219.py /var/lib/dietpi/dietpi-autostart/custom.sh
mv: cannot stat 'INA219.py': No such file or directory
root@DietPi:~# dietpi-autostart 17
[  OK  ] DietPi-AutoStart | mkdir -p /etc/systemd/system/[email protected]
e.d /etc/systemd/system/console-getty.service.d
[  OK  ] DietPi-AutoStart | systemctl daemon-reload
root@DietPi:~#

Also, what is the correct command to re-run the UPS_HAT python3 INA219.py script?

Thanks

AdamF100001 avatar Jan 27 '23 19:01 AdamF100001

INA219.py: No such file or directory

Obviously not. You seem to be not in the directory where the script is located. First of all you need to have the script. python3 INA219.py can be used to run it, if you are in the same dir. After moving it to /var/lib/dietpi/dietpi-autostart/custom.sh, it would be

/var/lib/dietpi/dietpi-autostart/custom.sh

But don't do that after reboot, since then it will be started by the autostart option already.


Or did you run the first command, so that it is now here?

ls -l /var/lib/postboot.d/INA219.py

MichaIng avatar Jan 27 '23 19:01 MichaIng

What am I doing wrong here?

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Fri Jan 27 18:55:14 2023 from 192.168.1.152
root@DietPi:~# cd /var/lib/dietpi/dietpi-autostart/custom.sh
-bash: cd: /var/lib/dietpi/dietpi-autostart/custom.sh: No such file or directory
root@DietPi:~# /var/lib/dietpi/dietpi-autostart/custom.sh
-bash: /var/lib/dietpi/dietpi-autostart/custom.sh: No such file or directory
root@DietPi:~# cd UPS_HAT_B
root@DietPi:~/UPS_HAT_B#

AdamF100001 avatar Jan 27 '23 19:01 AdamF100001

Did you activated the dietpi-autostart option for custom scripts already?

Joulinar avatar Jan 27 '23 23:01 Joulinar

I think some basics in shell commands and navigation are required 🙂. Can you list the content of the UPS_HAT_B directory you have just navigated to?

ls -l

MichaIng avatar Jan 27 '23 23:01 MichaIng

root@DietPi:~/UPS_HAT/UPS_HAT# ls -l
total 16
-rw-r--r-- 1 root root 8447 Mar 30  2020 INA219.py
drwx------ 2 root root 4096 Mar 30  2020 UPS_HAT
root@DietPi:~/UPS_HAT/UPS_HAT#

AdamF100001 avatar Jan 28 '23 00:01 AdamF100001

Did you activated the dietpi-autostart option for custom scripts already?

How do I activated the dietpi-autostart ?

AdamF100001 avatar Jan 28 '23 00:01 AdamF100001

How do I activated the dietpi-autostart ?

run dietpi-autostart and select the option to start a custom script

Joulinar avatar Jan 28 '23 00:01 Joulinar

Okay there is the script. Does it already have the shebang? Print the first line of the script:

head -1 INA219.py

MichaIng avatar Jan 28 '23 00:01 MichaIng

Okay there is the script. Does it already have the shebang? Print the first line of the script:

head -1 INA219.py
root@DietPi:~/UPS_HAT# head -1 INA219.py
head: cannot open 'INA219.py' for reading: No such file or directory

AdamF100001 avatar Jan 28 '23 00:01 AdamF100001

How do I activated the dietpi-autostart ?

run dietpi-autostart and select the option to start a custom script

start a custom script in the background or foreground?

AdamF100001 avatar Jan 28 '23 00:01 AdamF100001

Wait with the autostart option, this cannot work yet.

head: cannot open 'INA219.py' for reading: No such file or directory

Strange, you just showed the content of the current directory and it was right there 🤔. Was there some typo inside? Ah, it is somehow nested ~/UPS_HAT/UPS_HAT. Please go into this directory and check again:

cd /root/UPS_HAT/UPS_HAT
head -1 INA219.py

MichaIng avatar Jan 28 '23 00:01 MichaIng

Wait with the autostart option, this cannot work yet.

head: cannot open 'INA219.py' for reading: No such file or directory

Strange, you just showed the content of the current directory and it was right there 🤔. Was there some typo inside? Ah, it is somehow nested ~/UPS_HAT/UPS_HAT. Please go into this directory and check again:

cd /root/UPS_HAT/UPS_HAT
head -1 INA219.py

root@DietPi:~/UPS_HAT/UPS_HAT# cd /root/UPS_HAT/UPS_HAT
root@DietPi:~/UPS_HAT/UPS_HAT# head -1 INA219.py
import smbus
root@DietPi:~/UPS_HAT/UPS_HAT#

AdamF100001 avatar Jan 28 '23 01:01 AdamF100001

Okay not sure what you did, but you seem to have several of this directories nested into each other. However there is the script, without shebang. Add it, and make it executable:

sed -i '1i\#!/usr/bin/env python3' INA219.py
chmod +x INA219.py

A new first line has been added:

head -1 INA219.py

This is to tell the shell how to execute it. Before, you needed to do this to execute the script:

python3 INA219.py

So you needed to call it with Python explicitly. With the above change, you can do:

./INA219.py

and the the shebang tells the shell how it needs to be invoked. This is required for the autostart option as well as the postboot.d option, since they execute the files just like this. Now you can move the script to the autostart location and select it:

mv INA219.py /var/lib/dietpi/dietpi-autostart/custom.sh
dietpi-autostart

Choose "foreground", as in background you would not see the output if the script.

MichaIng avatar Jan 28 '23 01:01 MichaIng

Okay not sure what you did, but you seem to have several of this directories nested into each other. However there is the script, without shebang. Add it:

sed -i '1i\#!/usr/bin/env python3' INA219.py

This is to tell the shell how to execute it. Before, you needed to do this to execute the script:

python3 INA219.py

So you need to call it with Python explicitly. With the above change, you can do:

./INA219.py

and the the shebang tells the shell how it needs to be invoked. This is required for the autostart option as well as the postboot.d option, since they execute the files just like this. Now you can move the script to the autostart location:

mv INA219.py /var/lib/dietpi/dietpi-autostart/custom.sh

root@DietPi:~/UPS_HAT/UPS_HAT# sed -i '1i\#!/usr/bin/env python3' INA219.
sed: can't read INA219.: No such file or directory
root@DietPi:~/UPS_HAT/UPS_HAT# python3 INA219.py
Load Voltage:  19.136 V
Current:        0.000600 A
Power:          0.014 W
Percent:       100.0%
Load Voltage:  19.064 V
Current:        0.000600 A
Power:          0.010 W
Percent:       100.0%
Load Voltage:  19.064 V
Current:        0.000600 A
Power:          0.010 W
Percent:       100.0%
^CTraceback (most recent call last):
  File "/root/UPS_HAT/UPS_HAT/INA219.py", line 214,
in <module>
    time.sleep(2)
KeyboardInterrupt
root@DietPi:~/UPS_HAT/UPS_HAT# ./INA219.py
-bash: ./INA219.py: Permission denied
root@DietPi:~/UPS_HAT/UPS_HAT#

I didn't run the last command.

Please can you create a fully automated script for the full process.

Sorry, I'm not a programmer, so finding this difficult.

AdamF100001 avatar Jan 28 '23 01:01 AdamF100001