Hello Everyone, I need help on connecting my sender and receiver BLE UUID. my microprocessor im using is Seeed NRF8240:
Sender code:
#include <ArduinoBLE.h>
#include "fix_fft.h"
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
// Common BLE UUID
const char* bleUuid = "19B10000-E8F2-537E-4F6C-D104768A1215";
// EEG Variables
#define adc_input A0
const unsigned long eeg_sample_freq = 1024; // Hz (Max freq = 512Hz)
const short n = 64; // number of data points (must be power of 2)
const unsigned long eeg_sample_period = 1000000 / eeg_sample_freq; // uS
const short m = (short(log10(n) / log10(2))); // equivalent to m=log2(n) from which 2^m=n
char data[n]; // real values
char im[n]; // imaginary values
short dc_offset;
short dc_counter = 5;
bool eeg_data_valid = false;
short stressDetectedEEG = 0; // Variable to store EEG stress detection
// Oximeter Variables
MAX30105 particleSensor;
const byte RATE_SIZE = 4;
byte rates[RATE_SIZE];
byte rateSpot = 0;
long lastBeat = 0;
float beatsPerMinute;
int beatAvg;
long irValue;
short stressDetectedOXY = 0; // Variable to store Oximeter stress detection
// BLE Characteristic
BLECharacteristic combinedChar(bleUuid, BLERead | BLEWrite, 2);
// Timers and mode switch
unsigned long Timer1, Timer2;
unsigned long modeSwitchTimer = 0;
bool isEEGMode = true;
void setup() {
Serial.begin(115200);
// Initialize sensors
pinMode(adc_input, INPUT);
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30105 was not found. Please check wiring/power.");
while (1);
}
particleSensor.setup();
particleSensor.setPulseAmplitudeRed(0x0A);
particleSensor.setPulseAmplitudeGreen(0);
// BLE Initialization
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
// Create a BLE service
BLEService combinedService("19B10000-E8F2-537E-4F6C-D104768A1215"); // Use a unique UUID
// Add the characteristic to the service
combinedService.addCharacteristic(combinedChar);
BLE.setLocalName("XIAO12");
BLE.setAdvertisedServiceUuid(bleUuid);
BLE.advertise();
// Start timers
Timer1 = micros() + eeg_sample_period;
Timer2 = micros() + 1000000L;
modeSwitchTimer = millis();
}
void loop() {
BLEDevice central = BLE.central();
if (central.connected()) {
static bool firstCycleCompleted = false;
if (millis() - modeSwitchTimer > 30000) {
isEEGMode = !isEEGMode;
modeSwitchTimer = millis();
if (!isEEGMode) {
if (firstCycleCompleted) {
uint8_t finalStress = stressDetectedEEG && stressDetectedOXY;
uint8_t payload[2] = {finalStress, (uint8_t)min(255, beatAvg)};
combinedChar.writeValue(payload, sizeof(payload));
} else {
firstCycleCompleted = true;
}
}
}
if (isEEGMode) {
processEEGData();
} else {
processOximeterData();
}
} else {
Serial.println("Waiting for a connection...");
}
}
void processEEGData() {
static short i = 0; // Sample counter
static int eegThresholdViolations = 0; // Counts the number of threshold violations
static bool firstRun = true; // Indicates if this is the first run
// Reset the violation counter at the beginning of each EEG mode cycle
if (firstRun) {
eegThresholdViolations = 0;
firstRun = false;
}
// Loop control
while (micros() < Timer1); // Wheel-spin until next sample due
Timer1 += eeg_sample_period; // Reset loop timer
if (i < n) {
// Acquire the data
short value = analogRead(adc_input);
data[i] = map(value, 0, 1023, 0, 255);
im[i] = 0;
i++;
} else {
// Display data once per second
if (micros() > Timer2) {
Timer2 += 1000000L;
// Process data
fix_fft(data, im, m, 0);
// Calculate amplitudes
for (short j = 0; j < n / 2; j++) {
data[j] = (sqrt(data[j] * data[j] + im[j] * im[j]));
}
// Capture dc offset
if (!eeg_data_valid && (dc_counter > 0)) {
dc_counter--;
if (dc_counter < 1) {
eeg_data_valid = true;
dc_offset = data[0];
}
}
// Display the data
if (eeg_data_valid) {
// Remove dc offset
data[0] -= dc_offset;
// Send bin 1 to the Serial Plotter
Serial.println((short)data[1]);
// Stress detection logic
if (data[1] >= 200) {
eegThresholdViolations++;
}
}
}
if (i == n - 1) { // Check if this is the last sample in the cycle
if (eegThresholdViolations >= 2) { // If the value has gone over 200 two times within the cycle
Serial.println("Stressed in Beta band");
stressDetectedEEG = 1;
}
firstRun = true; // Reset for the next EEG cycle
eegThresholdViolations = 0; // Reset the count for the next cycle
}
// Prepare for next sample
i = 0; // Reset data acquisition
Timer1 = micros() + eeg_sample_period; // Reset loop timer
}
}
void processOximeterData() {
irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true) {
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20) {
rates[rateSpot++] = (byte)beatsPerMinute;
rateSpot %= RATE_SIZE;
beatAvg = 0;
for (byte x = 0; x < RATE_SIZE; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}
Serial.print("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.println(beatAvg);
if (beatAvg > 60){
stressDetectedOXY = 1;
}
}
Receiver code:
#include <ArduinoBLE.h>
void setup() {
Serial.begin(115200);
delay(3000);
Serial.println("Starting BLE setup...");
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
Serial.println("BLE initialized");
// Set receiver as central (unchanged)
BLE.setLocalName("ReceiverDevice");
Serial.println("Local name set");
}
void loop() {
// Add the local name to scan for (unchanged)
BLE.scanForName("XIAO12");
Serial.println("Scanning for device named XIAO12");
Serial.println("Bluetooth device active, waiting for connections...");
// Check for BLE devices (unchanged)
BLEDevice peripheral = BLE.available();
if (peripheral) {
// Check if the peripheral has the service we're interested in (unchanged)
if (peripheral.hasService("19B10000-E8F2-537E-4F6C-D104768A1215")) {
Serial.print("Found peripheral with desired service: ");
Serial.println(peripheral.address());
// Connect to the peripheral (unchanged)
if (peripheral.connect()) {
Serial.println("Connected to peripheral");
// Discover the service (unchanged)
if (peripheral.discoverService("19B10000-E8F2-537E-4F6C-D104768A1215")) {
// Retrieve the characteristic (unchanged)
BLECharacteristic charac = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1215");
// Read data if the characteristic is valid (unchanged)
if (charac) {
uint8_t data[2]; // Buffer to store the read data
while (peripheral.connected()) {
if (charac.valueUpdated()) {
charac.readValue(data, sizeof(data));
uint8_t stress = data[0];
uint8_t avgBPM = data[1];
Serial.print("Stress: ");
Serial.println(stress ? "Yes" : "No");
Serial.print("Avg BPM: ");
Serial.println(avgBPM);
}
}
Serial.println("Peripheral disconnected");
}
}
}
}
}
}