#include <WiFi.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define WIFI_SSID "Your WiFi SSID"
#define WIFI_PASSWORD "Your WiFi Password"
#define DHT_PIN 4
#define DHT_TYPE DHT11

const char* server = "YourServerAddress";
const int port = 80;

DHT_Unified dht(DHT_PIN, DHT_TYPE);

void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
 
  Serial.println("Connected to WiFi");
  dht.begin();
}

void loop() {
  delay(2000);

  sensors_event_t event;
  dht.temperature().getEvent(&event);
  float temperature = event.temperature;

  dht.humidity().getEvent(&event);
  float humidity = event.relative_humidity;

  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  WiFiClient client;
  if (client.connect(server, port)) {
    String data = "temperature=" + String(temperature) + "&humidity=" + String(humidity);
    client.println("POST /your-endpoint HTTP/1.1");
    client.println("Host: " + String(server));
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Content-Length: " + String(data.length()));
    client.println();
    client.println(data);
    client.println();

    Serial.println("Data sent to server:");
    Serial.println(data);
  }
  else {
    Serial.println("Failed to connect to server");
  }

  client.stop();
  delay(5000);
}

---------------------------------------------------------------------------------------------------------------------------------------

Make sure to replace "Your WiFi SSID", "Your WiFi Password", "YourServerAddress", and "your-endpoint" with your specific values. Additionally, ensure that you have the required libraries (Adafruit_Sensor, DHT, and DHT_U) installed in your Arduino IDE.

This code connects the ESP32 to a Wi-Fi network, reads temperature and humidity data from a DHT11 sensor, and sends the data to a server using HTTP POST request. The server endpoint should handle the incoming data and store it or perform any desired actions.

Remember to adapt the server endpoint to receive the data and process it accordingly.

Note: This is a basic example, and you can further enhance the code by adding error handling, data validation, and additional functionalities as per your project requirements.