leonzak
Published © GPL3+

ESP8266 NodeMCU Garage Door Check, Temp and Humidity

Using an ESP8266 NodeMCU to check doors and get Temp and Humidity.

BeginnerProtip2 hours1,186
ESP8266 NodeMCU Garage Door Check, Temp and Humidity

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
uArm Door Window Magnetic Switch
×1
Macchina 8 position terminal strip
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code snippet #1

Plain text
// 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);
  }
}

Code snippet #2

Plain text
// 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);
  }
}

Arduino ESP8266 NodeMCU

Arduino
Use Arduino IDE to load into 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("&nbsp;&nbsp;&nbsp;&nbsp;");

  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&nbsp;&nbsp;");
  client.println("<span style='font-size:smaller; text-align:center;'>");
  client.println("<span id='countdown'>&nbsp;</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("&nbsp;&nbsp;");
  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);
  }
}

Github

https://github.com/tzapu/WiFiManager

Credits

leonzak
1 project • 9 followers
Contact

Comments

Please log in or sign up to comment.