-
Notifications
You must be signed in to change notification settings - Fork 5
/
checkpower
75 lines (69 loc) · 3.3 KB
/
checkpower
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash -u
# Script_name : checkpower
# Author : Bert Lindeman
# Description : Check the battery voltage and warn if the voltage is
# less than 7 volts,
# also set the left EV3 led to blinking green, amber or red.
# Version : v1.3
# Date : 2014-11-04
# Usage : checkpower
# No arguments needed nor used
# Notes : ! TOBE verified: At what voltage does the brick shutdown.
# assumed 6 volts would be acceptable.
# o power (using an EV3 rechargable battery) usualy drops 0.1 V
# in about half an hour. So IF this script is run as cron task
# running it each 10 minutes should be sufficient.
# Copyright : 2014 Bert Lindeman
# License : GPL-3.0+
#
#============================================================================
#
# Changelog :
# Date By Description
# 20141104 BL prepare for release v1.3
# 20140827 BL o 6 volts is just a bit too low as last warning level
# Last drop of voltage e.g. was too much
# from 6303066 to 5668733 in 10 minutes (my check interval)
# Causing auto? shutdown.
# 20140902 BL o add the momentary current to the message
# 20140914 BL o add the voltage to the blinkvoltage call,
# to adapt the type of blinking.
# Since kernel 3.16.1 leds can be controlled better.
# 20140826 BL o Add header
# o Do not ALWAYS state that power is "OK"
# but show the status WARN / MED / LOW / OK
# o whole algorithm did NOT work :-(
# o Check if ENVIRONMENT variable is set.
# 20150206 BL Use leading blank (not leading zero) on day of month
# So it looks more like the other data in syslog.
#============================================================================
status=UNKWN # set default
cd /sys/devices/platform/legoev3-battery/power_supply/legoev3-battery
voltage=`cat voltage_now`
# voltage is in microvolt as documented in include/linux/power_supply.h
# Did not take the trouble to format this.
current=`cat current_now`
if [ $voltage -lt 6200000 ]; then
/usr/local/bin/blinkvoltage red $voltage
beep
status=LOW
# issue a warning; if setup, then cron can email messages like this.
echo -e "WARNING: voltage $status: $voltage"
elif [ $voltage -lt 6500000 ]; then
/usr/local/bin/blinkvoltage amber $voltage
status=MED
echo -e "WARNING: voltage $status: $voltage"
elif [ $voltage -lt 7000000 ]; then
/usr/local/bin/blinkvoltage green $voltage
status=WARN
else
/usr/local/bin/blinkvoltage none $voltage
status=OK
fi
# Assign "non-cron" if ENVIRONMENT is unset or null, and thus prevent error "variable unbound":
: "${ENVIRONMENT:=non-cron}"
if [ "$ENVIRONMENT" == "non-cron" ]; then # just a message; no need to add to syslog
echo -e "voltage $status: $voltage; current: $current "
else
echo -e "`date +"%b %_d %T"` $HOSTNAME cron: $ENVIRONMENT - voltage $status: $voltage; current: $current" >> /var/log/syslog
fi