NodeMCU based door sensor with power sleep mode.

This IoT Door sensor project shows how to make a WiFi door alarm/Notification that provides alerts wherever you open the door, with help of the IFTTT it will logs the time and date on the google drive and send you a notification on phone at time of status change. Other time it will scan and go back to the deep sleep with interval of 10 second. (It is possible to change in the Code)
An interesting thing here, we are keep storing the last status in the EEPROM, so whenever device wake up from the power sleep mode, It will scan a switch position and compare with last EEPROM data (which was older switch position stored in the EEPROM before it goes in to the power save mode). If there is change, it will send a notification to your mobile phone and stores data and time with door status on google drive.

This feature helps us in,
1) It does not connect to the even WIFI if status is not changed which will reduce overall power consumption of the device and increase battery life.
2) Sensor scans data at every 10 second but uploaded only a status change, So no garbage data on google drive.

Final Output:



Basics you need to know,
1) NodeMCU with Arduino IDE
2) Hardware
a.    ESP8266 12E
b.    Door sensor (reed switch based)
c.    Battery
3) IFTTT with Google drive.

To start with IFTTT,
Create a free IFTTT account.
Go the official site: https://ifttt.com/ and click the “Sign Up” button in top of the page.




Once you "Sign-up", you see…



In the top raw, select the "My applets" or create one.


Click on "this" to make trigger activity.
In our case we want to use a NodeMCU with door sensor to create a trigger.


Select "Maker webhooks" It will allow you to make the IOT hardware integration.



I created an event name "Door _status" (you need in Arduino code) written in the box. Do remember this name you will need this in future for the testing.


So It will show you if Door open/closed (On NodeMCU)  then what…!!!



It has multiple action services, which will allow you to connect through possible services with simple selection. In our-case we are sending door_status on the Google spread sheet. (You need to download an app for notification service)


In this Notification dialog, please add the message you want to see. Also please add ingredient call "value1", "value2",

"Value 1" stands for door status- Open/ Closed.

It will save data on the spread sheet name of IFTTT_maker_webhooks_Event



Compete with the action by a click "create action".

Hardware design,

Deep sleep mode of chip stop un-necessary processes when it not required. Like when we mention delay command in the code it mean controller is awake and doing process to execute it but how can we do without consuming more power. 

ESP8266 chip provides 3 configurable sleep modes. We can choose and configure the sleep mode as required.
The 3 sleep modes are:
Modem-sleep
Light-sleep
Deep-sleep





You need to connect D0 pin (Ping number 16) to reset, so whenever you counter reach out deepsleep time, it will enable a reset. Meaning it will reset a chip and code will work in normal mode.


Now Connect you reed switch with Pin number D5 and other lead goes to 3.3V to provide trigger signal.
Please make sure you connect and define right pin number. Arduino does not follow NodeMCU pin numbers.



Code:
#include <ESP8266WiFi.h>
#include <EEPROM.h>

const char* ssid = "WIFI network"
const char* password = "WIFI password"
const char* host = "maker.ifttt.com";
const char* apiKey = "IFTTT Key";
const int sleepTimeS = 10; // Deep sleep time

int lastButtonState;
int address = 120; // EEPROM stored data location it can be inbetween 0-511 
byte value;

int pin = 14;
int state;
volatile int flag = false;
const char* door_state; 

void setup() {
    pinMode(pin, INPUT);
    Serial.begin(115200);
    EEPROM.begin(512);
    delay(100);
  //  Serial.println("Preparing the Door Status Monitor project...");
   
    lastButtonState = EEPROM.read(address);
   // lastButtonState = value ;
    state= digitalRead(pin);
 
    if (state != lastButtonState) {
    if (state == HIGH)
    {
      //Serial.println("if state condition on");
      flag = true;
      door_state = "Open" ;
    }
    else
    {
     // Serial.println("if state confition off");
      flag = true;
      door_state = "Closed" ;
    }
    delay(10);

  }

   EEPROM.write(address, state);
   EEPROM.commit();
 //  Serial.println("Code End");
}

void loop() {

      if(flag)
      {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(50);
      //Serial.print(".");
       }
 
    Serial.println("");
    Serial.println("WiFi connected"); 
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

       
          Serial.println("CODE UPLOADED"); 
          Serial.print("connecting to ");
          Serial.println(host);
         
          WiFiClient client;
          const int httpPort = 80;
          if (!client.connect(host, httpPort)) {
            Serial.println("connection failed");
            return;
          }
   
          String url = "/trigger/Door_status/with/key/";
          url += apiKey;
         
          Serial.print("Requesting URL: ");
          Serial.println(url);
          client.print(String("POST ") + url + " HTTP/1.1\r\n" +
                       "Host: " + host + "\r\n" +
                       "Content-Type: application/x-www-form-urlencoded\r\n" +
                       "Content-Length: 13\r\n\r\n" +
                       "value1=" + door_state + "\r\n");
          flag = false;
         
      } 
      delay(10);
      ESP.deepSleep(sleepTimeS * 1000000);
}

On Google Driver

Comments

  1. what pins i need to connect on wemos board? D3 and GND or D3 and RST? i get ets Jan 8 2013,rst cause:2, boot mode:(3,6)

    load 0x4010f000, len 1264, room 16
    tail 0
    chksum 0x42
    csum 0x42
    ~ld

    ReplyDelete
  2. yeah the problem was pinMode(pin, INPUT); replaced with pinMode(pin, OUTPUT); and the error went away .. still can't see the utility of the eprom in this case as the sensor doesn't read anyway the data while in sleep mode to save it to eprom....

    ReplyDelete
    Replies
    1. Perfect, So if you see when-ever device reboot it does not know a previous status and that is where EEPROM comes in picture. It will hard-store previous status of door and compare with current status after every reboot. so if there is any change then only it will send an update to spread sheet other wise not.

      Delete
    2. yes but still the sleep time is 10 sec ... i guess in 10 seconds while the esp sleeps somebody could open the door and close it and when esp wakes up the status is closed and the prev read from eprom is closed as well .. so we miss the event. Is there any way for example to increase the sleep time and make the door sensor to wake up the esp? that i think will be really great solution.

      Delete

Post a Comment

Popular Posts