Syed Ahmed Ali
Syed Ahmed Ali

Reputation: 13

ESP32 to ESP32 WiFi Server/Client Problem

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:

  1. How do I send or write data to the server from the client?
  2. 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

//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

//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()
{

}

Upvotes: -1

Views: 7596

Answers (1)

user12592944
user12592944

Reputation:

Look at this very handy function:

void SetWifi(const char *name, const char *password) { // Turn on wifi with server
    Serial.println("Starting server");
    WiFi.disconnect();
    WiFi.softAP(name, password);
    delay(2000);
    IPAddress IP = WiFi.softAPIP();
    Serial.print("Server IP : ");
    Serial.println(IP);
    server.begin();
    server.setNoDelay(true);
    Serial.println("Server started");
}

You can write data with this function :

void sendDataTCP(String message) {  // function to send message back to client
    if (client && client.connected()) { //check if client is there
        client.println(message);
    }
    client.flush();
}

Receive data with this function:

void availableMessage() {
    if (client.available()) {//check if client is there
        while (client.available()) {
            String message = client.readStringUntil('\n');  //read string until enter (end of message)
            Serial.println("Received: " + message);
            message.toCharArray(buffer, BUFFER);           // put message in char array (buffer)
            client.flush(); // discard all bytes that have been read
        }
    }
}

Check if someone has connected:

void connectClient() {
    if (server.hasClient())                          // if server has a client
    {
        if (client = server.available()) {            // if client is connected
            Serial.println("Connected");
        }
    }
}

I think this will get you in the direction of accomplishing your goal.

Upvotes: 0

Related Questions