My app reads BME280 data but not BME680 data - why, please?

Greetings,

I have been away for awhile, taking a break from code. I am back and trying to play with a BME680 atmospheric module instead of a BME280. I have two Arduino sketches, one for the BME280 and one for the BME680. Both sketches behave well, and they both begin sending temperature, humidity, and air pressure data to the serial monitor when they receive a "connect" signal from my app. The only difference between the two sketches is that the BME280 sketch sends data almost immediately when my app connects to it, the BME680 sketch - not so much . . . like nothing, is received by my app.

I posted on the Arduino forum with no success so I hope the powers that be will allow this post to remain until someone responds who has greater coding accumen than I.

Also, using nRF Connect, I can see that the service UUID and the three characteristic UUIDs are detected from both sketches.

Here is a block image of the app:

This BME280 sketch sends data to the app:

#include <ArduinoBLE.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
#include <stdint.h>
int led = D0;
#define BLE_NAME "ATMO"
#define SERVICE_UUID "19B10000-E8F2-537E-4F6C-D104768A1214"
#define TEMPERATURE_UUID "19B10001-E8F2-537E-4F6C-D104768A1214"
#define HUMIDITY_UUID "19B10002-E8F2-537E-4F6C-D104768A1214"
#define PRESSURE_UUID "19B10003-E8F2-537E-4F6C-D104768A1214"
BLEService bmeService(SERVICE_UUID);
BLEFloatCharacteristic temperatureCharacteristic(TEMPERATURE_UUID, BLERead | BLENotify);
BLEFloatCharacteristic humidityCharacteristic(HUMIDITY_UUID, BLERead | BLENotify);
BLEFloatCharacteristic pressureCharacteristic(PRESSURE_UUID, BLERead | BLENotify);

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void setup() {
  Serial.begin(9600);
    startMillis = millis();
    pinMode(led, OUTPUT);
 digitalWrite(led, HIGH);

  if (!BLE.begin()) {
    Serial.println("Failed to start BLE!");
    while (1)
      ;
  }

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

  BLE.setLocalName(BLE_NAME);
  BLE.setAdvertisedService(bmeService);

  bmeService.addCharacteristic(temperatureCharacteristic);
  bmeService.addCharacteristic(humidityCharacteristic);
  bmeService.addCharacteristic(pressureCharacteristic);

  BLE.addService(bmeService);

  temperatureCharacteristic.writeValue(0);
  humidityCharacteristic.writeValue(0);
  pressureCharacteristic.writeValue(0);

  BLE.advertise();

  Serial.println("BLE peripheral is now active");
}

void loop() {
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());

    while (central.connected()) {
      // Read actual sensor values
      float temperature = bme.readTemperature();
      float humidity = bme.readHumidity();
      float pressure = bme.readPressure() / 100.0;  // Convert Pa to hPa
      float Twet = temperature * atan(0.151977 * sqrt(humidity + 8.313659)) + atan(temperature + humidity) - atan(humidity - 1.676331) + 0.00391838 * pow( humidity, 1.5) * atan(0.023101 * humidity) - 4.686035; 

 currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
  
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
      
  Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.println(" °C");

      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.println(" %");

      Serial.print("Pressure: ");
      Serial.print(pressure);
      Serial.println(" hPa");

  
      Serial.print("Wet Bulb: ");
      Serial.print(Twet);
      Serial.println("°");

      temperatureCharacteristic.writeValue(temperature);
      humidityCharacteristic.writeValue(humidity);
      pressureCharacteristic.writeValue(pressure);

    }

    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}
}

While this code fails to send charcteristic data to my app:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include <ArduinoBLE.h>

#define BLE_NAME "Psychrometer"
#define SERVICE_UUID "19B10000-E8F2-537E-4F6C-D104768A1214"
#define TEMPERATURE_UUID "19B10001-E8F2-537E-4F6C-D104768A1214"
#define HUMIDITY_UUID "19B10002-E8F2-537E-4F6C-D104768A1214"
#define PRESSURE_UUID "19B10003-E8F2-537E-4F6C-D104768A1214"

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME680 bme;
BLEService bmeService(SERVICE_UUID);
BLEFloatCharacteristic temperatureCharacteristic(TEMPERATURE_UUID, BLERead | BLENotify);
BLEFloatCharacteristic humidityCharacteristic(HUMIDITY_UUID, BLERead | BLENotify);
BLEFloatCharacteristic pressureCharacteristic(PRESSURE_UUID, BLERead | BLENotify);

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println(F("BME680 BLE Sensor"));

  if (!bme.begin()) {
    Serial.println("Could not find a valid BME680 sensor!");
    while (1);
  }
  if (!BLE.begin()) {
    Serial.println("Failed to start BLE!");
    while (1);
  }

  BLE.setLocalName(BLE_NAME);
  BLE.setAdvertisedService(bmeService);
  bmeService.addCharacteristic(temperatureCharacteristic);
  bmeService.addCharacteristic(humidityCharacteristic);
  bmeService.addCharacteristic(pressureCharacteristic);
  BLE.addService(bmeService);

  // Set initial values
  temperatureCharacteristic.writeValue(0);
  humidityCharacteristic.writeValue(0);
  pressureCharacteristic.writeValue(0);

  BLE.advertise();
  Serial.println("BLE is now active");
}

void loop() {
  BLEDevice central = BLE.central();
  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());
    while (central.connected()) {
      float temperature = bme.readTemperature();
      float humidity = bme.readHumidity();
      float pressure = bme.readPressure() / 100.0;

      Serial.println("Sensor Readings:");
      Serial.println("  Temperature = " + String(temperature, 2) + "°C");
      Serial.println("  Humidity = " + String(humidity, 1) + "%");
      Serial.println("  Pressure = " + String(pressure) + " hPa");
      Serial.println("--------------------------------------------------------------");

      temperatureCharacteristic.writeValue(temperature);
      humidityCharacteristic.writeValue(humidity);
      pressureCharacteristic.writeValue(pressure);

      delay(2000);
    }
     Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

If requested, I will create a Google Cloud URL and leave the .apk file there.

I would love to learn why one sketch runs and one sketch fails in my app. Thank you!

Can you upload both .ino files and the project .aia Baran?

Hi, Chris

It has been months - how the heck are you doing? Yes, I can absolutely get those files here and now. I do not understand why I cannot simply swap out all BME280 files and folders with BME680 files and folders and have a flawless production but . . . nooooo. Please note that the app is pretty raw and some fonts may be off. Please do not waste any time on helping it, per se, as it is more a proof-of-concept for my remote sketches.

Here is the .ino based on my BME280 sensor that prints to the serial monitor AND connects quickly..

Basic_Atmo.ino (3.3 KB)

And here is the .ino for the BME680. This code only prints to the serial monitor. For some reason, the UUID characteristics data fails to find its way into my app.

3_Readings.ino (3.9 KB)

Both sketches will wait after a hard start to be activated (command central signal from the app when the sketch name is selected from a list?) and my nRF connect can see the service UUID and all three characteristic UUIDs for both sketches, although I do not know how to set it to properly interpret the data.

Here is the basic app I have been using for testing.

Basic_BLE_Test.aia (916.1 KB)

I start my sketch, click the scan button in the app, when I see "Psychrometer" in the list, I stop scanning, click onto the sketch name and then click the connect button at the middle of the screen (which starts both .ino sketches braodcasting data). [The "Connect" button at the top seems to do nothing]

I would really like to undertand why the BME680 is such a bear to work with. No, actually, I just want to get over it;). There should be no code differences between the BME680 and the BME280 except for a "2" or a "6" in the files and folders, IMHO.

BTW, thank you for having the interest and willingness to help me get to the bottom of this. Also, I am not wedded to my sketches, per se. I would be very happy using an entirely different sketch to send my UUID characteristics. I simply haven't found one that works for me, yet - even in the Arduino IDE Examples tab.

So there is a physical difference between the two sensor modules, the 680 module has an extra MOX sensor to measure gas. Since you are only interested in temperature, humidity and pressure - why not use two 280s?

I'll take a look at your files tonight - one point though is that if the .ino files are basically the same, the like-for-like lines should be the same to ease comparison.

I'm assuming that the Tech Spec of each module only differs with regard to the MOX sensor - if that is not the case, then the .ino files could need to be quite different - and so the wiring likely would be too. What microprocessor is hosting these modules?

Good Morning, CHris

Thank you for that information and your willingness to help me learn from my current misunderstanding(s). The two sketches I attached represent my lack of understanding in how the BME series operates. The microprocessor (my preferred) is a Seeeduino ESP32C3 and the BME680 sensor is from Adafruit.

Regarding only using BME280 sensors - that was where I was but I really want access to a simple Air Quality Index that can only be calculated using the BME680. The fact that my BME680 sketch only shows three functions is intentional. I actually simplified a slightly more complex version to create more of an apples-to apples-comparison.. I figured that if I can make this work, throwing in a few more characteristic UUIDs would be no problem.

As I realized that I may be requesting a great deal of help to cover my ignorance, I decided to throw this at myninja.ai ($5.00 per month). Here is what the AI returned in commentary:

Looking at both sketches and the errors you're receiving, there are a few key differences between BME280 and BME680 that need to be addressed:

  1. The I2C address is different - BME280 uses 0x76 while BME680 typically uses 0x77 (though you've already made this change)
  2. The BME680 requires additional setup parameters. Here's how you can modify the setup section of your second sketch . . .
  3. For reading the sensor values in the loop, the BME680 requires performing a reading before accessing the values. Modify your reading section like this . . .
    The errors you're seeing in the serial monitor appear to be ESP32 boot messages rather than compilation errors. This suggests the code is compiling but the device is resetting repeatedly, likely due to the sensor initialization failing.

Also, I notice you changed the LED pin from D0 to 13. Make sure this pin number is correct for your board. [How nice of the AI to tap me on the shoulder here]

Try these modifications and let me know if you still experience issues. The BME680 is a more complex sensor than the BME280 as it includes gas sensing capabilities, so it requires more setup parameters to work properly.

Key changes made:

  1. Added proper BME680 initialization parameters
  2. Added performReading() check before accessing sensor data
  3. Changed how sensor values are read (using bme.temperature instead of bme.readTemperature(), etc.)
  4. Added error handling for failed readings
  5. Kept the LED pin as D0 to match your working sketch
  6. Maintained all your BLE functionality and wet bulb calculation

This sketch should now properly work with the BME680 sensor while maintaining all the BLE functionality from your original sketch. The sensor readings will be taken every second (as defined by your period variable) and broadcast over BLE when a central device is connected.

[And here is the (properly working) code that it came up with: ]

280_to_680.ino (3.8 KB)

So, kind sir, I thank you for your time and engagement but I want to save requests of others'(yours) time like this to the toughest challenges that I find insurmountable, although this once was one of them. I appreciate all that you contribute to others, and myself on this forum. Enjoy your Sunday, unencumbered by my recent request.

I'm very impressed!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.