Week 11 | Networking and Communications

Group Assignment

This week's group assignment consisted of establishing communication between two different projects by sending a message from one device to another using networking or communication protocols. The objective was to understand how devices can exchange information, how communication protocols work, and how networking enables distributed systems and interactive projects.

During this assignment, we tested communication between two boards, sending and receiving data to verify that the connection was working correctly. This allowed us to understand the basics of serial communication, data transmission, and device synchronization.

The complete documentation of the group work can be found in the following page:

Group assignment documentation


Individual Assignment

This week I worked on embedded networking and communications by implementing wireless communication between a smartphone and a XIAO ESP32-C3 using Bluetooth Low Energy (BLE). The goal was to create a system that could receive commands from a mobile device and control actuators remotely.

This assignment also helped me integrate previous work from earlier weeks, including input devices, output devices, and embedded programming. In this case, I connected a PIR sensor, a switch, an LED, and a servo motor to the XIAO ESP32-C3, and then added wireless communication through BLE.


1. System Description

The system was designed to work in two different modes:

  • Automatic mode: the PIR sensor detects movement and activates the servo when the switch is turned on.
  • Manual mode: a smartphone sends commands via Bluetooth to control the servo and LED remotely.

This allowed me to combine physical interaction through sensors with wireless control through a mobile device.


2. System Architecture

The communication architecture of the system is the following:

Smartphone → Bluetooth Low Energy (BLE) → XIAO ESP32-C3 → Servo motor / LED / PIR sensor

In this setup, the smartphone works as a BLE client, while the XIAO ESP32-C3 works as a BLE server. The phone sends commands to the microcontroller, and the microcontroller interprets those commands to perform specific actions.


3. Hardware Used

  • XIAO ESP32-C3
  • PIR motion sensor
  • Servo motor
  • LED
  • Switch
  • Jumper wires
  • Smartphone

4. Pin Configuration

  • PIR sensor → D4
  • Switch → D5
  • LED → D6
  • Servo motor → D3
w8-1

5. Bluetooth Low Energy Implementation

For the wireless communication, I configured the XIAO ESP32-C3 as a BLE server. I created a custom BLE service and a writable BLE characteristic. The smartphone connects to the microcontroller and sends text commands through this characteristic.

The BLE communication was implemented using:

  • A custom Service UUID
  • A custom Characteristic UUID
  • A writable characteristic to receive commands from the phone

To test the communication, I used the mobile application LightBlue.


6. Commands Implemented

I defined simple text commands to control the system:

  • OPEN → moves the servo to the open position
  • CLOSE → moves the servo to the closed position
  • LEDON → turns the LED on
  • LEDOFF → turns the LED off

7. Mobile Application Test

First, I used the LightBlue application to scan nearby BLE devices. Once the XIAO ESP32-C3 was detected, I connected to the device and accessed the writable BLE characteristic.

Scanning nearby BLE devices with LightBlue
Connected to the XIAO_Flower BLE device
Writing commands to the BLE characteristic

At first, I found that the application was set to Hex mode, which caused an error when trying to send text commands. After changing the mode to UTF-8 String, I was able to write commands correctly and send them to the XIAO.

The command history shown by the application confirmed that the values were being sent successfully.

OPEN command sent from LightBlue
LEDOFF command sent from LightBlue

8. Physical Test

After establishing the BLE connection, I tested the system physically by sending commands from the smartphone while the XIAO ESP32-C3 was connected to the servo and the rest of the circuit.

The servo responded correctly when the OPEN and CLOSE commands were sent from the phone, which demonstrated successful wireless communication between the smartphone and the embedded device.


9. Program Logic

The program running on the XIAO ESP32-C3 continuously checks whether a Bluetooth command has been received. If a command is detected, the system executes the corresponding action. At the same time, the automatic logic using the PIR sensor and the switch remains active.

This means the system integrates both local physical interaction and remote wireless interaction.


10. Arduino Code

The Arduino program for the XIAO ESP32-C3 was developed with the assistance of ChatGPT. I used ChatGPT to help structure the Bluetooth Low Energy (BLE) communication, define the commands, and integrate the servo motor, LED, PIR sensor, and switch logic into a single program.

The prompt used to generate the base structure of the code was the following:

"Write an Arduino program for a XIAO ESP32-C3 that works as a BLE server. The device should receive text commands from a smartphone via Bluetooth Low Energy. Commands: OPEN -> move servo to open position. CLOSE -> move servo to closed position. LEDON -> turn LED on. LEDOFF -> turn LED off. The system should also read a PIR sensor and a switch. If the switch is on and the PIR detects motion, the servo should move automatically. Use pins: PIR -> D4 Switch -> D5 LED -> D6 Servo -> D3"

After generating the initial code, I modified and adjusted it to match the pin configuration, servo angles, and system behavior required for my project.


#define PIR_PIN D4
#define SWITCH_PIN D5
#define LED_PIN D6
#define SERVO_PIN D3

#include 
#include 
#include 
#include 

Servo myServo;

// UUIDs personalizados
#define SERVICE_UUID        "12345678-1234-1234-1234-1234567890ab"
#define CHARACTERISTIC_UUID "abcd1234-5678-1234-5678-abcdef123456"

String receivedCommand = "";

BLECharacteristic *pCharacteristic;

class MyCallbacks : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic *pCharacteristic) {
    String value = pCharacteristic->getValue();

    if (value.length() > 0) {
      receivedCommand = value;
      receivedCommand.trim();

      Serial.print("Received BLE command: ");
      Serial.println(receivedCommand);
    }
  }
};

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

  pinMode(PIR_PIN, INPUT);
  pinMode(SWITCH_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);

  myServo.attach(SERVO_PIN);
  myServo.write(0);

  BLEDevice::init("XIAO_Flower");
  BLEServer *pServer = BLEDevice::createServer();

  BLEService *pService = pServer->createService(SERVICE_UUID);

  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_WRITE
                    );

  pCharacteristic->setCallbacks(new MyCallbacks());
  pService->start();

  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->start();

  Serial.println("BLE ready. Waiting for commands...");
}

void loop() {
  // BLE commands
  if (receivedCommand == "OPEN") {
    myServo.write(180);
    receivedCommand = "";
  }
  else if (receivedCommand == "CLOSE") {
    myServo.write(0);
    receivedCommand = "";
  }
  else if (receivedCommand == "LEDON") {
    digitalWrite(LED_PIN, HIGH);
    receivedCommand = "";
  }
  else if (receivedCommand == "LEDOFF") {
    digitalWrite(LED_PIN, LOW);
    receivedCommand = "";
  }

  // Original logic
  int encendido = digitalRead(SWITCH_PIN);
  Serial.print("boton: ");
  Serial.println(encendido);

  delay(200);

  if (encendido == 1) {
    digitalWrite(LED_PIN, HIGH);

    int estado = digitalRead(PIR_PIN);
    Serial.print("pir: ");
    Serial.println(estado);

    if (estado == 1) {
      delay(200);
      myServo.write(0);
      delay(1000);
      myServo.write(180);
      delay(1000);
    } else {
      myServo.write(0);
    }
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}
							

Results

The final result was a working BLE communication system between a smartphone and the XIAO ESP32-C3. Through this communication, I was able to remotely control the servo motor and the LED.

This assignment demonstrated:

  • Wireless embedded communication using Bluetooth Low Energy
  • Communication between a smartphone and a microcontroller
  • Remote control of actuators
  • Integration of networking, programming, sensors, and outputs

Conclusion

This week helped me understand how embedded systems can communicate wirelessly using BLE. I learned how to configure the XIAO ESP32-C3 as a BLE server, define services and characteristics, and use a smartphone as a BLE client.

I also found it interesting to combine this communication system with my previous electronic setup, since it allowed me to control the project both automatically and manually. This will be very useful for my final project, where interaction, responsiveness, and communication between devices are important.


Reflection

At the beginning, setting up Bluetooth communication was a bit confusing, especially when dealing with BLE services, characteristics, and the data format used by the mobile application. One of the first issues I encountered was that the LightBlue app was configured in Hex mode, which prevented me from sending text commands correctly. After understanding the correct format and adjusting the settings to UTF-8 string, the communication started to work properly.

This assignment made me realize how powerful wireless communication can be in interactive projects. It also helped me see that communication is not only about sending data, but also about designing how different parts of a system interact.