after that i did try to use Alexa installed on my RPI to command HA i decided that i needed to look in another direction.
Alexa on the RPI was nice but i ran into a lot off trouble to connect to HA.
so i decided to keep it closer to home.
lets see, i can manipulate HA with appdaemon with any python code.
is there a decent speechrecognition in python?
after some searching i found that pretty quick.
installing was quite easy also.
lets see what we can do with it.
like making a loop for my TTS i started in that direction. and i took note from what @aimc has given me
so i made a workerloop that listens to me.
and it works!!
i am now able to turn on and of 1 off my lights by voice.
i have sound feedback so i know when the commands are recognized.
next step:
creating more commands and trying to make it work for everyone. so lets take some things to the config.
and maybe i should try to find out how to work with callbacks. because that would make things a bit more easy.
after that some voice feedback!
and what will the endresult be?
our very own appdaemon Alexa.
but lets not call it Alexa!
lets call it Appi
###########################################################################################
# #
# Rene Tode ( [email protected] ) #
# ( with a lot off help from andrew cockburn (aimc) ) #
# 2017/01/18 Germany #
# #
###########################################################################################
import appdaemon.appapi as appapi
import datetime
import time
import random
import speech_recognition as sr
from queue import Queue
import threading
import tempfile
import subprocess
class hearspeech(appapi.AppDaemon):
def initialize(self):
# Create Queue
self.speechlog("workerloop; OOOOOOOOOOOOOOOOOOOOOOO Initialize start 00000000000000000000000000")
self.queue = Queue(maxsize=0)
self.startcommando = "hallo"
self.commandlisten = 0
self.startcommandseen = False
time = datetime.datetime.now() + datetime.timedelta(seconds=10)
repeat = 1
self.r = sr.Recognizer()
self.m = sr.Microphone()
self.log("A moment of silence, please...")
with self.m as source: self.r.adjust_for_ambient_noise(source)
self.speechlog("Set minimum energy threshold to {}".format(self.r.energy_threshold))
# Create worker thread
t = threading.Thread(target=self.worker)
t.daemon = True
self.speechlog("workerloop; OOOOOOOOOOOOOOOOOOOOOOO Initialize done 000000000000000000000000000")
t.start()
def worker(self):
while True:
if self.startcommandseen:
self.speechlog("workerloop; --------------------start listening for command--------------------------")
else:
self.speechlog("workerloop; ====================start listening for startcommand=====================")
spokentext = self.listen()
if spokentext != "":
self.speechlog("workerloop; text heard: " + spokentext)
self.speechlog2(spokentext)
command = self.find_known_command(spokentext)
if self.startcommando in spokentext and command == "":
self.commandlisten = 0
self.startcommandseen = True
self.speechlog("workerloop; startcommand found in: " + spokentext)
self.startcommand_beep()
elif command != "" and (self.startcommandseen or self.startcommando in spokentext):
self.speechlog("workerloop; known command found: " + command)
self.recognize_beep()
self.do_command(command)
self.startcommandseen = False
self.commandlisten = 0
self.speechlog("workerloop; ****************************command done and loop reset******************************")
elif command == "" and (self.startcommandseen or self.startcommando in spokentext):
self.speechlog("workerloop; no command found in: " + spokentext)
self.commandlisten = self.commandlisten + 1
else:
self.speechlog("workerloop; heard nothing")
if self.commandlisten > 5:
self.startcommandseen = False
self.commandlisten = 0
self.speechlog("workerloop; it took to long to hear a command")
self.end_beep()
def find_known_command(self,spokentext):
known_command_list= {"wandmeubel uit":"turn_off wandmeubel", "meubel uit":"turn_off wandmeubel", "Wandmeubel uit":"turn_off wandmeubel", "Meubel uit":"turn_off wandmeubel","wandmeubel aan":"turn_on wandmeubel", "meubel aan":"turn_on wandmeubel", "Wandmeubel aan":"turn_on wandmeubel"}
for known_command_key,known_command_value in known_command_list.items():
#self.speechlog("f_command: command: " + known_command_value + " text spoken: " + spokentext)
if known_command_key in spokentext:
return known_command_value
return ""
def do_command(self, command):
self.speechlog("commando; " + command)
if command == "turn_on wandmeubel":
self.turn_on("switch.ewandmeubel")
elif command == "turn_off wandmeubel":
self.turn_off("switch.ewandmeubel")
def listen(self):
value = ""
niksgezegd = False
self.speechlog("listen; start")
try:
with self.m as source: audio = self.r.listen(source)
self.speechlog("listen; stopped listening")
try:
self.speechlog("listen; try recognizing")
value = self.r.recognize_google(audio, language="nl-NL")
except:
self.speechlog("listen; didnt recoqnize any text")
niksgezegd = True
except:
self.speechlog("listen; didnt hear any text")
niksgezegd = True
self.speechlog("listen; i heard: " + str(value))
return value
def speechlog(self,logtext):
runtime = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f")
try:
log = open("/home/pi/.homeassistant/speech.log", 'a')
log.write(runtime + ";" + logtext + "\n")
log.close()
except:
self.log("SPEECHLOGFILE NIET BEREIKBAAR!!")
def speechlog2(self,logtext):
runtime = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f")
try:
log = open("/home/pi/.homeassistant/speech2.log", 'a')
log.write(runtime + ";" + logtext + "\n")
log.close()
except:
self.log("SPEECHLOGFILE NIET BEREIKBAAR!!")
def recognize_beep(self):
self.speechlog("beep; recognize")
self.beep("/home/pi/SPUTTER2.mp3")
def end_beep(self):
self.speechlog("beep; recognize")
self.beep("/home/pi/WARBLE.mp3")
def startcommand_beep(self):
self.speechlog("beep; recognize")
self.beep("/home/pi/POING.mp3")
def beep(self,filename):
self.speechlog("beep; " + filename)
cmd = ['mpg321',filename]
with tempfile.TemporaryFile() as f:
subprocess.call(cmd, stdout=f, stderr=f)
f.seek(0)
output = f.read()