#include <WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "your_wifi_ssid"; // Replace with your Wi-Fi credentials
const char* password = "your_wifi_password";
const char* serverIP = "192.168.1.46"; // Raspberry Pi's IP address
const int serverPort = 8888;
WiFiUDP udp;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
udp.beginPacket(serverIP, serverPort);
udp.print("Request humidity data"); // Send your desired message here
udp.endPacket();
delay(1000); // Wait for response
int packetSize = udp.parsePacket();
if (packetSize) {
char buffer[255];
int len = udp.read(buffer, sizeof(buffer));
if (len > 0) {
buffer[len] = '\0';
Serial.print("Received response: ");
Serial.println(buffer);
}
}
delay(10); // this speeds up the simulation
}
/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit [here](https://esp32io.com/tutorials/esp32-temperature-humidity-sensor)
*/
#include <DHT.h>
#define DHT_SENSOR_PIN 16 // ESP32 pin GPIO21 connected to DHT22 sensor
#define DHT_SENSOR_TYPE DHT22
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
void setup() {
Serial.begin(9600);
dht_sensor.begin(); // initialize the DHT sensor
}
void loop() {
// Read humidity
float humidity = dht_sensor.readHumidity();
// Read temperature in Celsius
float tempC = dht_sensor.readTemperature();
// Read temperature in Fahrenheit
float tempF = dht_sensor.readTemperature(true);
// Check whether the reading is successful or not
if (isnan(tempC) || isnan(tempF) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% | ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C ~ ");
Serial.print(tempF);
Serial.println("°F");
}
delay(2000);
}