#!/usr/bin/python # # Script for RPI Power Pack Hat Pro V1.1 with MeterN # # upshat.py rev. 1.1 # # enable I2C via raspi-config # sudo apt-get install i2c-tools # sudo apt-get install python-smbus # sudo apt-get install python-requests # ln -s /var/www/MyScripts/ups/upshat.py /usr/bin/ups # i2cset -y 1 0x62 0x0A 0x00 Wake-up the device I2C function; MODE register address 0x0A set to 0x00 # # # History # # 29-04-2018: Rilascio versione v1.0 # # - Versione iniziale # # 29-05-2018: Rilascio vesrione v1.1 # # - Aggiunto controllo per mancata lettura # Modifica nome file temporaneo svincolato da n. di misuratore # # # Lo script legge tramite bus i2C il valore di tensione e capacita della batteria # della scheda RPI Power Pack Hat Pro V1.1 # https://www.banggood.com/Geekworm-Power-Pack-Pro-V1_1-Lithium-Battery-Power-Source-UPS-HAT-Expansion-Board-For-Raspberry-Pi-p-1205973.html?rmmds=search&cur_warehouse=CN # Lo script effettua un controllo ogni 60s sulla capacita della batteria ed invia delle # notifiche push sul cellulare tramite il servizio gratuito RPINotify (Telegram) # nei seguenti casi: # 1 capacita < 95% notifica il funzionamento a batteria # 2 avviso per capacita residua < 20% # 3 avviso per capacita residua < 5% e successivo spegnimento del Raspberry-Pi # # Le letture vengono salvate nel file temporaneo /dev/shm/ups.txt pronte per # essere poi lette e formattate tramite reqsdm e visualizzate in MeterN # # A seguire le righe da aggiungere a /var/www/comapps/reqsdm.php per leggere la tensione # (con il comando reqsdm bat_ten) e la capacita della batteria (con il comando reqsdm bat_cap) # # } elseif ($argv[1] == 'bat_ten') { # $outstr = exec('cat /dev/shm/ups.txt | grep "*V)"'); # } elseif ($argv[1] == 'bat_cap') { # $outstr = exec('cat /dev/shm/ups.txt | grep "*%)"'); # import struct import smbus import sys import os import time import requests # Inserire il token per l'invio dei messaggi con RPInotify - Lasciare vuoto se non si utilizza il servizio di notifica TOKEN = "" # # da qui in poi non sono necessarie modifiche # file = "/dev/shm/ups.txt" control20 = 0 control95 = 0 controlNOK = 0 sys.stdout=open(file,"w") print "ups_1(0*V)" # Tensione print "ups_2(0*%)" # Capacita sys.stdout.close() while True: def readVoltage(bus): "This function returns as float the voltage from the Raspi UPS Hat via the provided SMBus object" address = 0x62 read = bus.read_word_data(address, 2) swapped = struct.unpack("H", read))[0] voltage = swapped * 305.0 /1000000 return voltage def readCapacity(bus): "This function returns as a float the remaining capacity of the battery connected to the Raspi UPS Hat via the provided SMBus object" address = 0x62 read = bus.read_word_data(address, 4) swapped = struct.unpack("H", read))[0] capacity = swapped/256 return capacity bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1) sys.stdout=open(file,"w") print "ups_1({0:0.2f}*V)".format(readVoltage(bus)) # Tensione print "ups_2({0:0.0f}*%)".format(readCapacity(bus)) # Capacita % sys.stdout.close() # Controllo lettura NO OK if readVoltage(bus) == 0 and controlNOK == 0: MSG = "RPI UPS Hat - Lettura UPS Hat NON FUNZIONANTE - Necessario comando i2cset -y 1 0x62 0x0A 0x00" req = requests.post('https://api.rpinotify.it/message/' + TOKEN + '/', data={'text': MSG}) controlNOK = 1 # Notifiche con RPINotify if TOKEN != "" and readVoltage(bus) > 0: if readCapacity(bus) > 20: control20 = 0 controlNOK = 0 if readCapacity(bus) <= 20 and control20 == 0: MSG = "RPI UPS Hat - ATTENZIONE Batteria residua 20%" req = requests.post('https://api.rpinotify.it/message/' + TOKEN + '/', data={'text': MSG}) control20 = 1 #print(req.text) if readCapacity(bus) > 95: control95 = 0 if readCapacity(bus) <= 95 and control95 == 0: MSG = "RPI UPS Hat - Funzionamento a batteria - Batteria residua 95%" req = requests.post('https://api.rpinotify.it/message/' + TOKEN + '/', data={'text': MSG}) control95 = 1 # Shutdown system if readCapacity(bus) < 5 and readVoltage(bus) > 0: #print "System will shutdown now,bye!" os.system("sudo poweroff") sys.exit(1) time.sleep(60)