Introduction: Get a Screen for Your Google Home Using Raspberry Pi and Dialogflow (Chromecast Alternative)

About: I am a student, tech lover, programmer, web developer and a person who is gathering a lot from 'LIFE'

Ever since I bought my Google home I have been wanting to control my devices at home using voice commands. It works awesome in every way, but I felt bad for its video feature. We can watch Youtube and Netflix only if we have a Chromecast device or TV with inbuilt Chromecast feature. I have a Raspberry Pi with me, so I thought of making use of it to play my favorite videos and movies with voice commands.

Step 1:

For this, I made the following setup

Google Home -> Dialogflow -> Rpi IP address on Dataplicity -> Nodejs Server -> Code to open URLs

Dialogflow is a google’s human-computer interaction technology based on natural-language conversations. With the help of this, we can develop our own conversations or tasks to give as an input to Google home. This works as follows

Intent [Input, question, conversation, command….] -> Google Home -> action [reply]

Here, the reply can either be a simple conversation or else data retrieved from a webhook, which we are going to make using Rpi server. So, go to Dialogflow website and create an account and a new agent.

Then create a new intent….

Step 2:

Then enter the training phrases. These are the commands that users speak when they try to access a specific task from Google Home.

Step 3:

Here, The word ‘website’ can be changed to anything like youtube, Netflix, amazon prime etc… So, this word acts as a variable and when you select the word website [using mouse] you get the following

Step 4:

From, the drop-down menu, select ‘@sys.any’ and press enter.

Step 5:

Then enter the same parameter name in the actions section. The values mentioned in this section are sent to the webhook as a JSON file, which we need to retrieve in the server to know which website has the user invoked.

The value of a variable is retrieved using ‘$variable_name’

Step 6:

Add the response which we need to hear after saying the command to Google Home. Then save the intent. If needed set this intent as the end of the conversation.

Step 7:

Now we will set up our back-end service to get those queries to open videos and websites on our raspberry pi.

The remaining setup

Dataplicity -> Nodejs server -> code

Why we need dataplicity? because Google Home connects to webhooks which are on the accessible over the internet. Even though our GHome is connected to the local network, the queries are coming from the Google cloud service and therefore, there is a need for porting our server on to the internet. Instead of that headache, we can use dataplicity services to place our raspberry pi on the internet with ease.

First, connect to the raspberry pi [directly with HDMI or even with SSh] Get the latest nodejs run-time from the following command in the terminal.

curl -sL  https://deb.nodesource.com/setup_8.x  | sudo -E bash -

Then install it using

sudo apt-get install -y nodejs

Then create a new file

nano webserver.js

Step 8:

Then, insert the below code into the file

Here, for demonstration, I am using only two websites (google, youtube). One can edit the code and change accordingly.

bodyParser = require(‘body-parser’);
var exec = require(‘child_process’).exec; var express = require(‘express’); var app = express(); app.use(bodyParser.json()); app.post(‘/’,function(req,res){ let variable =req.body.queryResult.parameters.website; exec(“midori www.”+variable+”.com”, function(error, stdout, stderr) { console.log(“stdout: “ + stdout); console.log(“stderr: “ + stderr); if (error !== null) { console.log(“exec errror: “ + error); } }); return res.end(); }); app.listen(80);

Step 9:

The above code was written only to explain the concept. I used Midori browser in the code as chromium does not work with remote commands. We can use Firefox as well. [ We have to install body-parser, express modules with help of npm before writing the code]

Remember that we should only listen at port 80 as dataplicity can forward only port 80 of the raspberry pi.

Now we need to setup Dataplicity

Go to the Dataplicity website and create your account and follow the instructions to add the raspberry pi to the dashboard.

Step 10:

After that, open raspberry pi from the devices list and select wormhole to get a unique IP Address for the raspberry pi on which we can deploy our node server.

Copy the IP Address to clipboard.

Step 11:

Now run the code from the raspberry pi terminal

sudo node webserver.js

If it shows any error about display or about protocols…. execute

sudo xhost  +

Now go back to dialogflow, and click on fulfillment section

Step 12:

Enter the IP Address or the URL from dataplicity in the above space.

Step 13:

Finally, go to the intents section and enable the webhook call from the fulfillment tab.

That’s it! Now go back to your google home and check how it works!

Until next time, Happy Hacking :)