Skip to content

Instantly share code, notes, and snippets.

@manoj153
Created June 2, 2018 12:59
Show Gist options
  • Save manoj153/d9504dd026128f5ccd919396c9f60c93 to your computer and use it in GitHub Desktop.
Save manoj153/d9504dd026128f5ccd919396c9f60c93 to your computer and use it in GitHub Desktop.
/*
changes that had to make
*/
#include "ntpUtils.h"
#include "authUtils.h"
#include <FirebaseArduino.h>
#include <DHT.h>
#define DHTPIN 4
#define FANPIN D6
#define LIGHTPIN D5
#define SENSORPIN D8
#define DHTTYPE DHT22
#define DELAY_TIME_TO_SEND 5 // 5 min
#define TABLE_LEITURA "data"
#define TABLE_MOTION "motion"
#define CLIENT_EMAIL "manojkumar"
//#define DEVICE_PATH "/devices/1/"
DHT dht(DHTPIN, DHTTYPE);
//char jsonx[200];
StaticJsonBuffer<2000> jsonBuffer;
JsonObject &data = jsonBuffer.createObject();
//JsonObject &motion = jsonBuffer.createObject();
int val = 0;
int count = 0;
String temperatura = "";
String umid = "";
bool motionSensorRead = false;
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
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);
//udp.begin(UDP_NTS_PORT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(FANPIN, OUTPUT);
pinMode(LIGHTPIN, OUTPUT);
pinMode(SENSORPIN, INPUT);
dht.begin();
//motionSensorRead = !digitalRead(SENSORPIN);
//digitalWrite(LED_BUILTIN, !Firebase.getBool(String(DEVICE_PATH) + "light"));
}
inline String getErrorString(String table) {
return String(TABLE_LEITURA) + " failed to send:";
}
void inline readMotionSensorAndSendToFirebase() {
if (digitalRead(SENSORPIN) != motionSensorRead) {
motionSensorRead = digitalRead(SENSORPIN);
//motion["state"] = String(motionSensorRead);
//motion["date"] = getEpoch();
//Firebase.push(TABLE_MOTION, motion);
Serial.println("sensor=" + String(motionSensorRead));
delay(500);
}
if (Firebase.failed()) {
Serial.print(getErrorString(TABLE_MOTION));
Serial.println(Firebase.error());
return;
}
}
void setLeitura(String temp1, String humidity ) {
if(val >=100)
{
val=0;
}
else
{
val++;
}
data["temp"] = temp1;
data["hum"] = humidity;
data["pH"] = val;
}
void readDataToSend() {
temperatura = (dht.readTemperature());
umid = (dht.readHumidity());
// char jsonx[] = umid ;
//Serial.println("D1:" + temperatura + "D2:" +umid + "C:" + count);
//Serial.println(jsonx);
//String data = getEpoch();
//String motion = String(digitalRead(SENSORPIN));
delay(500);
// Serial.println(
// "temp=" + String(temperatura) + " humm=" + String(umid) );
data.printTo(Serial);
}
void readLeituraAndSendToFirebase() {
// readDataToSend();
// setLeitura(temperatura, umid);
if (1) {
if (data["temp"] == "nan" || data["hum"] == "nan" ) {
Serial.println("fuckedUp");
count = 0;
}
else
{
count = 1;
}
if(count)
{
Firebase.push("D1", data); // parent node is D1
}
}
if (Firebase.failed()) {
Serial.print(getErrorString(TABLE_LEITURA));
Serial.println(Firebase.error());
return;
}
}
//TODO verify wheter is this okay or not
void inline updateNtp() {
WiFi.hostByName(ntpServerName, timeServer);
setSyncProvider(getNtpTime);
setSyncInterval(300);
}
void inline updateCountAndPrint() {
delay(500);
//count++;
//Serial.println("/-----Count-------");
//Serial.println(count);
//Serial.println("------Count------/");
}
void loop() {
//updateNtp();
//readMotionSensorAndSendToFirebase();
// readDataToSend();
temperatura = (dht.readTemperature());
umid = (dht.readHumidity());
data["temp"] = temperatura;
data["hum"] = umid;
data.printTo(Serial);
// data["pH"] = val;
//setLeitura(temperatura, umid);
readLeituraAndSendToFirebase();
//updateLighAndFan();
updateCountAndPrint();
Serial.println(ESP.getFreeHeap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment