Home Automation using Google Assistant + Dialogflow + Firebase + ESP8266/Wemos (Part 3)(Updated)

Vishal Dubey
3 min readOct 28, 2017

--

The tutorial has been updated with latest changes to Firebase Cloud Functions SDK v1.0 and Dialogflow.

In part 2, we have filled in all the required details for our application in Actions on google console. We have also trained our agent in the Dialogflow console with different types of user inputs. If you say “Switch my light off”, it now understands that the light has to be switched off. Next, we have to write a webhook which will push this on/off data on Firebase Realtime Database. Our hardware is listening for changes in data in the Realtime DB and switches on/off the light based on the state. The YouTube video series can be found on my channel. Check it out here. Also, don’t forget to like, share and subscribe. :P

1. Writing webhook using Firebase CLI:

You are going to write a Firebase cloud function HTTP trigger. Data is POSTed from Dialogflow to your HTTP endpoint of this cloud function where you can make use of the information available in the request. We will be using Firebase CLI to initiate our cloud function and deploy it.

  1. We start with installing firebase tools using NPM. Cloud functions are simple JS functions running independently in Node.js environment.
  2. Authenticate firebase tools by signing in with the same email used in actions on google app setup.
  3. Setup initial project structure.
npm install -g firebase-toolsfirebase loginmkdir myproject
cd myproject
firebase init functions

After these commands are run successfully, your project directory should look like this:

myproject
+- .firebaserc # Hidden file that helps you quickly switch between
| # projects with `firebase use`
|
+- firebase.json # Describes properties for your project
|
+- functions/ # Directory containing all your functions code
|
+- package.json # npm package file describing your Cloud Functions code
|
+- index.js # main source file for your Cloud Functions code
|
+- node_modules/ # directory where your dependencies (declared in
# package.json) are installed

4. Install actions on google nodejs client library.

cd myproject/functions
npm install --save actions-on-google firebase-admin

5. Your code for the webhook goes in index.js. Open index.js and copy paste the following code:

6. We have our code for the webhook ready. Deploy your cloud function.

firebase deploy

7. Once the deployment is complete, a link is generated which acts as our webhook HTTP endpoint.

✔  Deploy complete!Project Console: https://console.firebase.google.com/project/myproject-abb1d/overview
Function URL (receiveAssistantRequests): https://us-central1-myproject-abb1d.cloudfunctions.net/receiveAssistantRequests

8. Copy the Function URL. Open Dialogflow console and open Fulfilment section from the menu on the left side. Enable Webhook and in the URL section, replace your copied URL.

9. Click SAVE at the bottom of the screen.

We have successfully deployed a webhook for our logic fulfilment of lights switching on/off. The last step is to setup our hardware which will be covered in the next section.

--

--

Vishal Dubey

| Google Certified Developer |Intel Software Innovator | Full Stack Dev | React Native | IoT Enthusiast | Mentor | https://github.com/vishal-android-freak