Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- # @ Author: Luis
- # @ Create Time: 2021-01-20 00:56:39
- # @ Modified time: 2021-01-20 18:23:31
- # @ Description: Python GPIO - Alexa
- '''
- import logging
- import os
- import time
- import pickle
- from flask import Flask
- from flask_ask import Ask, request, session, question, statement
- import RPi.GPIO as GPIO
- app = Flask(__name__)
- ask = Ask(app, "/")
- logging.getLogger('flask_ask').setLevel(logging.DEBUG)
- STATUSON = ["open", "high", "on"] # all values that are defined as synonyms in type
- STATUSBTW = ["stop"]
- STATUSOFF = ["close", "low", "off"]
- @ask.launch
- def launch():
- speech_text = 'Hello'
- return question(speech_text).reprompt(speech_text).simple_card(speech_text)
- @ask.intent('LightIntent', mapping = {'status':'status'})
- def Gpio_Intent(status,room):
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(17,GPIO.OUT)
- if status in STATUSON:
- t1 = time.time()
- with open('time.pickle', 'wb') as f:
- pickle.dump(t1, f)
- GPIO.output(17,GPIO.HIGH)
- time.sleep(0.5)
- GPIO.output(17,GPIO.LOW)
- return statement('Ok')
- elif status in STATUSOFF:
- with open('time.pickle', 'rb') as f:
- t1 = pickle.load(f)
- if time.time() - t1 >= 15:
- GPIO.output(17,GPIO.HIGH)
- time.sleep(0.5)
- GPIO.output(17,GPIO.LOW)
- return statement('Ok')
- open("time.pickle", "w").close()
- elif time.time() - t1 <= 15:
- GPIO.output(17,GPIO.HIGH)
- time.sleep(0.5)
- GPIO.output(17,GPIO.LOW)
- time.sleep(2)
- GPIO.output(17,GPIO.HIGH)
- time.sleep(0.5)
- GPIO.output(17,GPIO.LOW)
- return statement('Ok')
- else:
- GPIO.output(17,GPIO.HIGH)
- time.sleep(0.5)
- GPIO.output(17,GPIO.LOW)
- return statement('Ok')
- else:
- return statement('Sorry, this command is not possible.')
- @ask.intent('AMAZON.HelpIntent')
- def help():
- speech_text = 'Você pode dizer olá para mim!'
- return question(speech_text).reprompt(speech_text).simple_card('HelloWorld', speech_text)
- @ask.session_ended
- def session_ended():
- return "{}", 200
- if __name__ == '__main__':
- if 'ASK_VERIFY_REQUESTS' in os.environ:
- verify = str(os.environ.get('ASK_VERIFY_REQUESTS', '')).lower()
- if verify == 'false':
- app.config['ASK_VERIFY_REQUESTS'] = False
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement