Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
348a742
Cleaning up of code
ColonelGerdauf Dec 21, 2019
3d62a13
Reset default time to 10 minutes (lower limit of the 450mAh battery)
ColonelGerdauf Dec 21, 2019
8aca2e3
Update README.md
ColonelGerdauf Dec 21, 2019
045521a
Update README.md
ColonelGerdauf Dec 21, 2019
82b82b6
Update README.md
ColonelGerdauf Dec 21, 2019
193ea9b
Update README.md
ColonelGerdauf Dec 21, 2019
8daecdc
Update README.md
ColonelGerdauf Dec 21, 2019
3ed7899
Update README.md
ColonelGerdauf Dec 21, 2019
6ce1111
Create ups.service
ColonelGerdauf Dec 21, 2019
043c6d0
Update ups.service
ColonelGerdauf Dec 21, 2019
54b57c8
Update README.md
ColonelGerdauf Dec 21, 2019
0ae135e
Update README.md
ColonelGerdauf Dec 21, 2019
80db274
Update README.md
ColonelGerdauf Dec 21, 2019
0c8e9d1
Update README.md
ColonelGerdauf Dec 21, 2019
f345f86
Update README.md
ColonelGerdauf Dec 21, 2019
3548715
create initial script
ColonelGerdauf Dec 22, 2019
b7feb5a
Update ups_installer.sh
ColonelGerdauf Dec 22, 2019
6d3a0f5
clean up of code
ColonelGerdauf Dec 22, 2019
e51e238
extending command options
ColonelGerdauf Dec 22, 2019
c56646a
Update ups_installer.sh
ColonelGerdauf Dec 22, 2019
2906f86
exclude erroneous inclusion of EOS
ColonelGerdauf Dec 22, 2019
d0d4422
Delete ups_installer.sh
ColonelGerdauf Dec 22, 2019
f533860
Moving of file
ColonelGerdauf Dec 22, 2019
efc34b9
Update README.md
ColonelGerdauf Dec 22, 2019
f6b9aef
Create elec.ups.service
ColonelGerdauf Dec 22, 2019
1049f1d
expansion of specificity of the initializaton
ColonelGerdauf Dec 22, 2019
b6a0ec9
specify standard startup file
ColonelGerdauf Dec 22, 2019
2f009a6
Specify generic type
ColonelGerdauf Dec 22, 2019
aa19cd7
Update README.md
ColonelGerdauf Dec 22, 2019
7008ae6
tidy up of code
ColonelGerdauf Dec 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
## bash script to talk to the ups board

#change the permissions for the script

sudo chmod +x ups.sh

#copy the script to the init.d directory to run the script on startup

sudo cp ups.sh /etc/init.d/

#update the rc file

sudo update-rc.d ups.sh defaults
# howto:
### Download the initialization script
```
wget -nd https://raw.githubusercontent.com/buyapi/ups/master/pi_ups_startup.sh
```
### Run the script
```
pi_ups_startup.sh
```
35 changes: 35 additions & 0 deletions pi_ups_startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

os=$(uname -o);
declare -ru os;



if [ "$os" = "LIBREELEC" ] || [ "$os" = "OPENELEC" ]
then
# these will run as normal, as the default user account is root
wget --non-verbose --tries=3 https://raw.githubusercontent.com/buyapi/ups/master/scripts/ups;
chmod --changes u=rx,g=rx,o=r ups;
chown --changes root:root ups;
mkdir --parents /storage/.kodi/addons/.ups_power/bin/;
mv --force ups /storage/.kodi/addons/.ups_power/bin/;
chmod --changes u=rx,g=rx,o=rx /storage/.kodi/addons/.ups_power/;

wget --non-verbose --tries=3 https://raw.githubusercontent.com/buyapi/ups/master/scripts/elec.ups.service;
chmod --changes u=rx,g=rx,o=r elec.ups.service;
chown --changes root:root elec.ups.service;
mv --force elec.ups.service /storage/.config/system.d/;
elif [ ]
then

else
wget --non-verbose --tries=3 https://raw.githubusercontent.com/buyapi/ups/master/scripts/ups;
chmod --changes u=rx,g=rx,o=r ups;
chown --changes root:root ups;
mv --force ups /bin/;

wget --non-verbose --tries=3 https://raw.githubusercontent.com/buyapi/ups/master/scripts/std_linux.ups.service;
chmod --changes u=rx,g=rx,o=r std_linux.ups.service;
chown --changes root:root std_linux.ups.service;
mv --force std_linux.ups.service /etc/systemd/system/;
fi
10 changes: 10 additions & 0 deletions scripts/elec.ups.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Allows the Raspberry Pi to communicate with the UPS HAT

[Install]
Alias=ups powerline

[Service]
Type=forking
ExecStart=/storage/.kodi/addons/.ups_power/bin
ExecStop=/usr/bin/kill -s QUIT ${MAINPID}
10 changes: 10 additions & 0 deletions scripts/std_linux.ups.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=Allows the Raspberry Pi to communicate with the UPS HAT

[Install]
Alias=ups powerline

[Service]
Type=forking
ExecStart=/bin/ups
ExecStop=/bin/kill -s QUIT ${MAINPID}
95 changes: 95 additions & 0 deletions scripts/ups
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/sh

# GPIO17 (input) used to read current power status.
# 0 - normal (or battery power was switched on manually).
# 1 - main power fault in USB-C (RPi4) or MicroUSB; switched to battery power.
printf 17 > /sys/class/gpio/export;
printf in > /sys/class/gpio/gpio17/direction;

# GPIO27 (input) used to indicate that UPS is online
printf 27 > /sys/class/gpio/export;
printf in > /sys/class/gpio/gpio27/direction;

# GPIO18 used to inform UPS that Pi is still working. After power-off this pin returns to Hi-Z state.
printf 18 > /sys/class/gpio/export;
printf out > /sys/class/gpio/gpio18/direction;
printf 0 > /sys/class/gpio/gpio18/value;

# variables used to store GPIO states
inval_power="0";
ups_online1="0";
ups_online2="0";

# variables used in incremental calculations
power_timer=0;
ups_online_timer=0;

# time parameters
pwoff_timer_min=10; # modify this number to whatever number of minutes you see fit to run the system on batteries before shutdown.
readonly TMP_TMR=$pwoff_timer_min;


while true
do
# read GPIO27 pin value
# normally, UPS toggles this pin every 0.5s
ups_online1=$(cat /sys/class/gpio/gpio27/value);

sleep 0.1;

ups_online2=$(cat /sys/class/gpio/gpio27/value);

ups_online_timer=${ups_online_timer+1};

# toggled?
if [ "$ups_online1" != "$ups_online2" ]
then
ups_online_timer=0;
fi

# end script if ups is offline longer than 5 seconds (no toggling detected)
if [ $ups_online_timer -gt 50 ]
then
printf "The UPS is offline. Exiting...";
printf "To reinitialize the script, please restart the system by entering \"sudo shutdown -r now\"";
exit;
fi

# read GPIO17 pin value
inval_power=$(cat /sys/class/gpio/gpio17/value);

if [ "$inval_power" = 1 ]
then
if [ $power_timer -le 0 ]
then
printf "\e[1;31;1mERROR: The system has lost connection to mains power. Power will transition to battery for 30 minutes, and then safely shut down.\e[0m\n";
/sbin/shutdown -P +$TMP_TMR;
power_timer=1;
else
power_timer=${power_timer+1};
div_rem=$power_timer%600;

if [ $div_rem -eq 0 ]
then
pwoff_timer_min=${pwoff_timer_min-1};

if [ $pwoff_timer_min -gt 1 ]
then
printf "\e[1;33;1m--%d minutes remaining until safe shutdown--\e[0m\n" $pwoff_timer_min;
elif [ $pwoff_timer_min -eq 1 ]
then
printf "\e[1;33;1m--1 minute remaining until safe shutdown--\e[0m\n";
printf "\e[1;33;1mWARNING: Please save your work.\e[0m\n";
fi
fi
fi
else
if [ $power_timer -gt 0 ]
then
printf "\e[1;32;1mNOTICE: Power mains has returned to service. Battery is now charging.\e[0m";
/sbin/shutdown -c;
power_timer=0;
pwoff_timer_min=$TMP_TMR;
fi
fi
done
74 changes: 0 additions & 74 deletions scripts/ups.sh

This file was deleted.