MDM331AID/Experiment 5
Experiment: Wi-Fi Connectivity and TCP/IP-Based Sensor Data Transmission Using ESP32
Aim
To configure and implement an IoT device using ESP32 to connect to a Wi-Fi network and transmit temperature and humidity data from a DHT11 sensor to a remote computer using TCP/IP communication.
Objectives
- To understand Wi-Fi connectivity in IoT devices.
- To interface a DHT11 temperature and humidity sensor with ESP32.
- To understand TCP/IP communication and socket programming.
- To establish a TCP connection between an ESP32 and a remote computer.
- To transmit and receive sensor data over a Wi-Fi network.
Components Required
| Sr. No. | Component | Quantity |
|---|---|---|
| 1 | ESP32 Development Board (COG-E32) | 1 |
| 2 | DHT11 Temperature and Humidity Sensor (COG-MSBA) | 1 |
| 3 | USB Data Cable | 1 |
| 4 | Patch Cords | As required |
| 5 | Computer with Arduino IDE | 1 |
| 6 | Remote Computer connected to the same Wi-Fi network | 1 |
| 7 | Wi-Fi Network | 1 |
Theory
Internet of Things (IoT) Device
An IoT device is a physical device equipped with sensors, processing capabilities, and communication interfaces. It collects data from its surroundings and transmits the information to another device or a remote server.
In this experiment, the ESP32 acts as the IoT device. The DHT11 sensor collects temperature and humidity readings, which are transmitted to a remote computer.
Wi-Fi Connectivity
Wi-Fi is a wireless networking technology based on IEEE 802.11 standards. It allows IoT devices to connect to a local wireless network and exchange information with other devices.
The ESP32 has built-in Wi-Fi connectivity, making it suitable for IoT communication experiments without requiring an additional Wi-Fi module.
TCP/IP Communication
TCP/IP is a protocol suite used for communication between devices over a network.
- IP (Internet Protocol): Provides addressing and routing of data packets between devices.
- TCP (Transmission Control Protocol): Provides reliable, ordered, connection-oriented data delivery.
- Socket: A software interface used by applications to send and receive data over a network.
In this experiment, the remote computer runs a TCP server that listens for incoming connections. The ESP32 acts as a TCP client and sends sensor readings to the server.
The basic communication flow is:
DHT11 Sensor
|
v
ESP32
|
v
Wi-Fi Network
|
v
Remote Computer
|
v
TCP Server
DHT11 Sensor
The DHT11 is a digital temperature and humidity sensor. It provides temperature and relative humidity readings through a single digital data interface.
Typical measurement ranges are:
- Temperature: 0 C to 50 C.
- Relative humidity: 20 percent to 90 percent.
The DHT11 is suitable for basic environmental monitoring demonstrations. Its accuracy and measurement range are limited compared with more advanced sensors.
System Architecture
DHT11 Sensor
|
v
+----------------+
| ESP32 |
| TCP Client |
+----------------+
|
Wi-Fi Network
|
v
+----------------+
| Remote Computer|
| TCP Server |
+----------------+
|
v
Display Sensor
Data
Pin Connections
| ESP32 Pin | DHT11 Pin | Description |
|---|---|---|
| 3V3 | VCC | Sensor power supply |
| GPIO 4 | DATA | Digital data interface |
| GND | GND | Common ground |
Note: The connections assume a compatible DHT11 module with clearly identified VCC, DATA, and GND pins. Verify the actual sensor-board pinout before making the connections.
Software Requirements
ESP32
- Arduino IDE
- ESP32 board support package
- DHT sensor library
- Wi-Fi library included with the ESP32 board package
Remote Computer
- Python 3
- TCP server program
- Wi-Fi connection
- Network access from the ESP32
Program 1: TCP Server on Remote Computer
The remote computer acts as a TCP server. It listens on port 5000 and displays the temperature and humidity data received from the ESP32.
Download the program:
TCP Server and ESP32 Client Programs ZIP
Program 2: ESP32 TCP Client
The ESP32 connects to the Wi-Fi network, reads the DHT11 sensor values, and sends the readings to the remote computer using TCP.
Download the program:
Procedure
Step 1: Configure the Remote Computer
- Connect the remote computer to the Wi-Fi network.
- Find the local IP address of the remote computer using the appropriate network command.
- Replace the example IP address in the ESP32 client program with the remote computer's actual IP address.
- Save the TCP server program as tcp_server.py.
- Run the server using Python:
<syntaxhighlight lang="bash"> python tcp_server.py </syntaxhighlight>
- Ensure that the firewall allows incoming TCP connections on port 5000.
- Keep the TCP server running while testing the ESP32.
Step 2: Configure ESP32
- Connect the DHT11 sensor to the ESP32 using the specified pin connections.
- Open Arduino IDE.
- Install the DHT sensor library if it is not already installed.
- Enter the Wi-Fi SSID and password in the ESP32 program.
- Enter the remote computer's IP address in the ESP32 program.
- Enter the TCP server port number in the ESP32 program.
- Select the appropriate ESP32 board and COM port.
- Upload the program.
- Open the Serial Monitor at 115200 baud.
- Observe the Wi-Fi connection status.
- Observe the TCP connection status.
- Observe the temperature and humidity values being transmitted.
Step 3: Verify TCP/IP Communication
- Check that the remote computer reports an incoming TCP connection.
- Observe the temperature and humidity data received by the TCP server.
- Compare the transmitted values with the sensor readings displayed by the ESP32.
- Verify that multiple sensor readings are received by the server.
- Stop the ESP32 program or disconnect the device.
- Observe the disconnection message on the TCP server.
Observations
| Sr. No. | Temperature (C) | Relative Humidity (percent) | Transmission Status |
|---|---|---|---|
| 1 | Record measured value | Record measured value | Successful / Failed |
| 2 | Record measured value | Record measured value | Successful / Failed |
| 3 | Record measured value | Record measured value | Successful / Failed |
Expected output on the remote computer:
TCP Server started
Waiting for ESP32 connection...
Connected by: ('192.168.1.105', 54321)
Received: Temperature: 28.0 C, Humidity: 65.0 %
Received: Temperature: 28.0 C, Humidity: 64.0 %
Received: Temperature: 29.0 C, Humidity: 64.0 %
The above values and IP addresses are illustrative examples, not actual measurements.
Result
An IoT application was implemented using an ESP32 and DHT11 sensor. The ESP32 successfully connected to a Wi-Fi network and transmitted temperature and humidity readings to a remote computer using TCP/IP socket communication.
Viva Questions
- What is the role of Wi-Fi in an IoT application?
- What is TCP?
- What is the difference between TCP and UDP?
- What is an IP address?
- What is a TCP socket?
- What is the difference between a TCP client and a TCP server?
- Why is TCP considered a reliable communication protocol?
- What is the function of the bind() function in Python socket programming?
- What is the purpose of the listen() function?
- What is the purpose of the accept() function?
- What is the function of sendall() in Python?
- Why should the remote computer's IP address be configured in the ESP32?
- What is the difference between a local IP address and a public IP address?
- Why is port 5000 used in this experiment?
- How can the application be extended to store sensor readings in a database or cloud platform?