Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- from gpiozero import DigitalInputDevice
- import telepot
- import picamera
- import RPi.GPIO as GPIO
- import time
- from time import sleep
- import os
- import subprocess
- from subprocess import call
- sensor = DigitalInputDevice(17, pull_up=True)
- def handle(msg):
- global telegramText
- global chat_id
- global receiveTelegramMessage
- chat_id = msg['chat']['id']
- telegramText = msg['text']
- print("Mensagem Recebida: " + str(chat_id))
- if telegramText == "/help":
- bot.sendMessage(chat_id, "Olá Eu Sou o RaspBerry Quem Comanda a Tua Campainha !")
- bot.sendMessage(chat_id, "Ativar Para Capturar Photo Quando é Detetado Movimento")
- bot.sendMessage(chat_id, "Rf Para Testar a Campainha.")
- bot.sendMessage(chat_id, "R1_On / R1_Off Para Testar a Campainha (Enquanto o R1 Rstiver Ativo a Campainha Ira Tocar .")
- bot.sendMessage(chat_id, "Abrir / Fechar portão Para Abrir ou Fechar O Portão")
- bot.sendMessage(chat_id, "Abrir porta Para Abrir A Porta ElectoMagnetica (10segundos) .")
- bot.sendMessage(chat_id, "Video Para Enviar o Video Gravado.")
- bot.sendMessage(chat_id, "Foto Para Capturar Uma Foto.")
- bot.sendMessage(chat_id, "Rvideo Para Gravar um video.")
- else:
- receiveTelegramMessage = True
- def capture():
- print("A Tirar a Foto...")
- camera = picamera.PiCamera()
- camera.capture('./photo.jpg')
- camera.close()
- print("A Enviar a Foto Para " + str(chat_id))
- bot.sendPhoto(chat_id, photo = open('./photo.jpg', 'rb'))
- camera = picamera.PiCamera()
- camera.resolution = (320, 480)
- camera.framerate = 30
- camera.start_recording("video.h264")
- sleep(10)
- camera.stop_recording()
- camera.close()
- os.remove("2video.mp4")
- command = "MP4Box -add video.h264 2video.mp4"
- call([command], shell=True)
- print("Video Converetido Com Sucesso")
- def sensorTrigger():
- global statusText
- global sendTelegramMessage
- global cameraEnable
- global sendPhoto
- statusText = "Alguem Ativou a Campainha!"
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(27, GPIO.OUT)
- print ("on")
- GPIO.output(27,GPIO.HIGH)
- time.sleep(1)
- print ("off")
- GPIO.output(27,GPIO.LOW)
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(13,GPIO.OUT)
- print ("R1_on")
- GPIO.output(13,GPIO.HIGH)
- time.sleep(3)
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(13,GPIO.OUT)
- print ("R1_Off")
- GPIO.output(13,GPIO.LOW)
- sendTelegramMessage = True
- if cameraEnable == True:
- sendPhoto = True
- bot = telepot.Bot('1376731924:AAF4x1yu-SOIP75oQxOBwLpBXn1o0NohKkE')
- bot.message_loop(handle)
- sensor.when_deactivated = sensorTrigger
- receiveTelegramMessage = False
- sendTelegramMessage = False
- cameraEnable = False
- sendPhoto = False
- statusText = ""
- print("Tudo a Funcionar :)")
- try:
- while True:
- if receiveTelegramMessage == True:
- receiveTelegramMessage = False
- statusText = ""
- if telegramText == "Ativar":
- cameraEnable = True
- statusText = "A Camera Esta Ativa"
- elif telegramText == "Foto":
- statusText = "Foto"
- print("A Tirar a Foto...")
- camera = picamera.PiCamera()
- camera.capture('./photo.jpg')
- camera.close()
- print("A Enviar a Foto Para " + str(chat_id))
- bot.sendPhoto(chat_id, photo = open('./photo.jpg', 'rb'))
- elif telegramText == "/start":
- statusText = "Ola..."
- bot.sendMessage(chat_id, "Olá Eu Sou o RaspBerry Quem Comanda a Tua Campainha !")
- statusText = "/help Para Ver os Comandos!"
- elif telegramText == "Rf":
- statusText = "A Tocar..."
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(27, GPIO.OUT)
- print ("on")
- GPIO.output(27,GPIO.HIGH)
- time.sleep(1)
- print ("off")
- GPIO.output(27,GPIO.LOW)
- elif telegramText == "R1_on":
- statusText = "Rele 1 ON..."
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(13,GPIO.OUT)
- print ("R1_on")
- GPIO.output(13,GPIO.HIGH)
- elif telegramText == "R1_off":
- statusText = "Rele 1 Off..."
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(13,GPIO.OUT)
- print ("R1_Off")
- GPIO.output(13,GPIO.LOW)
- elif telegramText == "R2_on":
- statusText = "Rele 2 ON..."
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(19,GPIO.OUT)
- print ("R2_on")
- GPIO.output(19,GPIO.HIGH)
- elif telegramText == "R2_off":
- statusText = "Rele 2 Off..."
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(19,GPIO.OUT)
- print ("R2_Off")
- GPIO.output(19,GPIO.LOW)
- elif telegramText == "R3_on":
- statusText = "Rele 3 ON..."
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(21,GPIO.OUT)
- print ("R3_on")
- GPIO.output(21,GPIO.HIGH)
- elif telegramText == "R3_off":
- statusText = "Rele 3 Off..."
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(21,GPIO.OUT)
- print ("R3_Off")
- GPIO.output(21,GPIO.LOW)
- elif telegramText == "Rt_off":
- statusText = "Todos Os Reles Off..."
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(13,GPIO.OUT)
- GPIO.setup(19,GPIO.OUT)
- GPIO.setup(26,GPIO.OUT)
- print ("Todos Os Reles Off")
- GPIO.output(13,GPIO.LOW)
- GPIO.output(19,GPIO.LOW)
- GPIO.output(26,GPIO.LOW)
- elif telegramText == "Rt_on":
- statusText="Todos Os Reles On..."
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(13,GPIO.OUT)
- GPIO.setup(19,GPIO.OUT)
- GPIO.setup(26,GPIO.OUT)
- print ("Todos Os Reles On")
- GPIO.output(13,GPIO.HIGH)
- GPIO.output(19,GPIO.HIGH)
- GPIO.output(26,GPIO.HIGH)
- elif telegramText == "Rvideo":
- statusText = "Video"
- camera = picamera.PiCamera()
- camera.resolution = (320, 480)
- camera.framerate = 30
- camera.start_recording("video.h264")
- sleep(5)
- camera.stop_recording()
- camera.close()
- os.remove("2video.mp4")
- command = "MP4Box -add video.h264 2video.mp4"
- call([command], shell=True)
- print("Video Converetido Com Sucesso")
- bot.sendVideo(chat_id, video = open("2video.mp4", 'rb'))
- print("A Enviar o Video Para " + str(chat_id))
- elif telegramText == "Video":
- bot.sendVideo(chat_id, video = open("2video.mp4", 'rb'))
- print("A Enviar o Video Para " + str(chat_id))
- elif telegramText == "Abrir porta":
- statusText = "Porta Aberta"
- bot.sendMessage(chat_id, "Porta Aberta")
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(21,GPIO.OUT)
- print ("R3_on")
- GPIO.output(21,GPIO.HIGH)
- sleep(10)
- statusText = "Ja Pode Fechar A Porta"
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(21,GPIO.OUT)
- print ("R3_Off")
- GPIO.output(21,GPIO.LOW)
- elif telegramText == "Abrir portao":
- statusText = "portão aberto"
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(19,GPIO.OUT)
- print ("R2_on")
- GPIO.output(19,GPIO.HIGH)
- sleep(2)
- statusText = "Portão Aberto"
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(19,GPIO.OUT)
- print ("R3_Off")
- GPIO.output(19,GPIO.LOW)
- elif telegramText == "Fechar portao":
- statusText = "Portão Fechado"
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(19,GPIO.OUT)
- print ("R2_on")
- GPIO.output(19,GPIO.HIGH)
- sleep(2)
- statusText = "Portão Fechado"
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(19,GPIO.OUT)
- print ("R3_Off")
- GPIO.output(19,GPIO.LOW)
- else:
- bot.sendMessage(chat_id, "Este Comando Não Conta Na Lista!")
- statusText = "/help Para Ver os Comandos"
- sendTelegramMessage = True
- if sendTelegramMessage == True:
- sendTelegramMessage = False
- chat_id = str(chat_id)
- bot.sendMessage(chat_id, statusText)
- if cameraEnable == True and sendPhoto == True:
- cameraEnable = False
- sendPhoto = False
- capture()
- except KeyboardInterrupt:
- sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement