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