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!