rudy wrote:Where is prepareHtmlPage() ?
Oh sorry, right code is below.
this is test cod if any client connect to server and send any message, server send Hello Client.
#include <ESP8266WiFi.h>
#include "FS.h"
//ESP SSDI AND PASS when config in softAP mode
char ESP_AP_SSID[20]= "*******";
char ESP_AP_PASS[20]= "*******";
//Home AP SSID AND PASS when ESP config in Station mode
char home_AP_SSID[20]= "*******";
char home_AP_PASS[20]= "*******";
WiFiServer server(8080);
void GetEspApValue(){
//espAP.text Save ESP/AP SSID AND PASS IF exsit read from if not exist create and write default SSID AND PASS
bool exist = SPIFFS.exists("/espAP.text");
if (exist){
File f = SPIFFS.open("/espAP.text", "r");
if (f){
for (int i=1; i<=2; i++){
String s=f.readStringUntil('\n');
if (i==1){
s.replace((0x0D),(0x00));
s.toCharArray(ESP_AP_SSID, 20);
}
if (i==2){
s.replace((0x0D),(0x00));
s.toCharArray(ESP_AP_PASS, 20);
}
}
f.close();
}
}
else{
File f = SPIFFS.open("/espAP.text", "w");
if (f){
f.println(ESP_AP_PASS);
f.println(ESP_AP_PASS);
}
f.close();
}
}
void GethomeApValue(){
//homeAP.text Save home/AP SSID AND PASS IF exsit read from if not exist create and write default SSID AND PASS
bool exist1 = SPIFFS.exists("/homeAP.text");
if (exist1){
File f = SPIFFS.open("/homeAP.text", "r");
if (f){
for (int i=1; i<=2; i++){
String s=f.readStringUntil('\n');
if (i==1){
s.replace((0x0D),(0x00));
s.toCharArray(home_AP_SSID, 20);
}
if (i==2){
s.replace((0x0D),(0x00));
s.toCharArray(home_AP_PASS, 20);
}
}
f.close();
}
}
else{
File f = SPIFFS.open("/homeAP.text", "w");
if (f){
f.println(home_AP_SSID);
f.println(home_AP_PASS);
}
f.close();
}
}
void setup() {
Serial.begin(115200);
SPIFFS.begin();
SPIFFS.format();
GetEspApValue();
GethomeApValue();
delay(20);
WiFi.softAP((const char*)ESP_AP_SSID, (const char*)ESP_AP_PASS);
Serial.print("Connecting to ");
Serial.println(home_AP_SSID);
WiFi.begin((const char*)home_AP_SSID, (const char*)home_AP_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
void loop() {
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
client.println("Hello Client");
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disonnected]");
}
}