Hello! Greetings! hope everyone is doing well.
I'm working on a project which uses ESP32. I have configured ESP32 as softAP and have a web server running on it. What I need to do now is after connecting to the ESP32AP via WLAN, opening a web browser and entering the IP, I land on web page. What I'm trying to do now is, if I type in something like this in web browser:
http://192.168.4.1/changeSSID
My new SSID is set to changeSSID, then I reboot the softAP with this new SSID. I have a code written but I can't get it to work
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#define Device 2
// Create the objects for server and client
WiFiServer server(80);
WiFiClient client;
const char* ssid = "ESP32-Data-On-Tag";// This is the SSID that ESP32 will broadcast
//const char* password = "12345678"; // password should be atleast 8 characters to make it work (later a self building password will be added)
// Create the global variable
String http;
String DeviceState = "off";
byte mac[6];
String one, Two, Pass;
const char* password;
const char* NewSSID;
void setup()
{
Serial.begin(115200);
delay(1000);
Serial.println();
Serial.print("Program Start...");
Create_Password_SetupAP();
Set_New_SSID();
pinMode(Device, OUTPUT);
digitalWrite(Device, LOW);
/*
WiFi.softAP(ssid, password);
Serial.println( "" );
Serial.println( "WiFi AP is now running" );
Serial.println( "IP address: " );
Serial.println( WiFi.softAPIP() );
*/
// Start our ESP32 server
server.begin();
}
void loop()
{
if ( client = server.available() ) { // Checks if a new client tries to connect to our server
Serial.println("New Client.");
String clientData = "";
while ( client.connected() ) { // Wait until the client finish sending HTTP request
if ( client.available() ) { // If there is a data,
char c = client.read(); // read one character
http += c; // then parse it
Serial.write(c);
if (c == '\n') { // If the character is carriage return,
// it means end of http request from client
if (clientData.length() == 0) { // Now that the clientData is cleared,
sendResponse(); // perform the necessary action
updateDevice();
updateWebpage();
break;
} else {
clientData = ""; // First, clear the clientData
}
} else if (c != '\r') { // Or if the character is NOT new line
clientData += c; // store the character to the clientData variable
}
}
}
http = "";
client.stop(); // Disconnect the client.
Serial.println("Client disconnected.");
Serial.println("");
}
}
void sendResponse() {
// Send the HTTP response headers
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
}
void updateWebpage() {
// In here we will display / update the webpage by sending the HTML
// to the connected client
// In order for us to use the HTTP GET functionality,
// the HTML hyperlinks or href is use in the buttons.
// So that, when you press the buttons, it will send a request to the
// web server with the href links by which our ESP32 web server will
// be check using HTTP GET and execute the equivalent action
// Send the whole HTML
client.println("<!DOCTYPE html><html>");
client.println("<head>");
client.println("<title>ESP32 WiFi Station</title>");
client.println("</head>");
// Web Page Heading
client.println("<body><h1>Data-On-Tag Plug</h1>");
// Display buttons for Blue LED
client.println("<p>1. Device is " + DeviceState + "</p>");
if (DeviceState == "off")
{
client.println("<p><a href=\"/Device/on\"><button>Turn ON</button></a></p>");
} else
{
client.println("<p><a href=\"/Device/off\"><button>Turn OFF</button></a></p>");
}
client.print("<hr>");
client.println("</body></html>");
client.println();
}
void updateDevice() {
// In here we will check the HTTP request of the connected client
// using the HTTP GET function,
// Then turns the LED on / off according to the HTTP request
if (http.indexOf("GET /Device/on") >= 0) {
Serial.println("Device on");
DeviceState = "on";
digitalWrite(Device, HIGH);
} else if (http.indexOf("GET /Device/off") >= 0) {
Serial.println("Device off");
DeviceState = "off";
digitalWrite(Device, LOW);
}
}
void Create_Password_SetupAP()
{
WiFi.begin();
Serial.println("WiFi began");
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[0],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.println(mac[5],HEX);
Serial.println("Creating Password from MAC ID");
int mac0 = mac[0];
int mac1 = mac[1];
one = mac0;
Two = one + mac1;
Pass = Two + "DOT";
Serial.println(Pass);
password = Pass.c_str();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.softAP(ssid, password);
Serial.println( "" );
Serial.println( "WiFi AP is now running" );
Serial.println( "IP address: " );
Serial.println( WiFi.softAPIP() );
}
void Set_New_SSID()
{
Check_New_Client();
}
void Check_New_Client()
{
if ( client = server.available() ) { // Checks if a new client tries to connect to our server
Serial.println("New Client.");
String clientData = "";
while ( client.connected() ) { // Wait until the client finish sending HTTP request
if ( client.available() ) { // If there is a data,
char c = client.read(); // read one character
http += c; // then parse it
Serial.write(c);
if (c == '\n') { // If the character is carriage return,
// it means end of http request from client
if (clientData.length() == 0) { // Now that the clientData is cleared,
sendResponse(); // perform the necessary action
updateSSID();
//updateSSIDWebpage();
break;
} else {
clientData = ""; // First, clear the clientData
}
} else if (c != '\r') { // Or if the character is NOT new line
clientData += c; // store the character to the clientData variable
}
}
}
http = "";
client.stop(); // Disconnect the client.
Serial.println("Client disconnected.");
Serial.println("");
}
}
void updateSSID()
{
/*Steps:
* 1. Fetch new SSID
* 2. Store it
* 3. Disconnect AP
* 4. Assign new SSID
* 5. Restore AP
* 6. Update to web page
*/
if(http.indexOf("GET /changeSSID") >= 0)
{
Serial.println(http);
NewSSID = http.c_str();
Serial.print("New SSID fetched:");
Serial.println(NewSSID);
}
Serial.println("Disconnecting SoftAP");
WiFi.softAPdisconnect (true);
Serial.println("Soft AP Disconnected");
Serial.print("Connecting to ");
Serial.println(NewSSID);
WiFi.softAP(NewSSID, password);
Serial.println( "" );
Serial.println( "WiFi AP is now running" );
Serial.println( "IP address: " );
Serial.println( WiFi.softAPIP() );
}
void updateSSIDWebpage()
{
}
I thought I might need to use POST method? if so how I do have to use it?
The ESP32 is not going to internet this all just works on WLAN
Any help please? Thanks in advance