// Load Wi-Fi library #include //#include #include const char* ssid = ".........."; const char* password = "............."; ESP8266WebServer server(80); // Set web server port number to 80 // Variable to store the HTTP request String header; // Auxiliar variables to store the current output state String output1State = "off"; String output2State = "off"; // Assign output variables to GPIO pins const int output1 = D1; const int output2 = D2; #define output1_OFF digitalWrite(output1, LOW) #define output1_ON digitalWrite(output1, HIGH) #define output2_OFF digitalWrite(output2, LOW) #define output2_ON digitalWrite(output2, HIGH) //HTML code const String HtmlHtml = "" ""; const String HtmlHtmlClose = ""; const String HtmlTitle = "

ESP8266 WebServer control


\n"; const String HtmlCenter = "
"; const String HtmlCloseCenter = "
"; const String HtmlButton1_ON = "
"; const String HtmlButton1_OFF = "
"; const String HtmlButton2_ON = "
"; const String HtmlButton2_OFF = "
"; //String HtmlButtons; = "
"; String HtmlLedState1, HtmlButtons1, HtmlLedState2, HtmlButtons2; void handleRoot() { output1_OFF; HtmlLedState1 = "LED is now OFF
\n"; HtmlButtons1 = HtmlButton1_OFF; output2_OFF; HtmlLedState2 = "LED is now OFF
\n"; HtmlButtons2 = HtmlButton2_OFF; response(); } void handleLed1On() { // stateLED = LOW; output1_ON; HtmlLedState1 = "LED is now ON
\n"; HtmlButtons1 = HtmlButton1_ON; response(); } void handleLed1Off() { // stateLED = HIGH; output1_OFF; HtmlLedState1 = "LED is now OFF
\n"; HtmlButtons1 = HtmlButton1_OFF; response(); } void handleLed2On() { // stateLED = LOW; output2_ON; HtmlLedState2 = "LED is now ON
\n"; HtmlButtons2 = HtmlButton2_ON; response(); } void handleLed2Off() { // stateLED = HIGH; output2_OFF; HtmlLedState2 = "LED is now OFF
\n"; HtmlButtons2 = HtmlButton2_OFF; response(); } void response(){ String htmlRes = HtmlHtml + HtmlCenter; htmlRes += HtmlTitle; htmlRes += HtmlLedState1; htmlRes += HtmlButtons1; htmlRes += HtmlLedState2; htmlRes += HtmlButtons2; htmlRes += HtmlCloseCenter; htmlRes += HtmlHtmlClose; server.send (200, "text/html", htmlRes); } void setup() { Serial.begin(115200); // Initialize the output variables as outputs pinMode(output1, OUTPUT); pinMode(output2, OUTPUT); // Set outputs to LOW output1_OFF; output2_OFF; // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.on("/", handleRoot); server.on("/LED1On", handleLed1On); server.on("/LED1Off", handleLed1Off); server.on("/LED2On", handleLed2On); server.on("/LED2Off", handleLed2Off); server.begin(); } void loop(){ server.handleClient(); }