-
-
Save maxpromer/4b5b4e000a4b14543dca10a60c1f9a9f 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 <ESP8266WiFi.h> | |
#include <FirebaseArduino.h> | |
#include <DHT.h> | |
#include <time.h> | |
// Config Firebase | |
#define FIREBASE_HOST "esp-data-logger.firebaseio.com" | |
#define FIREBASE_AUTH "<YOUR KEY>" | |
// Config connect WiFi | |
#define WIFI_SSID "<YOUR WIFI NAME>" | |
#define WIFI_PASSWORD "<YOUR WIFI PASSWORD>" | |
// Config DHT | |
#define DHTPIN 4 | |
#define DHTTYPE DHT22 | |
// Config time | |
int timezone = 7; | |
char ntp_server1[20] = "ntp.ku.ac.th"; | |
char ntp_server2[20] = "fw.eng.ku.ac.th"; | |
char ntp_server3[20] = "time.uni.net.th"; | |
int dst = 0; | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(9600); | |
WiFi.mode(WIFI_STA); | |
// 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()); | |
configTime(timezone * 3600, dst, ntp_server1, ntp_server2, ntp_server3); | |
Serial.println("Waiting for time"); | |
while (!time(nullptr)) { | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(); | |
Serial.println("Now: " + NowString()); | |
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); | |
dht.begin(); | |
} | |
void loop() { | |
// Read temp & Humidity for DHT22 | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
if (isnan(h) || isnan(t)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
delay(500); | |
return; | |
} | |
Serial.print("Humidity: "); | |
Serial.print(h); | |
Serial.print(" %\t"); | |
Serial.print("Temperature: "); | |
Serial.print(t); | |
Serial.print(" *C "); | |
Serial.println(); | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.createObject(); | |
root["temperature"] = t; | |
root["humidity"] = h; | |
root["time"] = NowString(); | |
// append a new value to /logDHT | |
String name = Firebase.push("logDHT", root); | |
// handle error | |
if (Firebase.failed()) { | |
Serial.print("pushing /logs failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
Serial.print("pushed: /logDHT/"); | |
Serial.println(name); | |
delay(30000); | |
} | |
String NowString() { | |
time_t now = time(nullptr); | |
struct tm* newtime = localtime(&now); | |
String tmpNow = ""; | |
tmpNow += String(newtime->tm_year + 1900); | |
tmpNow += "-"; | |
tmpNow += String(newtime->tm_mon + 1); | |
tmpNow += "-"; | |
tmpNow += String(newtime->tm_mday); | |
tmpNow += " "; | |
tmpNow += String(newtime->tm_hour); | |
tmpNow += ":"; | |
tmpNow += String(newtime->tm_min); | |
tmpNow += ":"; | |
tmpNow += String(newtime->tm_sec); | |
return tmpNow; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why have error at