I wanted to expand my Home Automation system so I can perform some quick tasks without opening my tablet or smart phone. This meant having some form of voice control, at the time I was considering this I came upon Googe Aiy Voice Kit and this sparked the idea.
NoteThis project assumes you have a setup and working AIY voice kit and Domoticz Home Automation System with already integrated home devices, you don't have to have a Domoticz system as this project will give you the idea to develop and adapt for your projects.
Requisites- Be able to control my fish tank - see my other projects....
- Control anything that I want to on my System
- Get sensor data from my Home system
My home automation system supports control over http get requests, providing it is sent in the required JSON format. for example:
http://domoticz-ip<:port>/json.htm?type=command¶m=switchlight&idx=99&switchcmd=On
This would send command On to device 99. my fishtank however has multiple switch levels as below:
So the http request changes to the below, the level value being the level or specific command I require (Lights Off):
http://domoticz-ip<:port>/json.htm?type=command¶m=switchlight&idx=99&switchcmd=Set%20Level&level=10
How to retrieve dataRetrieval just using a different request command such as below:
http://domoticz-ip<:port>/json.htm?type=devices&rid=99
In my case I was interested in getting my Google AIY to read back some data, I currently log average house temperatures which is gathered from 3 temperature sensors, this became my next task.
To request information from a device the http request is demonstrated as below:
http://domoticz-ip<:port>/json.htm?type=devices&rid=42
This requests data for device id 42 and returns the below JSON data:
{ "ActTime" : 1528138995,
"AstrTwilightEnd" : "00:00",
"AstrTwilightStart" : "00:00",
"CivTwilightEnd" : "22:16",
"CivTwilightStart" : "03:46",
"DayLength" : "16:48",
"NautTwilightEnd" : "23:41",
"NautTwilightStart" : "02:21",
"ServerTime" : "2018-06-04 20:03:15",
"SunAtSouth" : "13:05",
"Sunrise" : "04:37",
"Sunset" : "21:25",
"result" : [
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "21.4 C",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 3,
"HardwareName" : "Dummy",
"HardwareType" : "Dummy (Does nothing, use for virtual switches only)", "HardwareTypeVal" : 15,
"HaveTimeout" : false,
"ID" : "1407A",
"LastUpdate" : "2018-06-04 20:02:33",
"Name" : "Average House Temp",
"Notifications" : "false",
"PlanID" : "0",
"PlanIDs" : [ 0 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "LaCrosse TX3",
"Temp" : 21.399999999999999,
"Timers" : "false",
"Type" : "Temp",
"TypeImg" : "temperature",
"Unit" : 1,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "42"
}
],
"status" : "OK",
"title" : "Devices"
}
This returns more data than I require but I can see the info I need is the result list and the specific object Data or Temp can be used.
I tested the http request in browser first as a quick way to test my request was formatted correctly.
Add to Google AIY python programOnce you've got your AIY recognizing your voice and performing the basic tricks of turning the LED on and off and perhaps reading back its IP address, you can quickly see that the AIY recognises your voice and converts this to text, if the text it converts matches a command you have input then it can perform any task you wish. for example:
For this to work I had to add the python library 'requests' to make the HTTP get requests, then under the function 'process_event' and subsequent 'elilf' as below:
elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args:
print('You said:', event.args['text'])
text = event.args['text'].lower()
if text == 'power off':
assistant.stop_conversation()
power_off_pi()
elif text == 'fish tank lights on':
assistant.stop_conversation()
tanklights_on()
The text matching voice command "fish tank lights on" will call function the function 'tanklights_on()' which send the http request for the corresponding device.
def tanklights_on():
URL = "http://domoticz-ip<:port>/json.htm?type=command¶m=switchlight&idx=33&switchcmd=Set%20Level&level=20"
r = requests.get(url = URL)
Then, to retrieve the average House Temperature
elif text == 'what is the average house temperature':
assistant.stop_conversation()
AveHouseTemp()
This performs the below function and read out the data, AIY audios british accent!!
def AveHouseTemp():
URL = "http://domoticz-ip<:port>/json.htm?type=devices&rid=42"
r = requests.get(url = URL)
data = r.json()
data_result = data['result'][0]['Temp']
aiy.audio.say('The average house temp in celsius is %s' % data_result, lang="en-GB", volume=5, pitch=150)
Once you've understood the basic operation you make the AIY perform any task based on your pre-programmed commands, this project shows you how to use it with a Domoticz Home Automation server or to send HTTP requests, you could add other libraries to your python, MQTT for example and have send MQTT messages, the possibilities are endless.....
Comments