ESP32 WiFi communication issue

Hello,
Here is my problem:
I have two Arduino ESP32.
One is the server and generates an access point.
The access point works well and is well detected by the client when it approaches.
(server detection cycle every 10 seconds via WiFi)
The goal is that the client must send a data (string) to this access point, as soon as it detects it.
The data is a character string of max 256 ascii characters.
I therefore need a piece of code on the access point to retrieve this data.
I also need a code (POST?) On the client to send this data to the access point.
I tried GET and POST, but I'm turning in round.

Here is code for the access point:

// Server side (Access point)

#include <WiFi.h>
#include <ESPAsyncWebServer.h>

const char* ssid     = "DELTA";
const char* password = "";
String PostData;

IPAddress local_IP(192,168,4,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

AsyncWebServer server(80);

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

  Serial.print("Setting AP (Access Point)…");

  WiFi.softAP(ssid, password);
  IPAddress local_IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(local_IP);
}

void loop(){

  server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request) {
    request->send(200, "text/plain", PostData);
  });
  server.begin();
delay(2000);
Serial.print(PostData); // pas de réponse???
 // server.end();
delay(5000);

}
       

I receive in the serial:
"Setting AP (Access Point)…AP IP address: 192.168.4.1"

And client side:

// Côté  client

#include <strings.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
#include <WebServer.h>
#include <Arduino.h>

int cycle;

char ssid[] = "DELTA";

const char* hostURL = "http://192.168.4.1/";
char password[] = "";

int status = WL_IDLE_STATUS;
IPAddress server (192,168,4,1);

WiFiClient client;

String PostData = "someDataToPost";

void setup()
{  
  Serial.begin(9600);
  delay(1000); 
}

void loop()
{ 
  
Serial.println("Connecting to DELTA...");
WiFi.begin(ssid, password);
cycle=0;
 while ((WiFi.status() != WL_CONNECTED) and (cycle<15)) {
  delay(500);
  Serial.print("#");
  cycle = cycle +1;
  Serial.println("cycle: "+String(cycle));
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  if ((WiFi.status() == WL_CONNECTED)) {
  Serial.println("connected");
  HTTPClient http;
http.begin(hostURL);
if (client.connect(server, 80))
{
  Serial.println("client connected");
  client.println("POST /PostData HTTP/1.1");
  client.println("Host: DELTA");
  client.println("User-Agent: Arduino/1.0");
  client.println("Connection: close");
  client.print("Content-Length: ");
  client.println(PostData.length());
  client.println();
  client.println(PostData);
  Serial.println("correctly send");
  }else Serial.println("For POST, client not connected");

  if (client.connected()) {
    client.stop();
    WiFi.disconnect(); }
  }

  delay(10000);            
}

In the serial I receive:
"
Connecting to DELTA...
#cycle: 1
#cycle: 2
#cycle: 3
#cycle: 4
#cycle: 5

Connected, IP address: 192.168.4.2
connected
client connected
correctly send

"
I receive "correctly send" then is going into the if condition, but nothing received on the server???

Send a simple string by WiFi bw 2 ESP32 seems to be a nightmare,

Many tanks for your assistance or short code example,

Phil58

You define on the server to react only to requests sent to /post but your client sends the requests to /.

Try to change the URL line to:

const char* hostURL = "http://192.168.4.1/post";

and post results!

Hi pylon,
Many thanks for your fast answer :+1:
As you propose, I change by
const char* hostURL = "http://192.168.4.1/post";
on the client.
What do you mean by "and post results!"?
I define a string to send for the testing: "String PostData = "someDataToPost";"
Then I send:

client.println("POST /PostData HTTP/1.1");
  client.println("Host: DELTA");
  client.println("User-Agent: Arduino/1.0");
  client.println("Connection: close");
  client.print("Content-Length: ");
  client.println(PostData.length());
  client.println();
  client.println(PostData);

... but doesn't work yet for the moment :neutral_face:
Is there another wrong semantic to fix?
Many thanks pylon :ok_hand:
I really appreciate :+1:
Phil58

Excuse me, should have read the complete code. You don't use the HTTPClient but do the HTTP yourself. Change the POST line to

client.println("POST /post HTTP/1.1");

You can delete the HTTPClient stuff as you don't use it.

I see many mistakes there. You are using ASYNC webserver, and you are creating website callback in every loop iteration... It is async.. So call it only once in setup(). I don't see stuff related to receive datas at server side, so I am not sure if server is working properly. Server isn't awaiting any keys with datas... Have you checked that, webserver is working okay? Like, if you tried POST request from some test tool like Postman.. I will not test that. So, hope it works...
There should be at webserver something related to params, because I don't see any mechanism there implemented to receive value:

Client code
Also in client code, you are using httpclient with websocketsocket connection, you are mixing it up. So I edited whole client code, because it was chaotic. So, client code is working. If you will not have success with seeing datas at webserver side, problem is at webserver.

// Côté  client
//Edited by: martinius96

#include <strings.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <Arduino.h>

int cycle;

//WiFi RELATED
char ssid[] = "DELTA";
char password[] = "";

//Webserver RELATED
const char* hostURL = "192.168.4.1";
String url = "/post";
const int httpPort = 80;

WiFiClient client;

String PostData = "name=John&surname=Doe";

void setup()
{
  Serial.begin(9600);
  WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom
  cycle = 0;
  while (WiFi.status() != WL_CONNECTED) {
    cycle++;
    delay(1000);
    Serial.println("Cycle: " + String(cycle));
  }
  Serial.println(F(""));
  Serial.print(F("Wifi connected. Assigned IP: "));
  Serial.println(WiFi.localIP());
}

void loop()
{
  yield();
  if (WiFi.status() != WL_CONNECTED) {
    cycle = 0;
  }
  while (WiFi.status() != WL_CONNECTED) {
    cycle++;
    delay(500);
    Serial.println("Cycle: " + String(cycle));
  }
  if (client.connect(hostURL, 80)) {
    Serial.println(F("Connected to server successfully"));
    client.println("POST " + url + " HTTP/1.0");
    client.println("Host: " + (String)hostURL);
    client.println(F("User-Agent: ESP"));
    client.println(F("Connection: close"));
    client.println(F("Content-Type: application/x-www-form-urlencoded;"));
    client.print(F("Content-Length: "));
    client.println(PostData.length());
    client.println();
    client.println(PostData);
    Serial.println(F("Datas were sent to server successfully"));
    while (client.connected()) {
      String line = client.readStringUntil('\n');
      Serial.println(line); //IF YOU WANT TO PRINT HTTP HEADER OF WEBSERVER
      if (line == "\r") {
        break;
      }
    }
    String line = client.readStringUntil('\n');
    Serial.println(line); //IF YOU WANT TO PRINT FIRST ROW OF WEBSERVER RESPONSE
  } else {
    Serial.println(F("Connection to webserver was NOT successful"));
  }
  delay(10000);  //wait 10 seconds and run again loop()
}

Really happy to discuss with you, for me a litte bit late it is about 6 in the morning and doesn't sleep a minute till now...

So, here is edited sketch for webserver, it should work :wink:
You need to define params where ESP will read information.
So in this examplecase.. there is param name and param surname (as you see in client code, so we must edit server to these params to read correct information from request --> value that is added to key)...

Client will send request with name=Joe&surname=Doe
So Async server will parse value based on param.
So server will save to variable named message value Joe and to variable message2 value Doe.
And then server will send you back response: Async webserver received via HTTP method POST these datas: John Doe.

// Server side (Access point)
// Edited by: martinius96

#include <WiFi.h>
#include <ESPAsyncWebServer.h>

const char* ssid     = "DELTA";
const char* password = "";
String PostData;

IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

AsyncWebServer server(80);
const char* PARAM_MESSAGE = "name"; //key param name=Joe
const char* PARAM_MESSAGE2 = "surname"; //key param surname=Doe
void setup() {
  Serial.begin(9600);
  delay(1000);

  Serial.print("Setting AP (Access Point)…");

  WiFi.softAP(ssid, password);
  IPAddress local_IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(local_IP);
  server.on("/post", HTTP_POST, [](AsyncWebServerRequest * request) {
    String message;
    if (request->hasParam(PARAM_MESSAGE, true)) {
      message = request->getParam(PARAM_MESSAGE, true)->value();
    } else {
      message = "No message sent";
    }
    String message2;
    if (request->hasParam(PARAM_MESSAGE2, true)) {
      message2 = request->getParam(PARAM_MESSAGE2, true)->value();
    } else {
      message2 = "No message2 sent";
    }
    request->send(200, "text/plain", "Async webserver received via HTTP method POST these datas: " + message + " " + message2);
  });
  server.begin();
}

void loop() {
  yield();
}

So only problem was you need to define param. So you can have param like: message and you can send whole text that you want to send there to webserver such as: message=Hello world, I am client. Server will be awaiting value under key message... Simple as that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.