     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"));
+    pinMode(SENSORPIN, INPUT);
+    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 updateMontionSensor() {
+    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(1000);
+    }
+
+    if (Firebase.failed()) {
+        Serial.print(getErrorString(TABLE_MOTION));
+        Serial.println(Firebase.error());
+        return;
+    }
+}
+
 void inline setLeitura(String temperatura, String umidade, String cliente, String data) {
-    root["temperatura"] = temperatura;
-    root["umidade"] = umidade;
-    root["cliente"] = cliente;
-    root["data"] = data;
+    leitura["temperatura"] = temperatura;
+    leitura["umidade"] = umidade;
+    leitura["cliente"] = cliente;
+    leitura["data"] = data;
 }
 
 void inline readDataToSend() {

#define DHTPIN D2
#define FANPIN D6
#define LIGHTPIN D5
#define DHTTYPE DHT11
#define DELAY_TIME_TO_SEND  5000 // 15 min
#define TABLE_NAME "leitura"
#define CLIENT_EMAIL "well.oliveira.snt@gmail.com"
#define DEVICE_PATH "/devices/mkW3jbNvyKMzC8FLODaQd7GAum02/"

DHT dht(DHTPIN, DHTTYPE);

StaticJsonBuffer<1000> jsonBuffer;
JsonObject &root = jsonBuffer.createObject();

void inline readDataToSend();
void inline setLeitura(String temperatura, String umidade, String cliente, String data);

void setup() {
    Serial.begin(9600);

    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);
}
