Door and Temperature Status Logger Project

Intro: Door and Temperature Status Logger Project

This Instructable will show you how to make a simple door and temperature status logger for under $10.00 using an ESP8266 NodeMCU, a DHT11 temperature and humidity sensor, a door/window reed switch, a 10K ohm resistor and some hookup wire.

The genesis of this project came from my desire to do more home automation with the Arduino board, Since I had been reading a lot about the Arduino compatible EPS8266 NodeMCU, I decided this board would be the perfect low cost board to do some experimenting with. After searching the Internet for home automation projects using the ESP8266 boards, I settled on combining a temperature and door status logger for my first attempt. Eventually this project will be combined with servos, moisture sensors and other electronics to automate a small green house my grandfather designed and built 50 years ago. The temperature sensor will be used to determine if the heating system should be engaged or disengaged as well as signal the servos to open and close the venting system when needed. The state of the venting system will be monitored by the use of the magnetic reed switches. Finally, the moisture sensors will be used to automate a watering system.

Step 1: Disclaimer

Just a quick disclaimer to state that we take NO responsibility for anything that happens as a result of following this instructable. It's always best to follow the manufacturers instructions and safety sheets when building anything so please consult those documents for any of the parts and tools you use to build your own. We are simply just providing information on the steps we used to create ours. We are not professionals. As a matter of fact, 2 out of 3 of the individuals who participated in this build are children.

Step 2: Setup Free IFTTT Account

If you don't already have one, now is the time to setup a free IFTTT account by going to their home page.. IFTTT stands for If This Then That and is a free platform that allows you connect internet based services in new ways to enable you to leverage those services in new ways. For this project we are going to use IFTTT to allow a ESP8266 to log the status of a door via a reed switch and temperature and humidity via the DHT11 sensor in a Google Sheets document.

Step 3: Create an IFTTT Applet

While still in IFTTT, proceed to the “My Applets” section and create a new applet by clicking the “New Applet” button.

Step 4: Configure the "this" Portion of Your Applet.

Click on the “this” word that is in a blue color – as highlighted in the figure above.

Step 5: Add the WebHooks Service to Your Applet.

In the search bar, search for the “Webhooks” service and select the Webhooks icon.

Once you find the "Webhooks" service, click on it.

Step 6: Setup the Receive a Web Request Trigger.

Choose the “Receive a web request” trigger.

Step 7: Provide an Event Name

In the text box provide your new applet with an event name. I selected "Data Logger" but you can choose whatever you like.

Step 8: Configure the "that" Portion of Your Applet.

Click on the “that” word that is in a blue color – as highlighted in the figure above.

Step 9: Setup an Action Service

In the search box, search for the “Google Sheets” service, and click the Google Sheets icon.

Step 10: Connect to Google Sheets

If you haven't already done so, you will need t connect your IFTTT account to Google Sheets. Press the Connect button shown above and follow the on-screen instructions.

Step 11: Choose an Action

Click on "Add Row to Spreadsheet".

Step 12: Setup the Action

Provide a name in the "Spreadsheet name" text box. I choose to use "Data_Logger" for consistency. Leave the rest of the setting alone (you can experiment with those setting at some other time) and then press the "Create Action" button at the bottom of the screen.

Step 13: Review and Finalize Your Applet.

Once satisfied with your applet configuration press the "Finish" button.

Step 14: Retrieve Configuration Information Needed Later.

Click on "Webhooks" as highlighted above.

Step 15: Proceed to the Webhooks Documentation for the API Key

It may seem strange but click on the Documentation link in the upper right to proceed to the page with your unique API Key.

Step 16: Save the API Key

The first line of the Documentation screen displays your unique API Key. Copy and Save this key for use later.

It is also a good idea to test the applet here. Remember to change the {event} to Data_Logger or whatever you named your event and add some data to the 3 empty values then click the "Test It" button at the bottom of the page. You should see a green message saying “Event has been triggered”. If so, proceed to Google Docs and confirm that the data you entered in the test page showed up in the Google Sheets document.

Step 17: Gather the Components

Step 18: Assemble the Components

1) Connect one of the 3v3 pin on the ESP8266 to the vcc pin on the DHT11.

2) Connect one of the ground pins on the ESP8266 to the ground pin on the DHT11.

3) Connect pin D4 (a.k.a. pin 2 in the IDE) on the ESP8266 to the data pin on the DHT11.

4) Connect another 3v3 pin on the ESP8266 to the one side of the door/window reed switch.

5) Connect pin D5 (a.k.a. pin 14 in the IDE) on the ESP8266 to the other side of the door/window reed switch and also connect it to one side of the 10k ohm resistor.

6) Connect the other side of the 10k ohm resistor to another ground pin on the ESP8266.

For ESP8266 pin selections please refer to this helpful diagram or the very helpful video.

Step 19: Write the Arduino Code

Copy and paste the code below into your Arduino IDE.

<p>#include <ESP8266WiFi.h><br>#include <ESP8266HTTPClient.h>
#include "DHT.h"</p><p>#define DHTPIN 2     // what digital pin we're connected to
#define DOORPIN 14    // what digital pin the door switch is on. </p><p>#define DHTTYPE DHT11   // DHT 11</p><p>DHT dht(DHTPIN, DHTTYPE);</p><p>int count = 1;
   
const char* ssid =      "some_ssid";   // change this to use your ssid  
const char* password =  "some_password";  // change this to use your password  
int sleepTime = 100;  </p><p>// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";</p><p>// IFTTT URL resource
const char* resource = "/trigger/SOME_SERVICE_NAME/with/key/SOME_API_KEY";  //Make sure to use your service name and your api key.</p><p>String doorStatus = "Closed";
volatile bool stateChanged = false;</p><p>// If sleeping for hours then set interval by hr * 60 minutes * 60 seconds * 1000 milliseconds
const long interval = 1.0 * 60 * 60 * 1000;  // 1 hour
unsigned long previousMillis = 0 - (2 * interval); </p><p>void setup () {
  Serial.begin(115200);
  attachInterrupt(digitalPinToInterrupt(DOORPIN), eventTriggered, CHANGE);
  pinMode(DOORPIN, INPUT);  //Door Sensor 
  dht.begin();
  WiFi.begin(ssid, password);</p><p>  Serial.print("\nConnecting..");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.print("\n"); 
}</p><p>void eventTriggered(){
  stateChanged = true;
  Serial.println("Checking the door!");
  if (digitalRead(DOORPIN)==HIGH) // Check to see if door is open
  {
    Serial.println("Door is closed!");
    doorStatus = "Closed";
  } else {
    Serial.println("Door is open!");
    doorStatus = "Opened";
  }
}</p><p>void checkStatus(){
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status      
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      // Read temperature as Celsius (the default)
      float t = dht.readTemperature();
      // Read temperature as Fahrenheit (isFahrenheit = true)
      float f = dht.readTemperature(true);
    
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println("Failed to read from DHT sensor!"); //Serial.print(".");  //Failed to read from DHT sensor!
        return;
      }    
  
      // Compute heat index in Fahrenheit (the default)
      float hif = dht.computeHeatIndex(f, h);
      // Compute heat index in Celsius (isFahreheit = false)
      float hic = dht.computeHeatIndex(t, h, false);</p><p>      Serial.print("\n");
      Serial.print("Temperature: ");
      Serial.print(f);
      Serial.print(" *F (");
      Serial.print(t);
      Serial.print(" *C)");
      Serial.print("\t");
      Serial.print("Heat index: ");
      Serial.print(hif);
      Serial.print(" *F (");
      Serial.print(hic);
      Serial.print(" *C)%");
      Serial.print("\t");
      Serial.print("Humidity: ");
      Serial.println(h);</p><p>      if (digitalRead(DOORPIN)==HIGH) // Check to see if door is open
      {
        Serial.println("Door is closed!");
        doorStatus = "Closed";
      } else {
        Serial.println("Door is open!");
        doorStatus = "Opened";
      }
    
      String jsonObject = String("{\"value1\":\"")  + f +"*F (" + t + "*C) / "+ hif +"*F (" + hic + "*C)" + "\",\"value2\":\"" + h
                      + "\",\"value3\":\"" + doorStatus + " \"}";
 
      HTTPClient http;
      String completeUrl = "https://maker.ifttt.com/trigger/bme280_readings/with/key/cZFasEvy5_3JlrUSVAxQK9";
      http.begin(completeUrl); //    http.begin(server);
      http.addHeader("Content-Type", "application/json");
      http.POST(jsonObject);
      http.writeToStream(&Serial);
      http.end();   //Close connection</p><p>      stateChanged = false;
      int sleepTimeInMinutes = interval / 1000 / 60;
      Serial.print("\n\nGo to sleep for ");      
      Serial.print(sleepTimeInMinutes);
      Serial.println(" minute(s) ...");
    }    
}</p><p>void loop() {
    unsigned long currentMillis = millis();
    delay(4000);
    //If we surpassed the elapsed time then force a check of the door and temp.
    if(currentMillis - previousMillis >= interval){
      stateChanged = true;
      previousMillis = currentMillis;  
      Serial.print(count++);
      Serial.println(") Checking because of elapsed time!");  
    }else if(stateChanged){
      Serial.print(count++);
      Serial.println(") Checking because of state change!");      
    }</p><p>    //If state changed then check the door and temp.
    if(stateChanged){        
      checkStatus();
    }</p><p>    delay(sleepTime);      
}</p>

Step 20: Results

Once you upload the source code in the previous step you should have results like the example shown above.

Step 21: Credits

I found a lot of helpful hints and tips from Random Nerd Tutorials and would like to thank them for all of their help. Especially their excellent tutorial on ESP32 ESP8266 Publish Sensor Readings to Google Sheets which major portions of this Instructable are based upon.

Additionally, the DHT11 Instructable from TheCircuit helped me understand how to use this very inexpensive but interesting little sensor.

Furthermore, there are many tutorials dealing with monitoring your doors like the Garage Door Monitor and another one from Random Nerd Tutorials. I used bits and pieces of these to help me understand how to get my reed switch working properly.

Finally, with this information as well as other details I found around the Internet I was able to create a system that met my needs. I hope you find this Instructable useful and build one of your own.

Share

    Recommendations

    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest
    • Audio Contest 2018

      Audio Contest 2018

    Discussions