BLEService error

Greetings,

I am trying to use a sketch to send the temperature, humidity, and atmospheric pressure attributes of the BME280 over BLE using a Seeeduino ESP32C3.

I am experiencing this error:

Compilation error: no matching function for call to ‘BLEService::BLEService(const char [37])’

that prevents me from compiling the following code:

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

#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <BLEDevice.h>

// Create an Adafruit_BME280 object
Adafruit_BME280 bme;

// Create a BLEDevice object
BLEDevice bleDevice;

// Create a BLECharacteristic object
BLECharacteristic temperatureCharacteristic("2902", BLECharacteristic::PROPERTY_READ);
BLECharacteristic humidityCharacteristic("2903", BLECharacteristic::PROPERTY_READ);
BLECharacteristic pressureCharacteristic("2904", BLECharacteristic::PROPERTY_READ);

void setup() {
  // Initialize the serial port
  Serial.begin(115200);

  // Initialize the BME280 sensor
  bme.begin();

  // Initialize the BLE device
  bleDevice.init("BME280 Sensor");

  // Create the BLE service
  BLEService bleService("19B10000-E8F2-537E-4F6C-D104768A1214");

  // Add the BLE characteristics to the service
  bleService.addCharacteristic(temperatureCharacteristic);
  bleService.addCharacteristic(humidityCharacteristic);
  bleService.addCharacteristic(pressureCharacteristic);

  // Start the BLE advertisement
  bleDevice.advertiseService(bleService);
}

void loop() {
  // Read the temperature, humidity, and pressure from the BME280 sensor
  float temperature = bme.readTemperature();
  float humidity = bme.readHumidity();
  float pressure = bme.readPressure();

  // Update the BLE characteristics with the sensor data
  temperatureCharacteristic.setValue((uint8_t*)&temperature, sizeof(temperature));
  humidityCharacteristic.setValue((uint8_t*)&humidity, sizeof(humidity));
  pressureCharacteristic.setValue((uint8_t*)&pressure, sizeof(pressure));

  // Delay for 1 second
  delay(1000);
}

Any help, guidance, or general suggestions would be gratefully accepted.

Baran

Hi there,
So I went this route, your flow was out of order. and it advertises, connects and disconnects, you can edit it the rest of the way to read the sensor data.
HTH
GL :slight_smile: PJ :v:

//#include <BLEAdvertisedDevice.h>
//#include <BLEDevice.h>
//#include <BLEScan.h>
//#include <BLEServer.h>
//#include <BLEUtils.h>
#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
//#include <BLEDevice.h>
#include <ArduinoBLE.h>
// Create an Adafruit_BME280 object
Adafruit_BME280 bme;

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
 
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
 

// Create a BLEDevice object
#define SERVICE_UUID "19B10000-E8F2-537E-4F6C-D104768A1214"
// BLEDevice bleDevice;
  // Create the BLE service
BLEService blService ("19B10000-E8F2-537E-4F6C-D104768A1214");

// Create a BLECharacteristic object
//BLECharacteristic temperatureCharacteristic("2902", BLECharacteristic::PROPERTY_READ);
//BLECharacteristic humidityCharacteristic("2903", BLECharacteristic::PROPERTY_READ);
//BLECharacteristic pressureCharacteristic("2904", BLECharacteristic::PROPERTY_READ);


void setup() {
  // Initialize the serial port
  Serial.begin(115200);
  delay (3000);
Serial.println(); // only for mbed 2.9.x
  Serial.println("Power ON \n ");  // Let's BEGIN!!
  Serial.println("Processor came out of reset.");
  Serial.println();

  // Initialize the BME280 sensor
  bme.begin();
   // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");
 
    while (1);
  }
// set advertised local name and service UUID:
  BLE.setLocalName("THP Automation");            // this will appear in the App search result.
  BLE.setAdvertisedService(ledService);
  // Initialize the BLE device
  //bleDevice.init("BME280 Sensor");
  ledService.addCharacteristic(switchCharacteristic);
  // Add the BLE characteristics to the service
  //blService.addCharacteristic(temperatureCharacteristic);
  //blService.addCharacteristic(humidityCharacteristic);
  //blService.addCharacteristic(pressureCharacteristic);

  // Start the BLE advertisement
  //bleDevice.advertiseService(blService);
  BLE.advertise();
}

void loop() {

  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();
 
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

 // Read the temperature, humidity, and pressure from the BME280 sensor
  float temperature = bme.readTemperature();
  float humidity = bme.readHumidity();
  float pressure = bme.readPressure();

    
  while (central.connected()) {
        if (switchCharacteristic.written()) {
          // Update the BLE characteristics with the sensor data
          //temperature = temperatureCharacteristic.value();
        } 
     }
      // Delay for 1 second
  delay(1000);
  // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
 // temperatureCharacteristic.setValue((uint8_t*)&temperature, sizeof(temperature));
 // humidityCharacteristic.setValue((uint8_t*)&humidity, sizeof(humidity));
 // pressureCharacteristic.setValue((uint8_t*)&pressure, sizeof(pressure));
 
}

Serial output and Nrf connect for desktop and BLE dongle.


Power ON 
 
Processor came out of reset.

Connected to central: c8:65:29:7e:60:0c

Oh my god! I am humbled by your casual reference to the issue and how you recreated a functional sketch by simply changing several attributes of my sketch (and commenting out a great deal).

Thank you for your time in both fixing my mess and showing the outcome that i, as well, attained. I feel so much more confident when my sketches actually compile. Thanks, again, for sharing your knowledge.

This issue was solved by PJ Glasso! All hail the crazy chimp-faced-avatar-presenting genius of coding;).

1 Like

LOL,
Yea good stuff… I like the arduino BLE better, easy as pie to use.
Less memory also.:grin::+1:
GL :smiley: PJ

Hey P J,

At your leisure, would you please give this sketch a look over and suggest a correction or many? The basic code connects with my LightBlue app and I believe I have extracted only your revision from my original code but when I uncomment lines 60, 61, and 62 along with 71, 72, and 73, I receive the following error message:

Compilation error: ‘temperatureCharacteristic’ was not declared in this scope.

I believe this would continue to the humidityChar… and pressureChar… as well.

Here is the code I extracted from your example.

#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ArduinoBLE.h>

Adafruit_BME280 bme;  // Create an Adafruit_BME280 object

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
 
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
#define SERVICE_UUID "19B10000-E8F2-537E-4F6C-D104768A1214"   // Create a BLEDevice object
BLEService blService ("19B10000-E8F2-537E-4F6C-D104768A1214");    // Create the BLE service


void setup()
{
  Serial.begin(115200);  // Initialize the serial port
  delay (3000);
  Serial.println(); // only for mbed 2.9.x
  Serial.println("Power ON \n ");  // Let's BEGIN!!
  Serial.println("Processor came out of reset.");
  Serial.println();

 
  bme.begin();   // Initialize the BME280 sensor

  if (!BLE.begin()) 
  {
    Serial.println("starting Bluetooth® Low Energy module failed!");
    while (1);
  }

// set advertised local name and service UUID:
  BLE.setLocalName("THP Automation");            // this will appear in the App search result.
  BLE.setAdvertisedService(ledService);
  ledService.addCharacteristic(switchCharacteristic);
  BLE.advertise();
}

void loop() {

  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();
 
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());    // print the central's MAC address:

 // Read the temperature, humidity, and pressure from the BME280 sensor
  float temperature = bme.readTemperature();
  float humidity = bme.readHumidity();
  float pressure = bme.readPressure();

    
  while (central.connected()) {
        if (switchCharacteristic.written()) {
          // Update the BLE characteristics with the sensor data
          temperature = temperatureCharacteristic.value();
          humidity = humidityCharacteristic.value();
          pressure = pressureCharacteristic.value();
        } 
     }
      // Delay for 1 second
  delay(1000);
  // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
  temperatureCharacteristic.setValue((uint8_t*)&temperature, sizeof(temperature));
  humidityCharacteristic.setValue((uint8_t*)&humidity, sizeof(humidity));
  pressureCharacteristic.setValue((uint8_t*)&pressure, sizeof(pressure));
 
}

If you could guide me to getting the LightBlue app to read and display and of the three BME280 aspects, I know I can figure out the rest. I am still uncertain about how to get the data from my sketch to the app. Thank you, again, for your time and consideration.

I believe that I am closer here but still quite lost.

#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <ArduinoBLE.h>

Adafruit_BME280 bme;  // Create an Adafruit_BME280 object

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central


BLEService blService ("19B10000-E8F2-537E-4F6C-D104768A1214");   // Create the BLE service
// UUID definitions
#define SERVICE_UUID "19B10000-E8F2-537E-4F6C-D104768A1214"  // BLE service UUID
#define SWITCH_CHARACTERISTIC_UUID "19B10001-E8F2-537E-4F6C-D104768A1214" // Switch characteristic UUID
#define TEMPERATURE_CHARACTERISTIC_UUID "19B10002-E8F2-537E-4F6C-D104768A1214" // New UUID
#define HUMIDITY_CHARACTERISTIC_UUID "19B10003-E8F2-537E-4F6C-D104768A1214"    // New UUID
#define PRESSURE_CHARACTERISTIC_UUID "19B10004-E8F2-537E-4F6C-D104768A1214"    // New UUID

//BLEService blService(SERVICE_UUID);    // Create the BLE service
BLEByteCharacteristic switchCharacteristic(SWITCH_CHARACTERISTIC_UUID, BLERead | BLEWrite);
BLEFloatCharacteristic temperatureCharacteristic(TEMPERATURE_CHARACTERISTIC_UUID, BLERead | BLENotify);
BLEFloatCharacteristic humidityCharacteristic(HUMIDITY_CHARACTERISTIC_UUID, BLERead | BLENotify);
BLEFloatCharacteristic pressureCharacteristic(PRESSURE_CHARACTERISTIC_UUID, BLERead | BLENotify);

void setup() {
    Serial.begin(115200);  // Initialize the serial port
    delay(3000);
    Serial.println(); // only for mbed 2.9.x
    Serial.println("Power ON \n ");  // Let's BEGIN!!
    Serial.println("Processor came out of reset.");
    Serial.println();

    bme.begin();   // Initialize the BME280 sensor

    if (!BLE.begin()) {
        Serial.println("starting Bluetooth® Low Energy module failed!");
        while (1);
    }

    // Setup BLE service and characteristics
    BLE.setLocalName("THP Automation");
    BLE.setAdvertisedService(blService);
    blService.addCharacteristic(switchCharacteristic);
    blService.addCharacteristic(temperatureCharacteristic);
    blService.addCharacteristic(humidityCharacteristic);
    blService.addCharacteristic(pressureCharacteristic);
    BLE.advertise();
}

void loop() {
    // Existing code with modifications to use characteristics properly
    //  This is where I am relatively lost.  Also, this code seems to no longer be 
    //  recognized by the LightBlue app.
}

Ok, I see
you want to serv the 3 params , then do it like this Demo from Rui Santos
it utilizes the environmental service and 3 chars, for T, H, Pa. So it’s right on the mark.
Go look at the client end also you can do an MIT app inventor as a client.
you can do graphs and pictures , even save the BLE data to the mobile app for replay later.

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-ble-server-environmental-sensing-service/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. 
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

//BLE server name
#define bleServerName "THP Server"

// Default UUID for Environmental Sensing Service
// https://www.bluetooth.com/specifications/assigned-numbers/
#define SERVICE_UUID (BLEUUID((uint16_t)0x181A))

// Temperature Characteristic and Descriptor (default UUID)
// Check the default UUIDs here: https://www.bluetooth.com/specifications/assigned-numbers/
BLECharacteristic temperatureCharacteristic(BLEUUID((uint16_t)0x2A6E), BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor temperatureDescriptor(BLEUUID((uint16_t)0x2902));

// Humidity Characteristic and Descriptor (default UUID)
BLECharacteristic humidityCharacteristic(BLEUUID((uint16_t)0x2A6F), BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor humidityDescriptor(BLEUUID((uint16_t)0x2902));

// Pressure Characteristic and Descriptor (default UUID)
BLECharacteristic pressureCharacteristic(BLEUUID((uint16_t)0x2A6D), BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor pressureDescriptor(BLEUUID((uint16_t)0x2902));

// Create a sensor object
Adafruit_BME280 bme;

// Init BME280
void initBME(){
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
   // while (1);
   return;
  }
}

bool deviceConnected = false;

//Setup callbacks onConnect and onDisconnect
class MyServerCallbacks: public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    deviceConnected = true;
    Serial.println("Device Connected");
  };
  void onDisconnect(BLEServer* pServer) {
    deviceConnected = false;
    Serial.println("Device Disconnected");
  }
};

void setup() {
  // Start serial communication 
  Serial.begin(115200);

  // Start BME sensor
  initBME();

  // Create the BLE Device
  BLEDevice::init(bleServerName);

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *bmeService = pServer->createService(SERVICE_UUID);

  // Create BLE Characteristics and corresponding Descriptors
  bmeService->addCharacteristic(&temperatureCharacteristic);
  temperatureCharacteristic.addDescriptor(&temperatureDescriptor);
  
  bmeService->addCharacteristic(&humidityCharacteristic);
  humidityCharacteristic.addDescriptor(&humidityDescriptor);

  bmeService->addCharacteristic(&pressureCharacteristic);
  pressureCharacteristic.addDescriptor(&pressureDescriptor);
  
  // Start the service
  bmeService->start();

  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
}

void loop() {
  if (deviceConnected) {
    // Read temperature as Celsius (the default)
    float t = bme.readTemperature();
    // Read humidity
    float h = bme.readHumidity();
    // Read pressure
    float p = bme.readPressure()/100.0F;
    
    //Notify temperature reading
    uint16_t temperature = (uint16_t)t;
    //Set temperature Characteristic value and notify connected client
    temperatureCharacteristic.setValue(temperature);
    temperatureCharacteristic.notify();
    Serial.print("Temperature Celsius: ");
    Serial.print(t);
    Serial.println(" ºC");
   
    //Notify humidity reading
    uint16_t humidity = (uint16_t)h;
    //Set humidity Characteristic value and notify connected client
    humidityCharacteristic.setValue(humidity);
    humidityCharacteristic.notify();   
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.println(" %");

    //Notify pressure reading
    uint16_t pressure = (uint16_t)p;
    //Set humidity Characteristic value and notify connected client
    pressureCharacteristic.setValue(pressure);
    pressureCharacteristic.notify();   
    Serial.print("Pressure: ");
    Serial.print(p);
    Serial.println(" hPa");
    
    delay(10000);
  }
}

serial output

Device Connected
Temperature Celsius: 0.00 ºC
Humidity: 0.00 %
Pressure: 0.00 hPa
Temperature Celsius: 0.00 ºC
Humidity: 0.00 %
Pressure: 0.00 hPa
Temperature Celsius: 0.00 ºC
Humidity: 0.00 %
Pressure: 0.00 hPa

You can use the light blue app on mobile or Start using NRF connect also on Mobile so the interface is the same. desktop or mobile, $10 BLE dongle get’s you desktop access to BLE stuff.

you can load this code and go over to light blue or any BLE mobile app , SCAN Bluetooth and connect to the Environmental service and look at the chars, the rest is duck soup!
HTH
GL :slight_smile: PJ

“Quack, quack, quack . . .”

You seem to be a super-user from the standpoint of knowledge and application. My Arduino sketch works 100% using standard UUIDs. I can find the sketch on nRF Connect and, when I connect, the sketch starts writing to the serial monitor and nRF Connect. I was hung up on the string names not appearing using standard UUIDs as with the shortcuts (0x181A) without realizing we match the string names at the end in MIT App Inventor.

Would you mind, at your convenience, looking over what I have come up with and, perhaps, give me a clue as to what I am missing? I am so entirely new to the concept of block coding and I just need to create a basic working example upon which I can build.

The reason I have moved away from the 6-bit environmental shortcuts is because I ultimately want to calculate and send other points of data that don’t square with the pre-selected shortcut names.

I am not certain if this image of my cleaned-up blocks was sent.

Hi there,
Yes, You are missing some stuff. Here is how I like to do it.
a couple things , You need to use a timer to keep things going.
Also You still need to scan and connect to the device b4 getting near the Char’s, Just like in the Apps, Scan, Connect, parse UUiD chars, Read,Notify, Write,subscribe?
Try it like this:


HTH
GL :slight_smile: PJ

Hi, PJ

I love your non-chalet attitude of “try it this way . . .” I am trying to recreate the block code you listed above and I am having trouble actually finding blocks. For instance, “When BluetoothLE1 . Connected” I can find the green "Set: block but I am unable to select “Label Messages.” It currently shows “Text” with a drop-down button but “Label Messages” is not a choice for that box.

As choices are allowed in the block programmer based on the designer tab, I am wondering if you have something on your design tab that I do not have that allows you a greater choice of blocks and tags.

I expect to start from the outside-in, or left-to-right, and I would expect all of the possible choices to present themselves as I clicked further along a block command.

The MIT App help document is suspiciously silent on the subject of locating the proper blocks.

Hi, PJ

I apologize for being so very dense, but I am unable to conjure up the following block of code. May I ask what is in your design page that allows you access to the green “Set LabelMessages.Text” block that is nested in the “When BluetoothLE1” block. Please see below . . .

Hi there,
Hmmmm, Not sure. Make sure you got the latest BLE driver/library from AI2. is my only guess,
Check there forum I’m sure there is a difference.
HTH
GL :slight_smile: PJ :v:

Found it!
It’s a property under the Label on the design page , I used it to change the label text after the connection and UUID were correct.
That was over 2 years ago, that code. Thanks for the memory. :face_with_hand_over_mouth:
HTH
GL :slight_smile: PJ