Hello,
I am working on a project which controls a relay module, the system has 3 modes, either the channel is off/on/in sensor mode. All of the commands are sent using HTTP requests made from a custom built Android app to an MDNS server hosted by the ESP8266 board, after the board recieves the request the appropriate callback function is fired.
What I am planning to do is the following:
If motion is detected, then channel one should be enabled, it should stay enabled for set amount of time, say 5 seconds, but before those 5 seconds are over it should query the sensor again and check for movement, if there's movement the channel should stay on.
The reason for doing this is to get rid of the awkward waving and moving around that happens in apartment lights after their timer turns them off and we move just to make them on again.
Here's my full code, please take a look and point me in the right direction.
// Including Required Libraries
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
// Instantiating An ESP8266 WebServer Object
ESP8266WebServer myServer(80);
// Instantiating An MDNSResponder Object
MDNSResponder mDNS;
// Storing The WiFi SSID And Password In An Easy To Access Variables
char *wifiSSID = "x";
char *wifiPASS = "x";
// Declaring Two Variables That Store The Status Of The SENSORs
bool sensorONEStatus = false;
bool sensorTWOStatus = false;
// The Interval We Will Use To Check For Motion
const unsigned long checkInterval = 2000; // Check Every 2 Seconds
unsigned long previousTime = 0; // Will Be Used With The millis() Function
// The 'setup()' Function -> Sets And Initializes Our Script
void setup()
{
// Setting Up The PINs Of The NodeMCU
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D5, INPUT);
// Making Sure Both Relay Channels Are Off When The System Starts
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
// Starting The Serial Monitor On Port 9600
Serial.begin(9600);
// Connecting To The Predefined WiFi Network
WiFi.begin(wifiSSID, wifiPASS);
// As Long As WiFi Connection Is Not Successful
while (WiFi.status() != WL_CONNECTED)
{
// Print A Dot To The Serial Console Every Couple Of Seconds (Mimicking A Load Bar)
Serial.print(".");
// Wait 500 Milliseconds Before Printing The Next Dot
delay(500);
}
// Printing A Blank Line
Serial.println();
// Printing The IP Address Of The ESP8266 After Successful Connection
Serial.print("IP Address Of ESP8266: ");
Serial.print(WiFi.localIP());
// Printing A Blank Line
Serial.println();
// Starting The mDNS Server And Printing A Success Message Once Started
if (mDNS.begin("esp8266", WiFi.localIP()))
{
// Print A Message To The Console
Serial.println("mDNS Responder Has Started Successfully");
}
// Setting Up The Server Paths
myServer.on("/", []() {
myServer.send(200, "text/plain", "Hello, World!");
});
myServer.on("/channelOneSENSOR", []() {
sensorONEStatus = true;
myServer.send(200, "text/plain", "CHANNEL 1: SENSOR");
});
myServer.on("/channelTwoSENSOR", []() {
sensorTWOStatus = true;
myServer.send(200, "text/plain", "CHANNEL 2: SENSOR");
});
myServer.on("/channelOneOFF", []() {
sensorONEStatus = false;
digitalWrite(D1, HIGH);
myServer.send(200, "text/plain", "CHANNEL 1: OFF");
});
myServer.on("/channelTwoOFF", []() {
sensorTWOStatus = false;
digitalWrite(D2, HIGH);
myServer.send(200, "text/plain", "CHANNEL 2: OFF");
});
myServer.on("/channelOneON", []() {
digitalWrite(D1, LOW);
myServer.send(200, "text/plain", "CHANNEL 1: ON");
});
myServer.on("/channelTwoON", []() {
digitalWrite(D2, LOW);
myServer.send(200, "text/plain", "CHANNEL 2: ON");
});
// Starting The Server
myServer.begin();
// Broadcasting The Type Of Service The ESP8266 Provides Through mDNS
mDNS.addService("http", "tcp", 80); // Endpoint Name: _http._tcp
}
// The 'loop()' Function -> Runs Consecutively. Allowing Our Script To Change And Respond
void loop()
{
// Handling Requests The Server Recieves
myServer.handleClient();
// Allowing MDNS Processing
mDNS.update();
// If The 'sensorONEStatus' Is True Then Read Input From The Sensor Itself
if (sensorONEStatus)
{
Serial.println("SENSOR MODE = ON");
digitalWrite(D1, HIGH);
unsigned long currentTime = millis();
if (checkMotion()) {
digitalWrite(D1, LOW);
if ( currentTime - previousTime >= checkInterval ) {
if (checkMotion() == false) {
digitalWrite(D1, HIGH);
}
}
}
}
}
bool checkMotion() {
if ( digitalRead(D5) == HIGH) return true;
return false;
}