Post topics, source code that relate to the Arduino Platform

User avatar
By frank65
#56622
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.


Code: Select all


#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]");
  }
 
}

User avatar
By martinayotte
#56644 The code you provided is definitively written to manage only one connection at a time.
First the fact that there is only one WiFiClient variable, second, the fact that code stays in a while() loop until this client disconnect.

You can inspire yourself with another example which manage multiple clients here :
https://github.com/esp8266/Arduino/blob ... Serial.ino
User avatar
By frank65
#56658
martinayotte wrote:The code you provided is definitively written to manage only one connection at a time.
First the fact that there is only one WiFiClient variable, second, the fact that code stays in a while() loop until this client disconnect.

You can inspire yourself with another example which manage multiple clients here :
https://github.com/esp8266/Arduino/blob ... Serial.ino



Hi martinayotte, thanks so much for your reply

i'm test your sample code so that work really good, are you know how can get new client IP, because i will send message to selected client.

so i have another question about ESP8266WebServer.h library. i can not find any user manual or documentation about this library. what is different between ESP8266WebServer.h AND ESP8266WiFi.h. you know we can make TCP or HTTP server with ESP8266WiFi.h so i can't understand ESP8266WebServer.h library :(

for example please see below code.
i can not understand some function of this library
for example i don't know >> server.on << AND >> server.handleClient(); << function

would you please teach me how work this library and that function AND what is difference between ESP8266WebServer.h AND ESP8266WiFi.h

Thanks so so much

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char* ssid = "........";
const char* password = "........";

ESP8266WebServer server(80);

const int led = 13;

void handleRoot() {
  digitalWrite(led, 1);
  server.send(200, "text/plain", "hello from esp8266!");
  digitalWrite(led, 0);
}

void handleNotFound(){
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void){
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/inline", [](){
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
}
User avatar
By martinayotte
#56672 The ESP8266WebServer is on top of the ESP8266WiFi.
The ESP8266WiFi is a plain TCP Server/Client, so with basic connections functionalities.
It can be use for Telnet server for example, but for WebServer, you are on your own to add all the HTTP protocols by yourself, by that I means preparing headers, parsing requests, etc.
ESP8266WebServer is doing all that for you, so it is easier to create a WebServer and avoiding re-inventing the wheel.

BTW, server.on() is simply to attach callback functions depending of the URL path, while the server.handle() is really where all processes are running under the hood, it should be called by your loop() and your loop() should not stay in a blocking process.