Skip to main content

Group Assisnment Week 14

In this week, I am going to:

  • Send a message between two projects
  • Document your work to the group work page and reflect on your individual page what you learned

Principle of sending messages

I am going to send temperature and humidity data between two designed PCB abords:

We are using XIAO ESP32C3 board connecting to the DHT11 temperature and humidity sensor and one XIAO ESP32C3 board connecting a LED.

One is sending data over WiFi and another ESP32 device receiving this data on the same WiFi network, applying HTTP protocol, to complete the sending and receiving.

Connection and Preparatory Work

Programming the First XIAO ESP32C3 Board(Dion) as a Server

  1. Connect the data pin of the DHT11 to D2 on the XIAO ESP32.
  2. Downloading the DHT library to read temperature and humidity data.
  3. Downloading the Arduino and adding the XIAO ESP32C3 board. The Wi-Fi.h is alrady installed.
  4. Uploading the code below:
#include <WiFi.h>
#include <DHT.h>

const char* ssid = "AndroidAP26ab";
const char* password = "tawd7050";

#define DHTPIN 2 // D2 pin on ESP32C3
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

// Server configuration
WiFiServer server(12345);

void setup() {
// Set up Serial Monitor
Serial.begin(115200);
Serial.println("ESP32 Temperature & Humidity Sensor Test");

dht.begin();

delay(1000);

// Connect to Wi-Fi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");

// Print the IP address of the ESP32C3 MCU board
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

// Start the server
server.begin();
Serial.println("Server started");
}

void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (client) {
Serial.println("Client connected");

// Read humidity
float h = dht.readHumidity();
// Read temperature as Celsius (default)
float t = dht.readTemperature();

// Check if any reads failed
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% Temperature: ");
Serial.print(t);
Serial.println("°C ");

// Send data to the connected client
client.print("Humidity: ");
client.print(h);
client.print("% Temperature: ");
client.print(t);
client.println("°C");
}

// Close the connection
client.stop();
Serial.println("Client disconnected");
}

delay(2000);
}

This code will create a simple HTTP server(ip:192.168.21.239 and port:12345) that periodically reads temperature and humidity data, and sends these data upon receiving a request.

Programming the Second XIAO ESP32C3 Board(Matthew) as a Client

  1. Download Thonny software, and flashing ESP32C3 MicroPython firmware to XIAO ESP32C3.
  2. Upload the code below:
import socket
import network
import time

# Set up WiFi connection
ssid = 'AndroidAP26ab'
password = 'tawd7050'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

while not wlan.isconnected():
pass

print('Connected to WiFi:', wlan.ifconfig())

# Set up socket client
server_ip = '192.168.21.239' # IP address of the ESP32C3 MCU board
server_port = 12345

while True:
try:
# Create a new socket and connect to the server
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((server_ip, server_port))
print('Connected to server')

# Receive data from the server
data = client_socket.recv(1024).decode('utf-8')

if data:
# Process the received data
print("Received data:", data)

# Extract humidity and temperature values
humidity = float(data.split('Humidity: ')[1].split('%')[0])
temperature = float(data.split('Temperature: ')[1].split('°C')[0])

print(f"Humidity: {humidity}%")
print(f"Temperature: {temperature}°C")
else:
print("No data received")

except OSError as e:
print('Error:', e)

finally:
# Close the client socket
client_socket.close()
print('Disconnected from server')

# Delay before the next reading
time.sleep(2)

It will periodically send HTTP requests to the server ESP32 to fetch temperature and humidity data, and process them.

Final Output

Here is the sending part:

Connecting

image

Output

image

Here is the receiving part:

Connecting

image

Output

image

Video