Last active
January 13, 2017 08:26
-
-
Save arifwidip/f9879c133159463c2cff03805c4b5b43 to your computer and use it in GitHub Desktop.
WeMos with DHT22 sensor and Firebase
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 "DHT.h" | |
#include <ESP8266WiFi.h> | |
#include <FirebaseArduino.h> | |
#include "MQ135.h" | |
#define DHTPIN D3 | |
#define DHTTYPE DHT22 | |
#define ANALOGPIN A0 | |
#define RZERO 390.69 | |
MQ135 gasSensor = MQ135(ANALOGPIN); | |
#define FIREBASE_HOST "***" | |
#define FIREBASE_AUTH "***" | |
#define WIFI_SSID "***" | |
#define WIFI_PASSWORD "***" | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(115200); | |
// 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); | |
dht.begin(); | |
} | |
void loop() { | |
delay(2000); | |
DynamicJsonBuffer jsonBuffer; | |
float humidity = dht.readHumidity(); | |
float temperature = dht.readTemperature(); | |
float ppm = gasSensor.getCorrectedPPM(temperature, humidity); | |
if(isnan(humidity) || isnan(temperature)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
float heatIndexCelcius = dht.computeHeatIndex(temperature, humidity, false); | |
// Push to Firebase | |
JsonObject& temperatureObject = jsonBuffer.createObject(); | |
JsonObject& tempTime = temperatureObject.createNestedObject("timestamp"); | |
temperatureObject["value"] = temperature; | |
tempTime[".sv"] = "timestamp"; | |
Firebase.push("temperature", temperatureObject); | |
// handle error | |
if (Firebase.failed()) { | |
Serial.print("pushing /temperature failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
JsonObject& humidityObject = jsonBuffer.createObject(); | |
JsonObject& humidTime = humidityObject.createNestedObject("timestamp"); | |
humidityObject["value"] = humidity; | |
humidTime[".sv"] = "timestamp"; | |
Firebase.push("humidity", humidityObject); | |
// handle error | |
if (Firebase.failed()) { | |
Serial.print("pushing /humidity failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
// Co2 value | |
JsonObject& ppmObject = jsonBuffer.createObject(); | |
JsonObject& ppmTime = ppmObject.createNestedObject("timestamp"); | |
ppmObject["value"] = ppm; | |
ppmTime[".sv"] = "timestamp"; | |
Firebase.push("co2", ppmObject); | |
// handle error | |
if (Firebase.failed()) { | |
Serial.print("pushing /Co2 failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
Serial.println("Push success"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment