#include #include /* Set up your SSID and Password */ const char* ssid = "NodeMCU"; // SSID const char* password = "19216811"; // Password /* Set up your IP addresses */ IPAddress local_ip(192,168,1,1); IPAddress gateway(192,168,1,1); IPAddress subnet(255,255,255,0); ESP8266WebServer server(80); bool LED1status = LOW; // Initial setup void setup() { /* * It is recommended to use the lower baud rate. The higher the baud rate, * the higher the bit error rate will be, and the control action might fail. */ Serial.begin(4800); // Initialize the ESP8266 <=> MEGA2560 serial port WiFi.softAP(ssid, password); WiFi.softAPConfig(local_ip, gateway, subnet); delay(100); server.on("/", handle_OnConnect); server.on("/led1on", handle_led1on); server.on("/led1off", handle_led1off); server.onNotFound(handle_NotFound); server.begin(); //// Serial.println("HTTP server started"); } // The loop function runs over and over again forever void loop() { server.handleClient(); if (LED1status) Serial.print("1"); // Command for MEGA2560 to turn on MEGA2560's L LED else Serial.print("0"); // Command for MEGA2560 to turn off MEGA2560's L LED } void handle_OnConnect() { /* * Serial.print("LED1status: "); * if (LED1status) * Serial.println("ON"); * else * Serial.println("OFF"); */ server.send(200, "text/html", SendHTML(LED1status)); } void handle_led1on() { LED1status = HIGH; //// Serial.println("LED1status: ON"); server.send(200, "text/html", SendHTML(true)); } void handle_led1off() { LED1status = LOW; //// Serial.println("LED1status: OFF"); server.send(200, "text/html", SendHTML(false)); } void handle_NotFound() { server.send(404, "text/plain", "ERROR 404: Not Found"); } String SendHTML(uint8_t led1stat) { String ptr = ""; ptr += "\n"; ptr += "\n"; ptr += "\n"; ptr += "\n"; ptr += "\n"; ptr += "LED Control\n"; ptr += "\n"; ptr += "\n"; ptr += "\n"; //// ptr += "

ESP8266 Web Server

\n"; //// ptr += "

Using Access Point(AP) Mode

\n"; if (led1stat) ptr += "

LED1 Status: ON

\n"; else ptr += "

LED1 Status: OFF

\n"; ptr += "\n"; ptr += "\n"; return ptr; }