Skip to content

Instantly share code, notes, and snippets.

@aallan
Created October 29, 2017 00:14
Show Gist options
  • Save aallan/12161c808ad52fce698f059e76043304 to your computer and use it in GitHub Desktop.
Save aallan/12161c808ad52fce698f059e76043304 to your computer and use it in GitHub Desktop.
Magic Mirror weather implementation using Google Cloud Speech API
#!/usr/bin/env python3
import threading
import requests
import time
import aiy.audio
import aiy.cloudspeech
import aiy.voicehat
threads = []
def show_weather():
#aiy.audio.say('The weather today')
r = requests.get('http://127.0.0.1:8080/remote?action=SHOW&module=module_2_currentweather')
r = requests.get('http://127.0.0.1:8080/remote?action=SHOW&module=module_3_weatherforecast')
task = threading.Thread(target=hide_weather)
threads.append(task)
task.start()
def play_tinkle():
aiy.audio.play_wave("/home/pi/tinkle.wav")
def hide_weather():
time.sleep(5)
r = requests.get('http://127.0.0.1:8080/remote?action=HIDE&module=module_2_currentweather')
r = requests.get('http://127.0.0.1:8080/remote?action=HIDE&module=module_3_weatherforecast')
def main():
r = requests.get('http://127.0.0.1:8080/remote?action=HIDE&module=module_0_clock')
r = requests.get('http://127.0.0.1:8080/remote?action=HIDE&module=module_1_MMM-Remote-Control')
r = requests.get('http://127.0.0.1:8080/remote?action=HIDE&module=module_2_currentweather')
r = requests.get('http://127.0.0.1:8080/remote?action=HIDE&module=module_3_weatherforecast')
recognizer = aiy.cloudspeech.get_recognizer()
recognizer.expect_hotword(['Magic Mirror on the wall'])
recognizer.expect_phrase('show me the weather')
recognizer.expect_phrase('who is the fairest of them all?')
aiy.audio.get_recorder().start()
while True:
print('Listening...waiting for hotword.')
text = recognizer.recognize()
print('Got hotword.')
task = threading.Thread(target=play_tinkle)
threads.append(task)
task.start()
if text is None:
print('Sorry, I did not hear you.')
else:
print('You said "', text, '"')
if 'weather' in text:
show_weather()
if 'fairest' in text:
aiy.audio.say('Over the seven jewelled hills, beyond the seventh fall, in the cottage of the seven dwarfs, dwells Snow White, fairest one of all.')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment