Assignments
- Send a message between two projects
Individual apport
- Ernesto, Jorge, Marcela: Our contribution to the group work was to establish communication between two Seeeduino Xiao ESP32 boards using Wi-Fi and the MQTT protocol to send messages.
Process
Process of sending a message between two projects
WiFi Connection between Seeeduino ESP32C3 Devices
To connect two Seeeduino ESP32C3 devices via Wi-Fi, follow this process using the MQTT protocol:
- MQTT Server Configuration: Set up an MQTT server both devices can connect to. Services like HiveMQ or Mosquitto can be used.
- Server Code (Seeeduino 1): This device connects to the MQTT server, subscribes to a specific topic, and publishes messages to another topic using the PubSubClient library.
- Client Code (Seeeduino 2): This device connects to the same MQTT server, subscribes to the topic the server publishes, and publishes messages to the topic the server subscribes to.
- Connection Process:
- Connect to the Wi-Fi network using the SSID and password.
- Connect to the MQTT server.
- Use functions to subscribe and publish to the appropriate topics.
Server Code
#include <WiFi.h> #include <PubSubClient.h> const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* mqtt_server = "broker.hivemq.com"; WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); } void setup_wifi() { delay(10); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } } void callback(char* topic, byte* message, unsigned int length) { Serial.print("Message arrived: "); for (int i = 0; i < length; i++) { Serial.print((char)message[i]); } Serial.println(); } void reconnect() { while (!client.connected()) { if (client.connect("ESP32Client")) { client.subscribe("esp/test"); } else { delay(5000); } } } void loop() { if (!client.connected()) { reconnect(); } client.loop(); client.publish("esp/test", "Hello from ESP32"); delay(2000); }
Client Code
#include <WiFi.h> #include <PubSubClient.h> const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* mqtt_server = "broker.hivemq.com"; WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); } void setup_wifi() { delay(10); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } } void callback(char* topic, byte* message, unsigned int length) { Serial.print("Message arrived: "); for (int i = 0; i < length; i++) { Serial.print((char)message[i]); } Serial.println(); } void reconnect() { while (!client.connected()) { if (client.connect("ESP32Client")) { client.subscribe("esp/test"); } else { delay(5000); } } } void loop() { if (!client.connected()) { reconnect(); } client.loop(); client.publish("esp/test", "Hello from ESP32 Client"); delay(2000); }