I am trying to make a door relay switch system that i am able to operate from anywhere via port forwarding.
I have found a very helpfull guide and code where i based my program on: https://openhomeautomation.net/control-a-lamp-remotely-using-the-esp8266-wifi-chip https://github.com/openhomeautomation/esp8266-relay
I made the code a little different to reset the esp8266 after five seconds of putting the relay in high state. That all worked fine but when i accessed the port via multiple devices the esp did nothing anymore, only worked again after reset.
I tried to make an if statement to reset the esp after a client connected en did not send a message after 10 seconds but it is not working. I'm probably missing something really simple and i would appreciate it if someone could help me out.
Thanks in advance!
Kind regards,
Hille
The code with if statement:
#include <ESP8266WiFi.h>
const char* ssid = "------";//type your ssid
const char* password = "-------";//type your password
unsigned long previousMillis = 0;
const long interval = 10000;
int ledPin = 2; // GPIO2 of ESP8266
WiFiServer server(80);//Service Port
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, 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");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop()
{
unsigned long currentMillis = millis();
// Check if a client has connected
WiFiClient client = server.available();
if (!client)
{
return;
}
if (currentMillis - previousMillis <= interval)
{
// 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("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
Serial.println("door opening");
Serial.println("5");
delay(1000);
Serial.println("4");
delay(1000);
Serial.println("3");
delay(1000);
Serial.println("2");
delay(1000);
Serial.println("1");
delay(1000);
ESP.restart();
}
//Set ledPin according to the request
//digitalWrite(ledPin, 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("<p style='font-size:500%'>He ouwe boef!");
client.println("<br><br>");
client.println("<p style='font-size:500%'>click <a href=\"/LED=ON\">here</a> to open door.<br>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}
else
{
Serial.println("connection too long, disconnected");
ESP.restart();
}
}
The code without
#include <ESP8266WiFi.h>
const char* ssid = "--------";//type your ssid
const char* password = "------------";//type your password
int ledPin = 2; // GPIO2 of ESP8266
WiFiServer server(80);//Service Port
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, 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");
// 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("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
Serial.println("door open now");
Serial.println("5");
delay(1000);
Serial.println("4");
delay(1000);
Serial.println("3");
delay(1000);
Serial.println("2");
delay(1000);
Serial.println("1");
delay(1000);
ESP.restart();
}
//Set ledPin according to the request
//digitalWrite(ledPin, 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("<p style='font-size:500%'>He ouwe boef!");
client.println("<br><br>");
client.println("<p style='font-size:500%'>click <a href=\"/LED=ON\">here</a> to open door.<br>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}