Send data through WiFi with GET

Hello! First time here.

Im currently working on something where i needed to setup a WiFi communication between 2 ESP32. The host is sending his sensor's data to the other one.
I currently see the data on the URL i provided, which is great.

My problem is that i need to send back another data from the Client to the Host, but whenever i try to make a POST request, it just does nothing. The code still works, but if i got to the new URL i set up, an Error 500 appears.

This is my code (some variables and functions are in french, but don't mind them):
For the Host:

#ifndef LIBRARIES_ESP32SERVO_SRC_ESP32TONE_H_
#define LIBRARIES_ESP32SERVO_SRC_ESP32TONE_H_
#include "ESP32PWM.h"
void tone(int pin, unsigned int frequency);
void noTone(int pin);

#endif /* LIBRARIES_ESP32SERVO_SRC_ESP32TONE_H_ */

#include <NewPing.h>
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include <Wire.h>

//Variables du WiFi
const char* ssid = "ESP32-Access-Point";
// Créer un AsyncWebServer objet sur le port 80
AsyncWebServer server(80);

//Variables pour les autres options
const int BROCHE_BTN_S = 13;
byte const BROCHE_DEL_S = 18;
int buzzer_S = 32;
int distance_S;

//Variables pour la moyenne
int valeurmax_S = 10;
int moyenne_S = 0;
int compteur = 0;

//Variables du délai pour le capteur
unsigned long previousMillisC = 0;
const long intervalC = 50;

//Variables pour le capteur
#define TRIGGER_PIN_S  26 //Broche Trigger du capteur
#define ECHO_PIN_S     25 //Broche Echo
#define MAX_DISTANCE 300 //Distance maximale pour le capteur voulue (cm). Maximum du capteur: 400-500cm
NewPing sonar_S(TRIGGER_PIN_S, ECHO_PIN_S, MAX_DISTANCE); //Déclaration des broches pour le NewPing
String valeur;

void setup() {
  Serial.begin(115200);
  //Initialise les différentes options et leurs valeurs par défaut
  pinMode(BROCHE_DEL_S, OUTPUT);
  digitalWrite(BROCHE_DEL_S, LOW);
  pinMode(buzzer_S, OUTPUT);
  pinMode(BROCHE_BTN_S, INPUT);

  // Mettre la carte ESP comme point d'accès
  Serial.print("Setting AP (Access Point)…");
  WiFi.softAP(ssid);

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

  server.on("/compteur", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send_P(200, "text/plain", valeur.c_str());
  });

  bool status;

  // Start server
  server.begin();
}

void loop() {
  unsigned long currentMillisC = millis();

  if (currentMillisC - previousMillisC >= intervalC) {
    //Détection du capteur
    distance_S = sonar_S.ping_cm();
    Serial.print("Ping: ");
    Serial.print(distance_S); //Envoi le ping, obtient la distance en cm et affiche le résultat
    Serial.println("cm");
    Serial.println(compteur);

    if (0 < distance_S && distance_S < (valeurmax_S - 10)) {  //Si la détection se trouve à l'intérieur de la limite, buzzer et DEL s'activent et compteur incrémente
      digitalWrite(BROCHE_DEL_S, HIGH);
      tone(buzzer_S, 400);
      delay(500);
      noTone(buzzer_S);
      digitalWrite(BROCHE_DEL_S, LOW);
      delay(500);
      compteur++;
      valeur = String(compteur);

    }
    //Bouton sert à mettre valeur limite pour la détection
    if (digitalRead(BROCHE_BTN_S) == LOW) {
      Serial.println("BTN appuyé");
      moyenne_S = 0;
      for (int j = 0; j < 10; j++) {
        moyenne_S = moyenne_S + distance_S;
      }
      valeurmax_S = moyenne_S / 10;
      digitalWrite(BROCHE_BTN_S, HIGH);
      compteur = 0;
      valeur = String(compteur);
    }
    previousMillisC = currentMillisC;
  }
}

And for the Client:

#ifndef LIBRARIES_ESP32SERVO_SRC_ESP32TONE_H_
#define LIBRARIES_ESP32SERVO_SRC_ESP32TONE_H_
#include "ESP32PWM.h"
void tone(int pin, unsigned int frequency);
void noTone(int pin);

#endif /* LIBRARIES_ESP32SERVO_SRC_ESP32TONE_H_ */

#include <NewPing.h>
#include <HTTPClient.h>
#include "Ecran.h"
#include "ESPAsyncWebServer.h"
#include <Wire.h>
#include <WiFi.h>

//Variables du WiFi
const char* ssid = "ESP32-Access-Point";
// Créer un AsyncWebServer objet sur le port 80
AsyncWebServer server(80);

//Variables pour les autres options
const int BROCHE_BTN_E = 13;
byte const BROCHE_DEL_E = 18;
int buzzer_E = 32;
int distance_E;

//Variables pour la moyenne
int valeurmax_E = 10;
int moyenne_E = 0;
int compteur = 0;

String compteurS;

//L'adresse URL utilisée
const char* serverNameComp = "http://192.168.4.1/compteur";
const char* serverNameFinal = "http://192.168.4.1/final";

//Variables du délai pour l'écran
unsigned long previousMillis = 0;
const long interval = 1000;

//Variables du délai pour le capteur
unsigned long previousMillisC = 0;
const long intervalC = 50;
Ecran monEcran; //Création de l'objet 'ecran' de classe Ecran.

//Variables pour le capteur
#define TRIGGER_PIN_E  26 //Broche Trigger du capteur
#define ECHO_PIN_E     25 //Broche Echo
#define MAX_DISTANCE 300 //Distance maximale pour le capteur voulue (cm). Maximum du capteur: 400-500cm
NewPing sonar_E(TRIGGER_PIN_E, ECHO_PIN_E, MAX_DISTANCE); //Déclaration des broches pour le NewPing
String valeur;

//Variables de l'équation
int FinalE;
int FinalS;
int Final;
String Resultat;

void setup() {
  Serial.begin(115200);
  //Initialise les différentes options et leurs valeurs par défaut
  pinMode(BROCHE_DEL_E, OUTPUT);
  digitalWrite(BROCHE_DEL_E, LOW);
  pinMode(buzzer_E, OUTPUT);
  pinMode(BROCHE_BTN_E, INPUT);

  //Initialiser l'écran.
  monEcran.begin();

  //Connexion au WiFi
  WiFi.begin(ssid);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  Capteur();
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    //Vérifier le status de la connexion WiFi
    if (WiFi.status() == WL_CONNECTED ) {
      compteurS = httpGETRequest(serverNameComp);
      Serial.println("Compteur: " + compteurS);

      FinalE = valeur.toInt();
      FinalS = compteurS.toInt();

      Final = FinalE - FinalS;

      if (Final <= 0) {
        Final = 0;
      }
      Resultat = String(Final);

      //Afficher sur l'écran
      monEcran.effacer(); //Effacer tout l'écran
      monEcran.display();
      monEcran.ecrire("Il y a ", 0, 2);
      monEcran.ecrire(Resultat, 2, 2);
      monEcran.ecrire("personnes", 4, 2);
      monEcran.display();

      WiFiClient client;
      HTTPClient http;
      http.begin(client, serverNameFinal);
      int httpResponseCode = http.POST(Resultat);

      if (httpResponseCode > 0) {
        String response = http.getString();  //Get the response to the request
        Serial.println(httpResponseCode);   //Print return code
        Serial.println(response);           //Print request answer
      } else {
        Serial.print("Error on sending POST: ");
        Serial.println(httpResponseCode);

        http.end();

      }
      previousMillis = currentMillis;
    }
    else {
      Serial.println("WiFi Disconnected");
    }
  }
}

void Capteur() {

  unsigned long currentMillisC = millis();

  if (currentMillisC - previousMillisC >= intervalC) {
    //Détection du capteur
    distance_E = sonar_E.ping_cm();
    Serial.print("Ping: ");
    Serial.print(distance_E); //Envoi le ping, obtient la distance en cm et affiche le résultat
    Serial.println("cm");

    if (0 < distance_E && distance_E < (valeurmax_E - 10)) {  //Si la détection se trouve à l'intérieur de la limite, buzzer et DEL s'activent et compteur incrémente
      digitalWrite(BROCHE_DEL_E, HIGH);
      tone(buzzer_E, 400);
      delay(500);
      noTone(buzzer_E);
      digitalWrite(BROCHE_DEL_E, LOW);
      delay(500);
      compteur++;
      valeur = String(compteur);
    }
    //Bouton sert à mettre valeur limite pour la détection
    if (digitalRead(BROCHE_BTN_E) == LOW) {
      Serial.println("BTN appuyé");
      moyenne_E = 0;
      for (int i = 0; i < 10; i++) {
        moyenne_E = moyenne_E + distance_E;
      }
      valeurmax_E = moyenne_E / 10;
      digitalWrite(BROCHE_BTN_E, HIGH);
      compteur = 0;
      Final = 0;
    }

    previousMillisC = currentMillisC;
  }
}

String httpGETRequest(const char* serverName) {
  WiFiClient client;
  HTTPClient http;

  // Your Domain name with URL path or IP address with path
  http.begin(client, serverName);

  // Send HTTP POST request
  int httpResponseCode = http.GET();

  String payload = "--";

  if (httpResponseCode > 0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;

Hope anyone can help me with that :smiley:

Hola,
a ver si te sirve este enlace.

Hi,
See if this link works for you.

ESP8266 Client-Server Wi-Fi Communication Between Two Boards (NodeMCU) | Random Nerd Tutorials.

for communication between two (or more) ESP devices have a look at ESP-NOW which is very simple to use - also if required devices can connect to 'normal' WiFi

Hello @spartanoli
You might find this useful: Link to a very basic tutorial about TCP (evolved to: TCP-socket democode to send / receive data / characters similar to serial

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