Door Check, Temperature and Humidity using ESP8266 NodeMCU

We have two garages on our home. The one in the back has two doors – the larger door to bring in cars and such and a regular entrance door on the side. As it happens sometimes we forget to close them. That means going all the way out there to check them. I was playing around with an ESP8266 and had the thought that I could do something with that to let me know if either of the doors was open.

That’s what started this project. I found some magnetic door switches on line that would be pretty easy to interface to the ESP – they’re just switches. I hooked them up to D1 & D2 (pins 4 & 5) and set them as input – pullup. Then tied the other end to ground. It works great. The ESP can take 7v – 12v on VIN so I grabbed one of the wall plugs from an old phone system and used that as the power supply. You can see the setup in image 1 and image 2. 

You may also notice the little greenish/blue thing attached to it. That is a DHT11 temperature and humidity sensor. I picked those up from Adafruit.com for $5/each. I could have gotten more accuracy with a DHT22 but it’s a garage – do I need more than 2% accuracy on the temp? and more than 5% on the humidity? Pretty much sure I don’t. Also the range on the DHT11 is 0-50c (32F to 112f). Didn’t need better than that.

So for $5 more I would not only know if the doors were open but also what the temperature and humidity were – couldn’t pass that up.

Image 6 shows the corner of the garage with the two doors and on the shelf on the left side where mounted the ESP. The 440 Volt sign was already there – someone gave it to me years ago and I use it as decoration – but maybe this will keep people from screwing with the ESP 🙂

You could use other material to mount things on but I decided to make a 3d printed mount board. I used Openscad (https://www.openscad.org/) to design the piece and create the STL file included here. There are 4 files. I designed and printed one with the pin-out lettering on the board but when I reduced the size to make room for the components the letters didn’t print properly. They printed fine in the larger size (image 7, top). The SCAD file is included if you want to play around with it and get them to print. I ended up using a blank board. The mounting holes fit the 8 terminal board I used – if you get a different one you may have to move the holes around a bit. It’s a pretty straight forward print – just use your general printer settings, I used ABS.

Image 5 is the small entrance door and image 4 the switch on the side of the 10′ door. Image 1 and 2 is how I tested things on the bench.

Here’s how the web page will look:

Here’s a link to the actual page from my garage:
https://garagedoor.zaksoffice.com/

The page will refresh every 30 seconds. I added a countdown timer in between the temp and humidity just because I wanted to know the page was still working. If the door is open it shows Door x is OPEN in RED.

Read the code on how to use the WIFI manager. If you uncomment this line:

  wifiManager.resetSettings();

When you restart the ESP it will go into the configure mode and you can pick the wifi and set the password. Run the configure then comment the line and download it and it will continue to use that wifi point and password as long as it’s available.

At a minimum I used these two sites for reference:
  http://randomnerdtutorials.com

  https://github.com/tzapu/WiFiManager

Wiring is simple – connect one side of each switch to GND. The other side of one switch goes to D4, the other to D5. Connect the power supply (7v -12v) to GND and VIN. The DHT11 – goes to GND, the + goes to 3.3V (NOT VIN). The output goes to D3 of the ESP. That’s it.

Any questions – send them along. I have the code in ZIP format HERE. or below is the code:

Door Checker with Temperature and Humidity using ESP8266 NodeMCU


// I've used hints and tips from these sites as well as a few others
// http://randomnerdtutorials.com
// https://github.com/tzapu/WiFiManager


#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include "DHT.h"

// Set web server port number to 80
WiFiServer server(80);

// I'll be checking 2 doors in the garage - the big one and the small one
int SmallDoorStatus = 0;
int BigDoorStatus = 0;
int FlashSwitchStatus = 0;

int BlinkCount = 0; // I use this to count up and then turn led on or off, then restart the count
int BlinkCountMax = 2; // this is how fast the blink is, 1 cnt for each browser refresh
int BlinkOn = 0;

const int smalldoor = 4;
const int bigdoor = 5;
const int flashswitch = 0;

// Uncomment one of the lines below for whatever DHT sensor type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

uint8_t DHTPin = D3;

// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

float Temperature;
float Humidity;

void setup() {
  Serial.begin(115200);
  Serial.println("Starting esp8266 now.");
  pinMode(DHTPin, INPUT);
  dht.begin();


  // WiFiManager
  // Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  // Uncomment and run it once, if you want to erase all the stored information
  // wifiManager.resetSettings();

  // set custom ip for portal
  //wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

  // fetches ssid and pass from eeprom and tries to connect
  // if it does not connect it starts an access point with the specified name
  // here  "AutoConnectAP"
  // and goes into a blocking loop awaiting configuration
  wifiManager.autoConnect("DoorCheckerAutoConnect");
  // or use this for auto generated name ESP + ChipID
  //wifiManager.autoConnect();

  // if you get here you have connected to the WiFi
  Serial.println("Connected.");

  server.begin();

  // prepare LED
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 0); // start with it on
  pinMode(smalldoor, INPUT_PULLUP);
  pinMode(bigdoor, INPUT_PULLUP);
  pinMode(0, INPUT_PULLUP);
}

void loop() {
  // get temp and humidity - comes back in C, I like it in F
  Temperature = dht.readTemperature(); // Gets the values of the temperature
  Humidity = dht.readHumidity(); // Gets the values of the humidity
  Temperature = (Temperature * 1.8) + 32;

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  client.setTimeout(1000); // default is 1000


  while (client.available()) {
    // byte by byte is not very efficient
    client.read();
  }

  client.println("HTTP/1.1 200 OK"); //send new page
  client.println("Content-Type: text/html");
  client.println();

  client.println("<HTML>");
  client.println("<HEAD>");
  client.println("<TITLE>Door & Room Information</TITLE>");
  client.println("<meta http-equiv='refresh' content='30'>");
  client.println("</HEAD>");
  client.println("<BODY style='font-size:2.0em;'>");

  client.println("<div style='width:70%; text-align:center; margin:0 auto; border:1px solid black;'>");

  SmallDoorStatus = digitalRead(smalldoor);
  if (SmallDoorStatus == 1) {
    client.println("<span style='color:red;'>Small Door 1 is OPEN</span>");
  } else {
    client.println("<span style='color:green;'>Small Door 1 is CLOSED</span>");
  }
  client.println(" ");

  BigDoorStatus = digitalRead(bigdoor);
  if (BigDoorStatus == 1) {
    client.println("<span style='color:red;'>Big Door 2 is OPEN</span>");
  } else {
    client.println("<span style='color:green;'>Big Door 2 is CLOSED</span>");
  }
  client.println("<hr/>");
  client.println(Temperature, 1);
  client.println("f ");
  client.println("<span style='font-size:smaller; text-align:center;'>");
  client.println("<span id='countdown'> </span>");
  client.println("<script>");
  client.println("var i = 0;");
  client.println("var txt = '302928272625242322212019181716151413121110 9 8 7 6 5 4 3 2 1 0';");
  client.println("var speed = 1000; function typeWriter() {");
  client.println("if (i < txt.length) { document.getElementById('countdown').innerHTML = (txt.charAt(i) + txt.charAt(i+1));");
  client.println("i++; i++;");
  client.println("setTimeout(typeWriter, speed); } }");
  client.println("typeWriter();");
  client.println("</script>");
  client.println("</span>");
  client.println(" ");
  client.println(Humidity, 0);
  client.println("%");
  client.println("</div>");

  client.println("</BODY>");
  client.println("</HTML>");

  FlashSwitchStatus = digitalRead(flashswitch);
  if (FlashSwitchStatus == 0) {
    Serial.println("Trying to restart the esp.");
    Serial.println(FlashSwitchStatus);
    WiFiManager wifiManager;
    wifiManager.resetSettings();
    wifiManager.startConfigPortal("DoorCheckerAutoConnect1");
  }
  BlinkCount++;
  if (BlinkCount > BlinkCountMax) {
    BlinkCount = 0;
    BlinkOn = !BlinkOn;
    digitalWrite(LED_BUILTIN, BlinkOn);
  }
}