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.

--

--

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

64 Followers

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