Jump to content

MDM331AID/Experiment 5: Difference between revisions

From cognifront
CogWikiEditor (talk | contribs)
Created page with " = Wi-Fi and TCP/IP Communication Using ESP32 and DHT11 = == Aim == To connect an ESP32 to a Wi-Fi network and transmit temperature and humidity data from a DHT11 sensor to a remote computer using TCP/IP communication. == Components Required == {| class="wikitable" |- ! Sr. No. ! Component ! Quantity |- | 1 | ESP32 Development Board | 1 |- | 2 | DHT11 Sensor | 1 |- | 3 | USB Data Cable | 1 |- | 4 | Patch Cords | As required |- | 5 | Computer with Arduino IDE | 1 |- |..."
 
CogWikiEditor (talk | contribs)
No edit summary
Line 1: Line 1:


= Wi-Fi and TCP/IP Communication Using ESP32 and DHT11 =
= Experiment: Wi-Fi Connectivity and TCP/IP-Based Sensor Data Transmission Using ESP32/Raspberry Pi =


== Aim ==
== Aim ==


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


== Components Required ==
== Components Required ==
Line 15: Line 23:
|-
|-
| 1
| 1
| ESP32 Development Board
| ESP32 Development Board (COG-E32) or Raspberry Pi 4 (COG-RPI4)
| 1
| 1
|-
|-
| 2
| 2
| DHT11 Sensor
| DHT11 Temperature and Humidity Sensor (COG-MSBA)
| 1
| 1
|-
|-
| 3
| 3
| USB Data Cable
| USB data cable or Raspberry Pi power supply
| 1
| 1
|-
|-
| 4
| 4
| Patch Cords
| Patch cords
| As required
| As required
|-
|-
| 5
| 5
| Computer with Arduino IDE
| Computer with Arduino IDE or Python
| 1
| 1
|-
|-
| 6
| 6
| Remote Computer
| Remote computer connected to the same Wi-Fi network
| 1
|-
| 7
| Wi-Fi network
| 1
| 1
|}
|}
Line 41: Line 53:
== Theory ==
== Theory ==


An IoT device collects data using sensors and transmits it to other devices through a communication network.
=== 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 or Raspberry Pi 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. The Raspberry Pi 4 also supports Wi-Fi connectivity.
 
=== 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 or Raspberry Pi acts as a TCP client and sends sensor readings to the 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.


Wi-Fi enables wireless connectivity between the ESP32 and the local network.
Typical measurement ranges are:


TCP/IP is a communication protocol suite used for data exchange between devices.
* Temperature: 0°C to 50°C.
* Relative humidity: 20% to 90%.


* '''TCP:''' Provides reliable and ordered data transmission.
The DHT11 is suitable for basic environmental monitoring demonstrations. Its accuracy and measurement range are limited compared with more advanced sensors.
* '''IP:''' Provides addressing and routing of data packets.
* '''TCP Client:''' Initiates a connection and sends data.
* '''TCP Server:''' Listens for incoming connections and receives data.


The DHT11 sensor measures temperature and humidity. The ESP32 reads these values and transmits them to a remote computer.
=== System Architecture ===
 
<pre>
      DHT11 Sensor
            |
            v
    +----------------+
    | ESP32 /        |
    | Raspberry Pi  |
    +----------------+
            |
        Wi-Fi Network
            |
            v
    +----------------+
    | Remote Computer|
    | TCP Server    |
    +----------------+
            |
            v
    Display Sensor Data
</pre>


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


{| class="wikitable"
{| class="wikitable"
Line 64: Line 121:
| 3V3
| 3V3
| VCC
| VCC
| Power supply
| Sensor power supply
|-
|-
| GPIO 4
| GPIO 4
| DATA
| DATA
| Sensor data
| Digital data interface
|-
|-
| GND
| GND
| GND
| GND
| Ground
| Common ground
|}
|}


== Connection Diagram ==
=== Raspberry Pi Connections ===


<pre>
{| class="wikitable"
DHT11 Sensor
|-
    |
! Raspberry Pi Pin
    | DATA
! DHT11 Pin
    |
! Description
GPIO 4
|-
    |
| Physical Pin 1 (3.3V)
  ESP32
| VCC
    |
| Sensor power supply
  Wi-Fi
|-
    |
| Physical Pin 7 (GPIO4)
Remote Computer
| DATA
    |
| Digital data interface
TCP Server
|-
</pre>
| Physical Pin 6 (GND)
| GND
| Common ground
|}


== Program 1: Python TCP Server ==
'''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.


Run this program on the remote computer.
== Software Requirements ==


<syntaxhighlight lang="python">
=== ESP32 ===
import socket


server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
* Arduino IDE
* ESP32 board support package
* DHT sensor library
* Wi-Fi library included with the ESP32 board package


server.bind(("0.0.0.0", 5000))
=== Raspberry Pi ===
server.listen(1)


print("Waiting for connection...")
* Raspberry Pi OS
* Python 3
* Adafruit CircuitPython DHT library
* Blinka library
* TCP socket module (Python standard library)


while True:
=== Remote Computer ===
    conn, addr = server.accept()
    print("Connected:", addr)


    with conn:
* Python 3
        while True:
* TCP server program
            data = conn.recv(1024)
* Network access from the IoT device


            if not data:
                break


            print(data.decode().strip())
== Program 1: TCP Server on Remote Computer ==
</syntaxhighlight>


== Program 2: ESP32 TCP Client ==
The remote computer acts as a TCP server. It listens on port 5000 and displays the sensor data received from the IoT device.


Install the DHT sensor library in Arduino IDE before uploading the program.
Download the program: [[Media:IoT_TCP_DHT11_Programs.zip|TCP Server and IoT Client Programs ZIP]]


Replace the Wi-Fi name, password, and server IP address with your own network details.
== Program 2: ESP32 TCP Client ==


<syntaxhighlight lang="cpp">
The ESP32 connects to the Wi-Fi network, reads the DHT11 sensor values, and sends the readings to the remote computer using TCP.
#include <WiFi.h>
#include <DHT.h>


#define DHTPIN 4
Download the program: [[Media:IoT_TCP_DHT11_Programs.zip|ESP32 TCP Client Program]]
#define DHTTYPE DHT11


const char* ssid = "YOUR_WIFI_NAME";
== Program 3: Raspberry Pi TCP Client ==
const char* password = "YOUR_WIFI_PASSWORD";


const char* serverIP = "192.168.1.100";
The Raspberry Pi acts as a TCP client. It reads the DHT11 sensor and transmits the data to the remote computer.
const int serverPort = 5000;


DHT dht(DHTPIN, DHTTYPE);
Download the program: [[Media:IoT_TCP_DHT11_Programs.zip|Raspberry Pi TCP Client Program]]
WiFiClient client;
== Procedure ==


void setup() {
=== Step 1: Configure the Remote Computer ===
  Serial.begin(115200);
  dht.begin();


  WiFi.begin(ssid, password);
# Connect the remote computer to the Wi-Fi network.
# Find its local IP address using the appropriate network command.
# Replace the example IP address in the IoT client program with the remote computer's actual IP address.
# Save the TCP server program as tcp_server.py.
# Run the server using Python:


  while (WiFi.status() != WL_CONNECTED) {
<syntaxhighlight lang="bash">
    delay(500);
python tcp_server.py
    Serial.print(".");
</syntaxhighlight>
  }


  Serial.println("Wi-Fi Connected");
# Ensure that the firewall allows incoming TCP connections on port 5000.
  Serial.println(WiFi.localIP());
}


void loop() {
=== Step 2: Configure ESP32 ===
  if (!client.connected()) {
    if (!client.connect(serverIP, serverPort)) {
      Serial.println("Server connection failed");
      delay(3000);
      return;
    }
  }


  float t = dht.readTemperature();
# Connect the DHT11 sensor to the ESP32 using the specified pin connections.
  float h = dht.readHumidity();
# Open Arduino IDE.
# 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.
# Select the appropriate ESP32 board and COM port.
# Upload the program.
# Open the Serial Monitor at 115200 baud.
# Observe the Wi-Fi connection status and TCP transmission messages.


  if (!isnan(t) && !isnan(h)) {
=== Step 3: Configure Raspberry Pi (Alternative) ===
    client.print("Temperature: ");
    client.print(t);
    client.print(" C, Humidity: ");
    client.print(h);
    client.println(" %");


    Serial.println("Data 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:


  delay(3000);
<syntaxhighlight lang="bash">
}
python tcp_client.py
</syntaxhighlight>
</syntaxhighlight>


== Procedure ==
# Observe the sensor readings on the Raspberry Pi terminal and the remote computer.


# Connect the DHT11 sensor to the ESP32.
=== Step 4: Verify TCP/IP Communication ===
# Connect the ESP32 to the computer using a USB cable.
# Open Arduino IDE and select the appropriate ESP32 board and COM port.
# Install the DHT sensor library.
# Enter the Wi-Fi credentials and remote computer IP address.
# Run the Python TCP server on the remote computer.
# Upload the ESP32 program.
# Open the Serial Monitor at 115200 baud.
# Observe the temperature and humidity readings transmitted to the remote computer.


== Expected Output ==
# Check that the remote computer reports an incoming TCP connection.
# Observe the temperature and humidity data received by the server.
# Compare the transmitted values with the sensor readings displayed by the IoT device.
# Stop the client program and observe the disconnection message on the server.


'''ESP32 Serial Monitor'''
== Observations ==


<pre>
{| class="wikitable"
Wi-Fi Connected
|-
192.168.1.105
! Sr. No.
Data transmitted
! Temperature (°C)
Data transmitted
! Relative Humidity (%)
Data transmitted
! Transmission Status
</pre>
|-
| 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
|}


'''Remote Computer'''
'''Expected output on the remote computer:'''


<pre>
<pre>
Waiting for connection...
TCP Server started
Connected: ('192.168.1.105', 50000)
Waiting for IoT device connection...
Temperature: 28.0 C, Humidity: 65.0 %
Connected by: ('192.168.1.105', 54321)
Temperature: 28.0 C, Humidity: 64.0 %
Received: Temperature: 28.0 C, Humidity: 65.0 %
Received: Temperature: 28.0 C, Humidity: 64.0 %
Received: Temperature: 29.0 C, Humidity: 64.0 %
</pre>
</pre>


The values shown are illustrative examples.
The above values and IP addresses are illustrative examples, not actual measurements.


== Result ==
== Result ==


The ESP32 was configured to connect to a Wi-Fi network and transmit DHT11 temperature and humidity readings to a remote computer using TCP/IP communication.
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.


== Viva Questions ==
== Viva Questions ==


# What is Wi-Fi?
# What is the role of Wi-Fi in an IoT application?
# What is TCP/IP?
# What is the difference between TCP and UDP?
# What is the difference between TCP and UDP?
# What is a TCP client?
# What is an IP address?
# What is a TCP server?
# What is a TCP socket?
# What is the role of an IP address?
# What is the difference between a TCP client and a TCP server?
# What is the function of the DHT11 sensor?
# Why is TCP considered a reliable communication protocol?
# What is the purpose of the WiFiClient class?
# What is the function of the bind() function in Python socket programming?
# Why is TCP considered reliable?
# What is the purpose of the listen() and accept() functions?
# How can sensor data be sent to an IoT cloud platform?
# What is the function of sendall() in Python?
# Why should the remote computer's IP address be configured in the IoT device?
# What is the difference between local and public IP addresses?
# How can the application be extended to store sensor readings in a database or cloud platform?

Revision as of 17:15, 2 October 2026

Experiment: Wi-Fi Connectivity and TCP/IP-Based Sensor Data Transmission Using ESP32/Raspberry Pi

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.

Objectives

  • To understand Wi-Fi connectivity in IoT devices.
  • To interface a DHT11 temperature and humidity sensor with ESP32 or Raspberry Pi.
  • To understand TCP/IP communication and socket programming.
  • To establish a TCP connection between an IoT device 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) or Raspberry Pi 4 (COG-RPI4) 1
2 DHT11 Temperature and Humidity Sensor (COG-MSBA) 1
3 USB data cable or Raspberry Pi power supply 1
4 Patch cords As required
5 Computer with Arduino IDE or Python 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 or Raspberry Pi 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. The Raspberry Pi 4 also supports Wi-Fi connectivity.

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 or Raspberry Pi acts as a TCP client and sends sensor readings to the 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% to 90%.

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 /        |
     | Raspberry Pi   |
     +----------------+
            |
        Wi-Fi Network
            |
            v
     +----------------+
     | Remote Computer|
     | TCP Server     |
     +----------------+
            |
            v
     Display Sensor Data

Pin Connections

ESP32 Connections

ESP32 Pin DHT11 Pin Description
3V3 VCC Sensor power supply
GPIO 4 DATA Digital data interface
GND GND Common ground

Raspberry Pi Connections

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

ESP32

  • Arduino IDE
  • ESP32 board support package
  • DHT sensor library
  • 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

  • Python 3
  • TCP server program
  • Network access from the IoT device


Program 1: TCP Server on Remote Computer

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: TCP Server and IoT 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

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.

Download the program: Raspberry Pi TCP Client Program

Procedure

Step 1: Configure the Remote Computer

  1. Connect the remote computer to the Wi-Fi network.
  2. Find its local IP address using the appropriate network command.
  3. Replace the example IP address in the IoT 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.

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, password, and remote computer IP address in the program.
  5. Select the appropriate ESP32 board and COM port.
  6. Upload the program.
  7. Open the Serial Monitor at 115200 baud.
  8. Observe the Wi-Fi connection status and TCP transmission messages.

Step 3: Configure Raspberry Pi (Alternative)

  1. Connect the DHT11 sensor to the Raspberry Pi.
  2. Ensure that Raspberry Pi OS is connected to the same Wi-Fi network as the remote computer.
  3. Install the required Python libraries in the appropriate Python environment.
  4. Enter the remote computer's IP address in the Python client program.
  5. Run the program:

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

  1. Observe the sensor readings on the Raspberry Pi terminal and the remote computer.

Step 4: 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 server.
  3. Compare the transmitted values with the sensor readings displayed by the IoT device.
  4. Stop the client program and observe the disconnection message on the server.

Observations

Sr. No. Temperature (°C) Relative Humidity (%) 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 IoT device 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 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.

Viva Questions

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