digitalRead
#include <ESP8266WiFi.h>
const char* ssid = "Your wifi network name";//Write your wifi network's name here
const char* password = "Your passowrd";//Write your password here
int LOCK = 4; // GPIO4
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(LOCK, OUTPUT);
digitalWrite(LOCK, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
Serial.println("");
Serial.println("************WiFi doorlock******************");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value=LOW ;
if (request.indexOf("/LOCK=ON") != -1) {
digitalWrite(LOCK, HIGH);
value = HIGH;
}
if (request.indexOf("/LOCK=OFF") != -1) {
digitalWrite(LOCK, LOW);
value = LOW;
}
// Set LOCK according to the request
//digitalWrite(LOCK, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Door is now: ");
if(value == HIGH) {
client.print("Open");
} else {
client.print("Closed");
}
client.println("<br><br>");
client.println("<a href=\"/LOCK=ON\"\"><button>Turn On </button></a>");
client.println("<a href=\"/LOCK=OFF\"\"><button>Turn Off </button></a><br />");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
You can also use BLYNK app for similar projects. Here is an example of ESP 8266 Nodemcu Ws 2812 Neopixel Based LED MOOD Lamp Controlled by Local Web Server:
https://www.pcbway.com/project/sharepro ... 45c0f.html