This group work was developed at Universidad del Pacifico. I worked remotely with my group, since I could not travel to Lima. The objective was to implement a communication system between devices using the MQTT protocol, allowing the sending and receiving of messages through a broker using the publish/subscribe model. During the development, joint tests were carried out to validate the communication flow, verify the correct transmission of messages, and ensure the operation of the nodes involved.
The implemented system is composed of three main elements: 1) Broker, broker used: broker.emqx.io, port: 1883. The broker acts as an intermediary, in charge of receiving and distributing messages between devices. 2) Publisher (sending node), tool used: MQTTX, function: send messages to a topic. 3) Subscriber (receiving node), device: ESP32, function: receive messages and process them.
The code that was used:
// Programming Project 1 - Counter with OLED display - Client
// TU_WIFI
#include //Library for WiFi connection
#include //Library for MQTT client communication
#include //Library for I2C communication
#include //Library for graphics on OLED
#include //Library for OLED display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Create an instance of the OLED display
// WiFi
const char* ssid = "IoT_UP"; //WiFi network name
const char* password = "ti6WzfPsk3WnqZpt8d"; //WiFi network password
// MQTT
const char* mqtt_server = "broker.emqx.io"; //MQTT broker address
WiFiClient espClient; // Create a WiFi client object
PubSubClient client(espClient); // Create an MQTT client object using the WiFi client
String mensaje = ""; // Variable to store the received message
bool nuevoMensaje = false; // Flag to indicate if a new message has been received
void setup() {
Serial.begin(115200); // Start serial communication for debugging
// OLED
Wire.begin(D4, D5); // Initialize I2C communication with specified SDA and SCL pins
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize the OLED display with the I2C address 0x3C
display.clearDisplay(); // Clear the display buffer
display.setTextSize(2); // Set text size to 2 (double the normal size)
display.setTextColor(SSD1306_WHITE); // Set text color to white
display.setCursor(0, 20); // Set the cursor position to (0, 20) for text display
display.println("Contador"); // Print "Contador" on the OLED display
display.display(); // Update the OLED display with the contents of the display buffer
// WiFi
WiFi.begin(ssid, password); // Connect to the WiFi network using the specified SSID and password
while (WiFi.status() != WL_CONNECTED) {
delay(500); // Wait until the WiFi connection is established
}
// MQTT
client.setServer(mqtt_server, 1883); // Set the MQTT broker address and port for the client
client.setCallback(callback); // Set the callback function to handle incoming MQTT messages
}
void reconnect() {
while (!client.connected()) { // Keep trying to connect until the MQTT client is connected
if (client.connect("XIAO_OLED")) { // Attempt to connect to the MQTT broker with the client ID "XIAO_OLED"
client.subscribe("fabacademy/contador");// Subscribe to the MQTT topic "fabacademy/contador" to receive messages published to that topic
} else {
delay(2000); // Wait before retrying to connect if the connection attempt fails
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
mensaje = ""; // Clear the message variable before processing the new message
for (int i = 0; i < length; i++) { // Iterate through the payload bytes and append them to the message string
mensaje += (char)payload[i]; // Convert each byte in the payload to a character and append it to the message string
}
Serial.println(mensaje); // Print the received message to the serial monitor for debugging
nuevoMensaje = true; // Set the flag to indicate that a new message has been received and is ready to be displayed on the OLED
}
void loop() {
if (!client.connected()) {
reconnect(); // If the MQTT client is not connected, call the reconnect function to establish a connection to the MQTT broker
}
client.loop();
if (nuevoMensaje) { // If a new message has been received, update the OLED display with the new message
display.clearDisplay();
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.setCursor(20, 20);
display.println(mensaje); // Print the received message on the OLED display
display.display();
nuevoMensaje = false;
}
}
And the following code was used:
// Programming Project 2 - Button counter with MQTT - Server
// TU_WIFI
#include //Library for WiFi connection
#include //Library for MQTT client communication
// WiFi
const char* ssid = "IoT_UP"; //WiFi network name
const char* password = "ti6WzfPsk3WnqZpt8d"; //WiFi network password
// MQTT
const char* mqtt_server = "broker.emqx.io"; //MQTT broker address
WiFiClient espClient; // Create a WiFi client object
PubSubClient client(espClient); // Create an MQTT client object using the WiFi client
int boton = D7; // Define the pin for the button (change to the GPIO/pin you are using)
int contador = 0; // Variable to store the count of button presses
bool estadoAnterior = HIGH; // Variable to store the previous state of the button (initialized to HIGH assuming the button is not pressed)
void setup() {
Serial.begin(115200); // Start serial communication for debugging
pinMode(boton, INPUT_PULLUP); // Set the button pin as an input with an internal pull-up resistor (assuming the button is active LOW)
WiFi.begin(ssid, password); // Connect to the WiFi network using the specified SSID and password
while (WiFi.status() != WL_CONNECTED) { // Wait until the WiFi connection is established
delay(500);
}
client.setServer(mqtt_server, 1883); // Set the MQTT broker address and port for the client
}
void reconnect() {
while (!client.connected()) { // Keep trying to connect until the MQTT client is connected
client.connect("XIAO_BOTON"); // Attempt to connect to the MQTT broker with the client ID "XIAO_BOTON"
}
}
void loop() {
if (!client.connected()) reconnect(); // If the MQTT client is not connected, call the reconnect function to establish a connection to the MQTT broker
client.loop();
bool estadoActual = digitalRead(boton); // Read the current state of the button (HIGH or LOW)
// Detecta pulsación
if (estadoAnterior == HIGH && estadoActual == LOW) {
contador++; // Increment the counter when a button press is detected (transition from HIGH to LOW)
String msg = String(contador); // Convert the counter value to a string to be published as an MQTT message
Serial.println(msg); // Print the current count to the serial monitor for debugging
client.publish("fabacademy/contador", msg.c_str()); // Publish the current count to the MQTT topic "fabacademy/contador" as a string message
delay(300); // Add a small delay to debounce the button and prevent multiple counts from a single press
}
estadoAnterior = estadoActual; // Update the previous state of the button to the current state for the next iteration of the loop
}
Video 1: Board connection and output on the OLED
Video 2: Board connection and output was used to rotate the motor
During the group assignment, a communication system based on the MQTT protocol was successfully implemented, allowing message exchange between different devices. The tests carried out demonstrated that the broker acts as a central element that enables communication between nodes without requiring a direct connection between them.
The publish/subscribe model proved to be efficient for organizing data transmission, since it allows multiple devices to interact within the same system using structured topics. This makes it possible to scale the system and integrate additional nodes without modifying the core architecture.
Additionally, the integration of hardware such as ESP32 boards with output devices like OLED displays confirmed that it is possible to process and visualize data in real time, validating the functionality of the implemented communication network.
Overall, the group assignment demonstrated that MQTT is a robust and flexible protocol for IoT applications, enabling reliable communication between distributed systems.
Working on the group assignment allowed me to understand how collaborative development functions in distributed systems. Even though I participated remotely, it was possible to coordinate tasks, validate results, and contribute to the overall system implementation.
One of the most valuable aspects of this experience was understanding how each component within the network plays a specific role, and how the correct configuration of topics and communication flow is essential for the system to function properly.
I also reinforced my understanding of MQTT as a lightweight protocol specifically designed for IoT, and how it differs from other communication protocols by prioritizing efficiency and scalability.
Finally, this assignment helped me develop a clearer perspective on how real-world communication systems are structured, strengthening my ability to work with networks, debug communication issues, and integrate multiple devices into a single functional system.
This week, a wireless communication system was developed using an ESP32 board as an Access Point and web server. The objective was to establish a connection between an external device (mobile phone) and the board, allowing commands to be sent through a web interface and visualizing the results on an LCD screen.
This project demonstrates the implementation of communication protocols, the use of WiFi networks, and the interaction between devices in an embedded system.
Design and implement a communication system between:
Where the user can send commands from a web page and control an output device (LEDs and LCD screen).
The system works under the client-server model:
In order to carry out this part, I first performed a connection test. For this, I got help from ChatGPT to generate the code, I uploaded my assignment for this week and told it that I only had one board with a microcontroller and asked it to give me the step by step on how to complete the assignment successfully.
ChatGPT gave me this code:
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "Rocio-ESP32";
const char* password = "12345678";
IPAddress local_ip(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
WebServer server(80);
void handleRoot() {
server.send(200, "text/html", "<h1>ESP32 Working</h1><p>Local web server OK</p>");
}
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.softAPConfig(local_ip, gateway, subnet);
WiFi.softAP(ssid, password);
Serial.println("Access Point started");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
server.on("/", handleRoot);
server.begin();
Serial.println("Web server started");
}
void loop() {
server.handleClient();
}
Then I compiled it to my XIAO ESP32-C3.
Then the data for network configuration appeared in serial.
This configuration makes it possible to establish a local network without the need for internet, where the ESP32 functions as a router. The protocol used was: HTTP (HyperText Transfer Protocol). To do this, the WiFi connection is searched for on the mobile phone, in my case with the name Rocio-ESP32, then the password is entered, and we see that we are connected to the WiFi of our microcontroller. It will appear without an internet connection because it is a local connection and not a network connection. Then in the browser we enter the IP address and a web page will appear where we can add buttons to send commands to our board.
Img 5: On the mobile phone the WiFi with the name Rocio-ESP32 is searched for, the password is entered, and the IP address is searched
The ESP32 interprets the requests sent from the browser, for example: turn on LED D9, LED D10, turn everything off, or in the case of the LCD screen connection, make the screen say Hello, Temp, TDS, Clear. Each route represents a specific action within the system.
In the case of turning the microcontroller board LEDs on and off, I used this code generated by ChatGPT.
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "Rocio-ESP32";
const char* password = "12345678";
WebServer server(80);
// Pins of your LEDs
const int led1 = 9; // D9
const int led2 = 10; // D10
void handleRoot() {
String html = "<!DOCTYPE html><html><head><meta charset='UTF-8'>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<title>ESP32 LED Control</title></head><body>";
html += "<h1>ESP32 Web Control</h1>";
html += "<p><a href='/d9on'><button style='padding:15px;font-size:18px;'>LED D9 ON</button></a></p>";
html += "<p><a href='/d10on'><button style='padding:15px;font-size:18px;'>LED D10 ON</button></a></p>";
html += "<p><a href='/off'><button style='padding:15px;font-size:18px;'>ALL OFF</button></a></p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleD9On() {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
server.send(200, "text/html", "<h1>LED D9 ON</h1><p><a href='/'>Back</a></p>");
}
void handleD10On() {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
server.send(200, "text/html", "<h1>LED D10 ON</h1><p><a href='/'>Back</a></p>");
}
void handleOff() {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
server.send(200, "text/html", "<h1>All LEDs OFF</h1><p><a href='/'>Back</a></p>");
}
void setup() {
Serial.begin(115200);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
WiFi.softAP(ssid, password);
Serial.println("Access Point started");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
server.on("/", handleRoot);
server.on("/d9on", handleD9On);
server.on("/d10on", handleD10On);
server.on("/off", handleOff);
server.begin();
Serial.println("Web server started");
}
void loop() {
server.handleClient();
}
Video 3: It is shown how when from the mobile phone the order is given to turn on LED 10 on the microcontroller, the LED connected to D10 turns on
In the case of communication with the LCD screen, I used this code also provided by ChatGPT.
#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const char* ssid = "Rocio-ESP32";
const char* password = "12345678";
WebServer server(80);
// LCD (your library)
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// -------- WEB PAGE --------
void handleRoot() {
String html = "<!DOCTYPE html><html><head><meta charset='UTF-8'>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<title>ESP32 LCD</title></head><body>";
html += "<h1>ESP32 LCD Control</h1>";
html += "<p><a href='/hello'><button style='padding:15px;'>HELLO</button></a></p>";
html += "<p><a href='/temp'><button style='padding:15px;'>TEMP</button></a></p>";
html += "<p><a href='/tds'><button style='padding:15px;'>TDS</button></a></p>";
html += "<p><a href='/clear'><button style='padding:15px;'>CLEAR</button></a></p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
// -------- LCD FUNCTIONS --------
void handleHello() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HELLO");
lcd.setCursor(0, 1);
lcd.print("ROCIO");
server.send(200, "text/html", "<h1>Hello sent</h1><a href='/'>Back</a>");
}
void handleTemp() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TEMP:");
lcd.setCursor(0, 1);
lcd.print("25 C");
server.send(200, "text/html", "<h1>Temp sent</h1><a href='/'>Back</a>");
}
void handleTDS() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TDS:");
lcd.setCursor(0, 1);
lcd.print("300 ppm");
server.send(200, "text/html", "<h1>TDS sent</h1><a href='/'>Back</a>");
}
void handleClear() {
lcd.clear();
server.send(200, "text/html", "<h1>LCD cleared</h1><a href='/'>Back</a>");
}
// -------- SETUP --------
void setup() {
Serial.begin(115200);
// LCD
Wire.begin(6, 7);
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Starting...");
// WIFI
WiFi.softAP(ssid, password);
Serial.println("WiFi started");
Serial.println(WiFi.softAPIP());
// ROUTES
server.on("/", handleRoot);
server.on("/hello", handleHello);
server.on("/temp", handleTemp);
server.on("/tds", handleTDS);
server.on("/clear", handleClear);
server.begin();
// Initial LCD message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Ready");
lcd.setCursor(0, 1);
lcd.print("192.168.4.1");
}
// -------- LOOP --------
void loop() {
server.handleClient();
}
Video 4
This week I also worked with communication protocols to develop a data exchange system between devices using MQTT. The process was carried out progressively, starting with basic software tests and advancing toward communication between microcontrollers.
In this first stage I performed tests using only MQTTX to understand the operation of the protocol.
fabacademy/rocio/test
Result: The messages were received correctly in real time within MQTTX.
Learning:
I understood the operation of the publish/subscribe model, where:
In this stage I integrated hardware into the system, using my XIAO ESP32-C3 board together with an LCD screen. For this part I reviewed teacher Ulises' video, you can check it here: Ulises video
I used the following code created by ChatGPT:
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// WiFi
const char* ssid = "php";
const char* password = "jair12345";
// MQTT
const char* mqtt_server = "broker.emqx.io";
const int mqtt_port = 1883;
const char* mqtt_user = "admin";
const char* mqtt_pass = "public";
// Topic
const char* topic = "fabacademy/rocio/lcd";
// LCD
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char* incomingTopic, byte* payload, unsigned int length) {
String message = "";
for (unsigned int i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.print("Mensaje recibido en topic: ");
Serial.println(incomingTopic);
Serial.print("Contenido: ");
Serial.println(message);
lcd.clear();
lcd.setCursor(0, 0);
if (message == "hello") {
lcd.print("HELLO");
lcd.setCursor(0, 1);
lcd.print("ROCIO");
}
else if (message == "temp") {
lcd.print("TEMP:");
lcd.setCursor(0, 1);
lcd.print("25 C");
}
else if (message == "tds") {
lcd.print("TDS:");
lcd.setCursor(0, 1);
lcd.print("300 ppm");
}
else {
lcd.print("MSG:");
lcd.setCursor(0, 1);
lcd.print(message.substring(0, 16));
}
}
void setup_wifi() {
delay(10);
Serial.println("Conectando a WiFi...");
Serial.print("Red: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi conectado");
Serial.print("IP local: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
while (!client.connected()) {
Serial.println("Conectando a MQTT...");
String clientId = "rocioESP32-";
clientId += String((uint32_t)ESP.getEfuseMac(), HEX);
if (client.connect(clientId.c_str(), mqtt_user, mqtt_pass)) {
Serial.println("MQTT conectado");
client.subscribe(topic);
Serial.print("Suscrita al topic: ");
Serial.println(topic);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQTT Connected");
lcd.setCursor(0, 1);
lcd.print("Waiting msg...");
} else {
Serial.print("Fallo MQTT, rc=");
Serial.println(client.state());
Serial.println("Reintentando en 2 segundos...");
delay(2000);
}
}
}
void setup() {
Serial.begin(115200);
Wire.begin(6, 7);
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Starting...");
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi OK");
lcd.setCursor(0, 1);
lcd.print("Connecting...");
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
fabacademy/rocio/lcd
Img 17: I run communication tests in MQTTX, it can be seen that the messages sent from MQTTX appear in the serial monitor of my ESP32-C3 and the message also appears on the LCD
Result:
Video 5: Test of sending a message in MQTTX and it is shown on the LCD connected to my microcontroller
Learning:
I learned to integrate:
In this stage I implemented communication between two microcontrollers using MQTT:
For this I asked ChatGPT for the code and it gave me this:
#include <WiFi.h>
#include <PubSubClient.h>
// ====== DATOS WIFI ======
const char* ssid = "php";
const char* password = "jair12345";
// ====== DATOS MQTT ======
const char* mqtt_server = "broker.emqx.io";
const int mqtt_port = 1883;
const char* topic_pub = "fabacademy/rocio/lcd";
WiFiClient espClient;
PubSubClient client(espClient);
void conectarWiFi() {
Serial.print("Conectando a WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi conectado");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void conectarMQTT() {
while (!client.connected()) {
Serial.print("Conectando a MQTT...");
String clientId = "ESP32_Publicador_";
clientId += String((uint32_t)ESP.getEfuseMac(), HEX);
if (client.connect(clientId.c_str())) {
Serial.println(" conectado");
Serial.print("Publicando en topic: ");
Serial.println(topic_pub);
Serial.println("Escribe un mensaje en el Monitor Serial y presiona Enter");
} else {
Serial.print(" fallo, rc=");
Serial.print(client.state());
Serial.println(" reintentando en 2 segundos");
delay(2000);
}
}
}
void setup() {
Serial.begin(115200);
conectarWiFi();
client.setServer(mqtt_server, mqtt_port);
}
void loop() {
if (!client.connected()) {
conectarMQTT();
}
client.loop();
if (Serial.available()) {
String mensaje = Serial.readStringUntil('\n');
mensaje.trim();
if (mensaje.length() > 0) {
if (client.publish(topic_pub, mensaje.c_str())) {
Serial.print("Mensaje enviado: ");
Serial.println(mensaje);
} else {
Serial.println("Error al publicar");
}
}
}
}
For this I asked ChatGPT for the code and it gave me this:
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// ====== DATOS WIFI ======
const char* ssid = "php";
const char* password = "jair12345";
// ====== DATOS MQTT ======
const char* mqtt_server = "broker.emqx.io";
const int mqtt_port = 1883;
const char* topic_sub = "fabacademy/rocio/lcd";
// Direction and size
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// ====== LCD I2C ======
// Correct constructor for New-LiquidCrystal
LiquidCrystal_I2C lcd(LCD_ADDRESS, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
WiFiClient espClient;
PubSubClient client(espClient);
void mostrarEnLCD(String texto) {
lcd.clear();
// First line
lcd.setCursor(0, 0);
lcd.print("Msg MQTT:");
// Second line: maximum 16 visible characters
lcd.setCursor(0, 1);
if (texto.length() <= 16) {
lcd.print(texto);
} else {
lcd.print(texto.substring(0, 16));
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String mensaje = "";
for (unsigned int i = 0; i < length; i++) {
mensaje += (char)payload[i];
}
Serial.println("Mensaje recibido:");
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Contenido: ");
Serial.println(mensaje);
mostrarEnLCD(mensaje);
}
void conectarWiFi() {
Serial.print("Conectando a WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi conectado");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void conectarMQTT() {
while (!client.connected()) {
Serial.print("Conectando a MQTT...");
String clientId = "ESP32_Receptor_";
clientId += String((uint32_t)ESP.getEfuseMac(), HEX);
if (client.connect(clientId.c_str())) {
Serial.println(" conectado");
client.subscribe(topic_sub);
Serial.print("Suscrito a: ");
Serial.println(topic_sub);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQTT conectado");
lcd.setCursor(0, 1);
lcd.print("Esperando...");
} else {
Serial.print(" fallo, rc=");
Serial.print(client.state());
Serial.println(" reintentando en 2 segundos");
delay(2000);
}
}
}
void setup() {
Serial.begin(115200);
// Correct initialization for your LCD and your XIAO ESP32-C3
Wire.begin(6, 7);
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Iniciando...");
conectarWiFi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
conectarMQTT();
}
client.loop();
}
This test was carried out using two different sketches in Arduino IDE, simulating two independent nodes.
For this I asked ChatGPT to give me the code and I used this, I only had to change the WiFi part, with my mobile phone WiFi.
Img 19: Sending messages from the serial monitor of the ESP32-WROOM-32 and receiving them in the serial monitor of the ESP32-C3
Both devices were connected to the same MQTT broker.
Topic used
fabacademy/rocio/lcd
Message sent from the serial monitor: Hello FAB ACADEMY
Result: The message was correctly received by the XIAO ESP32-C3.
The received message was shown on the LCD screen connected to the XIAO.
Result: Correct real-time visualization.
Video 6: Demonstration of the WiFi connection of the ESP32-WROOM-32 and the XIAO ESP32-C3, and the messages are displayed on the serial monitor and also on the LCD connected to the XIAO
During week 11, it was possible to implement a communication system using the MQTT protocol, progressing from basic software tests to interaction between microcontrollers. Through this process, it was verified that the use of a broker allows devices to be decoupled, facilitating communication between nodes without the need for a direct connection between them.
The use of the publish/subscribe model made it possible to organize data transmission efficiently, demonstrating that multiple devices can send and receive information in real time through the same channel structured by topics.
Likewise, it was validated that ESP32 boards can be easily integrated into network systems, allowing not only the reception of data, but also its processing and visualization through output devices such as the LCD screen.
Finally, the implementation of communication between two microcontrollers using different sketches showed that it is possible to simulate distributed networks and build functional IoT systems even in controlled testing environments.
During this week, the learning process was progressive and practical, beginning with understanding the operation of MQTT through tests in MQTTX, which made it possible to clearly understand the communication flow between publisher, broker, and subscriber.
One of the most important aspects was recognizing the difference between protocols such as HTTP and MQTT, understanding that the latter is specifically designed for IoT systems due to its lightness and efficiency in data transmission.
The integration of hardware represented an additional challenge, especially in WiFi configuration, library management, and the correct interpretation of received messages. However, overcoming these challenges made it possible to better understand the relationship between software and hardware in embedded systems.
The experience of working with two different boards and using different sketches helped visualize how distributed systems work in practice, reinforcing the idea that each device can act as an independent node within a network.
In general, this week made it possible to move from a theoretical understanding of network communication to a real implementation, strengthening skills in programming, problem solving, and IoT system design.
This section includes the files used for the networking and communication assignment, including the code for transmitting and receiving data, PCB design files, and device documentation. The communication tests and results are documented above.