MDM331AID/Experiment 5
Wi-Fi and TCP/IP Communication Using ESP32 and DHT11
Aim
To connect an ESP32 to a Wi-Fi network and transmit temperature and humidity data from a DHT11 sensor to a remote computer using TCP/IP communication.
Components Required
| Sr. No. | Component | Quantity |
|---|---|---|
| 1 | ESP32 Development Board | 1 |
| 2 | DHT11 Sensor | 1 |
| 3 | USB Data Cable | 1 |
| 4 | Patch Cords | As required |
| 5 | Computer with Arduino IDE | 1 |
| 6 | Remote Computer | 1 |
Theory
An IoT device collects data using sensors and transmits it to other devices through a communication network.
Wi-Fi enables wireless connectivity between the ESP32 and the local network.
TCP/IP is a communication protocol suite used for data exchange between devices.
- TCP: Provides reliable and ordered data transmission.
- IP: Provides addressing and routing of data packets.
- TCP Client: Initiates a connection and sends data.
- TCP Server: Listens for incoming connections and receives data.
The DHT11 sensor measures temperature and humidity. The ESP32 reads these values and transmits them to a remote computer.
Pin Connections
| ESP32 Pin | DHT11 Pin | Description |
|---|---|---|
| 3V3 | VCC | Power supply |
| GPIO 4 | DATA | Sensor data |
| GND | GND | Ground |
Connection Diagram
DHT11 Sensor
|
| DATA
|
GPIO 4
|
ESP32
|
Wi-Fi
|
Remote Computer
|
TCP Server
Program 1: Python TCP Server
Run this program on the remote computer.
<syntaxhighlight lang="python"> import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 5000)) server.listen(1)
print("Waiting for connection...")
while True:
conn, addr = server.accept()
print("Connected:", addr)
with conn:
while True:
data = conn.recv(1024)
if not data:
break
print(data.decode().strip())
</syntaxhighlight>
Program 2: ESP32 TCP Client
Install the DHT sensor library in Arduino IDE before uploading the program.
Replace the Wi-Fi name, password, and server IP address with your own network details.
<syntaxhighlight lang="cpp">
- include <WiFi.h>
- include <DHT.h>
- define DHTPIN 4
- define DHTTYPE DHT11
const char* ssid = "YOUR_WIFI_NAME"; const char* password = "YOUR_WIFI_PASSWORD";
const char* serverIP = "192.168.1.100"; const int serverPort = 5000;
DHT dht(DHTPIN, DHTTYPE); WiFiClient client;
void setup() {
Serial.begin(115200); dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Wi-Fi Connected");
Serial.println(WiFi.localIP());
}
void loop() {
if (!client.connected()) {
if (!client.connect(serverIP, serverPort)) {
Serial.println("Server connection failed");
delay(3000);
return;
}
}
float t = dht.readTemperature(); float h = dht.readHumidity();
if (!isnan(t) && !isnan(h)) {
client.print("Temperature: ");
client.print(t);
client.print(" C, Humidity: ");
client.print(h);
client.println(" %");
Serial.println("Data transmitted");
}
delay(3000);
} </syntaxhighlight>
Procedure
- Connect the DHT11 sensor to the ESP32.
- Connect the ESP32 to the computer using a USB cable.
- Open Arduino IDE and select the appropriate ESP32 board and COM port.
- Install the DHT sensor library.
- Enter the Wi-Fi credentials and remote computer IP address.
- Run the Python TCP server on the remote computer.
- Upload the ESP32 program.
- Open the Serial Monitor at 115200 baud.
- Observe the temperature and humidity readings transmitted to the remote computer.
Expected Output
ESP32 Serial Monitor
Wi-Fi Connected 192.168.1.105 Data transmitted Data transmitted Data transmitted
Remote Computer
Waiting for connection...
Connected: ('192.168.1.105', 50000)
Temperature: 28.0 C, Humidity: 65.0 %
Temperature: 28.0 C, Humidity: 64.0 %
The values shown are illustrative examples.
Result
The ESP32 was configured to connect to a Wi-Fi network and transmit DHT11 temperature and humidity readings to a remote computer using TCP/IP communication.
Viva Questions
- What is Wi-Fi?
- What is TCP/IP?
- What is the difference between TCP and UDP?
- What is a TCP client?
- What is a TCP server?
- What is the role of an IP address?
- What is the function of the DHT11 sensor?
- What is the purpose of the WiFiClient class?
- Why is TCP considered reliable?
- How can sensor data be sent to an IoT cloud platform?