I have created (well modified) my first sketch using the esp8266 as a webserver. I plan on making this drive a DC motor with limit switches and using the webserver component to tell the motor to go clockwise or counter clockwise.
Problem is the code below seems to work fine when my phone accesses the webserver and I click on the buttons. It also works from microsoft edge on my laptop. However fro some reason after clicking the buttons a few times the webserver seems to disappear and I need to reboot the esp8266. It only seems to happen when I access it from chrome on my laptop (its fine when accessed from chrome on my phone or edge on my laptop.
Any ideas?
#include <ESP8266WiFi.h>
const char* ssid = "PeLa";
const char* password = "molly197";
const char* host = "192.168.43.230"; //it will tell you the IP once it starts up
//just write it here afterwards and upload
int CcwPin = D1;
int CwPin = D2;
WiFiServer server(301); //just pick any port number you like
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(WiFi.localIP());
// prepare GPIO2
pinMode(CcwPin, OUTPUT);
pinMode(CwPin, OUTPUT);
digitalWrite(D1, LOW);
digitalWrite(D2, 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.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
client.flush();
// Match the request
if (req.indexOf("") != -10) { //checks if you're on the main page
if (req.indexOf("/CW") != -1) { //checks if you clicked ON
digitalWrite(CcwPin, LOW);
digitalWrite(CwPin, HIGH);
Serial.println("Opening the Projector Door Blinds");
}
if (req.indexOf("/CCW") != -1) { //checks if you clicked ON
digitalWrite(CcwPin, HIGH);
digitalWrite(CwPin, LOW);
Serial.println("Closing the Projector Door Blinds");
}
}
else {
Serial.println("invalid request");
client.stop();
return;
}
// Prepare the response
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
s += "
<input type=\"button\" name=\"bl\" value=\"Open Projector Door Blinds \" onclick=\"location.href='/CCW'\">";
s += "
";
s += "
<input type=\"button\" name=\"bl\" value=\"Close Projector Door Blinds\" onclick=\"location.href='/CW'\">";
s += "</html>\n";
client.flush();
// Send the response to the client
client.print(s);
delay(1);
}