-
-
Save geoffmomin/13e789717b4813c044d16156f568a40b to your computer and use it in GitHub Desktop.
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 <FirebaseArduino.h> | |
#include <ESP8266WiFi.h> | |
// Set these to run example. | |
#define WIFI_SSID "YOUR_WIFI_SSID" | |
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD" | |
#define FIREBASE_DB_URL "your_firebase_url" | |
#define FIREBASE_DB_SECRET_KEY "your_database_secret_key" | |
void setup() { | |
Serial.begin(115200); | |
pinMode(D1, OUTPUT); | |
pinMode(D2, OUTPUT); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
} | |
Firebase.begin(FIREBASE_DB_URL, FIREBASE_DB_SECRET_KEY); | |
Firebase.stream("/automation"); | |
} | |
void loop() { | |
if (Firebase.failed()) { | |
Serial.println("streaming error"); | |
Serial.println(Firebase.error()); | |
} | |
if (Firebase.available()) { | |
FirebaseObject event = Firebase.readEvent(); | |
String eventType = event.getString("type"); | |
eventType.toLowerCase(); | |
Serial.print(eventType); | |
if (eventType == "put") { | |
String path = event.getString("path"); | |
String data = event.getString("data"); | |
if (path.equals("/fan/value")) { | |
if (data.equals("off")) { | |
digitalWrite(D1, HIGH); | |
} else { | |
digitalWrite(D1, LOW); | |
} | |
} else if (path.equals("/light/value")) { | |
if (data.equals("off")) { | |
digitalWrite(D2, HIGH); | |
} else { | |
digitalWrite(D2, LOW); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment