Reputation: 31
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("");
}
Upvotes: 3
Views: 208
Reputation: 2218
WiFiServer is generic TCP server which is not aware of HTTP protocol.
Modern browsers use HTTP/1.1 protocol with Keep-Alive option enabled by default so browser does not close connection itself.
You would need to close TCP connection manually by issuing client.close();
after the line client.println("</html>");
This way you could be able to process requests from other clients.
But I suggest to use HTTP aware server (for example ESP8266WebServer) to simplify the code.
Here is an example how I would solve the task of switching relays.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "--------------";
const char* password = "----------------";
const short int LED_PIN = 2;
bool led_is_on = false;
ESP8266WebServer server(80);
void flipLed() {
led_is_on = !led_is_on;
//this is for led , i.e. open collector
digitalWrite(LED_PIN, led_is_on ? LOW : HIGH);
//this is for relay when you need to turn relay ON with High level of output
// digitalWrite(LED_PIN, led_is_on ? HIGH : LOW);
server.sendHeader("Location", "/", true);
server.send( 302, "text/plain", "");
}
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);
}
void handleRoot() {
char response[400];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf(response, sizeof(response),
"<html><body>\
uptime: %02d:%02d:%02d<br>\
wifi signal level: %d<br>\
Light is : %s<br>\
<a href=\"/flip-led\">flip</a>\
</body></html>",
hr, min % 60, sec % 60,
WiFi.RSSI(),
led_is_on?"ON":"OFF"
);
server.send(200, "text/html", response);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
ESP.restart();
}
WiFi.hostname("led-ctrl"); //esp will use it for dhcp request and wifi router will display the name in clients list
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
server.onNotFound(handleNotFound);
server.on("/flip-led", flipLed);
server.on("/", handleRoot);
server.begin();
}
void loop() {
server.handleClient();
}
Upvotes: 1