Jump to content

MDM331AID/Experiment 5

From cognifront

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 configure a computer as a TCP server.
  • To configure ESP32 as a TCP client.
  • 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 with Python 3 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, computer, or remote server.

In this experiment, the ESP32 acts as the IoT device. The DHT11 sensor measures temperature and relative humidity. The ESP32 processes these readings and transmits them to a remote computer through a Wi-Fi network using TCP/IP communication.

Wi-Fi Connectivity

Wi-Fi is a wireless networking technology based on IEEE 802.11 standards. It allows devices to communicate over a wireless local area network.

The ESP32 contains built-in Wi-Fi capability. Therefore, no separate Wi-Fi module is required.

In this experiment, both the ESP32 and the remote computer should be connected to the same Wi-Fi network so that the ESP32 can establish a TCP connection with the computer.

TCP/IP Communication

TCP/IP is a protocol suite used for communication between networked devices.

  • IP (Internet Protocol): Provides addressing and routing of packets between devices.
  • TCP (Transmission Control Protocol): Provides reliable, ordered and connection-oriented communication.
  • Port: A logical communication endpoint used to identify a particular application or service.
  • Socket: A software interface used by applications to establish network communication.

In this experiment:

  • The remote computer operates as the TCP server.
  • The ESP32 operates as the TCP client.
  • The Python program listens for incoming TCP connections.
  • The ESP32 connects to the IP address and port of the remote computer.
  • The DHT11 readings are transmitted through the established TCP connection.

TCP Client and TCP Server

The TCP server waits for incoming client connections. In this experiment, the Python program running on the remote computer is the TCP server.

The TCP client initiates a connection to the server. In this experiment, the ESP32 is the TCP client.

The communication flow is:

DHT11 Sensor
      |
      | Temperature and Humidity
      v
+------------------+
|      ESP32       |
|    TCP Client    |
+------------------+
      |
      | Wi-Fi
      v
+------------------+
|  Wi-Fi Network   |
+------------------+
      |
      | TCP/IP
      v
+------------------+
| Remote Computer  |
|   TCP Server     |
|  Python Program  |
+------------------+
      |
      v
Display Sensor Data

DHT11 Sensor

The DHT11 is a digital temperature and humidity sensor. It provides temperature and relative humidity measurements through a digital data interface.

Typical specifications include:

  • Temperature measurement range: 0 °C to 50 °C
  • Relative humidity measurement range: 20% to 90%
  • Digital single-wire data interface

The DHT11 is suitable for basic IoT and environmental monitoring experiments.

System Architecture

                 DHT11
                   |
             Temperature
             & Humidity
                   |
                   v
          +----------------+
          |     ESP32      |
          |   TCP Client   |
          +----------------+
                   |
                   | Wi-Fi
                   v
          +----------------+
          | Wi-Fi Router   |
          +----------------+
                   |
                   | TCP/IP
                   v
          +----------------+
          | Remote Computer|
          |  Python Server |
          +----------------+
                   |
                   v
          Temperature &
          Humidity Display

Pin Connections

ESP32 Pin DHT11 Pin Description
3V3 VCC Sensor power supply
GPIO 4 DATA Digital data connection
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
  • Adafruit Unified Sensor library
  • Wi-Fi library included with the ESP32 board package

Remote Computer

  • Python 3
  • Wi-Fi connection
  • TCP/IP network connectivity
  • TCP port 5000 available for the experiment

The Python socket module is included with Python 3. No additional Python package is required.

Programs

Two programs are required for this experiment.

Program Platform Function
Program 1: TCP Server Remote Computer Receives temperature and humidity data from ESP32.
Program 2: ESP32 TCP Client ESP32 Connects to Wi-Fi, reads DHT11 data and transmits it to the TCP server.

Download Programs

The complete source code for both programs is provided in a single ZIP file.

Download:

Download ESP32 and Python TCP/IP Programs

The ZIP file contains:

IoT_TCP_DHT11_Programs.zip
|
+-- tcp_server.py
|
+-- ESP32_DHT11_TCP_Client
    |
    +-- ESP32_DHT11_TCP_Client.ino

Program 1: Python TCP Server

The Python TCP Server runs on the remote computer.

Its functions are:

  • Creates a TCP socket.
  • Listens for incoming connections on port 5000.
  • Accepts the connection from the ESP32.
  • Receives sensor data transmitted by the ESP32.
  • Displays the received temperature and humidity values.
  • Detects when the ESP32 disconnects.

The Python program is provided in the downloaded ZIP file as:

tcp_server.py

Program 2: ESP32 TCP Client

The ESP32 TCP Client runs on the ESP32.

Its functions are:

  • Connects the ESP32 to the specified Wi-Fi network.
  • Reads temperature from the DHT11 sensor.
  • Reads relative humidity from the DHT11 sensor.
  • Establishes a TCP connection with the remote computer.
  • Transmits temperature and humidity data to the Python TCP server.
  • Displays Wi-Fi, TCP and sensor information on the Serial Monitor.

The Arduino program is provided in the downloaded ZIP file as:

ESP32_DHT11_TCP_Client/ESP32_DHT11_TCP_Client.ino

Program Configuration

Before uploading the ESP32 program, configure the following parameters in the downloaded Arduino program.

Parameter Description Example
Wi-Fi SSID Name of the Wi-Fi network MyWiFi
Wi-Fi Password Password of the Wi-Fi network mypassword123
Server IP Address IPv4 address of the remote computer running the Python server 192.168.1.100
Server Port TCP port used by the Python server 5000
DHT Pin ESP32 GPIO connected to DHT11 DATA GPIO 4

The Wi-Fi SSID, Wi-Fi password and Server IP Address must be changed according to the actual laboratory setup.

The TCP port must remain the same in both programs.

Procedure

Step 1: Connect the Hardware

  1. Connect the DHT11 sensor to the ESP32 according to the pin connection table.
  2. Connect the ESP32 to the computer using the USB data cable.
  3. Connect the remote computer to the Wi-Fi network.

Step 2: Find the IP Address of the Remote Computer

The IP address of the remote computer is required because the ESP32 must know where the TCP server is located.

Windows

  1. Connect the remote computer to the required Wi-Fi network.
  2. Open Command Prompt.
  3. Type the following command:
ipconfig
  1. Press Enter.
  2. Locate the Wireless LAN adapter Wi-Fi section.
  3. Find the IPv4 Address entry.

Example:

Wireless LAN adapter Wi-Fi:

   IPv4 Address. . . . . . . . . . . : 192.168.1.100

In this example, the remote computer IP address is:

192.168.1.100
  1. Record this address.
  2. Enter this address as the TCP server IP address in the ESP32 program.

Important: Use the IPv4 Address of the remote computer. Do not use the Default Gateway address.

Linux

Open the Terminal and use:

hostname -I

or:

ip addr

Identify the IPv4 address of the Wi-Fi interface.

Example:

192.168.1.100

Use this address as the TCP server IP address in the ESP32 program.

Step 3: Download the Programs

  1. Download the program ZIP file:

Download ESP32 and Python TCP/IP Programs

  1. Extract the ZIP file.
  2. Verify that the following files are available:
tcp_server.py

ESP32_DHT11_TCP_Client/
    ESP32_DHT11_TCP_Client.ino

Step 4: Configure the Python TCP Server

  1. Open the extracted tcp_server.py file.
  2. The Python server uses TCP port 5000.
  3. Open Command Prompt or Terminal.
  4. Navigate to the folder containing tcp_server.py.
  5. Run the following command:
python tcp_server.py

If required, use:

python3 tcp_server.py

The expected output is:

----------------------------------
ESP32 DHT11 TCP Receiver
----------------------------------
TCP Server started
Listening on port: 5000
Waiting for ESP32 connection...

Keep the Python TCP server running during the experiment.

Step 5: Configure the ESP32 Program

  1. Open Arduino IDE.
  2. Open the file ESP32_DHT11_TCP_Client.ino from the downloaded ZIP file.
  3. Enter the Wi-Fi network name.
  4. Enter the Wi-Fi password.
  5. Enter the IPv4 address of the remote computer.
  6. Verify that the TCP port is set to 5000.
  7. Verify that the DHT11 DATA pin is configured as GPIO 4.
  8. Select the appropriate ESP32 board.
  9. Select the correct COM port.

Step 6: Install Required Arduino Libraries

  1. Open Arduino IDE.
  2. Open Library Manager.
  3. Search for DHT sensor library.
  4. Install the DHT sensor library by Adafruit.
  5. Install Adafruit Unified Sensor if required.
  6. Ensure that the ESP32 board package is installed.

The WiFi library is included with the ESP32 board support package.

Step 7: Upload the ESP32 Program

  1. Compile the ESP32 program.
  2. Upload the program to the ESP32.
  3. Open the Serial Monitor.
  4. Set the baud rate to 115200.
  5. Observe the Wi-Fi connection status.
  6. Observe the ESP32 IP address.
  7. Observe the TCP connection status.
  8. Observe the transmitted DHT11 readings.

Expected output:

ESP32 DHT11 TCP/IP Client
----------------------------------
Connecting to Wi-Fi....
Wi-Fi connected!
ESP32 IP Address: 192.168.1.120

Connecting to TCP server: 192.168.1.100:5000
TCP server connected!

Sent: Temperature: 28.4 C, Humidity: 61.0 %
Sent: Temperature: 28.5 C, Humidity: 61.0 %

Step 8: Verify Data on the Remote Computer

The Python TCP server should display:

ESP32 connected!
Client IP: 192.168.1.120
----------------------------------
Received: Temperature: 28.4 C, Humidity: 61.0 %
Received: Temperature: 28.5 C, Humidity: 61.0 %

The IP addresses and sensor readings shown above are illustrative examples. Actual values will depend on the network and environment.

Step 9: Verify TCP/IP Communication

  1. Confirm that the ESP32 successfully connects to Wi-Fi.
  2. Confirm that the ESP32 obtains an IP address.
  3. Confirm that the ESP32 establishes a TCP connection with the remote computer.
  4. Confirm that the Python server reports the ESP32 connection.
  5. Verify that temperature and humidity values are received.
  6. Compare the values transmitted by the ESP32 with the values displayed by the Python server.
  7. Disconnect or reset the ESP32.
  8. Observe the disconnection message on the Python server.

TCP Communication Sequence

1. Start Python TCP Server
             |
             v
2. Server listens on TCP port 5000
             |
             v
3. ESP32 connects to Wi-Fi
             |
             v
4. ESP32 connects to Remote Computer
             |
             v
5. TCP connection established
             |
             v
6. ESP32 reads DHT11
             |
             v
7. ESP32 transmits sensor data
             |
             v
8. Python server receives data
             |
             v
9. Temperature and humidity displayed

Important Network Configuration

The following conditions must be satisfied for successful communication.

Parameter Requirement
ESP32 Wi-Fi Connected to the Wi-Fi network
Remote Computer Connected to the same Wi-Fi or LAN
Server IP Must be the IPv4 address of the remote computer
TCP Port 5000
Python Server Must be running before the ESP32 attempts connection
Firewall Must allow TCP communication on port 5000

Important: The IP address of the remote computer may change when it reconnects to the Wi-Fi network. If the ESP32 cannot connect after a network change, check the computer's IPv4 address again using ipconfig and update the ESP32 program if necessary.

Windows Firewall Configuration

If the ESP32 cannot connect to the Python server, Windows Firewall may be blocking incoming connections.

  1. Open Windows Security.
  2. Select Firewall & network protection.
  3. Select Advanced settings.
  4. Select Inbound Rules.
  5. Create a new inbound rule.
  6. Select Port.
  7. Select TCP.
  8. Enter port 5000.
  9. Select Allow the connection.
  10. Apply the rule to the appropriate network profile.
  11. Give the rule a suitable name such as ESP32 TCP Server Port 5000.

Alternatively, Windows may display a firewall permission dialog when Python first attempts to accept a network connection. Allow Python to communicate on the appropriate network.

Troubleshooting

Problem Possible Cause Solution
ESP32 cannot connect to Wi-Fi Incorrect SSID or password Verify the Wi-Fi name and password.
ESP32 connects to Wi-Fi but TCP connection fails Incorrect server IP Run ipconfig and verify the computer's IPv4 address.
Python server does not receive data Server not running Start python tcp_server.py.
Connection refused TCP server is not listening Start the Python TCP server and verify port 5000.
Connection timeout Firewall blocking TCP communication Allow TCP port 5000 through the firewall.
DHT11 reading fails Incorrect sensor connection Check VCC, DATA, GND and GPIO 4 connections.
Devices cannot communicate Different Wi-Fi networks Connect ESP32 and remote computer to the same network.
Wrong IP address Gateway address used instead of computer IP Use the computer's IPv4 Address shown by ipconfig.
Python command not recognized Python is not installed or not added to PATH Install Python 3 and add Python to PATH.

Observations

Sr. No. Temperature (C) Relative Humidity (percent) TCP 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
4 Record measured value Record measured value Successful / Failed
5 Record measured value Record measured value Successful / Failed

Expected Output

ESP32 Serial Monitor

ESP32 DHT11 TCP/IP Client
----------------------------------
Connecting to Wi-Fi....
Wi-Fi connected!
ESP32 IP Address: 192.168.1.120

Connecting to TCP server: 192.168.1.100:5000
TCP server connected!

Sent: Temperature: 28.4 C, Humidity: 61.0 %
Sent: Temperature: 28.5 C, Humidity: 61.0 %
Sent: Temperature: 28.5 C, Humidity: 62.0 %

Remote Computer

----------------------------------
ESP32 DHT11 TCP Receiver
----------------------------------
TCP Server started
Listening on port: 5000
Waiting for ESP32 connection...

ESP32 connected!
Client IP: 192.168.1.120
----------------------------------
Received: Temperature: 28.4 C, Humidity: 61.0 %
Received: Temperature: 28.5 C, Humidity: 61.0 %
Received: Temperature: 28.5 C, Humidity: 62.0 %

The IP addresses and sensor readings shown above are illustrative examples and may differ during the actual experiment.

Result

An IoT application was successfully implemented using ESP32 and DHT11. The ESP32 was connected to a Wi-Fi network and configured as a TCP client. A remote computer was configured as a Python TCP server. Temperature and humidity data obtained from the DHT11 sensor was successfully transmitted from the ESP32 to the remote computer using TCP/IP socket communication.

Viva Questions

  1. What is the role of Wi-Fi in this experiment?
  2. What is TCP?
  3. What is the difference between TCP and UDP?
  4. What is an IP address?
  5. How do you find the IPv4 address of a Windows computer?
  6. Why is the remote computer's IPv4 address entered in the ESP32 program?
  7. What is a TCP socket?
  8. What is the difference between a TCP client and a TCP server?
  9. Why is the ESP32 configured as a TCP client?
  10. Why is the remote computer configured as a TCP server?
  11. Why is TCP considered a reliable protocol?
  12. What is the purpose of the bind() function?
  13. What is the purpose of the listen() function?
  14. What is the purpose of the accept() function?
  15. What is the purpose of the recv() function?
  16. What is the purpose of client.connect() in the ESP32 program?
  17. Why must the TCP port number be the same in both programs?
  18. Why is port 5000 used in this experiment?
  19. What happens if Windows Firewall blocks port 5000?
  20. Why should the ESP32 and remote computer normally be connected to the same Wi-Fi network?
  21. What is the purpose of WiFi.localIP()?
  22. What is the difference between the ESP32 IP address and the remote computer IP address?
  23. How can this experiment be extended to store sensor readings in a database?
  24. How can this experiment be extended to send data to a cloud platform?
  25. What are the advantages of using TCP for sensor data transmission?