I have been trying to communicate between two ESP32 boards where one is the server and the other is the client. I am taking reading of temperature from DHT11 by ESP32 Server and want to send these readings to the ESP32 Client. The task is to send last 5 readings to the Client where it would find the minimum reading among the 5 readings and return the value to the server. But currently I can only send a single reading and do not know how to send 5 readings as an array to the client. Can someone help me with this?
Code on ESP32 Server
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include "DHT.h"
#define DHTPIN 32
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float t = 0;
const char* ssid = "XXXXX";
const char* password = "XXXXXX";
AsyncWebServer server(80);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid,password);
while (WiFi.status()!= WL_CONNECTED){
delay(200);
Serial.println("Connecting to Wifi...");
}
Serial.println("Connected to Wifi");
Serial.println(WiFi.localIP());
server.on("/test", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("Request received from esp32client");
Serial.println(request->client()->remoteIP());
request->send(200, "text/plain", String(t));
});
server.begin();
}
void loop() {
// put your main code here, to run repeatedly:
t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// print the result to Terminal
Serial.print("Temperature: ");
Serial.print(t);
Serial.println();
delay(2000);
}
Code on ESP32 Client
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "XXXX";
const char* password ="XXXXXX";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid,password);
while (WiFi.status()!= WL_CONNECTED){
delay(500);
Serial.println("Connecting to Wifi...");
}
Serial.println("Connected to Wifi Network...");
Serial.println(WiFi.localIP());
}
void loop() {
HTTPClient http;
http.begin("http://192.168.43.35/test");
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end();
delay(3000);
}