ESP32 to ESP32 WiFi Server/Client Problem

SyedAhmed
Posts: 2
Joined: Thu Dec 06, 2018 4:45 pm

ESP32 to ESP32 WiFi Server/Client Problem

Postby SyedAhmed » Sat Dec 08, 2018 6:45 pm

I've got one ESP32 acting as client and another ESP32 acting as an access-point for direct communication and outdoor use. I have set up a server on the AP end and would like the client to communicate with it but I can't seem to make this work.

I would like to know two things:

How do I send or write data to the server from the client?
How do I read and display the data that was sent to the server from the client?
I have attached the code below:

Code for AP/Server

Code: Select all

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

// Replace with your network credentials
const char* ssid     = "ESP32-Access-Point";
const char* password = "SyedAhmedAli";
 //Set web server port number to 80

WiFiServer server(80);


void setup() {
  Serial.begin(115200);
  Serial.println("Setting AP (Access Point)…");

  WiFi.softAP(ssid, password);  

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address ");
  Serial.println(IP);
  Serial.print("MAC address ");
  Serial.println(WiFi.softAPmacAddress());

  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





    while (client.connected()) 
    {           
    Serial.println("Client connected.");
    Serial.println(client.available());

     if (client.available() > 0) 
     {
      // read the bytes incoming from the client:
      char thisChar = client.read();
      // echo the bytes back to the client:
      server.write(thisChar);
      // echo the bytes to the server as well:
      Serial.write(thisChar);
     }


    }
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println();
  }
}
Code for Client

Code: Select all

//Client
#include <WiFi.h>

const char* ssid = "ESP32-Access-Point";
const char* password =  "SyedAhmedAli";


WiFiClient client;

IPAddress server(192, 168, 4, 1);    

void setup()
{
  Serial.begin(115200);
  Serial.println();

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
   while (WiFi.status() != WL_CONNECTED)
   {
    delay(500);
    Serial.print(".");
   }
    Serial.println(" connected");
     if(client.connect(server, 80))
      {
        Serial.println("connected to server");
        client.write("Data");
      }
     else
     {
        Serial.println("failed to connect to server");
     }

}


void loop()
{

}


DedHatabich
Posts: 1
Joined: Wed Dec 12, 2018 9:04 am

Re: ESP32 to ESP32 WiFi Server/Client Problem

Postby DedHatabich » Wed Dec 12, 2018 9:06 am

I have a similar problem.

Who is online

Users browsing this forum: No registered users and 5 guests