Selasa, 27 Agustus 2019

Python Var Global dan Telegram RX TX

import time, datetime
import RPi.GPIO as GPIO
import telepot
from telepot.loop import MessageLoop
import os
import sys
from picamera import PiCamera
from time import sleep
from sys import argv
import gps
import requests

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

relay = 20
door1 = 21
door2 = 16
door3 = 12
door4 = 05

GPIO.setup(door1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(door2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(door3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(door4, GPIO.IN, pull_up_down=GPIO.PUD_UP)

anow = datetime.datetime.now()
camera = PiCamera()
GPIO.setup(relay, GPIO.OUT)
GPIO.output(relay, 0) #Off initially

session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

def action(msg):
    global out
    rep = session.next()
    print str(out)+"###"
    chat_id = msg['chat']['id']
    command = msg['text']
    print 'Received: %s' % command
    try :
        if command == 'lokasi'  and out==0:
            (rep["class"] == "TPV")
            addr=str(rep.lat) + "," + str(rep.lon)
            print(addr)
            URI="https://maps.google.com?q="+addr
            print(URI)
            telegram_bot.sendMessage (chat_id, text=URI)
            print "sukses terkirim"                         
            #os.system('curl -s -X POST https://api.telegram.org/bot763220421:AAHt3f2V5qnJ1sjSg2kRva1uxL4ytt5df6Q/sendMessage -d chat_id=647876166 -d text="'+URI+'"') #% (URI)
                           
                       
        if 'hidupkan' in command and out==0:
            out=0
            message = "hidupkan"
            if 'mesin' in command:
                message = message + "mesin"
                GPIO.output(relay, 0)
                os.system('curl -s -X POST https://api.telegram.org/bot841400617:AAGCmnMJlPrdVK5cvlI2ZHdgri6Pp7jUzes/sendMessage -d chat_id=444460946 -d text="Mesin hidup"')
               
        if 'matikan' in command and out==0:
            out=0
            message = "matikan"
            if 'mesin' in command:
                message = message + "mesin "
                GPIO.output(relay, 1)
                os.system('curl -s -X POST https://api.telegram.org/bot841400617:AAGCmnMJlPrdVK5cvlI2ZHdgri6Pp7jUzes/sendMessage -d chat_id=444460946 -d text="Mesin mati"')
               
        if command == 'foto'  and out==0:
            camera.start_preview()
            sleep(3)
            camera.capture('/home/pi/data/image.jpg')
            camera.stop_preview()
            telegram_bot.sendPhoto(chat_id=chat_id, photo=open('/home/pi/data/image.jpg', 'rb'))
            print "sukses terkirim"
            out=0
           
        if command == 'video'  and out==0:
            camera.start_preview()
            camera.start_recording('/home/pi/data/video.h264')
            sleep(5)
            camera.stop_recording()
            camera.stop_preview()
            telegram_bot.sendVideo(chat_id, video=open('/home/pi/data/video.h264'))
            print "sukses terkirim"
            out=0

     
       
        if command == 'tidak aktif':
            out=1
            print "Sistem tidak Aktif"

        if command == 'aktif':
            out=0
            print "Sistem Aktif"
            os.system('curl -s -X POST https://api.telegram.org/bot841400617:AAGCmnMJlPrdVK5cvlI2ZHdgri6Pp7jUzes/sendMessage -d chat_id=444460946 -d text="Sistem diaktifkan"')

         
    except Exception as e :
        print("Got exception " + str(e))
           
        return out


out=212
telegram_bot = telepot.Bot('841400617:AAGCmnMJlPrdVK5cvlI2ZHdgri6Pp7jUzes')
print (telegram_bot.getMe())
out=MessageLoop(telegram_bot, action).run_as_thread()
print 'Up and Running....'

while True:
    global out
    #print str(out)+"@@@"
    if out==0:
        if GPIO.input(door1):
           print("Pintu-1 Terbuka")
           os.system('curl -s -X POST https://api.telegram.org/bot841400617:AAGCmnMJlPrdVK5cvlI2ZHdgri6Pp7jUzes/sendMessage -d chat_id=444460946 -d text="Pintu-1 Terbuka"')
           time.sleep(1)
        if GPIO.input(door1) == False:
           #print("Pintu Tertutup")
           time.sleep(1)

        if GPIO.input(door2):
           print("Pintu-2 Terbuka")
           os.system('curl -s -X POST https://api.telegram.org/bot841400617:AAGCmnMJlPrdVK5cvlI2ZHdgri6Pp7jUzes/sendMessage -d chat_id=444460946 -d text="Pintu-2 Terbuka"')
           time.sleep(1)
        if GPIO.input(door2) == False:
           #print("Pintu Tertutup")
           time.sleep(1)

        if GPIO.input(door3):
           print("Pintu-3 Terbuka")
           os.system('curl -s -X POST https://api.telegram.org/bot841400617:AAGCmnMJlPrdVK5cvlI2ZHdgri6Pp7jUzes/sendMessage -d chat_id=444460946 -d text="Pintu-3 Terbuka"')
           time.sleep(1)
        if GPIO.input(door3) == False:
           #print("Pintu Tertutup")
           time.sleep(1)

        if GPIO.input(door4):
           print("Pintu-4 Terbuka")
           os.system('curl -s -X POST https://api.telegram.org/bot841400617:AAGCmnMJlPrdVK5cvlI2ZHdgri6Pp7jUzes/sendMessage -d chat_id=444460946 -d text="Pintu-4 Terbuka"')
           time.sleep(1)
        if GPIO.input(door4) == False:
           #print("Pintu Tertutup")
           time.sleep(1)

       

while 1:
    time.sleep(10)
   


Tidak ada komentar:

Posting Komentar