//#include <Arduino.h> // not necessary in the ESP32 Library
#include <WiFi.h>
#include <string.h>
const char ssid[] = "ESP32_SoftAP"; // SSID
const char password[] = "12345678"; // at least 8 characters!
IPAddress hostadresse; // host-address IPv4
int ESP_wifi_mode; // WiFi-mode
WiFiServer ESPserver; // server-class instance
WiFiClient extClient; // client-class instance
void setup()
{
Serial.begin(115200);
delay(1000); // Wait for Serial to initialize
Serial.println("Setup WIFI Network in SoftAP Mode.");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("Secret password: ");
Serial.println(password);
WiFi.mode(WIFI_AP); // set WIFI-Mode in AP-Mode
WiFi.softAP(ssid, password); // start ESP as Access-Point
hostadresse = WiFi.softAPIP(); // read the IP-address
ESP_wifi_mode = WiFi.getMode();
ESPserver.begin(80); // start webserver on port 80
Serial.println("********************");
Serial.print("WiFi-Modus: ");
Serial.println(ESP_wifi_mode); // display the WiFi-mode
Serial.print("Hostadresse: ");
Serial.println(hostadresse); //display the host-address
Serial.println("********************");
}
void loop()
{
char host_send_char; // byte sent by the client
char host_send_data[500]; // char-array of the bytes sent by the client
int i; // index-variable
int button_state = 0; // state-variable for the button
extClient = ESPserver.available(); // is there a client?
i = 0; // reset the index-variable
while (extClient.available()) // if there is a client availlable then read the data sent by the client
{
host_send_char = extClient.read(); // read the byte of data from the client
Serial.write(host_send_char); // write the client-data on the serial monitor
host_send_data[i] = host_send_char; // append the databyte to the char-array
i++;
if (i > 499)
{
i = 499; // avoid overflow
}
}
host_send_data[i] = 0; // terminate the data char-array
// ***************************
// evaluate the client-message
// ***************************
if (strstr(host_send_data, "GET /OFF HTTP") != NULL)
{
button_state = 0;
}
if (strstr(host_send_data, "GET /ON HTTP") != NULL)
{
button_state = 1;
}
// *************
// HTTP response
// *************
extClient.println("HTTP/1.1 200 OK"); // Status-Line
extClient.println("Content-type:text/html"); // Header
extClient.println("Connection: keep-alive");
extClient.println(); // the header ends with a blank line
// *************************
// Display the HTML web page
// *************************
extClient.println("<!DOCTYPE html><html>");
extClient.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
extClient.println("<title>Webserver toggle button</title>");
extClient.println("</head>");
extClient.println("<body>");
extClient.println("<h1>Button state</h1>");
extClient.print("<p>");
// print the state of the Button
if (button_state == 0)
{
extClient.print("OFF");
}
else
{
extClient.print("ON");
}
extClient.println("</p>");
extClient.println("<h2>Link to the button</h2>");
extClient.print("<p>");
extClient.print("<a href=\"");
// print the state of the Button
if (button_state == 1)
{
extClient.print("OFF");
}
else
{
extClient.print("ON");
}
extClient.print("\">");
extClient.print("Turn the button ");
// print the state of the Button
if (button_state == 1)
{
extClient.print("OFF");
}
else
{
extClient.print("ON");
}
extClient.print("</a>");
extClient.println("</p>");
extClient.println("</body></html>");
// The HTTP response ends with a blank line
extClient.println();
}