How to send data via BLE?

Hello,

I'm trying to create a project with a Central BLE that collects the data (temperature, humidity, pressure, gyro, etc.) and send it to several Peripheral BLEs on demand by the user. Basically, the data will only be sent to the Peripheral with a press of a button (on the peripheral side) by the user.

I created a previous project with the help of you guys on sending time data from one Arduino to another via BLE, and I used bits and pieces of the codes from that project to start with this new project.

Here is the code from the time project I created previously:

BLECharacteristic currentTimeCharacteristic = peripheral.characteristic( BLE_UUID_CURRENT_TIME );
if ( currentTimeCharacteristic )
{
  currentTimeCharacteristic.writeValue( currentTimeData.bytes, sizeof currentTimeData.bytes );
}

And I modified it with this:

BLECharacteristic currentPressureCharacteristic = peripheral.characteristic(BLE_UUID_PRESSURE);
if (currentPressureCharacteristic) {
  currentPressureCharacteristic.writeValue(currentPressureData, sizeof currentPressureData);
}
       
BLECharacteristic currentTemperatureCharacteristic = peripheral.characteristic(BLE_UUID_TEMPERATURE);
if (currentTemperatureCharacteristic) {
  currentTemperatureCharacteristic.writeValue(currentTemperatureData, sizeof currentTemperatureData);
}
        
BLECharacteristic currentHumidityCharacteristic = peripheral.characteristic(BLE_UUID_HUMIDITY);
if (currentHumidityCharacteristic) {
  currentHumidityCharacteristic.writeValue(currentHumidityData, sizeof currentHumidityData);
}

Doing this modifications is giving me an error - no matching function for call to 'BLECharacteristic::writeValue(floats&, unsigned int)'.

I don't understand why I can't just modify the previous codes to work to send data like, humidity, temperature, etc. Can you please help me understand the basics on how to send data via BLE? Please be patient with me.

I attached the sketch. Please let me know what I'm doing wrong. I actually can work on the rest of the ideas I wanted to create. But the only problem I'm having is how I can send data. Maybe a really basic example can also help?

Thanks in advanced.

Central.ino (8.81 KB)

The issue is that the writeValue() function of the BLECharacteristic has multiple variants but none of them match the parameter you have passed. Have a look at the documentation

and the following header file

\your projects folder\libraries\ArduinoBLE\src\BLECharacteristic.h

You are calling the function with two parameters. This only works when the buffer (first parameter) is an array of bytes. A solution could be to remove the second parameter, but you pass a float in the first parameter. There is no variant for that type. So, the compiler would not know which other type to use because you can cast float into all kinds of other types.
Note: This could/should work for int types.

If you want to use float you can create a union of a float and a byte array and send the byte array similar to the time types, we used in the previous examples.

Another solution which seems to work is the following cast. I found something similar on Google and tried this until the values seemed OK.

float floatValue;
BLECharacteristic floatValueCharacteristic = peripheral.characteristic( BLE_UUID_FLOAT_VALUE );
if ( floatValueCharacteristic )
{
  floatValueCharacteristic.writeValue( ( byte* ) &floatValue, sizeof( float ) );
}

Note: You can shorten the variable and characteristic names by removing "current". I only used that before because that is what the BLE specification uses for the currentTime service.
I would use temperatureCharacteristic, pressureCharacteristic, ...

OK, Thanks again, Klaus. I was able to get pass that error.

But another problem again. I wanted the BLE to turn off after it times out, or after sending. It works at first -- it turns on, times out, then ends the BLE. But after that, it won't turn on again when it calls bleTask(). I don't understand why.

switch (state) {
    case BLE_STATE_OFF:
      if (updatePeripheral) {
        bleActive = true;
        updatePeripheral = false;
        Serial.println("BLE Active.");
        state++;
        break;
      }
      break;
    case BLE_STATE_BEGIN:
      if (!BLE.begin()) {
        Serial.println("Starting BLE Failed.");
        break;
      }
      digitalWrite(LED_BUILTIN, HIGH);
      state++;
      break;
    case BLE_STATE_START_SCAN:
      BLE.scanForUuid(BLE_UUID_PRESSURE);
      BLE.scanForUuid(BLE_UUID_TEMPERATURE);
      BLE.scanForUuid(BLE_UUID_HUMIDITY);
      previousMillis = millis();
      Serial.println("State BLE Start Scan");
      state++;
      break;
    case BLE_STATE_SCAN:
      if (millis() - previousMillis > BLE_SCAN_TIMEOUT) {
        BLE.stopScan();
        state = BLE_STATE_END;
        break;
      }
      peripheral = BLE.available();
      if (!peripheral) {
        break;
      }
      if (peripheral.localName() != "Test Arduino") {
        break;
      }
      state++;
      Serial.println("Test Arduino found.");
      break;
    case BLE_STATE_STOP_SCAN:
      BLE.stopScan();
      Serial.println("State BLE Scan Stop");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_CONNECT:
      if (!peripheral.connect()) {
        state = BLE_STATE_START_SCAN;
      }
      Serial.println("State Peripheral Connect");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_DISCOVER:
      if (!peripheral.discoverAttributes()) {
        peripheral.disconnect();
        state = BLE_STATE_START_SCAN;
      }
      Serial.println("State Peripheral Discover");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_WRITE: {
        BLECharacteristic currentPressureDataCharacteristic = peripheral.characteristic(BLE_UUID_PRESSURE);
        if (currentPressureDataCharacteristic) {
          currentPressureDataCharacteristic.writeValue((byte*) &currentPressureDataCharacteristic, sizeof(float));
        }
        BLECharacteristic currentTemperatureDataCharacteristic = peripheral.characteristic(BLE_UUID_TEMPERATURE);
        if (currentTemperatureDataCharacteristic) {
          currentTemperatureDataCharacteristic.writeValue((byte*) &currentTemperatureDataCharacteristic, sizeof(float));
        }
        BLECharacteristic currentHumidityDataCharacteristic = peripheral.characteristic(BLE_UUID_HUMIDITY);
        if (currentHumidityDataCharacteristic) {
          currentHumidityDataCharacteristic.writeValue((byte*) &currentHumidityDataCharacteristic, sizeof(float));
        }
        Serial.println("State Peripheral Write");
      }
      state++;
      break;
    case BLE_STATE_END:
      state = BLE_STATE_OFF;
      digitalWrite(LED_BUILTIN, LOW);
      BLE.end();
      bleActive = false;
      Serial.println("BLE End.1");
      break;
    default:
      state = BLE_STATE_OFF;
      digitalWrite(LED_BUILTIN, LOW);
      BLE.end();
      bleActive = false;
      Serial.println("BLE End.2");
      break;
  }

I attached the sketch. It's similar to the Time Central, but I just removed the WiFi part.

Basically, the loop is just this:

void loop() {
  #define UPDATE_INTERVAL 60 * 1000

  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > UPDATE_INTERVAL) {
    previousMillis = currentMillis;
    sensorsCheck();
  }
  
  bleTask();
  
}

I tried adding BLE.begi() in the case BLE_STATE_OFF, but it would crash when I run it. Adding BLE.begin() in the if (!BLE.begin()) statement of case BLE_STATE_BEGIN doesn't fix it either.

Weather_Station_Central.ino (10.3 KB)

Add a peripheral.disconnect(); just before the BLE.end() in the BLE_STATE_END and default state. This should fix the issue.

I added that line before the BLE.end() in those cases. But I'm still unable to get the BLE running after the first "cycle".

Here's what my bleTask() looks like:

void bleTask() {
  enum BLE_STATE_TYPE { BLE_STATE_OFF,
                        BLE_STATE_BEGIN,
                        BLE_STATE_START_SCAN,
                        BLE_STATE_SCAN,
                        BLE_STATE_STOP_SCAN,
                        BLE_STATE_PERIPHERAL_CONNECT,
                        BLE_STATE_PERIPHERAL_DISCOVER,
                        BLE_STATE_PERIPHERAL_WRITE,
                        BLE_STATE_END,
                        BLE_STATE_RESTART = 255
                      };

  #define BLE_SCAN_TIMEOUT 10000

  static int state = BLE_STATE_OFF;
  static unsigned long previousMillis = 0;
  static BLEDevice peripheral;

  switch (state) {
    case BLE_STATE_OFF:
      if (updatePeripheral) {
        bleActive = true;
        updatePeripheral = false;
        Serial.println("BLE Active.");
        state++;
        break;
      }
      break;
    case BLE_STATE_BEGIN:
      if (!BLE.begin()) {
        Serial.println("Starting BLE Failed.");
        break;
      }
      digitalWrite(LED_BUILTIN, HIGH);
      state++;
      break;
    case BLE_STATE_START_SCAN:
      BLE.scanForUuid(BLE_UUID_PRESSURE);
      BLE.scanForUuid(BLE_UUID_TEMPERATURE);
      BLE.scanForUuid(BLE_UUID_HUMIDITY);
      previousMillis = millis();
      Serial.println("State BLE Start Scan");
      state++;
      break;
    case BLE_STATE_SCAN:
      if (millis() - previousMillis > BLE_SCAN_TIMEOUT) {
        BLE.stopScan();
        state = BLE_STATE_END;
        break;
      }
      peripheral = BLE.available();
      if (!peripheral) {
        break;
      }
      if (peripheral.localName() != "Test Arduino") {
        break;
      }
      state++;
      Serial.println("Test Arduino found.");
      break;
    case BLE_STATE_STOP_SCAN:
      BLE.stopScan();
      Serial.println("State BLE Scan Stop");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_CONNECT:
      if (!peripheral.connect()) {
        state = BLE_STATE_START_SCAN;
      }
      Serial.println("State Peripheral Connect");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_DISCOVER:
      if (!peripheral.discoverAttributes()) {
        peripheral.disconnect();
        state = BLE_STATE_START_SCAN;
      }
      Serial.println("State Peripheral Discover");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_WRITE: {
        BLECharacteristic currentPressureDataCharacteristic = peripheral.characteristic(BLE_UUID_PRESSURE);
        if (currentPressureDataCharacteristic) {
          currentPressureDataCharacteristic.writeValue((byte*) &currentPressureDataCharacteristic, sizeof(float));
        }
        BLECharacteristic currentTemperatureDataCharacteristic = peripheral.characteristic(BLE_UUID_TEMPERATURE);
        if (currentTemperatureDataCharacteristic) {
          currentTemperatureDataCharacteristic.writeValue((byte*) &currentTemperatureDataCharacteristic, sizeof(float));
        }
        BLECharacteristic currentHumidityDataCharacteristic = peripheral.characteristic(BLE_UUID_HUMIDITY);
        if (currentHumidityDataCharacteristic) {
          currentHumidityDataCharacteristic.writeValue((byte*) &currentHumidityDataCharacteristic, sizeof(float));
        }
        Serial.println("State Peripheral Write");
      }
      state++;
      break;
    case BLE_STATE_END:
      peripheral.disconnect();
      state = BLE_STATE_OFF;
      digitalWrite(LED_BUILTIN, LOW);
      BLE.end();
      bleActive = false;
      Serial.println("BLE End.1");
      break;
    default:
      peripheral.disconnect();
      state = BLE_STATE_OFF;
      digitalWrite(LED_BUILTIN, LOW);
      BLE.end();
      bleActive = false;
      Serial.println("BLE End.2");
      break;
  }
}

I had a look at your Weather_Station_Central.ino. You are using delay(). Before looking at any other issue. Using delay is a very bad idea. You want your loop to keep running.

I suggest you divide your sensorsCheck function. You can create a similar state machine we used for the BLE stack and use it for the display. Use millis for the timing. Ether inside or outside the state machine.

I tried removing the delay, but it didn't fix it.

I removed all the codes and I only left the bleTask with no delay. But the BLE still won't start after the first "cycle".

#include <ArduinoBLE.h> //BLE Library.
#include <Arduino_LSM9DS1.h> //Include the library for 9-axis IMU.
#include <Arduino_LPS22HB.h> //Include library to read Pressure.
#include <Arduino_HTS221.h> //Include library to read Temperature and Humidity.
#include <Arduino_APDS9960.h> //Include library for colour, proximity, light intensity, and gesture recognition.
#include <Adafruit_SSD1306.h> //OLED Display Library.
#include <mbed.h> //mBedOS Library.

#define SCREEN_WIDTH 128 //OLED Display Width.
#define SCREEN_HEIGHT 64 //OLED Display Height.

//Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 //Reset pin # (or -1 if sharing Arduino reset pin).
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define BLE_UUID_CURRENT_TIME "2A2B"
#define BLE_UUID_PRESSURE "2A6D"
#define BLE_UUID_TEMPERATURE "2A6E"
#define BLE_UUID_HUMIDITY "2A6F"

boolean updatePeripheral = false;
boolean updateNow = false;
boolean bleActive = false;

float accel_x, accel_y, accel_z;
float gyro_x, gyro_y, gyro_z;
float pressure, currentPressureData;
float temperature, humidity, currentTemperatureData, currentHumidityData;
int proximity;
int r, g, b, a;

int sensorCount = 0;

void setup() {
  Serial.begin(9600); //Serial monitor to display all sensor values 

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Initialize with I2c addres 0x3C.
  display.clearDisplay();
  display.display();

  pinMode(LED_BUILTIN, OUTPUT); //Built-in LED.
  digitalWrite(LED_BUILTIN, LOW);

 }

void loop() {
  #define UPDATE_INTERVAL 20 * 1000

  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > UPDATE_INTERVAL) {
    previousMillis = currentMillis;
  
    bleTask();
  }
  
}

void bleTask() {
  enum BLE_STATE_TYPE { BLE_STATE_OFF,
                        BLE_STATE_BEGIN,
                        BLE_STATE_START_SCAN,
                        BLE_STATE_SCAN,
                        BLE_STATE_STOP_SCAN,
                        BLE_STATE_PERIPHERAL_CONNECT,
                        BLE_STATE_PERIPHERAL_DISCOVER,
                        BLE_STATE_PERIPHERAL_WRITE,
                        BLE_STATE_END,
                        BLE_STATE_RESTART = 255
                      };

  #define BLE_SCAN_TIMEOUT 10000

  static int state = BLE_STATE_OFF;
  static unsigned long previousMillis = 0;
  static BLEDevice peripheral;

  switch (state) {
    case BLE_STATE_OFF:
      //if (updatePeripheral) {
        bleActive = true;
        updatePeripheral = false;
        Serial.println("BLE Active.");
        state++;
        break;
      // }
      //break;
    case BLE_STATE_BEGIN:
      if (!BLE.begin()) {
        Serial.println("Starting BLE Failed.");
        break;
      }
      
      //Set advertised local name.
      BLE.setDeviceName("Weather Station");
      BLE.setLocalName("Weather Station");
     
      //Start advertising.
      BLE.advertise();
    
      digitalWrite(LED_BUILTIN, HIGH);
      state++;
      break;
    case BLE_STATE_START_SCAN:
      BLE.scanForUuid(BLE_UUID_PRESSURE);
      BLE.scanForUuid(BLE_UUID_TEMPERATURE);
      BLE.scanForUuid(BLE_UUID_HUMIDITY);
      previousMillis = millis();
      Serial.println("State BLE Start Scan");
      state++;
      break;
    case BLE_STATE_SCAN:
      if (millis() - previousMillis > BLE_SCAN_TIMEOUT) {
        BLE.stopScan();
        state = BLE_STATE_END;
        break;
      }
      peripheral = BLE.available();
      if (!peripheral) {
        break;
      }
      if (peripheral.localName() != "Weather Satelite") {
        break;
      }
      state++;
      Serial.println("Weather Satelite found.");
      break;
    case BLE_STATE_STOP_SCAN:
      BLE.stopScan();
      Serial.println("State BLE Scan Stop");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_CONNECT:
      if (!peripheral.connect()) {
        state = BLE_STATE_START_SCAN;
      }
      Serial.println("State Peripheral Connect");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_DISCOVER:
      if (!peripheral.discoverAttributes()) {
        peripheral.disconnect();
        state = BLE_STATE_START_SCAN;
      }
      Serial.println("State Peripheral Discover");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_WRITE: {
        BLECharacteristic currentPressureDataCharacteristic = peripheral.characteristic(BLE_UUID_PRESSURE);
        if (currentPressureDataCharacteristic) {
          currentPressureDataCharacteristic.writeValue((byte*) &currentPressureDataCharacteristic, sizeof(float));
        }
        BLECharacteristic currentTemperatureDataCharacteristic = peripheral.characteristic(BLE_UUID_TEMPERATURE);
        if (currentTemperatureDataCharacteristic) {
          currentTemperatureDataCharacteristic.writeValue((byte*) &currentTemperatureDataCharacteristic, sizeof(float));
        }
        BLECharacteristic currentHumidityDataCharacteristic = peripheral.characteristic(BLE_UUID_HUMIDITY);
        if (currentHumidityDataCharacteristic) {
          currentHumidityDataCharacteristic.writeValue((byte*) &currentHumidityDataCharacteristic, sizeof(float));
        }
        Serial.println("State Peripheral Write");
      }
      state++;
      break;
    case BLE_STATE_END:
      state = BLE_STATE_OFF;
      digitalWrite(LED_BUILTIN, LOW);
      peripheral.disconnect();
      BLE.end();
      bleActive = false;
      Serial.println("BLE End.1");
      break;
    default:
      state = BLE_STATE_OFF;
      digitalWrite(LED_BUILTIN, LOW);
      peripheral.disconnect();
      BLE.end();
      bleActive = false;
      Serial.println("BLE End.2");
      break;
  }
}

One issue is that you call the bleTask only every 20 seconds. You have to call the bleTask all the time. Use the updatePeripheral flag to control the timing of the start. Once the bleTask switches from BLE_STATE_OFF to BLE_STATE_BEGIN it needs to be executed many times a second to ensure the BLE stack works.

Do not worry about calling it in BLE_STATE_OFF. The test of the updatePeripheral flag costs you virtually nothing. Just a few processor cycles. That is why I created the this state in the first place. The BLE module is not used in this state.

Ok. As I understood, I have to use the updatePeripheral to activate the BLE. I created a counter that will count up to 7, which will call BLE_STATE_OFF. So this way, bleTask is called all the time, as you said.

But I don't know why the BLE won't run after the first cycle.

#include <ArduinoBLE.h> //BLE Library.
#include <Arduino_LSM9DS1.h> //Include the library for 9-axis IMU.
#include <Arduino_LPS22HB.h> //Include library to read Pressure.
#include <Arduino_HTS221.h> //Include library to read Temperature and Humidity.
#include <Arduino_APDS9960.h> //Include library for colour, proximity, light intensity, and gesture recognition.
#include <Adafruit_SSD1306.h> //OLED Display Library.

#define SCREEN_WIDTH 128 //OLED Display Width.
#define SCREEN_HEIGHT 64 //OLED Display Height.

//Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 //Reset pin # (or -1 if sharing Arduino reset pin).
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define BLE_UUID_CURRENT_TIME "2A2B"
#define BLE_UUID_PRESSURE "2A6D"
#define BLE_UUID_TEMPERATURE "2A6E"
#define BLE_UUID_HUMIDITY "2A6F"

boolean updatePeripheral = false;
boolean updateNow = false;
boolean bleActive = false;

float accel_x, accel_y, accel_z;
float gyro_x, gyro_y, gyro_z;
float pressure, currentPressureData;
float temperature, humidity, currentTemperatureData, currentHumidityData;
int proximity;
int r, g, b, a;

int sensorCount = 0;

void setup() {
  Serial.begin(9600); //Serial monitor to display all sensor values 

  if (!IMU.begin()) { //Initialize IMU sensor 
    Serial.println("Failed to initialize IMU!"); 
    while (1);
  }

  if (!BARO.begin()) { //Initialize Pressure sensor 
    Serial.println("Failed to initialize Pressure Sensor!"); 
    while (1);
  }

  if (!HTS.begin()) { //Initialize Temperature and Humidity sensor 
    Serial.println("Failed to initialize Temperature and Humidity Sensor!"); 
    while (1);
  }

  if (!APDS.begin()) { //Initialize Colour, Proximity and Gesture sensor 
    Serial.println("Failed to initialize Colour, Proximity and Gesture Sensor!"); 
    while (1);
  }

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Initialize with I2c addres 0x3C.
  display.clearDisplay();
  display.display();

  pinMode(LED_BUILTIN, OUTPUT); //Built-in LED.
  digitalWrite(LED_BUILTIN, LOW);

  //sensorsCheck();
 }

void loop() {
  #define UPDATE_INTERVAL 5 * 1000

  if (updatePeripheral == false) {
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > UPDATE_INTERVAL) {
    previousMillis = currentMillis;
    //sensorsCheck();
    sensorCount ++;
    Serial.print("Sensor Count = ");Serial.println(sensorCount);
  }
  }

  if (sensorCount == 7) {
    updateNow = false;
    updatePeripheral = true;
    sensorCount = 0;
  }
  
  
  
  bleTask();
  
}


void bleTask() {
  enum BLE_STATE_TYPE { BLE_STATE_OFF,
                        BLE_STATE_BEGIN,
                        BLE_STATE_START_SCAN,
                        BLE_STATE_SCAN,
                        BLE_STATE_STOP_SCAN,
                        BLE_STATE_PERIPHERAL_CONNECT,
                        BLE_STATE_PERIPHERAL_DISCOVER,
                        BLE_STATE_PERIPHERAL_WRITE,
                        BLE_STATE_END,
                        BLE_STATE_RESTART = 255
                      };

  #define BLE_SCAN_TIMEOUT 10000

  static int state = BLE_STATE_OFF;
  static unsigned long previousMillis = 0;
  static BLEDevice peripheral;

  switch (state) {
    case BLE_STATE_OFF:
      if (updatePeripheral) {
        bleActive = true;
        //updatePeripheral = false;
        Serial.println("BLE Active.");
        state++;
        break;
       }
      break;
    case BLE_STATE_BEGIN:
      if (!BLE.begin()) {
        Serial.println("Starting BLE Failed.");
        break;
      }
      
      //Set advertised local name.
      BLE.setDeviceName("Weather Station");
      BLE.setLocalName("Weather Station");
     
      //Start advertising.
      BLE.advertise();
    
      digitalWrite(LED_BUILTIN, HIGH);
      state++;
      break;
    case BLE_STATE_START_SCAN:
      BLE.scanForUuid(BLE_UUID_PRESSURE);
      BLE.scanForUuid(BLE_UUID_TEMPERATURE);
      BLE.scanForUuid(BLE_UUID_HUMIDITY);
      previousMillis = millis();
      Serial.println("State BLE Start Scan");
      state++;
      break;
    case BLE_STATE_SCAN:
      if (millis() - previousMillis > BLE_SCAN_TIMEOUT) {
        BLE.stopScan();
        state = BLE_STATE_END;
        break;
      }
      peripheral = BLE.available();
      if (!peripheral) {
        break;
      }
      if (peripheral.localName() != "Weather Satelite") {
        break;
      }
      state++;
      Serial.println("Weather Satelite found.");
      break;
    case BLE_STATE_STOP_SCAN:
      BLE.stopScan();
      Serial.println("State BLE Scan Stop");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_CONNECT:
      if (!peripheral.connect()) {
        state = BLE_STATE_START_SCAN;
      }
      Serial.println("State Peripheral Connect");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_DISCOVER:
      if (!peripheral.discoverAttributes()) {
        peripheral.disconnect();
        state = BLE_STATE_START_SCAN;
      }
      Serial.println("State Peripheral Discover");
      state++;
      break;
    case BLE_STATE_PERIPHERAL_WRITE: {
        BLECharacteristic currentPressureDataCharacteristic = peripheral.characteristic(BLE_UUID_PRESSURE);
        if (currentPressureDataCharacteristic) {
          currentPressureDataCharacteristic.writeValue((byte*) &currentPressureDataCharacteristic, sizeof(float));
        }
        BLECharacteristic currentTemperatureDataCharacteristic = peripheral.characteristic(BLE_UUID_TEMPERATURE);
        if (currentTemperatureDataCharacteristic) {
          currentTemperatureDataCharacteristic.writeValue((byte*) &currentTemperatureDataCharacteristic, sizeof(float));
        }
        BLECharacteristic currentHumidityDataCharacteristic = peripheral.characteristic(BLE_UUID_HUMIDITY);
        if (currentHumidityDataCharacteristic) {
          currentHumidityDataCharacteristic.writeValue((byte*) &currentHumidityDataCharacteristic, sizeof(float));
        }
        Serial.println("State Peripheral Write");
      }
      state++;
      break;
    case BLE_STATE_END:
      state = BLE_STATE_OFF;
      updatePeripheral = false;
      digitalWrite(LED_BUILTIN, LOW);
      peripheral.disconnect();
      BLE.end();
      bleActive = false;
      Serial.println("BLE End.1");
      break;
    default:
      state = BLE_STATE_OFF;
      updatePeripheral = false;
      digitalWrite(LED_BUILTIN, LOW);
      peripheral.disconnect();
      BLE.end();
      bleActive = false;
      Serial.println("BLE End.2");
      break;
  }
}

Imgur: The magic of the Internet

My Arduino Nano 33 IoT is running an experiment right now. So, I cannot test my hypothesis. Here is what I would test first.

  1. Remove/comment the following lines. This is the central. No need to advertise anything. You did not setup any service or characteristic anyways.
      //Set advertised local name.
      BLE.setDeviceName("Weather Station");
      BLE.setLocalName("Weather Station");
     
      //Start advertising.
      BLE.advertise();
  1. Scan for only one UUID. I used the service UUID in my example and then knew it has the characteristics I wanted to access. I do not know whether you can scan for 3 UUIDs and whether the BLE stack tracks that. The next question would be, would you need to retrieve all the results. Right now, the state machine is happy with one result and never comes back to get any additional results.
    Scanning for one unique UUID avoids this potential issue.
      BLE.scanForUuid(BLE_UUID_PRESSURE);
      BLE.scanForUuid(BLE_UUID_TEMPERATURE);
      BLE.scanForUuid(BLE_UUID_HUMIDITY);

crispywisp:
Ok. As I understood, I have to use the updatePeripheral to activate the BLE. I created a counter that will count up to 7, which will call BLE_STATE_OFF. So this way, bleTask is called all the time, as you said.

But I don't know why the BLE won't run after the first cycle.

#include <ArduinoBLE.h> //BLE Library.

#include <Arduino_LSM9DS1.h> //Include the library for 9-axis IMU.
#include <Arduino_LPS22HB.h> //Include library to read Pressure.
#include <Arduino_HTS221.h> //Include library to read Temperature and Humidity.
#include <Arduino_APDS9960.h> //Include library for colour, proximity, light intensity, and gesture recognition.
#include <Adafruit_SSD1306.h> //OLED Display Library.

#define SCREEN_WIDTH 128 //OLED Display Width.
#define SCREEN_HEIGHT 64 //OLED Display Height.

//Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 //Reset pin # (or -1 if sharing Arduino reset pin).
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define BLE_UUID_CURRENT_TIME "2A2B"
#define BLE_UUID_PRESSURE "2A6D"
#define BLE_UUID_TEMPERATURE "2A6E"
#define BLE_UUID_HUMIDITY "2A6F"

boolean updatePeripheral = false;
boolean updateNow = false;
boolean bleActive = false;

float accel_x, accel_y, accel_z;
float gyro_x, gyro_y, gyro_z;
float pressure, currentPressureData;
float temperature, humidity, currentTemperatureData, currentHumidityData;
int proximity;
int r, g, b, a;

int sensorCount = 0;

void setup() {
 Serial.begin(9600); //Serial monitor to display all sensor values

if (!IMU.begin()) { //Initialize IMU sensor
   Serial.println("Failed to initialize IMU!");
   while (1);
 }

if (!BARO.begin()) { //Initialize Pressure sensor
   Serial.println("Failed to initialize Pressure Sensor!");
   while (1);
 }

if (!HTS.begin()) { //Initialize Temperature and Humidity sensor
   Serial.println("Failed to initialize Temperature and Humidity Sensor!");
   while (1);
 }

if (!APDS.begin()) { //Initialize Colour, Proximity and Gesture sensor
   Serial.println("Failed to initialize Colour, Proximity and Gesture Sensor!");
   while (1);
 }

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Initialize with I2c addres 0x3C.
 display.clearDisplay();
 display.display();

pinMode(LED_BUILTIN, OUTPUT); //Built-in LED.
 digitalWrite(LED_BUILTIN, LOW);

//sensorsCheck();
}

void loop() {
 #define UPDATE_INTERVAL 5 * 1000

if (updatePeripheral == false) {
 static unsigned long previousMillis = 0;
 unsigned long currentMillis = millis();
 if (currentMillis - previousMillis > UPDATE_INTERVAL) {
   previousMillis = currentMillis;
   //sensorsCheck();
   sensorCount ++;
   Serial.print("Sensor Count = ");Serial.println(sensorCount);
 }
 }

if (sensorCount == 7) {
   updateNow = false;
   updatePeripheral = true;
   sensorCount = 0;
 }
 
 
 
 bleTask();
 
}

void bleTask() {
 enum BLE_STATE_TYPE { BLE_STATE_OFF,
                       BLE_STATE_BEGIN,
                       BLE_STATE_START_SCAN,
                       BLE_STATE_SCAN,
                       BLE_STATE_STOP_SCAN,
                       BLE_STATE_PERIPHERAL_CONNECT,
                       BLE_STATE_PERIPHERAL_DISCOVER,
                       BLE_STATE_PERIPHERAL_WRITE,
                       BLE_STATE_END,
                       BLE_STATE_RESTART = 255
                     };

#define BLE_SCAN_TIMEOUT 10000

static int state = BLE_STATE_OFF;
 static unsigned long previousMillis = 0;
 static BLEDevice peripheral;

switch (state) {
   case BLE_STATE_OFF:
     if (updatePeripheral) {
       bleActive = true;
       //updatePeripheral = false;
       Serial.println("BLE Active.");
       state++;
       break;
      }
     break;
   case BLE_STATE_BEGIN:
     if (!BLE.begin()) {
       Serial.println("Starting BLE Failed.");
       break;
     }
     
     //Set advertised local name.
     BLE.setDeviceName("Weather Station");
     BLE.setLocalName("Weather Station");
   
     //Start advertising.
     BLE.advertise();
   
     digitalWrite(LED_BUILTIN, HIGH);
     state++;
     break;
   case BLE_STATE_START_SCAN:
     BLE.scanForUuid(BLE_UUID_PRESSURE);
     BLE.scanForUuid(BLE_UUID_TEMPERATURE);
     BLE.scanForUuid(BLE_UUID_HUMIDITY);
     previousMillis = millis();
     Serial.println("State BLE Start Scan");
     state++;
     break;
   case BLE_STATE_SCAN:
     if (millis() - previousMillis > BLE_SCAN_TIMEOUT) {
       BLE.stopScan();
       state = BLE_STATE_END;
       break;
     }
     peripheral = BLE.available();
     if (!peripheral) {
       break;
     }
     if (peripheral.localName() != "Weather Satelite") {
       break;
     }
     state++;
     Serial.println("Weather Satelite found.");
     break;
   case BLE_STATE_STOP_SCAN:
     BLE.stopScan();
     Serial.println("State BLE Scan Stop");
     state++;
     break;
   case BLE_STATE_PERIPHERAL_CONNECT:
     if (!peripheral.connect()) {
       state = BLE_STATE_START_SCAN;
     }
     Serial.println("State Peripheral Connect");
     state++;
     break;
   case BLE_STATE_PERIPHERAL_DISCOVER:
     if (!peripheral.discoverAttributes()) {
       peripheral.disconnect();
       state = BLE_STATE_START_SCAN;
     }
     Serial.println("State Peripheral Discover");
     state++;
     break;
   case BLE_STATE_PERIPHERAL_WRITE: {
       BLECharacteristic currentPressureDataCharacteristic = peripheral.characteristic(BLE_UUID_PRESSURE);
       if (currentPressureDataCharacteristic) {
         currentPressureDataCharacteristic.writeValue((byte*) &currentPressureDataCharacteristic, sizeof(float));
       }
       BLECharacteristic currentTemperatureDataCharacteristic = peripheral.characteristic(BLE_UUID_TEMPERATURE);
       if (currentTemperatureDataCharacteristic) {
         currentTemperatureDataCharacteristic.writeValue((byte*) &currentTemperatureDataCharacteristic, sizeof(float));
       }
       BLECharacteristic currentHumidityDataCharacteristic = peripheral.characteristic(BLE_UUID_HUMIDITY);
       if (currentHumidityDataCharacteristic) {
         currentHumidityDataCharacteristic.writeValue((byte*) &currentHumidityDataCharacteristic, sizeof(float));
       }
       Serial.println("State Peripheral Write");
     }
     state++;
     break;
   case BLE_STATE_END:
     state = BLE_STATE_OFF;
     updatePeripheral = false;
     digitalWrite(LED_BUILTIN, LOW);
     peripheral.disconnect();
     BLE.end();
     bleActive = false;
     Serial.println("BLE End.1");
     break;
   default:
     state = BLE_STATE_OFF;
     updatePeripheral = false;
     digitalWrite(LED_BUILTIN, LOW);
     peripheral.disconnect();
     BLE.end();
     bleActive = false;
     Serial.println("BLE End.2");
     break;
 }
}




![](https://imgur.com/a/fPRr2ez) https://imgur.com/a/fPRr2ez

is it still working after the first cycle?