This page is part of the series ThingSpeak battery driven temperature logger (ESP8266) – Intro.

So, the time has come to connect my Google home to the temperature logger.

This is what we are going to do:

home   ->    Screenshot from 2017-02-25 21-15-05.png  ->Screenshot from 2017-02-25 21-13-28.png -> Screenshot from 2017-02-25 21-14-12.png  ->   Screenshot from 2017-02-12 19-46-52.png

Google Home will communicate with api.ai that will make a call to an AWS Lambda function. This Lambda function will then call ThingSpeak to get the latest temperature reading and return it to google home who will read it out loud to the user.

In order for you to follow this guide, you first need to log in via api.ai with the same Google account that you use for your Google Home. Api.ai is a “machine” that handles the text-to-voice part of our system.

Besides that you also need a service somewhere that can transform the ThingSpeak json to a json string that api.ai understands. For this demo I will be using an AWS Lambda function written in node.js, but you can use any kind of server, even a local service if that’s preferrable.

For the sake of this demo, I added another temperature sensor at work so I can ask “What’s the temperature in my Kitchen” or “What’s the temperature at work” and google home with respond with the corresponding temperature.

So, lets get started…

AWS Lambda function

Log in to AWS and choose the Lambda service. Then choose to create a new Lambda function. Choose “Blank Function”

screenshot-from-2017-02-12-18-25-31

Skip the Configure triggers and just clik next.

Screenshot from 2017-02-12 18-25-44.png

Now fill in the details as shown below. Note that the code can be inserted from the snippet further down in the post.

Screenshot from 2017-02-12 18-27-36.png

Screenshot from 2017-02-12 18-31-58.png

You need to change the Channel ID and the key. Both should correspond to the Channel ID and key from your ThingSpeak channel.

Here’s where you can find the Channel ID and key “Read API key”:

Also note on AWS that I have chosen 128 MB of memory which should be more than enough for our simple function. You will be asked what role you want to use, here you can just choose the lambda_basic_execution role.

This is the code you need for your function if you are running with 2 temperature loggers:

var http = require('http');

exports.handler = function(event, context, callback) {
 console.log(event);
 console.log("Received event [" + event.result + "]");

var houseLocation = event.result.parameters.houselocation;

console.log("HouseLocation [" + houseLocation + "]");

if (houseLocation.toUpperCase() == "OFFICE" || houseLocation.toUpperCase() == "WORK") {
 return http.get({
 host: 'api.thingspeak.com',
 path: '/channels//feed/last.json?key='
 }, function(response) {
 // Continuously update stream with data
 var body = '';
 response.on('data', function(d) {
 body += d;
 });
 response.on('end', function() {

// Data reception is done, do whatever with it!
 var jsonResponse = JSON.parse(body);

var temp = jsonResponse.field1;
 var hum = jsonResponse.field2;

var tempText = "The temperature in the " + houseLocation + " is " + temp + " degree celsius and the humidity is " + hum;
 console.log("Temperature: " + jsonResponse.field1);
 console.log("Humidity: " + jsonResponse.field2);

callback(null,{
 speech: tempText,
 displayText: "Temperatue display text ",
 source: "GoogleHomeTemperature"
 });
 });
 });
 } else if (houseLocation.toUpperCase() == "KITCHEN") {
 return http.get({
 host: 'api.thingspeak.com',
 path: '/channels//feed/last.json?key='
 }, function(response) {
 // Continuously update stream with data
 var body = '';
 response.on('data', function(d) {
 body += d;
 });
 response.on('end', function() {

// Data reception is done, do whatever with it!
 var jsonResponse = JSON.parse(body);

var temp = jsonResponse.field1;
 var hum = jsonResponse.field2;

var tempText = "The temperature in the " + houseLocation + " is " + temp + " degree celsius and the humidity is " + hum;
 console.log("Temperature: " + jsonResponse.field1);
 console.log("Humidity: " + jsonResponse.field2);

callback(null,{
 speech: tempText,
 displayText: "Temperatue display text ",
 source: "GoogleHomeTemperature"
 });
 });
 });
 } else {
 console.log("Reading temperature in ");
 callback(null,{
 speech: "Sorry, reading the temperature in the " + houseLocation + " is not supported yet.",
 displayText: "Temperatue display text ",
 source: "GoogleHomeTemperature"
 });
 }
 };

Now click next, review your settings and create the function.

API Gateway

AWS Lambda is build so that you cannot send requests to it directly, instead you send HTTP requests to an API Gatway that then invokes your Lambda function, so we need to create a resource and method in the API Gateway.

First, choose the service API Gateway.

Now create a new API, fill in the required information and create the API.

Screenshot from 2017-02-12 18-41-33.png

Then create a new Resource.

Screenshot from 2017-02-12 18-42-50.png

And finally create the needed POST method, choose the region that you created your Lambda function in, and write the name of the Lambda function you want to invoke. Then click Save. You will be asked to accept that API gateway makes requests to your Lambda function, this should just be accepted.

Screenshot from 2017-02-12 18-46-42.png

Now the last thing is to “Deploy” your API. You wont be able to call your service until you have published it via the deploy function.

Screenshot from 2017-02-12 18-48-20.png

Fill in the blanks and choose deploy.

Screenshot from 2017-02-12 18-49-20.png

Now this part is important. Copy the “Invole url” as this is what you need in the api.ai to call your function.

Screenshot from 2017-02-12 18-50-51.png

That’s it, you now (hopefully) have a fully functional Lambda service in AWS. If you make an HTTP POST request to the url with a json document containing something similar to this you should get a text response with the last read temperature from your temperature logger:

screenshot-from-2017-02-12-18-56-52

Response:

screenshot-from-2017-02-12-18-57-38

This is the text that api.ai will read out on your Google Home. 🙂

Setting up API.AI

Now that we have a Lambda function with a url we can have the Google Home call that via api.ai.

Start out by signing in to api.ai using the same google account you used when you installed your Google Home.

Then Create a new agent using the main menu. Fill in the details like this and click Save:

Screenshot from 2017-02-25 10-31-30.png

This Agent will handle all calls to our temperature service. In order for us to connect it to the API Gateway and Lambda function, we need to add what’s called a “Fulfillment”, so click that and fill in the details:

Screenshot from 2017-02-25 20-44-51.png

Add whatever url you have from the API Gateway and click save.

Now we are ready to create our first intent that will take action when the user for example says “What is the temperature in my Kitchen”.

screenshot-from-2017-02-25-20-47-08

screenshot-from-2017-02-25-20-47-26

On top you specify the name of the intent. Then, in the “User says”, you write a number of examples of what the user can say in order to invoke your service.

As you can see some words in the “User says” section are marked as yellow. When you double click a word in the “User says”, you get the option of marking the word as an “entity” (variable) that will be sent in the json request api.ai sends to your service (in this case our lambda function). In the above example, we’ve created a parameter entity called “house-location”. This entity can be created on the “Entities” page in api.ai.

Here’s an example of how the house-location entity looks:

Screenshot from 2017-02-25 20-52-37.png

Going back to the intent page, the page contains a “response” section where you write a text that will be read out loud to your users if whatever they ask for isn’t understood.

Finally, in the “Fulfillment” section you tell api.ai to call your service by clicking the “Use Webhook”.

You should now be able to test it using the “Try it now” in the right part of the page:

Screenshot from 2017-02-25 20-58-04.png

Try writing “What is the temperature in my kitchen?” and hit enter. This should cause api.ai to call your service and have it respond.

Connecting your Google Home to API.ai

When you are satisfied that your service works, you can deploy it to google cloud from where your Google Home will be able to access it.

I will not go in to details about how to deploy it so that you can use it for a longer period of time as that requires that you issue a request to Google of what service you have made.

Instead I’ll go over how you activate it for up to 24 hours, as that’s much simpler and is fine as long as you are just testing your service.

So, click “integrations” in the menu and activate the “Actions on Google” setting. Then fill in the details:

Screenshot from 2017-02-25 21-03-57.png

Write the invocation name for the service (the name you want to call your service from your Google Home) and choose whatever TTS voice you want. Then click “Authorize”.

Now a new option”Preview” has appeared. Click that and you should see a small message that allows you to open up the “Web Simulator”:

Screenshot from 2017-02-25 21-06-20.png

From the Web simulator you can test your service in Googles environment. Once you have clicked Preview it should actually work, but you can try it out first in the web simulator just to make sure it behaves the way you want it to.

Click the start button and try out your service (note that in the below I’ve tested it with the name “iot connector” instead of “temperature service”:

Screenshot from 2017-02-25 21-08-48.png

Pretty nice… Now you should be able to do the exact same on your live Google Home…

Try saying “Hey Google, talk to temperature service”. It should respond and connect you to your new service.

And one final note. If you want, you can click the “Action Package” button. From here you can extend the preview to 24 hours so you have time to test your service properly and who knows, maybe show your friends….

Congratulations… you now have a fully functional Google Home service… Enjoy! 🙂

Advertisements