niedziela, 3 stycznia 2016

Powiadomienia sms gdy brak prądu na Raspberry Pi

Potrzebujemy, stary telefon, starą ładowarkę do telefonu, transoptor cny17, 2 oporniki i płytkę raspberry Pi z zainstalowanym systemem. Ja użyłem raspbian wheezy

1. transoptor cny17-1
2. Schemat połączenia

Z lewej strony podłączam starą ładowarkę 5V od telefonu
Z prawej strony Raspberry Pi, Vcc 3.3V, output signal daję na pin 24, output gnd do gnd
Tak wygląda na płytce stykowej
z lewej, ładowarka, czerwony +, czarny -
z prawej, raspberry pi, czerwony 3,3V, żółty pin 24 input, czarny -


3. Test, jak jest 1 to brak prądu, 0 to prąd jest


4. Test z python


5. instalacja gammu

apt-get install gammu
apt-get install python-gammu
pip install python-gammu --upgrade

6. Podłączenie starego telefonu i test czy nasz system widzi telefon. Najpierw trzeba zrobić plik ~/.gammurc. Do wygenerowania pliku użyłem wammu, graficznej nakładki na gammu


7. Program do monitoringu
import RPi.GPIO as GPIO
import gevent
from time import time
import gammu
class Alert:
    def __init__(self):
        self.telephone = ['telephone number 1','telephone number 2']
        self.telephone_alert_sleep = 1800 # 30 minutes
        self.country_code = '+48'
        GPIO.setmode(GPIO.BCM)
        self.pin = 24
        self.clear()
        print "Start"
         
    def __del__(self):
        GPIO.cleanup()
     
    def clear(self):
        self.time_send_sms = 0
    def error(self):
        GPIO.setup(self.pin, GPIO.IN)
        if GPIO.input(self.pin) == 1:
            print "no energy"
            if (int(time()) - self.time_send_sms) > self.telephone_alert_sleep:
                try:
                    self.send_sms(message='no energy')
                except Exception as e:
                    print 'sms ' + str(e)
        else:
            print "yes energy"
            self.clear()
         
    def send_sms(self, message):
        state_machine = gammu.StateMachine()
        state_machine.ReadConfig()
        state_machine.Init()
     
        for num in self.telephone:
            print "send sms %s %s" % (num, message)
            m = {
                'Text': message,
                'SMSC': {'Location': 1},
                'Number': self.country_code+num,
            }          
            state_machine.SendSMS(m)
            gevent.sleep(1)
         
        for dv in self.telephone:
            print "dial voice %s " % dv
            state_machine.DialVoice(dv)
            gevent.sleep(30)
         
        self.time_send_sms = int(time())
    def run(self):
        while True:
            print "run"
            try:
                self.error()
                gevent.sleep(5)
            except Exception as e:
                print str(e)
if __name__ == '__main__':
    alert = Alert()
    gevent.spawn(alert.run())

Brak komentarzy:

Prześlij komentarz