Skip to content

Instantly share code, notes, and snippets.

@li2hub
Last active January 5, 2022 08:05
Show Gist options
  • Save li2hub/95238c6af6ab69d10fc18683bc8cca5a to your computer and use it in GitHub Desktop.
Save li2hub/95238c6af6ab69d10fc18683bc8cca5a to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> //Library to handle Web Server commands
#include <WiFiClient.h>
int irpin = D1;
int buzzerPin = D2;
ESP8266WebServer server(80); //Declare a server object
void setup()
{
WiFi.disconnect(); //Ensure the NodeMCU is disconnected from other devices initially
delay(3000);
Serial.begin(115200);
Serial.println("Start");
WiFi.begin("Your_SSID", "Your_Password"); //Connect to your router
while ((!(WiFi.status() == WL_CONNECTED))) {
delay(300);
Serial.print("..");
}
Serial.println("Connected");
Serial.println((WiFi.localIP().toString())); //Your IP address, the URL to visit
server.begin();
pinMode(irpin, INPUT);
pinMode(buzzerPin, OUTPUT);
server.on ( "/", handleRoot ); //Event handling function attachment
server.on ( "/buzzer=0", handleIntrusion);
server.onNotFound ( handleNotFound );
server.begin(); //Begin the server
Serial.println("HTTP server started");
}
void loop()
{
if (digitalRead(irpin) == 0) {
digitalWrite(buzzerPin, 1);
}
server.handleClient(); //Event handling function
//delay(1000);
}
String getPage(String buzzerText) {
String page = "<html lang=fr-FR><head><meta http-equiv='refresh' content='5'/>";
page += "<title>ESP8266 Vault Security</title>";
page += "<style>body { background-color:#FFFFFF; font-family: Bookman; font-size: 1.5em; Color:#000000; } h1 { Color: #000000; }</style></head>";
page += "<body><h1><center>ESP8266 Vault Security";
page += "</center></h1>";
page += "<p><center>";
page += buzzerText;
page += "</center></p>";
page += "<p><center>";
page += "<a href='/'>";
page += "Refresh</a></center></p>";
page += "<center><a href='/buzzer=0'><button>Switch OFF buzzer";
page += "</button></a></center>";
return page;
}
void handleRoot() {
int buzzerState = digitalRead(buzzerPin);
/* Dynamically generate the LED toggle link, based on its current state (on or off)*/
char buzzerText[80];
if (buzzerState) {
strcpy(buzzerText, "Vault Status: Possible breach !!! ");
}
else {
strcpy(buzzerText, "Vault Status: Secure ");
}
server.send ( 200, "text/html", getPage(buzzerText) );
/*Build an HTML page to display on the web-server root address*/
}
void handleIntrusion() {
digitalWrite (buzzerPin, server.arg("buzzer").toInt());
int buzzerState = digitalRead(buzzerPin);
/* Dynamically generate the LED toggle link, based on its current state (on or off)*/
char buzzerText[80];
if (buzzerState) {
strcpy(buzzerText, "Vault Status: Possible breach !!! ");
}
else {
strcpy(buzzerText, "Vault Status: Secure ");
}
server.send ( 200, "text/html", getPage(buzzerText) );
/*Build an HTML page to display on the web-server root address*/
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) {
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.send ( 404, "text/plain", message );
}
@abidink08
Copy link

after verifying and uploading this code in arduino ide for node mcu 8266, am getting an error
Invalid library found in C:\Users\Dell\Documents\Arduino\libraries\Blink: no headers files (.h) found in C:\Users\Dell\Documents\Arduino\libraries\Blink
Invalid library found in C:\Users\Dell\Documents\Arduino\libraries\sketch_oct09a: no headers files (.h) found in C:\Users\Dell\Documents\Arduino\libraries\sketch_oct09a

please suggest which library to add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment