MDM331AID/Experiment 5
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
- 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:
<syntaxhighlight lang="bash"> python tcp_server.py </syntaxhighlight>
- Ensure that the firewall allows incoming TCP connections on port 5000.
Step 2: Configure ESP32
- Connect the DHT11 sensor to the ESP32 using the specified pin connections.
- Open Arduino IDE.
- Install the DHT sensor library if it is not already installed.
- Enter the Wi-Fi SSID, 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.
Step 3: Configure Raspberry Pi (Alternative)
- 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
- 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.
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
- What is the role of Wi-Fi in an IoT application?
- What is the difference between TCP and UDP?
- What is an IP address?
- What is a TCP socket?
- What is the difference between a TCP client and a TCP server?
- Why is TCP considered a reliable communication protocol?
- What is the function of the bind() function in Python socket programming?
- What is the purpose of the listen() and accept() functions?
- 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?