Jump to content

MDM331AID/Experiment 5: Difference between revisions

From cognifront
CogWikiEditor (talk | contribs)
No edit summary
CogWikiEditor (talk | contribs)
No edit summary
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 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 22:
|-
|-
| 1
| 1
| ESP32 Development Board (COG-E32) or Raspberry Pi 4 (COG-RPI4)
| ESP32 Development Board (COG-E32)
| 1
| 1
|-
|-
Line 31: Line 30:
|-
|-
| 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 connected to the same Wi-Fi network
| 1
| 1
|-
|-
| 7
| 7
| Wi-Fi network
| Wi-Fi Network
| 1
| 1
|}
|}
Line 57: Line 56:
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 or a 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 collects temperature and humidity readings, which are transmitted to a remote computer.


=== Wi-Fi Connectivity ===
=== Wi-Fi Connectivity ===
Line 63: Line 62:
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 IoT devices to connect to a local wireless network and exchange information with other devices.


The ESP32 has built-in Wi-Fi connectivity. The Raspberry Pi 4 also supports Wi-Fi connectivity.
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 Communication ===
Line 73: Line 72:
* '''Socket:''' A software interface used by applications to send and receive data over a network.
* '''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.
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:
 
<pre>
DHT11 Sensor
    |
    v
  ESP32
    |
    v
Wi-Fi Network
    |
    v
Remote Computer
    |
    v
TCP Server
</pre>


=== DHT11 Sensor ===
=== DHT11 Sensor ===
Line 81: Line 98:
Typical measurement ranges are:
Typical measurement ranges are:


* Temperature: 0°C to 50°C.
* Temperature: 0 C to 50 C.
* Relative humidity: 20% to 90%.
* 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.
The DHT11 is suitable for basic environmental monitoring demonstrations. Its accuracy and measurement range are limited compared with more advanced sensors.
Line 93: Line 110:
             v
             v
     +----------------+
     +----------------+
     | ESP32 /        |
     |     ESP32     |
     | Raspberry Pi   |
     |   TCP Client   |
     +----------------+
     +----------------+
             |
             |
Line 102: Line 119:
     +----------------+
     +----------------+
     | Remote Computer|
     | Remote Computer|
     | TCP Server     |
     |   TCP Server   |
     +----------------+
     +----------------+
             |
             |
             v
             v
    Display Sensor Data
      Display Sensor
          Data
</pre>
</pre>


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


{| class="wikitable"
{| class="wikitable"
Line 132: Line 148:
|}
|}


=== 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.
 
{| class="wikitable"
|-
! Raspberry Pi Pin
! DHT11 Pin
! Description
|-
| Physical Pin 1 (3.3V)
| VCC
| Sensor power supply
|-
| Physical Pin 7 (GPIO4)
| DATA
| Digital data interface
|-
| Physical Pin 6 (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. Raspberry Pi GPIO operates at 3.3V and is not 5V tolerant. Use only a data interface with suitable 3.3V logic levels.


== Software Requirements ==
== Software Requirements ==
Line 163: Line 158:
* DHT sensor library
* DHT sensor library
* Wi-Fi library included with the ESP32 board package
* Wi-Fi library included with the ESP32 board package
=== Raspberry Pi ===
* Raspberry Pi OS
* Python 3
* Adafruit CircuitPython DHT library
* Blinka library
* TCP socket module (Python standard library)


=== Remote Computer ===
=== Remote Computer ===
Line 176: Line 163:
* Python 3
* Python 3
* TCP server program
* TCP server program
* Network access from the IoT device
* Wi-Fi connection
* Network access from the ESP32


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


== 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.


The remote computer acts as a TCP server. It listens on port 5000 and displays the sensor data received from the IoT device.
Download the program:


Download the program: [[Media:IoT_TCP_DHT11_Programs.zip|TCP Server and IoT Client Programs ZIP]]
[[Media:IoT_TCP_DHT11_Programs.zip|TCP Server and ESP32 Client Programs ZIP]]


== Program 2: ESP32 TCP Client ==
== Program 2: ESP32 TCP Client ==
Line 189: Line 178:
The ESP32 connects to the Wi-Fi network, reads the DHT11 sensor values, and sends the readings to the remote computer using TCP.
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: [[Media:IoT_TCP_DHT11_Programs.zip|ESP32 TCP Client Program]]
Download the program:
 
== Program 3: Raspberry Pi TCP Client ==


The Raspberry Pi acts as a TCP client. It reads the DHT11 sensor and transmits the data to the remote computer.
[[Media:IoT_TCP_DHT11_Programs.zip|ESP32 TCP Client Program]]


Download the program: [[Media:IoT_TCP_DHT11_Programs.zip|Raspberry Pi TCP Client Program]]
== Procedure ==
== Procedure ==


Line 201: Line 187:


# Connect the remote computer to the Wi-Fi network.
# Connect the remote computer to the Wi-Fi network.
# Find its local IP address using the appropriate network command.
# Find the local IP address of the remote computer using the appropriate network command.
# Replace the example IP address in the IoT client program with the remote computer's actual IP address.
# 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.
# Save the TCP server program as tcp_server.py.
# Run the server using Python:
# Run the server using Python:
Line 211: Line 197:


# Ensure that the firewall allows incoming TCP connections on port 5000.
# Ensure that the firewall allows incoming TCP connections on port 5000.
# Keep the TCP server running while testing the ESP32.


=== Step 2: Configure ESP32 ===
=== Step 2: Configure ESP32 ===
Line 217: Line 204:
# Open Arduino IDE.
# Open Arduino IDE.
# Install the DHT sensor library if it is not already installed.
# Install the DHT sensor library if it is not already installed.
# Enter the Wi-Fi SSID, password, and remote computer IP address in the program.
# 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.
# Select the appropriate ESP32 board and COM port.
# Upload the program.
# Upload the program.
# Open the Serial Monitor at 115200 baud.
# Open the Serial Monitor at 115200 baud.
# Observe the Wi-Fi connection status and TCP transmission messages.
# Observe the Wi-Fi connection status.
 
# Observe the TCP connection status.
=== Step 3: Configure Raspberry Pi (Alternative) ===
# Observe the temperature and humidity values being transmitted.
 
# Connect the DHT11 sensor to the Raspberry Pi.
# 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">
python tcp_client.py
</syntaxhighlight>
 
# Observe the sensor readings on the Raspberry Pi terminal and the remote computer.


=== Step 4: Verify TCP/IP Communication ===
=== Step 3: Verify TCP/IP Communication ===


# Check that the remote computer reports an incoming TCP connection.
# Check that the remote computer reports an incoming TCP connection.
# Observe the temperature and humidity data received by the server.
# Observe the temperature and humidity data received by the TCP server.
# Compare the transmitted values with the sensor readings displayed by the IoT device.
# Compare the transmitted values with the sensor readings displayed by the ESP32.
# Stop the client program and observe the disconnection message on the server.
# 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 ==
== Observations ==
Line 249: Line 228:
|-
|-
! Sr. No.
! Sr. No.
! Temperature (°C)
! Temperature (C)
! Relative Humidity (%)
! Relative Humidity (percent)
! Transmission Status
! Transmission Status
|-
|-
Line 273: Line 252:
<pre>
<pre>
TCP Server started
TCP Server started
Waiting for IoT device connection...
Waiting for ESP32 connection...
Connected by: ('192.168.1.105', 54321)
Connected by: ('192.168.1.105', 54321)
Received: Temperature: 28.0 C, Humidity: 65.0 %
Received: Temperature: 28.0 C, Humidity: 65.0 %
Line 284: Line 263:
== 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 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 ==
== Viva Questions ==


# What is the role of Wi-Fi in an IoT application?
# What is the role of Wi-Fi in an IoT application?
# 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?
Line 295: Line 275:
# Why is TCP considered a reliable communication protocol?
# Why is TCP considered a reliable communication protocol?
# What is the function of the bind() function in Python socket programming?
# What is the function of the bind() function in Python socket programming?
# What is the purpose of the listen() and accept() functions?
# What is the purpose of the listen() function?
# What is the purpose of the accept() function?
# What is the function of sendall() in Python?
# What is the function of sendall() in Python?
# Why should the remote computer's IP address be configured in the IoT device?
# Why should the remote computer's IP address be configured in the ESP32?
# What is the difference between local and public IP addresses?
# 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?
# How can the application be extended to store sensor readings in a database or cloud platform?

Revision as of 16:27, 7 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 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:

ESP32 TCP Client Program

Procedure

Step 1: Configure the Remote Computer

  1. Connect the remote computer to the Wi-Fi network.
  2. Find the local IP address of the remote computer using the appropriate network command.
  3. Replace the example IP address in the ESP32 client program with the remote computer's actual IP address.
  4. Save the TCP server program as tcp_server.py.
  5. Run the server using Python:

<syntaxhighlight lang="bash"> python tcp_server.py </syntaxhighlight>

  1. Ensure that the firewall allows incoming TCP connections on port 5000.
  2. Keep the TCP server running while testing the ESP32.

Step 2: Configure ESP32

  1. Connect the DHT11 sensor to the ESP32 using the specified pin connections.
  2. Open Arduino IDE.
  3. Install the DHT sensor library if it is not already installed.
  4. Enter the Wi-Fi SSID and password in the ESP32 program.
  5. Enter the remote computer's IP address in the ESP32 program.
  6. Enter the TCP server port number in the ESP32 program.
  7. Select the appropriate ESP32 board and COM port.
  8. Upload the program.
  9. Open the Serial Monitor at 115200 baud.
  10. Observe the Wi-Fi connection status.
  11. Observe the TCP connection status.
  12. Observe the temperature and humidity values being transmitted.

Step 3: Verify TCP/IP Communication

  1. Check that the remote computer reports an incoming TCP connection.
  2. Observe the temperature and humidity data received by the TCP server.
  3. Compare the transmitted values with the sensor readings displayed by the ESP32.
  4. Verify that multiple sensor readings are received by the server.
  5. Stop the ESP32 program or disconnect the device.
  6. 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

  1. What is the role of Wi-Fi in an IoT application?
  2. What is TCP?
  3. What is the difference between TCP and UDP?
  4. What is an IP address?
  5. What is a TCP socket?
  6. What is the difference between a TCP client and a TCP server?
  7. Why is TCP considered a reliable communication protocol?
  8. What is the function of the bind() function in Python socket programming?
  9. What is the purpose of the listen() function?
  10. What is the purpose of the accept() function?
  11. What is the function of sendall() in Python?
  12. Why should the remote computer's IP address be configured in the ESP32?
  13. What is the difference between a local IP address and a public IP address?
  14. Why is port 5000 used in this experiment?
  15. How can the application be extended to store sensor readings in a database or cloud platform?