Jump to content

MDM331AID/Experiment 5

From cognifront
Revision as of 17:07, 2 October 2026 by CogWikiEditor (talk | contribs) (Created page with " = 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 == {| class="wikitable" |- ! 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 |- |...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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">

  1. include <WiFi.h>
  2. include <DHT.h>
  1. define DHTPIN 4
  2. 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

  1. Connect the DHT11 sensor to the ESP32.
  2. Connect the ESP32 to the computer using a USB cable.
  3. Open Arduino IDE and select the appropriate ESP32 board and COM port.
  4. Install the DHT sensor library.
  5. Enter the Wi-Fi credentials and remote computer IP address.
  6. Run the Python TCP server on the remote computer.
  7. Upload the ESP32 program.
  8. Open the Serial Monitor at 115200 baud.
  9. 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

  1. What is Wi-Fi?
  2. What is TCP/IP?
  3. What is the difference between TCP and UDP?
  4. What is a TCP client?
  5. What is a TCP server?
  6. What is the role of an IP address?
  7. What is the function of the DHT11 sensor?
  8. What is the purpose of the WiFiClient class?
  9. Why is TCP considered reliable?
  10. How can sensor data be sent to an IoT cloud platform?