Advertisement
luis_gustta

gpio_door

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