WifFI basics for esp32 and esp8266

WiFi connections key words 

 1. WiFi.status();

  Syntax:

      while ((!(WiFi.status() == WL_CONNECTED))){   
    Serial.print("..");
    delay(300);

  }

 2. WiFi.localIP()

  Syntax:
    WiFi.localIP()

#include <WiFi.h>

void setup()
{
Serial.begin(9600);

  WiFi.begin("ssid","password");
  Serial.println("Start");
  Serial.print("please wait connecting to WIFI");
  while ((!(WiFi.status() == WL_CONNECTED))){   
    Serial.print("..");
    delay(300);

  }
  Serial.println("connected ");
  Serial.println("IP address :");
  Serial.print((WiFi.localIP()));

}


Result



Server creation steps and client 

1. WiFiSever sever(80);  port creation in WiFi making esp as sever and ip address as a client
2, WiFiClient client ; creating client to WiFi 
3. sever.begin() will start start server
4. client=server.available()  checking sever and configure to server ip to client address 
5.check client configure success are not client return value true or false  value 
6.client.available() check client in server and connected  local network ip and printing data.



code

#include <WiFi.h>


WiFiServer server(80);

WiFiClient client;
void setup()
{
Serial.begin(9600);

  WiFi.begin("3floor","vijaysai1234");
  Serial.println("Start");
  Serial.print("please wait connecting to WIFI");
  while ((!(WiFi.status() == WL_CONNECTED))){
    Serial.print("..");
    delay(300);

  }
  Serial.println("connected ");
  Serial.println("IP address :");
  Serial.print((WiFi.localIP()));
  Serial.println("server started ");
  server.begin();

}


void loop()
{

    client = server.available();
    if (!client) { return; }
    while(!client.available()){  delay(1); }
    Serial.println("server connected ");
    client.println("<h1 style=""color:#ff0000"">");
    client.println("Welcome to sever ");
    client.println("</h1>");
    client.flush();


}


client


#include <WiFi.h>

WiFiClient client;

String myurl = "/";
void setup()
{
Serial.begin(9600);

  WiFi.disconnect();
  delay(3000);
  Serial.println("START");
  WiFi.begin("3floor","vijaysai1234");
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
    Serial.print("..");

  }
  Serial.println("Connected");
  Serial.println("Your IP is");
  Serial.println((WiFi.localIP()));

}


void loop()
{

    if (client.connect("192.168.1.10", 80)) {
      myurl += "data";
      client.print(String("GET ") + myurl + " HTTP/1.1\r\n" +
                "Host: " + "192.168.1.10" + "\r\n" +
               "Connection: close\r\n\r\n");
      myurl="/";

    }
    delay(5000);


}


sever  


#include <WiFi.h>

String  ClientRequest;
IPAddress staticIP399_10(192,168,1,10);
IPAddress gateway399_10(192,168,1,1);
IPAddress subnet399_10(255,255,255,0);

WiFiServer server(80);

WiFiClient client;

String myresultat;

String ReadIncomingRequest(){
while(client.available()) {
ClientRequest = (client.readStringUntil('\r'));
 if ((ClientRequest.indexOf("HTTP/1.1")>0)&&(ClientRequest.indexOf("/favicon.ico")<0)){
myresultat = ClientRequest;
}
}
return myresultat;
}

void setup()
{
ClientRequest = "";

Serial.begin(9600);

  WiFi.disconnect();
  delay(3000);
  Serial.println("START");
  WiFi.begin("3floor","vijaysai1234");
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
    Serial.print("..");

  }
  Serial.println("Connected");
  WiFi.config(staticIP399_10, gateway399_10, subnet399_10);
  Serial.println("Your IP is");
  Serial.println((WiFi.localIP()));
  server.begin();

}


void loop()
{

    client = server.available();
    if (!client) { return; }
    while(!client.available()){  delay(1); }
    ClientRequest = (ReadIncomingRequest());
    client.flush();
    Serial.println(ClientRequest);


}



dynamic client 


/////////////////////////////////
// Generated with a lot of love//
// with TUNIOT FOR ESP32     //
// Website: Easycoding.tn      //
/////////////////////////////////
#include <WiFi.h>

WiFiClient client;

String myurl = "/";
void setup()
{
Serial.begin(9600);

  WiFi.disconnect();
  delay(3000);
  Serial.println("START");
  WiFi.begin("3floor","vijaysai1234");
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
    Serial.print("..");

  }
  Serial.println("Connected");
  Serial.println("Your IP is");
  Serial.println((WiFi.localIP()));

}


void loop()
{

    if (client.connect("192.168.0.5", 80)) {
      myurl += "data";
      client.print(String("GET ") + myurl + " HTTP/1.1\r\n" +
                "Host: " + "192.168.0.5" + "\r\n" +
               "Connection: close\r\n\r\n");
      myurl="/";

    }
    delay(5000);
    if (client.connect("192.168.0.5", 80)) {
      myurl += "data";
      Serial.println("ok");
      client.print(String("GET ") + myurl + " HTTP/1.1\r\n" +
                "Host: " + "192.168.0.5" + "\r\n" +
               "Connection: close\r\n\r\n");
      myurl="/";

    }
    delay(5000);


}


dynamic server

/////////////////////////////////
// Generated with a lot of love//
// with TUNIOT FOR ESP32     //
// Website: Easycoding.tn      //
/////////////////////////////////
#include <WiFi.h>

String  ClientRequest;
IPAddress staticIP200_10(192,168,1,10);
IPAddress gateway200_10(192,168,1,1);
IPAddress subnet200_10(255,255,255,0);

WiFiServer server(80);

WiFiClient client;

String myresultat;

String ReadIncomingRequest(){
while(client.available()) {
ClientRequest = (client.readStringUntil('\r'));
 if ((ClientRequest.indexOf("HTTP/1.1")>0)&&(ClientRequest.indexOf("/favicon.ico")<0)){
myresultat = ClientRequest;
}
}
return myresultat;
}

void setup()
{
ClientRequest = "";

Serial.begin(9600);

  WiFi.disconnect();
  delay(3000);
  Serial.println("START");
  WiFi.begin("3floor","vijaysai1234");
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
    Serial.print("..");

  }
  Serial.println("Connected");
//  WiFi.config(staticIP200_10, gateway200_10, subnet200_10);
  Serial.println("Your IP is");
  Serial.println((WiFi.localIP()));
  server.begin();

}


void loop()
{

    client = server.available();
    if (!client) { return; }
    while(!client.available()){  delay(1); }
    ClientRequest = (ReadIncomingRequest());
    client.flush();
    Serial.println(ClientRequest);


}

extension example for led high


/////////////////////////////////
// Generated with a lot of love//
// with TUNIOT FOR ESP32     //
// Website: Easycoding.tn      //
/////////////////////////////////
#include <WiFi.h>

String  ClientRequest;
IPAddress staticIP200_10(192,168,1,10);
IPAddress gateway200_10(192,168,1,1);
IPAddress subnet200_10(255,255,255,0);

WiFiServer server(80);

WiFiClient client;

String myresultat;

String ReadIncomingRequest(){
while(client.available()) {
ClientRequest = (client.readStringUntil('\r'));
 if ((ClientRequest.indexOf("HTTP/1.1")>0)&&(ClientRequest.indexOf("/favicon.ico")<0)){
myresultat = ClientRequest;
}
}
return myresultat;
}

void setup()
{
ClientRequest = "";

Serial.begin(9600);

  WiFi.disconnect();
  delay(3000);
  Serial.println("START");
  WiFi.begin("3floor","vijaysai1234");
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
    Serial.print("..");

  }
  Serial.println("Connected");
//  WiFi.config(staticIP200_10, gateway200_10, subnet200_10);
  Serial.println("Your IP is");
  Serial.println((WiFi.localIP()));
  server.begin();

}


void loop()
{

    client = server.available();
    if (!client) { return; }
    while(!client.available()){  delay(1); }
    ClientRequest = (ReadIncomingRequest());
    client.flush();
    Serial.println(ClientRequest);
    ClientRequest.remove(0, 5);
    ClientRequest.remove(ClientRequest.length()-9,9);
    Serial.println(ClientRequest);
     if (ClientRequest.equals("data")) {
      digitalWrite(LED_BUILTIN ,HIGH);
      delay(3000);

    }


}



// Load Wi-Fi library
#include <WiFi.h>

// Replace with your network credentials
const char* ssid     = "3floor";
const char* password = "vijaysai1234";

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;


void setup() {
  Serial.begin(115200);
  
  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
            
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
           
            
            // Web Page Heading
            client.println("<body><h1>ESP32 Web Server</h1>");
            

            client.println("</body></html>");
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }

}

Comments