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(); }) } };

--

--

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