[ create a new paste ] login | about

Link: http://codepad.org/ckQ0LuTf    [ raw code | fork ]

meigrafd - Python, pasted on Apr 20:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import datetime
import telepot
import shlex
from subprocess import Popen, PIPE
from RPi import GPIO

geraet_1 = '192.168.178.31'
geraet_2 = '192.168.178.20'

# GPIO 7 = Bewegungsmelder
# GPIO 11 = Relais
GPIO_PIR = 7
GPIO_MON = 11


#Board Mode anstelle GPIO BCM Nummer
GPIO.setmode(GPIO.BOARD)
GPIO.setup(GPIO_PIR, GPIO.IN)
GPIO.setup(GPIO_MON, GPIO.OUT)


def finde_geraet(IP='127.0.0.1', count=5):
    command = shlex.split('/usr/bin/nmap -F -Pn -sF %s' % IP)
    while count > 0:
        returncode=None
        found=False
        print "%s:  <%d>  Suche Gerät: %s" % (datetime.datetime.now(), count, IP)
        process = Popen(command, stdout=PIPE, stderr=PIPE, bufsize=1)
        for output in iter(process.stdout.readline, b''):
            if "host up)" in output.strip():
                found=True
        if process is not None:
            returncode = process.poll()
            process.stdout.close()
            process.wait()
        if found:
            return True
        if (returncode is not None) and (returncode != 0):
            printD("Error! Returncode: %s" % returncode)
        count -= 1
    return False


def handle(msg):
    if 'text' in msg and msg['text'] == '/start':
        with open('/tmp/bewegungsmelder_id.txt', 'w') as f:
            f.write(str(msg['chat']['id']))


def printD(message):
    print "%s:  %s" % (datetime.datetime.now(), message)


Current_State  = 0
Previous_State = 0
try:
    while True:
        Current_State = GPIO.input(GPIO_PIR)
        time.sleep(0.2)
        
        if Current_State==1 and Previous_State==0:
            printD("Bewegung erkannt!")
            Previous_State=1
            GPIO.output(GPIO_MON, GPIO.LOW)
            time.sleep(1)
            GPIO.output(GPIO_MON, GPIO.HIGH)
            time.sleep(1)
            
            printD("Suche Gerät1 ...")
            device1 = finde_geraet(geraet_1)
            if device1:
                printD("Gerät 1: GEFUNDEN")
            else:
                printD("Gerät 1: keine Antwort")
            
            printD("Suche Gerät2 ...")
            device2 = finde_geraet(geraet_2)
            if device2:
                printD("Gerät 2: GEFUNDEN")
            else:
                printD("Gerät 2: keine Antwort")
            
            if not device1 and not device2:
                printD("KEIN GERÄT GEFUNDEN! Starte den TelegramBot")
                
                bot = telepot.Bot('******')
                bot.message_loop(handle)
                printD("TelegramBot: Suche Empfänger-ID aus Liste ...")
                with open('/usr/bin/bewegungsmelder_id.txt', 'r') as idfile:
                    chat_id=int(idfile.read())
                bot.sendMessage(chat_id, "\n========================\nACHTUNG!\nJemand ist zuhause!")
                printD("TelegramBot: Nachricht erfolgreich abgeschickt!")
            else:
                printD("Kein Alarm! Mindestens ein Gerät wurde im Netzwerk gefunden")
            
            printD("Warte 5 Minuten")
            time.sleep(300)
        
        elif Current_State==0 and Previous_State==1:

            printD("Fertig! Warte auf Bewegung...")
            Previous_State=0
            GPIO.output(GPIO_MON, GPIO.LOW)
            time.sleep(1)
            GPIO.output(GPIO_MON, GPIO.HIGH)
            time.sleep(1)
  
except KeyboardInterrupt:
    print " Exit"
    GPIO.cleanup()


Create a new paste based on this one


Comments: