 #define DHTPIN D2
 #define FANPIN D6
 #define LIGHTPIN D5
+#define SENSORPIN D8
 #define DHTTYPE DHT11
 #define DELAY_TIME_TO_SEND  5000 // 15 min
-#define TABLE_NAME "leitura"
+#define TABLE_LEITURA "leitura"
+#define TABLE_MOTION "motion"
 #define CLIENT_EMAIL "well.oliveira.snt@gmail.com"
 #define DEVICE_PATH "/devices/mkW3jbNvyKMzC8FLODaQd7GAum02/"
 
 DHT dht(DHTPIN, DHTTYPE);
 
-StaticJsonBuffer<1000> jsonBuffer;
-JsonObject &root = jsonBuffer.createObject();
+StaticJsonBuffer<2000> jsonBuffer;
+JsonObject &leitura = jsonBuffer.createObject();
+JsonObject &motion = jsonBuffer.createObject();
 
-void inline readDataToSend();
-void inline setLeitura(String temperatura, String umidade, String cliente, String data);
+int count = 15000;
+bool motionSensorRead = false;
 
 void setup() {
     Serial.begin(9600);

    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(FANPIN, OUTPUT);
    pinMode(LIGHTPIN, OUTPUT);
    digitalWrite(LED_BUILTIN, !Firebase.getBool(String(DEVICE_PATH) + "light"));
}

String errorToPrint = String(TABLE_NAME) + " failed to send:";

int count = 15000;

void loop() {

    Serial.print(count);

    count++;
    if(count > 1500) {
        WiFi.hostByName(ntpServerName, timeServer);
        setSyncProvider(getNtpTime);
        setSyncInterval(300);
        readDataToSend();
        delay(100);
        count = 0;  
        Firebase.push(TABLE_NAME, root);
    } else {
        delay(100);
    }

    if (Firebase.failed()) {
        Serial.print(errorToPrint);
        Serial.println(Firebase.error());
        return;
    }

    Serial.println(Firebase.getBool(String(DEVICE_PATH) + "light"));

    digitalWrite(FANPIN, !Firebase.getBool(String(DEVICE_PATH) + "fan"));
    digitalWrite(LIGHTPIN, !Firebase.getBool(String(DEVICE_PATH) + "light"));
    digitalWrite(LED_BUILTIN, !Firebase.getBool(String(DEVICE_PATH) + "light"));

}

void inline setLeitura(String temperatura, String umidade, String cliente, String data) {
    root["temperatura"] = temperatura;
    root["umidade"] = umidade;
    root["cliente"] = cliente;
    root["data"] = data;
}

void inline readDataToSend() {

    setLeitura(temperatura, umid, cliente, data);
    Serial.println("temp=" + temperatura + " umid=" + umid + " clnt=" + cliente + " dateTime=" + data);
}

void inline updateLeitura() {
    if (count > 1500) {
        readDataToSend();
        count = 0;
        Firebase.push(TABLE_LEITURA, leitura);
    }

    if (Firebase.failed()) {
        Serial.print(getErrorString(TABLE_LEITURA));
        Serial.println(Firebase.error());
        return;
    }
}

void inline updateLighAndFan() {
    digitalWrite(FANPIN, !Firebase.getBool(String(DEVICE_PATH) + "fan"));
    digitalWrite(LIGHTPIN, !Firebase.getBool(String(DEVICE_PATH) + "light"));
    digitalWrite(LED_BUILTIN, !Firebase.getBool(String(DEVICE_PATH) + "light"));
}


void loop() {

    WiFi.hostByName(ntpServerName, timeServer);
    setSyncProvider(getNtpTime);
    setSyncInterval(300);

    updateMontionSensor();

    updateLeitura();

    updateLighAndFan();

    count++;
    delay(100);
    Serial.println(count);
}
