ESP32 - AirTag Documentation
Loading...
Searching...
No Matches
server.ino File Reference

ESP32 BLE Server with IMU integration and buzzer control. More...

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "IMU.h"
#include "IMU_STRUCT.h"
#include "IMU_REGISTER_MAP.h"
#include <Wire.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

Classes

class  ServerCallbacks
 Handles BLE server connect/disconnect events. More...
class  ButtonCallbacks
 Handles write events to the Button characteristic. More...

Macros

#define BUZZER_PIN   2
 GPIO pin for buzzer output.
#define SERVICE_UUID   "275dc6e0-dff5-4b56-9af0-584a5768a02a"
 BLE Service UUID.
#define BUTTON_CHAR_UUID   "b51bd845-2910-4f84-b062-d297ed286b1f"
 Button characteristic UUID.
#define IMU_CHAR_UUID   "0679c389-0d92-4604-aac4-664c43a51934"
 IMU characteristic UUID.

Functions

void IMUTask (void *pvParameters)
 Task that reads IMU data, processes orientation, and detects movement.
void ButtonRelayTask (void *pvParameters)
 Task that relays button press signals from BLE writes.
void BuzzerSetTask (void *pvParameters)
 Task that toggles buzzer on/off when button events occur.
void setup ()
 Arduino setup function. Initializes Serial, I2C, BLE, creates GATT server and tasks.
void loop ()
 Arduino loop function (unused, tasks handle logic).

Variables

BLECharacteristic * buttonChar
 Pointer to Button characteristic.
BLECharacteristic * imuChar
 Pointer to IMU characteristic.
BLEServer * server
 Pointer to BLE server object.
volatile bool deviceConnected = false
 Flag indicating central connection status.
volatile bool bleMessageReceived = false
 Flag indicating BLE message received.
SemaphoreHandle_t xButtonSignalSemaphore
 Semaphore used to signal button events between tasks.

Detailed Description

ESP32 BLE Server with IMU integration and buzzer control.

Implements a BLE GATT server with two characteristics:

  • Button characteristic (read/write)
  • IMU characteristic (notify for movement detection)

The system uses FreeRTOS tasks:

  • IMUTask: Reads IMU sensor data and detects movement
  • ButtonRelayTask: Handles BLE write events and signals via semaphore
  • BuzzerSetTask: Toggles a buzzer based on BLE commands

Includes orientation computation, linear acceleration processing, and movement detection with a smoothing filter.

Note
Requires BLE libraries and FreeRTOS on ESP32.
Author
George Evans Daenuwy & Rasya Fawwaz
Date
2025-08-21

Macro Definition Documentation

◆ BUTTON_CHAR_UUID

#define BUTTON_CHAR_UUID   "b51bd845-2910-4f84-b062-d297ed286b1f"

Button characteristic UUID.

◆ BUZZER_PIN

#define BUZZER_PIN   2

GPIO pin for buzzer output.

< Initializes BLE general functionality < Provides BLE server (peripheral role) functionality < BLE helper utilities such as UUID handling < Descriptor class for Client Characteristic Configuration Descriptor (CCCD) < Custom IMU driver < IMU data structure definition < IMU register map < I2C communication library

◆ IMU_CHAR_UUID

#define IMU_CHAR_UUID   "0679c389-0d92-4604-aac4-664c43a51934"

IMU characteristic UUID.

◆ SERVICE_UUID

#define SERVICE_UUID   "275dc6e0-dff5-4b56-9af0-584a5768a02a"

BLE Service UUID.

Function Documentation

◆ ButtonRelayTask()

void ButtonRelayTask ( void * pvParameters)

Task that relays button press signals from BLE writes.

Parameters
pvParametersFreeRTOS task parameter (unused).

◆ BuzzerSetTask()

void BuzzerSetTask ( void * pvParameters)

Task that toggles buzzer on/off when button events occur.

Parameters
pvParametersFreeRTOS task parameter (unused).

< Turn buzzer on

< Turn buzzer off

◆ IMUTask()

void IMUTask ( void * pvParameters)

Task that reads IMU data, processes orientation, and detects movement.

  • Reads accelerometer and gyroscope data
  • Applies complementary filter for orientation
  • Removes gravitational bias
  • Computes linear acceleration magnitude
  • Applies SMA filter to detect sustained movement
  • Notifies central via BLE when movement starts/stops
Parameters
pvParametersFreeRTOS task parameter (unused).

◆ loop()

void loop ( )

Arduino loop function (unused, tasks handle logic).

◆ setup()

void setup ( )

Arduino setup function. Initializes Serial, I2C, BLE, creates GATT server and tasks.

< Configure buzzer PWM

< Increase MTU size for better throughput

Variable Documentation

◆ bleMessageReceived

volatile bool bleMessageReceived = false

Flag indicating BLE message received.

◆ buttonChar

BLECharacteristic* buttonChar

Pointer to Button characteristic.

◆ deviceConnected

volatile bool deviceConnected = false

Flag indicating central connection status.

◆ imuChar

BLECharacteristic* imuChar

Pointer to IMU characteristic.

◆ server

BLEServer* server

Pointer to BLE server object.

◆ xButtonSignalSemaphore

SemaphoreHandle_t xButtonSignalSemaphore

Semaphore used to signal button events between tasks.