Jump to content

MDM331AID/Experiment 5: Difference between revisions

From cognifront
CogWikiEditor (talk | contribs)
No edit summary
CogWikiEditor (talk | contribs)
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
= Experiment: Wi-Fi Connectivity and TCP/IP-Based Sensor Data Transmission Using ESP32 =
= Experiment: Wi-Fi Connectivity and TCP/IP-Based Sensor Data Transmission Using ESP32/Raspberry Pi =


== Aim ==
== Aim ==


To configure and implement an IoT device using ESP32 or Raspberry Pi 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.
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 ==
== Objectives ==


* To understand Wi-Fi connectivity in IoT devices.
* To understand Wi-Fi connectivity in IoT devices.
* To interface a DHT11 temperature and humidity sensor with ESP32 or Raspberry Pi.
* To interface a DHT11 temperature and humidity sensor with ESP32.
* To understand TCP/IP communication and socket programming.
* To understand TCP/IP communication and socket programming.
* To establish a TCP connection between an IoT device and a remote computer.
* 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.
* To transmit and receive sensor data over a Wi-Fi network.


Line 23: Line 24:
|-
|-
| 1
| 1
| ESP32 Development Board (COG-E32) or Raspberry Pi 4 (COG-RPI4)
| ESP32 Development Board (COG-E32)
| 1
| 1
|-
|-
Line 31: Line 32:
|-
|-
| 3
| 3
| USB data cable or Raspberry Pi power supply
| USB Data Cable
| 1
| 1
|-
|-
| 4
| 4
| Patch cords
| Patch Cords
| As required
| As required
|-
|-
| 5
| 5
| Computer with Arduino IDE or Python
| Computer with Arduino IDE
| 1
| 1
|-
|-
| 6
| 6
| Remote computer connected to the same Wi-Fi network
| Remote Computer with Python 3
| 1
| 1
|-
|-
| 7
| 7
| Wi-Fi network
| Wi-Fi Network
| 1
| 1
|}
|}
Line 55: Line 56:
=== Internet of Things (IoT) Device ===
=== 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.
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 or Raspberry Pi acts as the IoT device. The DHT11 sensor collects temperature and humidity readings, which are transmitted to a remote computer.
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 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.
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 has built-in Wi-Fi connectivity. The Raspberry Pi 4 also supports Wi-Fi connectivity.
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 Communication ===


TCP/IP is a protocol suite used for communication between devices over a network.
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.


* '''IP (Internet Protocol):''' Provides addressing and routing of data packets between devices.
The TCP client initiates a connection to the server. In this experiment, the ESP32 is the TCP client.
* '''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 or Raspberry Pi acts as a TCP client and sends sensor readings to the server.
The communication flow is:
 
<pre>
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
</pre>


=== DHT11 Sensor ===
=== DHT11 Sensor ===


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


Typical measurement ranges are:
Typical specifications include:


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


The DHT11 is suitable for basic environmental monitoring demonstrations. Its accuracy and measurement range are limited compared with more advanced sensors.
The DHT11 is suitable for basic IoT and environmental monitoring experiments.


=== System Architecture ===
== System Architecture ==


<pre>
<pre>
      DHT11 Sensor
                DHT11
            |
                  |
            v
            Temperature
    +----------------+
            & Humidity
    | ESP32 /        |
                  |
    | Raspberry Pi   |
                  v
    +----------------+
          +----------------+
            |
          |     ESP32     |
        Wi-Fi Network
          |   TCP Client   |
            |
          +----------------+
            v
                  |
    +----------------+
                  | Wi-Fi
    | Remote Computer|
                  v
    | TCP Server     |
          +----------------+
    +----------------+
          | Wi-Fi Router  |
            |
          +----------------+
            v
                  |
    Display Sensor Data
                  | TCP/IP
                  v
          +----------------+
          | Remote Computer|
          | Python Server |
          +----------------+
                  |
                  v
          Temperature &
          Humidity Display
</pre>
</pre>


== Pin Connections ==
== Pin Connections ==
=== ESP32 Connections ===


{| class="wikitable"
{| class="wikitable"
Line 125: Line 179:
| GPIO 4
| GPIO 4
| DATA
| DATA
| Digital data interface
| Digital data connection
|-
|-
| GND
| GND
Line 132: Line 186:
|}
|}


=== Raspberry Pi Connections ===
'''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.


{| class="wikitable"
{| class="wikitable"
|-
|-
! Raspberry Pi Pin
! Program
! DHT11 Pin
! 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:'''
 
[[Media:IoT_TCP_DHT11_Programs.zip|Download ESP32 and Python TCP/IP Programs]]
 
The ZIP file contains:
 
<pre>
IoT_TCP_DHT11_Programs.zip
|
+-- tcp_server.py
|
+-- ESP32_DHT11_TCP_Client
    |
    +-- ESP32_DHT11_TCP_Client.ino
</pre>
 
=== 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:
 
<pre>
tcp_server.py
</pre>
 
=== 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:
 
<pre>
ESP32_DHT11_TCP_Client/ESP32_DHT11_TCP_Client.ino
</pre>
 
== Program Configuration ==
 
Before uploading the ESP32 program, configure the following parameters in the downloaded Arduino program.
 
{| class="wikitable"
|-
! Parameter
! Description
! Description
! Example
|-
| Wi-Fi SSID
| Name of the Wi-Fi network
| MyWiFi
|-
|-
| Physical Pin 1 (3.3V)
| Wi-Fi Password
| VCC
| Password of the Wi-Fi network
| Sensor power supply
| mypassword123
|-
| Server IP Address
| IPv4 address of the remote computer running the Python server
| 192.168.1.100
|-
|-
| Physical Pin 7 (GPIO4)
| Server Port
| DATA
| TCP port used by the Python server
| Digital data interface
| 5000
|-
|-
| Physical Pin 6 (GND)
| DHT Pin
| GND
| ESP32 GPIO connected to DHT11 DATA
| Common ground
| GPIO 4
|}
|}


'''Note:''' The connections assume a compatible DHT11 module with clearly identified VCC, DATA, and GND pins. Verify the actual sensor-board pinout. Raspberry Pi GPIO operates at 3.3V and is not 5V tolerant. Use only a data interface with suitable 3.3V logic levels.
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 ==


== Software Requirements ==
=== Step 1: Connect the Hardware ===
 
# Connect the DHT11 sensor to the ESP32 according to the pin connection table.
# Connect the ESP32 to the computer using the USB data cable.
# 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 ====
 
# Connect the remote computer to the required Wi-Fi network.
# Open '''Command Prompt'''.
# Type the following command:
 
<pre>
ipconfig
</pre>
 
# Press Enter.
# Locate the '''Wireless LAN adapter Wi-Fi''' section.
# Find the '''IPv4 Address''' entry.
 
Example:
 
<pre>
Wireless LAN adapter Wi-Fi:
 
  IPv4 Address. . . . . . . . . . . : 192.168.1.100
</pre>
 
In this example, the remote computer IP address is:
 
<pre>
192.168.1.100
</pre>
 
# Record this address.
# Enter this address as the TCP server IP address in the ESP32 program.


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


* Arduino IDE
==== Linux ====
* ESP32 board support package
* DHT sensor library
* Wi-Fi library included with the ESP32 board package


=== Raspberry Pi ===
Open the Terminal and use:


* Raspberry Pi OS
<pre>
* Python 3
hostname -I
* Adafruit CircuitPython DHT library
</pre>
* Blinka library
* TCP socket module (Python standard library)


=== Remote Computer ===
or:


* Python 3
<pre>
* TCP server program
ip addr
* Network access from the IoT device
</pre>


Identify the IPv4 address of the Wi-Fi interface.


== Program 1: TCP Server on Remote Computer ==
Example:


The remote computer acts as a TCP server. It listens on port 5000 and displays the sensor data received from the IoT device.
<pre>
192.168.1.100
</pre>


Download the program: [[Media:IoT_TCP_DHT11_Programs.zip|TCP Server and IoT Client Programs ZIP]]
Use this address as the TCP server IP address in the ESP32 program.


== Program 2: ESP32 TCP Client ==
=== Step 3: Download the Programs ===


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 ZIP file:


Download the program: [[Media:IoT_TCP_DHT11_Programs.zip|ESP32 TCP Client Program]]
[[Media:IoT_TCP_DHT11_Programs.zip|Download ESP32 and Python TCP/IP Programs]]


== Program 3: Raspberry Pi TCP Client ==
# Extract the ZIP file.
# Verify that the following files are available:


The Raspberry Pi acts as a TCP client. It reads the DHT11 sensor and transmits the data to the remote computer.
<pre>
tcp_server.py


Download the program: [[Media:IoT_TCP_DHT11_Programs.zip|Raspberry Pi TCP Client Program]]
ESP32_DHT11_TCP_Client/
== Procedure ==
    ESP32_DHT11_TCP_Client.ino
</pre>


=== Step 1: Configure the Remote Computer ===
=== Step 4: Configure the Python TCP Server ===


# Connect the remote computer to the Wi-Fi network.
# Open the extracted '''tcp_server.py''' file.
# Find its local IP address using the appropriate network command.
# The Python server uses TCP port 5000.
# Replace the example IP address in the IoT client program with the remote computer's actual IP address.
# Open Command Prompt or Terminal.
# Save the TCP server program as tcp_server.py.
# Navigate to the folder containing '''tcp_server.py'''.
# Run the server using Python:
# Run the following command:


<syntaxhighlight lang="bash">
<pre>
python tcp_server.py
python tcp_server.py
</syntaxhighlight>
</pre>
 
If required, use:
 
<pre>
python3 tcp_server.py
</pre>


# Ensure that the firewall allows incoming TCP connections on port 5000.
The expected output is:


=== Step 2: Configure ESP32 ===
<pre>
----------------------------------
ESP32 DHT11 TCP Receiver
----------------------------------
TCP Server started
Listening on port: 5000
Waiting for ESP32 connection...
</pre>
 
Keep the Python TCP server running during the experiment.
 
=== Step 5: Configure the ESP32 Program ===
 
# Open Arduino IDE.
# Open the file '''ESP32_DHT11_TCP_Client.ino''' from the downloaded ZIP file.
# Enter the Wi-Fi network name.
# Enter the Wi-Fi password.
# Enter the IPv4 address of the remote computer.
# Verify that the TCP port is set to 5000.
# Verify that the DHT11 DATA pin is configured as GPIO 4.
# Select the appropriate ESP32 board.
# Select the correct COM port.
 
=== Step 6: Install Required Arduino Libraries ===


# Connect the DHT11 sensor to the ESP32 using the specified pin connections.
# Open Arduino IDE.
# Open Arduino IDE.
# Install the DHT sensor library if it is not already installed.
# Open Library Manager.
# Enter the Wi-Fi SSID, password, and remote computer IP address in the program.
# Search for '''DHT sensor library'''.
# Select the appropriate ESP32 board and COM port.
# Install the '''DHT sensor library by Adafruit'''.
# Upload the program.
# Install '''Adafruit Unified Sensor''' if required.
# Open the Serial Monitor at 115200 baud.
# Ensure that the ESP32 board package is installed.
# Observe the Wi-Fi connection status and TCP transmission messages.
 
The '''WiFi''' library is included with the ESP32 board support package.
 
=== Step 7: Upload the ESP32 Program ===
 
# Compile the ESP32 program.
# Upload the program to the ESP32.
# Open the Serial Monitor.
# Set the baud rate to '''115200'''.
# Observe the Wi-Fi connection status.
# Observe the ESP32 IP address.
# Observe the TCP connection status.
# Observe the transmitted DHT11 readings.
 
Expected output:
 
<pre>
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 %
</pre>
 
=== Step 8: Verify Data on the Remote Computer ===
 
The Python TCP server should display:
 
<pre>
ESP32 connected!
Client IP: 192.168.1.120
----------------------------------
Received: Temperature: 28.4 C, Humidity: 61.0 %
Received: Temperature: 28.5 C, Humidity: 61.0 %
</pre>
 
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 ===
 
# Confirm that the ESP32 successfully connects to Wi-Fi.
# Confirm that the ESP32 obtains an IP address.
# Confirm that the ESP32 establishes a TCP connection with the remote computer.
# Confirm that the Python server reports the ESP32 connection.
# Verify that temperature and humidity values are received.
# Compare the values transmitted by the ESP32 with the values displayed by the Python server.
# Disconnect or reset the ESP32.
# Observe the disconnection message on the Python server.
 
== TCP Communication Sequence ==
 
<pre>
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
</pre>
 
== Important Network Configuration ==
 
The following conditions must be satisfied for successful communication.
 
{| class="wikitable"
|-
! 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
|}


=== Step 3: Configure Raspberry Pi (Alternative) ===
'''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.


# Connect the DHT11 sensor to the Raspberry Pi.
== Windows Firewall Configuration ==
# Ensure that Raspberry Pi OS is connected to the same Wi-Fi network as the remote computer.
# Install the required Python libraries in the appropriate Python environment.
# Enter the remote computer's IP address in the Python client program.
# Run the program:


<syntaxhighlight lang="bash">
If the ESP32 cannot connect to the Python server, Windows Firewall may be blocking incoming connections.
python tcp_client.py
</syntaxhighlight>


# Observe the sensor readings on the Raspberry Pi terminal and the remote computer.
# Open '''Windows Security'''.
# Select '''Firewall & network protection'''.
# Select '''Advanced settings'''.
# Select '''Inbound Rules'''.
# Create a new inbound rule.
# Select '''Port'''.
# Select '''TCP'''.
# Enter port '''5000'''.
# Select '''Allow the connection'''.
# Apply the rule to the appropriate network profile.
# Give the rule a suitable name such as '''ESP32 TCP Server Port 5000'''.


=== Step 4: Verify TCP/IP Communication ===
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.


# Check that the remote computer reports an incoming TCP connection.
== Troubleshooting ==
# Observe the temperature and humidity data received by the server.
 
# Compare the transmitted values with the sensor readings displayed by the IoT device.
{| class="wikitable"
# Stop the client program and observe the disconnection message on the server.
|-
! 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 ==
== Observations ==
Line 249: Line 638:
|-
|-
! Sr. No.
! Sr. No.
! Temperature (°C)
! Temperature (C)
! Relative Humidity (%)
! Relative Humidity (percent)
! Transmission Status
! TCP Transmission Status
|-
|-
| 1
| 1
Line 264: Line 653:
|-
|-
| 3
| 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
| Record measured value
| Record measured value
Line 269: Line 668:
|}
|}


'''Expected output on the remote computer:'''
== Expected Output ==
 
=== ESP32 Serial Monitor ===


<pre>
<pre>
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 %
</pre>
=== Remote Computer ===
<pre>
----------------------------------
ESP32 DHT11 TCP Receiver
----------------------------------
TCP Server started
TCP Server started
Waiting for IoT device connection...
Listening on port: 5000
Connected by: ('192.168.1.105', 54321)
Waiting for ESP32 connection...
Received: Temperature: 28.0 C, Humidity: 65.0 %
 
Received: Temperature: 28.0 C, Humidity: 64.0 %
ESP32 connected!
Received: Temperature: 29.0 C, Humidity: 64.0 %
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 %
</pre>
</pre>


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


== Result ==
== Result ==


An IoT application was implemented using an ESP32 or Raspberry Pi and a DHT11 sensor. The device connected to a Wi-Fi network and transmitted temperature and humidity readings to a remote computer using TCP/IP socket communication.
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 ==
== Viva Questions ==


# What is the role of Wi-Fi in an IoT application?
# What is the role of Wi-Fi in this experiment?
# What is TCP?
# What is the difference between TCP and UDP?
# What is the difference between TCP and UDP?
# What is an IP address?
# What is an IP address?
# How do you find the IPv4 address of a Windows computer?
# Why is the remote computer's IPv4 address entered in the ESP32 program?
# What is a TCP socket?
# What is a TCP socket?
# What is the difference between a TCP client and a TCP server?
# What is the difference between a TCP client and a TCP server?
# Why is TCP considered a reliable communication protocol?
# Why is the ESP32 configured as a TCP client?
# What is the function of the bind() function in Python socket programming?
# Why is the remote computer configured as a TCP server?
# What is the purpose of the listen() and accept() functions?
# Why is TCP considered a reliable protocol?
# What is the function of sendall() in Python?
# What is the purpose of the '''bind()''' function?
# Why should the remote computer's IP address be configured in the IoT device?
# What is the purpose of the '''listen()''' function?
# What is the difference between local and public IP addresses?
# What is the purpose of the '''accept()''' function?
# How can the application be extended to store sensor readings in a database or cloud platform?
# What is the purpose of the '''recv()''' function?
# What is the purpose of '''client.connect()''' in the ESP32 program?
# Why must the TCP port number be the same in both programs?
# Why is port 5000 used in this experiment?
# What happens if Windows Firewall blocks port 5000?
# Why should the ESP32 and remote computer normally be connected to the same Wi-Fi network?
# What is the purpose of '''WiFi.localIP()'''?
# What is the difference between the ESP32 IP address and the remote computer IP address?
# How can this experiment be extended to store sensor readings in a database?
# How can this experiment be extended to send data to a cloud platform?
# What are the advantages of using TCP for sensor data transmission?

Latest revision as of 00:54, 8 October 2026

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?