Advertisement
luis_gustta

gpio_door_new

Jan 20th, 2021
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. '''
  2. # @ Author: Luis
  3. # @ Create Time: 2021-01-20 00:56:39
  4. # @ Modified time: 2021-01-20 18:23:31
  5. # @ Description: Python GPIO - Alexa
  6. '''
  7.  
  8. import logging
  9. import os
  10. import time
  11. import pickle
  12.  
  13. from flask import Flask
  14. from flask_ask import Ask, request, session, question, statement
  15. import RPi.GPIO as GPIO
  16.  
  17. app = Flask(__name__)
  18. ask = Ask(app, "/")
  19. logging.getLogger('flask_ask').setLevel(logging.DEBUG)
  20.  
  21. STATUSON = ["open", "high", "on"] # all values that are defined as synonyms in type
  22. STATUSBTW = ["stop"]
  23. STATUSOFF = ["close", "low", "off"]
  24.  
  25. @ask.launch
  26. def launch():
  27.     speech_text = 'Hello'
  28.     return question(speech_text).reprompt(speech_text).simple_card(speech_text)
  29.  
  30. @ask.intent('LightIntent', mapping = {'status':'status'})
  31. def Gpio_Intent(status,room):
  32.     GPIO.setwarnings(False)
  33.     GPIO.setmode(GPIO.BCM)
  34.     GPIO.setup(17,GPIO.OUT)
  35.     if status in STATUSON:
  36.         t1 = time.time()
  37.         with open('time.pickle', 'wb') as f:
  38.             pickle.dump(t1, f)      
  39.         GPIO.output(17,GPIO.HIGH)
  40.         time.sleep(0.5)
  41.         GPIO.output(17,GPIO.LOW)
  42.         return statement('Ok')
  43.     elif status in STATUSOFF:
  44.         with open('time.pickle', 'rb') as f:
  45.             t1 = pickle.load(f)        
  46.         if time.time() - t1 >= 15:
  47.             GPIO.output(17,GPIO.HIGH)
  48.             time.sleep(0.5)
  49.             GPIO.output(17,GPIO.LOW)
  50.             return statement('Ok')
  51.             open("time.pickle", "w").close()
  52.         elif time.time() - t1 <= 15:
  53.             GPIO.output(17,GPIO.HIGH)
  54.             time.sleep(0.5)
  55.             GPIO.output(17,GPIO.LOW)
  56.             time.sleep(2)
  57.             GPIO.output(17,GPIO.HIGH)
  58.             time.sleep(0.5)
  59.             GPIO.output(17,GPIO.LOW)
  60.             return statement('Ok')
  61.         else:
  62.             GPIO.output(17,GPIO.HIGH)
  63.             time.sleep(0.5)
  64.             GPIO.output(17,GPIO.LOW)
  65.             return statement('Ok')
  66.     else:
  67.         return statement('Sorry, this command is not possible.')
  68.  
  69. @ask.intent('AMAZON.HelpIntent')
  70. def help():
  71.     speech_text = 'Você pode dizer olá para mim!'
  72.     return question(speech_text).reprompt(speech_text).simple_card('HelloWorld', speech_text)
  73.  
  74.  
  75. @ask.session_ended
  76. def session_ended():
  77.     return "{}", 200
  78.  
  79.  
  80. if __name__ == '__main__':
  81.     if 'ASK_VERIFY_REQUESTS' in os.environ:
  82.         verify = str(os.environ.get('ASK_VERIFY_REQUESTS', '')).lower()
  83.         if verify == 'false':
  84.             app.config['ASK_VERIFY_REQUESTS'] = False
  85.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement