// Create A Simple ESP8266 NodeMCU Web Server In Arduino IDE // Quelle: https://lastminuteengineers.com/creating-esp8266-web-server-arduino-ide/ // Schalte GPIO16 mit Button und Java im Access Point modus // AKTUELLER Stand: 13.07.2023 13h32 #include #include /* Put your SSID & Password */ const char* ssid = "xxxxxxx"; // Enter SSID here const char* password = "xxxxxxx"; //Enter Password here /* Put IP Address details */ IPAddress local_ip(192, 168, 4, 1); IPAddress gateway(192, 168, 4, 1); IPAddress subnet(255, 255, 255, 0); // declare an object of ESP8266WebServer library ESP8266WebServer server(80); // declare the NodeMCU’s GPIO pins to which LEDs are connected, // as well as their initial state uint8_t LED1pin = D0; bool LED1status = LOW; //uint8_t LED2pin = D6; uint8_t LED2pin = D1; bool LED2status = LOW; // In the setup function, we configure our HTTP server // First, we establish a serial connection for debugging purposes // and configure the GPIO pins to behave as an OUTPUT void setup() { Serial.begin(115200); pinMode(LED1pin, OUTPUT); pinMode(LED2pin, OUTPUT); // configure a soft access point to create a Wi-Fi network WiFi.softAP(ssid, password); WiFi.softAPConfig(local_ip, gateway, subnet); delay(100); // first line of the code snippet below, for example, indicates // that when a server receives an HTTP request on the // root (/) path, it will call the handle_OnConnect() function. // Important to note that the URL specified is a relative path. server.on("/", handle_OnConnect); server.on("/led1on", handle_led1on); server.on("/led1off", handle_led1off); server.on("/led2on", handle_led2on); server.on("/led2off", handle_led2off); // ..if the client requests a URL that isn’t specified with // server.on(). It should give a 404 error (Page Not Found) // as a response server.onNotFound(handle_NotFound); // to start the server call the server object’s begin() method server.begin(); Serial.println("HTTP server started"); } // Actual incoming HTTP requests are handled in the loop function void loop() { server.handleClient(); if (LED1status) { digitalWrite(LED1pin, HIGH); } else { digitalWrite(LED1pin, LOW); } if (LED2status) { digitalWrite(LED2pin, HIGH); } else { digitalWrite(LED2pin, LOW); } } void handle_OnConnect() { LED1status = LOW; LED2status = LOW; Serial.println("GPIO7 Status: OFF | GPIO6 Status: OFF"); server.send(200, "text/html", SendHTML(LED1status, LED2status)); } // write five more functions to handle LED ON/OFF requests and the 404 Error page void handle_led1on() { LED1status = HIGH; Serial.println("GPIO7 Status: ON"); server.send(200, "text/html", SendHTML(true, LED2status)); } void handle_led1off() { LED1status = LOW; Serial.println("GPIO7 Status: OFF"); server.send(200, "text/html", SendHTML(false, LED2status)); } void handle_led2on() { LED2status = HIGH; Serial.println("GPIO6 Status: ON"); server.send(200, "text/html", SendHTML(LED1status, true)); } void handle_led2off() { LED2status = LOW; Serial.println("GPIO6 Status: OFF"); server.send(200, "text/html", SendHTML(LED1status, false)); } void handle_NotFound() { server.send(404, "text/plain", "Not found"); } // Displaying the HTML Web Page // The first text you should always send is the declaration, // which indicates that we’re sending HTML code String SendHTML(uint8_t led1stat, uint8_t led2stat) { String 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

OFF\n"; ptr += "

LED1 Status: ON

1 > OFF\n"; } else { // ptr += "

LED1 Status: OFF

ON\n"; ptr += "

LED1 Status: OFF

1 > ON\n"; } if (led2stat) { ptr += "

LED2 Status: ON

OFF\n"; } else { ptr += "

LED2 Status: OFF

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