Skip to content

12. Networking and Communications

Learning Outcomes:

  • Demonstrate workflows used in network design
  • Implement and interpret networking protocols and/or communication protocols

Introduction

In this documentation, I will describe my contribution to the group assignment “Sending Messages using ESP32-C3” during the Networking and Communications week of Fab Academy. The assignment aimed to establish communication between two devices using the Xiao ESP32-C3 microcontroller, focusing on Bluetooth as the communication method. I will summarize the tasks accomplished in the group assignment and explain how I extended the functionality to control an LED and a buzzer connected to a development board that I designed and produced in a previous week.

Group Assignment

For the group assignment, our group collaborated on a project centered around establishing communication between two projects using the Xiao ESP32-C3 microcontroller. The primary objective was to send messages wirelessly between devices, utilizing Bluetooth as the communication method.

We began by selecting Bluetooth due to its widespread availability and compatibility with various devices. Bluetooth offers a reliable means of exchanging data over short distances, making it suitable for our project’s requirements.

Our setup consisted of two nodes: Node 1, comprising the Xiao ESP32-C3 microcontroller, and Node 2, representing a mobile device such as a smartphone. Node 1 was programmed to receive messages from Node 2 over Bluetooth and display them on the serial monitor of the connected computer.

To achieve this, we utilized Arduino libraries for Bluetooth Low Energy (BLE) communication, enabling seamless interaction between the devices. The code for Node 1 included callbacks to handle incoming messages, while Node 2 utilized a mobile application for sending messages to Node 1.

After thorough testing, we successfully demonstrated the transmission of messages between Node 1 and Node 2. This project not only enhanced our understanding of embedded networking and communications but also showcased the practical application of Bluetooth technology in enabling seamless device-to-device communication.

Moving forward, we aim to explore further applications and extensions of this project, leveraging our newfound knowledge to tackle real-world challenges in IoT and embedded systems.

Bluetooth Communication Protocol

Bluetooth is a wireless technology standard used for exchanging data over short distances using UHF radio waves. It operates in the 2.4 GHz ISM band and uses frequency hopping to reduce interference and improve security. BLE (Bluetooth Low Energy) is a subset of Bluetooth designed for lower power consumption, making it ideal for battery-operated devices.

Addressing and Pairing Process

The addressing process in Bluetooth involves unique MAC addresses for each device. Pairing ensures secure communication between devices by exchanging security keys. During our project, we ensured that only the paired mobile device could communicate with the ESP32-C3 by using BLE services and characteristics with specific UUIDs. Additional nodes can be added to the system by defining unique characteristics and services, though practical limits depend on the Bluetooth controller’s capacity.

Individual Assignment

Description of the Development Board

The development board used in this project was designed and produced in the electronics design week. It serves as a platform for experimentation and prototyping, featuring various input and output components. For this project, I utilized the board’s capabilities to interface with an LED and a buzzer.

Implementation

Xiao ESP32C3

To achieve my goal, I connected the Xiao ESP32-C3 microcontroller to the development board, enabling communication between the mobile device and the LED/buzzer components. I modified the existing code to interpret incoming messages from the mobile device and trigger actions accordingly. This involved designing a protocol for message interpretation and implementing the necessary logic to control the LED and buzzer based on the received commands.

Code and Explanation

Here is the code used for the project:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

#define LED_1 10
#define LED_2 4

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

      if (value.length() > 0) {
        Serial.println("Received message: ");
        Serial.println(value.c_str());

        if(value == "LED1") {
          digitalWrite(LED_1, HIGH);
          Serial.println("LED 1 turned on");
        }
        else if(value == "Buzzer") {
          digitalWrite(LED_2, HIGH);
          Serial.println("Buzzer turned on");
        }
        else if(value == "LED1.") {
          digitalWrite(LED_1, LOW);
          Serial.println("LED 1 turned off");
        }
        else if(value == "Buzzer.") {
          digitalWrite(LED_2, LOW);
          Serial.println("Buzzer turned off");
        }
        else {
          Serial.println("Unknown command");
        }
      }
    }
};

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

  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);

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

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

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

  pCharacteristic->setCallbacks(new MyCallbacks());

  pCharacteristic->setValue("Hello World");
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}

Explanation

  1. Initialization:

    • Libraries for BLE communication are included.
    • SERVICE_UUID and CHARACTERISTIC_UUID are defined for BLE service and characteristic identification.
    • LED_1 and LED_2 pins are defined for the LED and buzzer.
  2. Callbacks Class:

    • MyCallbacks class extends BLECharacteristicCallbacks to handle incoming BLE messages.
    • The onWrite method checks the received message and triggers the corresponding action (turning the LED or buzzer on/off).
  3. Setup Function:

    • Serial communication is initiated.
    • Pins for the LED and buzzer are set as output.
    • BLE device, server, service, and characteristic are initialized.
    • Callback functions are set to handle write operations on the characteristic.
    • BLE advertising is started to make the device discoverable.
  4. Loop Function:

    • The main loop runs continuously with a delay, keeping the BLE server active.

Uploading the code:

Code IDE

Mobile App: LightBlue

To facilitate communication between the mobile device and the ESP32-C3, we used the LightBlue app. Here’s a brief overview of its setup:

  1. Download and Install:

  2. Connecting to ESP32-C3:

    • Open the app and scan for nearby Bluetooth devices.
    • Select “MyESP32” from the list to connect.
Light Blue
  1. Sending Messages:

    • Once connected, navigate to the services and characteristics.
    • Use the characteristic UUID to send messages like “LED1” or “Buzzer” to control the respective components on the ESP32-C3.

For more detailed instructions, refer to this guide.

Testing and Results

Throughout the development process, I conducted extensive testing to ensure the functionality and reliability of the system. By sending different messages from the mobile device, I verified the LED and buzzer responded appropriately, confirming successful communication between the devices. The results demonstrated the effectiveness of the integrated solution in enabling remote control of physical components.

Conclusion

In conclusion, my contribution to the group assignment extended its functionality by incorporating hardware control capabilities. This project provided valuable insights into the practical applications of embedded networking and communications, demonstrating how simple devices can be interconnected to enable remote control and automation. Moving forward, I envision further enhancements and applications for this technology, contributing to the advancement of IoT and embedded systems.