Switch Google Assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const functions = require('firebase-functions'); | |
const {WebhookClient} = require('dialogflow-fulfillment'); | |
const {Card, Suggestion} = require('dialogflow-fulfillment'); | |
const admin = require('firebase-admin'); | |
admin.initializeApp(); | |
const db = admin.database(); | |
const ref = db.ref("/switch"); | |
process.env.DEBUG = 'dialogflow:debug'; | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { | |
const agent = new WebhookClient({ request, response }); | |
function turnOn(agent) { | |
ref.set({value: 1}); | |
agent.add("Encendido, ¿algo más?"); | |
} | |
function turnOff(agent) { | |
ref.set({value: 0}); | |
agent.add("Apagado, ¿algo más?"); | |
} | |
let intentMap = new Map(); | |
intentMap.set('turn_on', turnOn); | |
intentMap.set('turn_off', turnOff); | |
agent.handleRequest(intentMap); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <WiFiClient.h> | |
const char* WIFI_SSID = "SSID"; | |
const char* WIFI_PASS = "PASS"; | |
const String URL = "FIREBASE_URL"; | |
const String FINGERPRINT = "YOUR_FINGERPRINT"; | |
const int SWITCH = D4; | |
const int HTTP_OK = 200; | |
const int LOOP_DELAY = 500; | |
int switchValue = 0; | |
void setup() { | |
Serial.begin(115200); | |
pinMode(SWITCH, OUTPUT); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
Serial.println(); | |
while(WiFi.status() != WL_CONNECTED) { | |
delay(LOOP_DELAY); | |
Serial.print("."); | |
} | |
Serial.println("ESP connected!"); | |
} | |
void loop() { | |
int newValue = getSwitchValue(); | |
if(newValue != switchValue) { | |
switchValue = newValue; | |
digitalWrite(SWITCH, switchValue); | |
Serial.println("New switch value: " + String(switchValue)); | |
} | |
delay(LOOP_DELAY); | |
} | |
int getSwitchValue() { | |
HTTPClient client; | |
client.begin(URL, FINGERPRINT); | |
int responseCode = client.GET(); | |
int newValue = switchValue; | |
if(responseCode == HTTP_OK) { | |
newValue = client.getString().toInt(); | |
} | |
return newValue; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment