Advertisement
ReiZeMuitoGordo

Untitled

Jun 12th, 2021
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.75 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from gpiozero import DigitalInputDevice
  4. import telepot
  5. import picamera
  6. import RPi.GPIO as GPIO
  7. import time
  8. from time import sleep
  9. import os
  10. import subprocess
  11. from subprocess import call
  12.  
  13.  
  14. sensor = DigitalInputDevice(17, pull_up=True)
  15.  
  16.  
  17. def handle(msg):
  18.   global telegramText
  19.   global chat_id
  20.   global receiveTelegramMessage
  21.  
  22.   chat_id = msg['chat']['id']
  23.   telegramText = msg['text']
  24.  
  25.   print("Mensagem Recebida: " + str(chat_id))
  26.  
  27.   if telegramText == "/help":
  28.     bot.sendMessage(chat_id, "Olá Eu Sou o RaspBerry Quem Comanda a Tua Campainha !")
  29.     bot.sendMessage(chat_id, "Ativar Para Capturar Photo Quando é Detetado Movimento")
  30.     bot.sendMessage(chat_id, "Rf Para Testar a Campainha.")
  31.     bot.sendMessage(chat_id, "R1_On / R1_Off Para Testar a Campainha (Enquanto o R1 Rstiver Ativo  a Campainha Ira Tocar .")
  32.     bot.sendMessage(chat_id, "Abrir / Fechar portão Para Abrir ou Fechar O Portão")
  33.     bot.sendMessage(chat_id, "Abrir porta Para Abrir A Porta ElectoMagnetica (10segundos) .")
  34.     bot.sendMessage(chat_id, "Video Para Enviar o Video Gravado.")
  35.     bot.sendMessage(chat_id, "Foto Para Capturar Uma Foto.")
  36.     bot.sendMessage(chat_id, "Rvideo Para Gravar um video.")
  37.   else:
  38.    
  39.     receiveTelegramMessage = True
  40.  
  41. def capture():
  42.     print("A Tirar a Foto...")
  43.     camera = picamera.PiCamera()
  44.     camera.capture('./photo.jpg')
  45.     camera.close()
  46.     print("A Enviar a Foto Para  " + str(chat_id))
  47.     bot.sendPhoto(chat_id, photo = open('./photo.jpg', 'rb'))
  48.     camera = picamera.PiCamera()
  49.     camera.resolution = (320, 480)
  50.     camera.framerate = 30
  51.     camera.start_recording("video.h264")
  52.     sleep(10)
  53.     camera.stop_recording()
  54.     camera.close()
  55.     os.remove("2video.mp4")
  56.     command = "MP4Box -add video.h264 2video.mp4"
  57.     call([command], shell=True)
  58.     print("Video Converetido Com Sucesso")
  59.        
  60.  
  61. def sensorTrigger():
  62.     global statusText
  63.     global sendTelegramMessage
  64.     global cameraEnable
  65.     global sendPhoto
  66.     statusText = "Alguem Ativou a Campainha!"
  67.     GPIO.setwarnings(False)
  68.     GPIO.setmode(GPIO.BCM)
  69.     GPIO.setup(27, GPIO.OUT)
  70.     print ("on")
  71.     GPIO.output(27,GPIO.HIGH)
  72.     time.sleep(1)
  73.     print ("off")
  74.     GPIO.output(27,GPIO.LOW)
  75.     GPIO.setmode(GPIO.BCM)
  76.     GPIO.setwarnings(False)
  77.     GPIO.setup(13,GPIO.OUT)
  78.     print ("R1_on")
  79.     GPIO.output(13,GPIO.HIGH)
  80.     time.sleep(3)
  81.     GPIO.setmode(GPIO.BCM)
  82.     GPIO.setwarnings(False)
  83.     GPIO.setup(13,GPIO.OUT)
  84.     print ("R1_Off")
  85.     GPIO.output(13,GPIO.LOW)
  86.     sendTelegramMessage = True
  87.     if cameraEnable == True:
  88.         sendPhoto = True
  89.    
  90.  
  91. bot = telepot.Bot('1376731924:AAF4x1yu-SOIP75oQxOBwLpBXn1o0NohKkE')
  92. bot.message_loop(handle)
  93.  
  94. sensor.when_deactivated = sensorTrigger
  95.  
  96. receiveTelegramMessage = False
  97. sendTelegramMessage = False
  98. cameraEnable = False
  99. sendPhoto = False
  100.  
  101. statusText = ""
  102.  
  103. print("Tudo a Funcionar :)")
  104.  
  105.  
  106. try:
  107.     while True:
  108.        
  109.         if receiveTelegramMessage == True:
  110.             receiveTelegramMessage = False
  111.            
  112.             statusText = ""
  113.            
  114.             if telegramText == "Ativar":
  115.                 cameraEnable = True
  116.                 statusText = "A Camera Esta Ativa"
  117.                
  118.             elif telegramText == "Foto":
  119.                     statusText = "Foto"
  120.                     print("A Tirar a Foto...")
  121.                     camera = picamera.PiCamera()
  122.                     camera.capture('./photo.jpg')
  123.                     camera.close()
  124.                     print("A Enviar a Foto Para  " + str(chat_id))
  125.                     bot.sendPhoto(chat_id, photo = open('./photo.jpg', 'rb'))
  126.                  
  127.                
  128.             elif telegramText == "/start":
  129.                     statusText = "Ola..."
  130.                     bot.sendMessage(chat_id, "Olá Eu Sou o RaspBerry Quem Comanda a Tua Campainha !")
  131.                     statusText = "/help Para Ver os Comandos!"
  132.                    
  133.             elif telegramText == "Rf":
  134.                     statusText = "A Tocar..."
  135.                     GPIO.setwarnings(False)
  136.                     GPIO.setmode(GPIO.BCM)
  137.                     GPIO.setup(27, GPIO.OUT)
  138.                     print ("on")
  139.                     GPIO.output(27,GPIO.HIGH)
  140.                     time.sleep(1)
  141.                     print ("off")
  142.                     GPIO.output(27,GPIO.LOW)
  143.                    
  144.             elif telegramText == "R1_on":
  145.                     statusText = "Rele 1 ON..."
  146.                     GPIO.setmode(GPIO.BCM)
  147.                     GPIO.setwarnings(False)
  148.                     GPIO.setup(13,GPIO.OUT)
  149.                     print ("R1_on")
  150.                     GPIO.output(13,GPIO.HIGH)
  151.  
  152.             elif telegramText == "R1_off":
  153.                     statusText = "Rele 1 Off..."
  154.                     GPIO.setmode(GPIO.BCM)
  155.                     GPIO.setwarnings(False)
  156.                     GPIO.setup(13,GPIO.OUT)
  157.                     print ("R1_Off")
  158.                     GPIO.output(13,GPIO.LOW)
  159.                    
  160.             elif telegramText == "R2_on":
  161.                     statusText = "Rele 2 ON..."
  162.                     GPIO.setmode(GPIO.BCM)
  163.                     GPIO.setwarnings(False)
  164.                     GPIO.setup(19,GPIO.OUT)
  165.                     print ("R2_on")
  166.                     GPIO.output(19,GPIO.HIGH)
  167.                    
  168.             elif telegramText == "R2_off":
  169.                     statusText = "Rele 2 Off..."
  170.                     GPIO.setmode(GPIO.BCM)
  171.                     GPIO.setwarnings(False)
  172.                     GPIO.setup(19,GPIO.OUT)
  173.                     print ("R2_Off")
  174.                     GPIO.output(19,GPIO.LOW)
  175.  
  176.             elif telegramText == "R3_on":
  177.                     statusText = "Rele 3 ON..."
  178.                     GPIO.setmode(GPIO.BCM)
  179.                     GPIO.setwarnings(False)
  180.                     GPIO.setup(21,GPIO.OUT)
  181.                     print ("R3_on")
  182.                     GPIO.output(21,GPIO.HIGH)
  183.  
  184.             elif telegramText == "R3_off":
  185.                     statusText = "Rele 3 Off..."
  186.                     GPIO.setmode(GPIO.BCM)
  187.                     GPIO.setwarnings(False)
  188.                     GPIO.setup(21,GPIO.OUT)
  189.                     print ("R3_Off")
  190.                     GPIO.output(21,GPIO.LOW)
  191.  
  192.             elif telegramText == "Rt_off":
  193.                     statusText = "Todos Os Reles Off..."
  194.                     GPIO.setmode(GPIO.BCM)
  195.                     GPIO.setwarnings(False)
  196.                     GPIO.setup(13,GPIO.OUT)
  197.                     GPIO.setup(19,GPIO.OUT)
  198.                     GPIO.setup(26,GPIO.OUT)
  199.                     print ("Todos Os Reles Off")
  200.                     GPIO.output(13,GPIO.LOW)
  201.                     GPIO.output(19,GPIO.LOW)
  202.                     GPIO.output(26,GPIO.LOW)
  203.  
  204.             elif telegramText == "Rt_on":
  205.                     statusText="Todos Os Reles On..."
  206.                     GPIO.setmode(GPIO.BCM)
  207.                     GPIO.setwarnings(False)
  208.                     GPIO.setup(13,GPIO.OUT)
  209.                     GPIO.setup(19,GPIO.OUT)
  210.                     GPIO.setup(26,GPIO.OUT)
  211.                     print ("Todos Os Reles On")
  212.                     GPIO.output(13,GPIO.HIGH)
  213.                     GPIO.output(19,GPIO.HIGH)
  214.                     GPIO.output(26,GPIO.HIGH)
  215.                    
  216.             elif telegramText == "Rvideo":
  217.                     statusText = "Video"
  218.                     camera = picamera.PiCamera()
  219.                     camera.resolution = (320, 480)
  220.                     camera.framerate = 30
  221.                     camera.start_recording("video.h264")
  222.                     sleep(5)
  223.                     camera.stop_recording()
  224.                     camera.close()
  225.                     os.remove("2video.mp4")
  226.                     command = "MP4Box -add video.h264 2video.mp4"
  227.                     call([command], shell=True)
  228.                     print("Video Converetido Com Sucesso")
  229.                     bot.sendVideo(chat_id, video = open("2video.mp4", 'rb'))
  230.                     print("A Enviar o Video Para " + str(chat_id))
  231.  
  232.             elif telegramText == "Video":
  233.                     bot.sendVideo(chat_id, video = open("2video.mp4", 'rb'))
  234.                     print("A Enviar o Video Para " + str(chat_id))
  235.  
  236.             elif telegramText == "Abrir porta":
  237.                     statusText = "Porta Aberta"
  238.                     bot.sendMessage(chat_id, "Porta Aberta")
  239.                     GPIO.setmode(GPIO.BCM)
  240.                     GPIO.setwarnings(False)
  241.                     GPIO.setup(21,GPIO.OUT)
  242.                     print ("R3_on")
  243.                     GPIO.output(21,GPIO.HIGH)
  244.                     sleep(10)
  245.                     statusText = "Ja Pode Fechar A Porta"
  246.                     GPIO.setmode(GPIO.BCM)
  247.                     GPIO.setwarnings(False)
  248.                     GPIO.setup(21,GPIO.OUT)
  249.                     print ("R3_Off")
  250.                     GPIO.output(21,GPIO.LOW)
  251.  
  252.             elif telegramText == "Abrir portao":
  253.                     statusText = "portão aberto"
  254.                     GPIO.setmode(GPIO.BCM)
  255.                     GPIO.setwarnings(False)
  256.                     GPIO.setup(19,GPIO.OUT)
  257.                     print ("R2_on")
  258.                     GPIO.output(19,GPIO.HIGH)
  259.                     sleep(2)
  260.                     statusText = "Portão Aberto"
  261.                     GPIO.setmode(GPIO.BCM)
  262.                     GPIO.setwarnings(False)
  263.                     GPIO.setup(19,GPIO.OUT)
  264.                     print ("R3_Off")
  265.                     GPIO.output(19,GPIO.LOW)
  266.  
  267.             elif telegramText == "Fechar portao":
  268.                     statusText = "Portão Fechado"
  269.                     GPIO.setmode(GPIO.BCM)
  270.                     GPIO.setwarnings(False)
  271.                     GPIO.setup(19,GPIO.OUT)
  272.                     print ("R2_on")
  273.                     GPIO.output(19,GPIO.HIGH)
  274.                     sleep(2)
  275.                     statusText = "Portão Fechado"
  276.                     GPIO.setmode(GPIO.BCM)
  277.                     GPIO.setwarnings(False)
  278.                     GPIO.setup(19,GPIO.OUT)
  279.                     print ("R3_Off")
  280.                     GPIO.output(19,GPIO.LOW)
  281.                    
  282.  
  283.    
  284.             else:
  285.                 bot.sendMessage(chat_id, "Este Comando Não Conta Na Lista!")
  286.                 statusText = "/help Para Ver os Comandos"
  287.            
  288.             sendTelegramMessage = True
  289.        
  290.         if sendTelegramMessage == True:
  291.             sendTelegramMessage = False
  292.             chat_id = str(chat_id)
  293.             bot.sendMessage(chat_id, statusText)
  294.            
  295.  
  296.         if cameraEnable == True and sendPhoto == True:
  297.             cameraEnable = False
  298.             sendPhoto = False
  299.             capture()
  300.            
  301. except KeyboardInterrupt:
  302.     sys.exit(0)
  303.  
  304.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement