OVERVIEW
After building our new shed, it happened to rain pretty hard one day. Of course I forgot to close the windows and water got in.
So I decided to make a WiFi window monitor that I can access from inside the house and check if any windows are left opened when it starts to rain.
Since our shed is close enough to our house, the house WiFi is accessible from inside the shed.
We will be using the NodeMCU board and a 4 channel IR obstacle avoidance sensor.
The shed only has two windows that opens so I will be using only 2 channels, but if you want you can use the other two for doors or something.
PARTS USED
4 Channel IR Sensor Kit
NODEMCU V2
These are Amazon affiliate links...
They don't cost you anything and it helps me keep the lights on
if you buy something on Amazon. Thank you!
CONNECTIONS
The NodeMCU board already has WiFi built in and can provide 3.3V to the IR board.
We only need one pin per channel since the IR board takes care of the rest.
Pin D0 and D1 of the NodeMCU are connected to IN1 and IN2
3.3V and GND of the NodeMCU are connected to the corresponding pins of the IR Board.
THE CODE
The IR board return a value of HIGH on the IN1 and IN2 pins if nothing is positioned in front of the sensors.
So I have placed the sensors in a way that when the windows are closed they sit in front of the sensors.
We have the NodeMCU check the D0 and D1 pins to see if they are HIGH or LOW.
HIGH means the window is opened and LOW means closed.
We then create a webpage that displays each window as a button of a different color (Red for opened and Green for closed).
As always for more information about the tutorial and explanation of the code please watch our tutorial video.
/* NodeMCU Open or Closed Windows monitor project
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
#include "ESP8266WiFi.h" // WiFi Library for NodeMCU
const char* ssid = "xxxxxxxx"; // Name of your WiFi Network
const char* password = "xxxxxxxx"; // Password of WiFi Network
WiFiServer server(80); // Define Web Server Port
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// Wait until connected to WiFi
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
// Confirmation that WiFi is connected
Serial.println("");
Serial.println("WiFi connected");
// Start the Web Server
server.begin();
Serial.println("Web Server Started");
// Display IP address
Serial.print("You can connect to the Server here: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println();
Serial.println();
}
void loop() {
// Check if someone is connected to WebPage
WiFiClient client = server.available();
if (!client) {
return;
}
// Create Web Page
client.println("HTTP/1.1 200 OK"); // HTML Header
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>"); // Start of HTML
client.println("<head>");
client.println("<meta http-equiv='refresh' content='5' />"); // Refresh webpage every 5 seconds
client.println("</head>");
client.println("<style>");
client.println("body {background-color: #8baee8;}"); // Set Background Color
client.println("</style>");
client.print("SHED WINDOWS STATUS: ");
client.println("<br><br>");
if (!digitalRead(D0)) { // if window #1 is closed set color to Green
client.println("<button style='height:200px;width:150px;font-size:13pt;background-color:lightgreen'>Window #1 CLOSED </button>");
}
else { // if window #1 is open set color to Red
client.println("<button style='height:200px;width:150px;font-size:13pt;background-color:red'>Window #1 OPEN </button>");
}
if (!digitalRead(D1)) { // if window #2 is closed set color to Green
client.println("<button style='height:200px;width:150px;font-size:13pt;background-color:lightgreen'>Window #2 CLOSED </button>");
}
else { // if window #2 is open set color to Red
client.println("<button style='height:200px;width:150px;font-size:13pt;background-color:red'>Window #2 OPEN </button>");
}
client.println("</html>"); // End of HTML
delay(10);
}
TUTORIAL VIDEO
DOWNLOAD
Copy the above Sketch code you want to use above in your Arduino IDE software to program your Arduino.
To install the NodeMCU board in the Arduino IDE you will need to go to:
FILE –> PREFERENCES — >
and add the following information in “Additional Boards Manager URLs:”
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Comments