Envoi d'un string entre deux ESP32
Bonjour,
Je tente d'envoyer une chaine de caractères d'un ESP32 vers un autre ESP32 et ceci via WiFi.
Le premier est le serveur et est le point d'accès.
Le second est le client et régulièrement, tente de détecter le serveur.
Quand il est à sa portée, il le détecte bien, et c'est à ce moment là queje souhaite que le client envoie ce string, que le serveur le capte et l'envoie dans le moniteur série.
Tout semble ok, sauf que le message n'est pas reçu :weird:
Voici les deux codes.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| // Côté serveur (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???
delay(5000);
} |
et
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #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(1000);
} |
Je reçois bien "correctly send" donc passe bien dans la boucle.
Déjà 1000 mercis pour tout vos conseils :ccool:
PhiLu