Skip to content

11. Networking and communications

For this week’s group assignment, we had to send a message between two projects.


Dilijan Lab

This week, Mariam and Hrach had to establish communication between two ESP32-C3 boards so that one board could send a message while the other received it.

They used the MQTT publish/subscribe method through an MQTT broker.

Their main goal was to connect Mariam’s Final Project joystick with Hrach’s Final Project Drone SOS Case. The idea was that when the button on the joystick is pressed, the Drone SOS Case automatically opens.


🔌 Experiment 1 — Basic MQTT Communication

Before moving on to that, they first connected the two ESP32-C3 MCUs to fully understand how MQTT works.

This is the code for the first ESP32-C3, which acts as the publisher and sends a message to the second ESP32-C3 through the Serial Monitor:

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid     = "staff";
const char* password = "SRGB2020";
const char* broker   = "broker.hivemq.com";
const char* topic    = "chat/xiao";
const char* DEVICE_ID = "device_A";  // change to "device_B" on the other one

WiFiClient wifiClient;
PubSubClient client(wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
  String msg = "";
  for (int i = 0; i < length; i++) msg += (char)payload[i];
  Serial.println(">> " + msg);
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Connecting MQTT...");
    if (client.connect(DEVICE_ID)) {
      Serial.println("New message from #1");
      client.subscribe(topic);
    } else {
      delay(2000);
    }
  }
}

AI prompt: I have two ESP32-C3 boards connected through MQTT using broker.hivemq.com.

And this is the code for the second ESP32-C3, which connects to the first MCU and receives the message:

#include <WiFi.h>
#include <PubSubClient.h>

WiFiClient wifiClient;
PubSubClient mqtt(wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("Received: ");

  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }

  Serial.println();
}

void setup() {

  Serial.begin(115200);

  WiFi.begin("staff", "SRGB2020");

  while (WiFi.status() != WL_CONNECTED);

  mqtt.setServer("broker.hivemq.com", 1883);
  mqtt.setCallback(callback);

  while (!mqtt.connected()) {
    mqtt.connect("ESP32_2");
  }

  mqtt.subscribe("fablab/test");
}

void loop() {
  mqtt.loop();
}

AI prompt: I have two ESP32-C3 boards connected through MQTT using broker.hivemq.com.


Serial Monitor showing the received message ReciverMessage


đŸ•šī¸ Experiment 2 — Button Triggered Message

Hrach’s and Mariam’s second experiment was again to send a message, but this time the message would be sent when the button on the joystick was pressed.

Here is the joystick code:

#include <WiFi.h>
#include <PubSubClient.h>

#define PIN_SW 5

const char* ssid = "staff";
const char* password = "SRGB2020";

const char* mqtt_server = "broker.hivemq.com";

WiFiClient espClient;
PubSubClient client(espClient);

void reconnect() {
  while (!client.connected()) {

    String clientId = "JoystickESP32-";
    clientId += String((uint32_t)ESP.getEfuseMac(), HEX);

    if (client.connect(clientId.c_str())) {
      Serial.println("MQTT Connected");
    } else {
      Serial.print("MQTT Failed, rc=");
      Serial.println(client.state());
      delay(2000);
    }
  }
}

void setup() {
  Serial.begin(115200);

  pinMode(PIN_SW, INPUT_PULLUP);

  WiFi.begin(ssid, password);

  Serial.print("Connecting WiFi");

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

  Serial.println("\nWiFi Connected");

  client.setServer(mqtt_server, 1883);
}

void loop() {

  if (!client.connected())
    reconnect();

  client.loop();

  static bool lastState = HIGH;

  bool currentState = digitalRead(PIN_SW);

  if (lastState == HIGH && currentState == LOW) {

    client.publish("fablabarmenia/robot/button", "PRESSED");

    Serial.println("Published: PRESSED");

    delay(300);
  }

  lastState = currentState;
}

AI prompt: When I press the joystick button (SW), ESP32-C3 #2 publishes an MQTT message.

The code for the second ESP32-C3 is the same as the one shown above.

Serial Monitor showing the received message ReciverJoy


🚁 Final Goal — Opening the Drone Parachute Case

Finally, Mariam and Hrach achieved their goal of opening Hrach’s drone parachute case using the button on the joystick.

The joystick code does not change because it simply sends a message to the second chip again when the button is pressed.


And the following code was uploaded to the Drone Parachute Case chip:

#include <WiFi.h>
#include <PubSubClient.h>
#include <ESP32Servo.h>

const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";

const char* mqtt_server = "broker.hivemq.com";

const int SERVO_PIN = D6;   // GPIO21

Servo deployServo;

bool triggered = false;   // latch: deploy once, don't re-trigger mid-flight


WiFiClient espClient;
PubSubClient client(espClient);

void callback(char* topic, byte* payload, unsigned int length) {

  String message = "";

  for (int i = 0; i < length; i++) {
    message += (char)payload[i];
  }

  Serial.print("Topic: ");
  Serial.println(topic);

  Serial.print("Message: ");
  Serial.println(message);

  if (message == "PRESSED") {
    Serial.println("JOYSTICK BUTTON PRESSED!");
    deployServo.write(180);
    delay(500);
    deployServo.write(90);
  }
}

void reconnect() {

  while (!client.connected()) {

    String clientId = "HrachESP32-";
    clientId += String((uint32_t)ESP.getEfuseMac(), HEX);

    if (client.connect(clientId.c_str())) {

      Serial.println("MQTT Connected");

      client.subscribe("fablabarmenia/robot/button");

    } else {

      Serial.print("MQTT Failed, rc=");
      Serial.println(client.state());

      delay(2000);
    }
  }
}

void setup() {

  Serial.begin(115200);

  WiFi.begin("staff", "SRGB2020");

  deployServo.attach(SERVO_PIN);
  deployServo.write(90);  // start position

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

  Serial.println("\nWiFi Connected");

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected())
    reconnect();

  client.loop();
}

AI prompt: When I press the joystick button (SW), ESP32-C3 #2 publishes an MQTT message.

Here is the result in video format.


Last update: June 18, 2026