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

  • The text communication between the two projects consisted of sending words using the serial monitor through a Wi-Fi with MQTT Wi-Fi connection protocol. We sent the word "avanzando" between each board.
  • Both boards have a XIAO seeeduino esp32c3 with wifi connectivity.
  • To use the MQTT protocol for Wi-Fi communication, installed these 2 libraries:
  • wifi.h Library for Arduino IDE programming.
  • PubSubClient.h or Arduino IDE programming.
  • MQTT Broker Information: the server used was: broker.hivemq.com
  • We used an own wifi SSID with its password
  • WiFi Connection between Seeeduino ESP32C3 Devices

    To connect two Seeeduino ESP32C3 devices via Wi-Fi, follow this process using the MQTT protocol:

    1. MQTT Server Configuration: Set up an MQTT server both devices can connect to. Services like HiveMQ or Mosquitto can be used.
    2. 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.
    3. 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.
    4. 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);
    }
        

    Files: