Skip to content

Instantly share code, notes, and snippets.

@sergiocasero
Created January 12, 2019 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiocasero/9683ae1cbb2f71421bb352b1494119c4 to your computer and use it in GitHub Desktop.
Save sergiocasero/9683ae1cbb2f71421bb352b1494119c4 to your computer and use it in GitHub Desktop.
Switch Google Assistant
'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);
});
#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