I`ve got an X728 ups hat and is working right, using the .sh provided by geekworm. What I expect next is to write some code make the .sh to send an email when power is down or up, or when batteries are low (almost 3V) and is going to send the shutdown signal. I am a newby in python, so any help will be welcome. What I have clear:
from email.mime.multipart import MIMEMultipart
import email.mime.application
import smtplib
#replace the next three lines with your credentials
from_email_addr = 'Sender_Email_ID@gmail.com'
from_email_password = 'Password'
to_email_addr = 'Receiver_Email_ID@gmail.com'
#Create the Message
msg = MIMEMultipart()
msg[ 'Subject'] = 'POWER UP!!'#OR DOWN OR IS GOING TO SHUTDOWN. HOW TO IMPLEMENT THIS?
msg['From'] = from_email_addr
msg['To'] = to_email_addr
from x728.sh (what appears to be x728bat.py)
#X728 Battery voltage & precentage reading
#!/bin/bash
sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local
echo '#!/usr/bin/env python
import struct
import smbus
import sys
import time
def readVoltage(bus):
address = 0x36
read = bus.read_word_data(address, 2)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 1.25 /1000/16
return voltage
def readCapacity(bus):
address = 0x36
read = bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped/256
return capacity
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
while True:
print "******************"
print "Voltage:%5.2fV" % readVoltage(bus)
print "Battery:%5i%%" % readCapacity(bus)
if readCapacity(bus) == 100:
print "Battery FULL"
if readCapacity(bus) < 20:
print "Battery LOW"
print "******************"
time.sleep(2)
' > /home/pi/x728bat.py
sudo chmod +x /home/pi/x728bat.py
So, if I write some code as
#X728 Battery voltage & precentage reading
#!/bin/bash
sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local
echo '#!/usr/bin/env python
import struct
import smbus
import sys
import time
from email.mime.multipart import MIMEMultipart
import email.mime.application
import smtplib
def readVoltage(bus):
address = 0x36
read = bus.read_word_data(address, 2)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 1.25 /1000/16
return voltage
def readCapacity(bus):
address = 0x36
read = bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped/256
return capacity
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
while True:
print "******************"
print "Voltage:%5.2fV" % readVoltage(bus)
print "Battery:%5i%%" % readCapacity(bus)
if readCapacity(bus) == 100:
print "Battery FULL"
if readCapacity(bus) < 20:
if readCapacity(bus) < 22
#replace the next three lines with your credentials
from_email_addr = 'Sender_Email_ID@gmail.com'
from_email_password = 'Password'
to_email_addr = 'Receiver_Email_ID@gmail.com'
#Create the Message
msg = MIMEMultipart()
msg[ 'Subject'] = 'Raspberry is going to die'
msg['From'] = from_email_addr
msg['To'] = to_email_addr
print "Battery LOW"
print "******************"
time.sleep(2)
' > /home/pi/x728bat.py
sudo chmod +x /home/pi/x728bat.py
and from x728.sh too (what appears to be x728pld.py
#X728 AC Power loss / power adapter failture detection
#!/bin/bash
sudo sed -e '/button/ s/^#*/#/' -i /etc/rc.local
echo '#!/usr/bin/env python
import RPi.GPIO as GPIO
from email.mime.multipart import MIMEMultipart
import email.mime.application
import smtplib
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN)
def my_callback(channel):
if GPIO.input(6): # if port 6 == 1
print "---AC Power Loss OR Power Adapter Failure---"
#replace the next three lines with your credentials
from_email_addr = 'Sender_Email_ID@gmail.com'
from_email_password = 'Password'
to_email_addr = 'Receiver_Email_ID@gmail.com'
#Create the Message
msg = MIMEMultipart()
msg[ 'Subject'] = 'Power off'
msg['From'] = from_email_addr
msg['To'] = to_email_addr
else: # if port 6 != 1
print "---AC Power OK,Power Adapter OK---"
#replace the next three lines with your credentials
from_email_addr = 'Sender_Email_ID@gmail.com'
from_email_password = 'Password'
to_email_addr = 'Receiver_Email_ID@gmail.com'
#Create the Message
msg = MIMEMultipart()
msg[ 'Subject'] = 'Power on'
msg['From'] = from_email_addr
msg['To'] = to_email_addr
GPIO.add_event_detect(6, GPIO.BOTH, callback=my_callback)
print "1.Make sure your power adapter is connected"
print "2.Disconnect and connect the power adapter to test"
print "3.When power adapter disconnected, you will see: AC Power Loss or Power Adapter Failure"
print "4.When power adapter reconnected, you will see: AC Power OK, Power Adapter OK"
raw_input("Testing Started")
' > /home/pi/x728pld.py
sudo chmod +x /home/pi/x728pld.py
After some work and help from Andrew, I rewrote x728.sh Geekworm Version (from https://github.com/geekworm-com/x728/blob/master/x728.sh) as follows:
#X728 RTC setting up
sudo sed -i '$ i rtc-ds1307' /etc/modules
sudo sed -i '$ i echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device' /etc/rc.local
sudo sed -i '$ i hwclock -s' /etc/rc.local
sudo sed -i '$ i #Start power management on boot' /etc/rc.local
#x728 Powering on /reboot /full shutdown through hardware
#!/bin/bash
sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local
echo '#!/bin/bash
SHUTDOWN=5
REBOOTPULSEMINIMUM=200
REBOOTPULSEMAXIMUM=600
echo "$SHUTDOWN" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction
BOOT=12
echo "$BOOT" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$BOOT/direction
echo "1" > /sys/class/gpio/gpio$BOOT/value
echo "X728 Shutting down..."
while [ 1 ]; do
shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
if [ $shutdownSignal = 0 ]; then
/bin/sleep 0.2
else
pulseStart=$(date +%s%N | cut -b1-13)
while [ $shutdownSignal = 1 ]; do
/bin/sleep 0.02
if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then
echo "X728 Shutting down", SHUTDOWN, ", halting Rpi ..."
sudo poweroff
exit
fi
shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
done
if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then
echo "X728 Rebooting", SHUTDOWN, ", recycling Rpi ..."
sudo reboot
exit
fi
fi
done' > /etc/x728pwr.sh
sudo chmod +x /etc/x728pwr.sh
sudo sed -i '$ i /etc/x728pwr.sh &' /etc/rc.local
#X728 full shutdown through Software
#!/bin/bash
sudo sed -e '/button/ s/^#*/#/' -i /etc/rc.local
echo '#!/bin/bash
BUTTON=13
echo "$BUTTON" > /sys/class/gpio/export;
echo "out" > /sys/class/gpio/gpio$BUTTON/direction
echo "1" > /sys/class/gpio/gpio$BUTTON/value
SLEEP=${1:-4}
re='^[0-9\.]+$'
if ! [[ $SLEEP =~ $re ]] ; then
echo "error: sleep time not a number" >&2; exit 1
fi
echo "X728 Shutting down..."
/bin/sleep $SLEEP
#restore GPIO 13
echo "0" > /sys/class/gpio/gpio$BUTTON/value
' > /usr/local/bin/x728softsd.sh
sudo chmod +x /usr/local/bin/x728softsd.sh
#X728 Battery voltage & precentage reading
#!/bin/bash
sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local
echo '#!/usr/bin/env python
import struct
import smbus
import sys
import time
from email.mime.multipart import MIMEMultipart
import email.mime.application
import smtplib
import datetime
#replace the next three lines with your credentials
from_email_addr = 'Sender_Email_ID@gmail.com'
from_email_password = 'Password'
to_email_addr = 'Receiver_Email_ID@gmail.com'
#Create the Message of Shutdown
def Send_Shutdown_email():
msg = MIMEMultipart()
msg[ 'Subject'] = 'VOLTAGE IS LOW!! + datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S')'
msg['From'] = from_email_addr
msg['To'] = to_email_addr
text = MIMEText("Warning, voltage is very low! System is very close to shutdown")
msg.attach(text)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_email_addr, from_email_password)
server.sendmail(from_email_addr, to_email_addr, msg.as_string())
server.quit()
print('Email sent')
def readVoltage(bus):
address = 0x36
read = bus.read_word_data(address, 2)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 1.25 /1000/16
return voltage
def readCapacity(bus):
address = 0x36
read = bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped/256
return capacity
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
while True:
print "******************"
print "Voltage:%5.2fV" % readVoltage(bus)
print "Battery:%5i%%" % readCapacity(bus)
if readCapacity(bus) == 100:
print "Battery FULL"
if readCapacity(bus) < 22:
Send_Shutdown_email()
if readCapacity(bus) < 20:
print "Battery LOW"
print "******************"
time.sleep(2)
' > /home/pi/x728bat.py
sudo chmod +x /home/pi/x728bat.py
#X728 AC Power loss / power adapter failture detection
#!/bin/bash
sudo sed -e '/button/ s/^#*/#/' -i /etc/rc.local
echo '#!/usr/bin/env python
import RPi.GPIO as GPIO
from email.mime.multipart import MIMEMultipart
import email.mime.application
import smtplib
import datetime
#replace the next three lines with your credentials
from_email_addr = 'Sender_Email_ID@gmail.com'
from_email_password = 'Password'
to_email_addr = 'Receiver_Email_ID@gmail.com'
#Create the Message of Power Down
def Send_PowerDown_email():
msg = MIMEMultipart()
msg[ 'Subject'] = 'POWER DOWN!! + datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S')'
msg['From'] = from_email_addr
msg['To'] = to_email_addr
text = MIMEText("Power Down! Energy has left the room! Did you pay the bill??")
msg.attach(text)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_email_addr, from_email_password)
server.sendmail(from_email_addr, to_email_addr, msg.as_string())
server.quit()
print('---AC Power Loss OR Power Adapter Failure---Email sent')
#Create the Message of Power Up
def Send_PowerUP_email():
msg = MIMEMultipart()
msg[ 'Subject'] = 'POWER UP!! + datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S')'
msg['From'] = from_email_addr
msg['To'] = to_email_addr
text = MIMEText("Power Up! The energy has come to us! :-)")
msg.attach(text)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_email_addr, from_email_password)
server.sendmail(from_email_addr, to_email_addr, msg.as_string())
server.quit()
print('---AC Power OK,Power Adapter OK---Email sent')
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN)
def my_callback(channel):
if GPIO.input(6): # if port 6 == 1
Send_PowerDown_email()
else: # if port 6 != 1
Send_PowerUP_email()
GPIO.add_event_detect(6, GPIO.BOTH, callback=my_callback)
print "1.Make sure your power adapter is connected"
print "2.Disconnect and connect the power adapter to test"
print "3.When power adapter disconnected, you will see: AC Power Loss or Power Adapter Failure"
print "4.When power adapter disconnected, you will see: AC Power OK, Power Adapter OK"
raw_input("Testing Started")
' > /home/pi/x728pld.py
sudo chmod +x /home/pi/x728pld.py
I expect it will work!