Home Automation using Sonoff WiFi Switch and Google Assistant

const char* mqtt_server = "iot.eclipse.org";
{ "actions": [ { "description": "Default Welcome Intent", "name": "MAIN", "fulfillment": { "conversationName": "sonoff_app" }, "intent": { "name": "actions.intent.MAIN", "trigger": { "queryPatterns": [ "talk to sonos" ] } } }, { "description": "Turn on/off intent", "name": "TURNONOFF", "fulfillment": { "conversationName": "sonoff_app" }, "intent": { "name": "com.sonoff.example.home", "parameters": [ { "name": "cmd", "type": "SchemaOrg_Text" } ], "trigger": { "queryPatterns": [ "Turn $SchemaOrg_Text:cmd kitchen light" ] } } } ], "conversations": { "sonoff_app": { "name": "sonoff_app", "url": "https://hook.io/krvarma/eclipsemqtt" } } }
module['exports'] = function myService (hook) { console.log(hook.params.data); var intent = hook.params.inputs[0].intent; if(intent === "assistant.intent.action.MAIN"){ var response = { "expect_user_response": false, "final_response": { "speech_response": { "text_to_speech": "Hello!" } } }; console.log(response); hook.res.writeHead(200, { 'Content-Type': 'application/json'}); hook.res.write(JSON.stringify(response)); hook.res.end(); } else{ var mqtt = require('mqtt') var mqttClient = mqtt.connect('mqtt://iot.eclipse.org', 1883) mqttClient.on('connect', function () { var arguments = hook.params.inputs[0].arguments; var cmdText = ''; for(var arg of arguments){ console.log(arg); console.log(arg.name + "=" + arg.raw_text); if(arg.name === "cmd"){ cmdText = arg.raw_text; } } var cmd = (cmdText === "on" ? '1' : '0'); mqttClient.publish('mqtteclipse/cmd', cmd); var response = { "expect_user_response": false, "final_response": { "speech_response": { "text_to_speech": "Done!" } } }; hook.res.writeHead(200, { 'Content-Type': 'application/json'}); hook.res.write(JSON.stringify(response)); hook.res.end(); }) } };
  1. Logon to Actions on Google Console
  2. Create a Project and give it a name
  3. Choose Use Action SDK options to define the action
  4. Fill the App Information page. Here you can change the app name, invocation name, etc…
  5. Deploy the hook.io microservice.
    a. Sign in or Sign up on hook.io b. Create a microservice using node.js as the language
  6. c. Name it as eclipseqmtt.
  7. d. Copy and paste the microservice and save the service
  8. Save and test the action using any Google Assistant platform.

--

--

--

A Software Architect from Kerala, India, Open Source, Cloud Native enthusiast. Likes Golang, Rust, C/C++, Kubernetes, Kafka, etc.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Coder Ready to Code #14

2D Galaxy Shooter: Shield Strength

The Importance of Imports in Java

AWS CloudWatch — Cross-Account Cross-Region Monitoring

Drinking tea and developing Minecraft server in golang

How to query Neo4j from Python

Neo4j Browser Window

How to Deploy Dockerized Spring Boot App In K8s MiniKube For Beginners

How to Write Gradle Scripts with Kotlin DSL — Better than Groovy?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Krishnaraj Varma

Krishnaraj Varma

A Software Architect from Kerala, India, Open Source, Cloud Native enthusiast. Likes Golang, Rust, C/C++, Kubernetes, Kafka, etc.

More from Medium

React tricks: Hooks inside Function as a Child Component

How I learned TypeScript

Build a Gatsby Blog Post Filter

Minimize extracted CSS file using Webpack. (CssMinimizerWebpackPlugin)