2 Channels Web Server Switch Sketch
2 Channels Web Server Switch Sketch
#include <ESP8266WiFi.h>
WiFiServer server(80);
String header;
// Current time
// Previous time
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output5, OUTPUT);
pinMode(output4, OUTPUT);
digitalWrite(output5, LOW);
digitalWrite(output4, LOW);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
delay(500);
Serial.print(".");
Serial.println("");
Serial.println("WiFi connected.");
Serial.println(WiFi.localIP());
server.begin();
void loop(){
String currentLine = ""; // make a String to hold incoming data from the client
currentTime = millis();
previousTime = currentTime;
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the
client's connected
currentTime = millis();
header += c;
// if the current line is blank, you got two newline characters in a row.
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
Serial.println("GPIO 5 on");
output5State = "on";
digitalWrite(output5, HIGH);
Serial.println("GPIO 5 off");
output5State = "off";
digitalWrite(output5, LOW);
Serial.println("GPIO 4 on");
output4State = "on";
digitalWrite(output4, HIGH);
} else if (header.indexOf("GET /4/off") >= 0) {
Serial.println("GPIO 4 off");
output4State = "off";
digitalWrite(output4, LOW);
client.println("<!DOCTYPE html><html>");
// Feel free to change the background-color and font-size attributes to fit your preferences
if (output5State=="off") {
} else {
}
// Display current state, and ON/OFF buttons for GPIO 4
if (output4State=="off") {
} else {
client.println("</body></html>");
client.println();
break;
currentLine = "";
} else if (c != '\r') { // if you got anything else but a carriage return character,
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}