Skip to content

Instantly share code, notes, and snippets.

@jewel3g
Created October 22, 2018 18:16
Show Gist options
  • Save jewel3g/0777b849a8b904bc5b137147140eadd0 to your computer and use it in GitHub Desktop.
Save jewel3g/0777b849a8b904bc5b137147140eadd0 to your computer and use it in GitHub Desktop.
#include <Firebase.h>
#include <FirebaseArduino.h>
#include <FirebaseCloudMessaging.h>
#include <FirebaseError.h>
#include <FirebaseHttpClient.h>
#include <FirebaseObject.h>
#include "DHT.h"
#include <ESP8266WiFi.h>
//#include <FirebaseArduino.h>
#define DHTPIN 3
#define DHTTYPE DHT22
#define FIREBASE_HOST "test-503b7.firebaseio.com/"
#define FIREBASE_AUTH "OdRVfO4CwfiJ9R2ZfvmaeeZiKZo9ZiLVJBLe1We3"
#define WIFI_SSID "Robot"
#define WIFI_PASSWORD "121121121"
DHT dht(DHTPIN, DHTTYPE);
//
//// Set these to run example.
//#define FIREBASE_HOST "nasa2018-77e29.firebaseio.com"
//#define FIREBASE_AUTH "8rwBzWLyLGo3Hs7HBNRM6KMbOvMPt5pJideg3Kjy"
//#define WIFI_SSID "Robot"
//#define WIFI_PASSWORD "121121121"
void setup() {
Serial.begin(9600);
dht.begin();
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
int n = 0;
void loop() {
// set value
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Firebase.setFloat("Humidity",h);
// handle error
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error());
return;
}
delay(1000);
// update value
Firebase.setFloat("Temperature", t);
// handle error
if (Firebase.failed()) {
Serial.print("setting /number failed:");
Serial.println(Firebase.error());
return;
}
delay(1000);
// get value
Serial.print("number: ");
Serial.println(Firebase.getFloat("Humidity"));
delay(1000);
Serial.print("number: ");
Serial.println(Firebase.getFloat("Temperature"));
delay(1000);
// remove value
Firebase.remove("number");
delay(1000);
// set string value
Firebase.setString("message", "hello world");
// handle error
if (Firebase.failed()) {
Serial.print("setting /message failed:");
Serial.println(Firebase.error());
return;
}
delay(1000);
// set bool value
Firebase.setBool("truth", false);
// handle error
if (Firebase.failed()) {
Serial.print("setting /truth failed:");
Serial.println(Firebase.error());
return;
}
delay(1000);
// append a new value to /logs
String name = Firebase.pushInt("logs", n++);
// handle error
if (Firebase.failed()) {
Serial.print("pushing /logs failed:");
Serial.println(Firebase.error());
return;
}
Serial.print("pushed: /logs/");
Serial.println(name);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment