Hi all,
I'd like to get temperature and humidity readings from a Nano 33 BLE Sense via bluetooth. The following code works fine:
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h> // even if we aren't using the accelerometer, its library needs to be included?
#include <Arduino_HTS221.h>
int humX = 1;
int tempY = 1;
float x, y;
BLEService customService("1101");
BLEUnsignedIntCharacteristic customXChar("2101",
BLERead | BLENotify);
BLEUnsignedIntCharacteristic customYChar("2102",
BLERead | BLENotify);
void read_temphum() {
x = HTS.readHumidity();
y = HTS.readTemperature();
humX = x*100;
tempY = y*100;
}
void setup() {
IMU.begin(); // Without this line (and the associated include LSM9DS1.h at the top) arduino returns 0 - 0 and freezes on BLE connect
HTS.begin();
Serial.begin(9600);
while (!Serial);
pinMode(LED_BUILTIN,OUTPUT); // Ready the onboard LED
// BLE error catching
if (!BLE.begin()){
Serial.println("BLE failed to initiate");
while(1);
}
BLE.setLocalName("arduinoTsensor");
BLE.setAdvertisedService(customService);
customService.addCharacteristic(customXChar);
customService.addCharacteristic(customYChar);
BLE.addService(customService);
customXChar.writeValue(humX);
customYChar.writeValue(tempY);
BLE.advertise();
Serial.println("Bluetooth device now active, waiting for connect...");
}
void loop() {
BLEDevice central = BLE.central();
if (central){
Serial.print("Connected to central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH);
while (central.connected()){
delay(1000);
read_temphum();
customXChar.writeValue(humX);
customYChar.writeValue(tempY);
Serial.print("At main function");
Serial.println("");
Serial.print(humX);
Serial.print(" - ");
Serial.println(tempY);
Serial.println("");
Serial.println("");
}
digitalWrite(LED_BUILTIN,LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
But, if I comment out or delete both:
- the inclusion of the Arduino_LSM9DS1.h library, and;
- the IMU.begin() statement;
then Arduino returns "0 - 0" upon BLE connect and then freezes.
Can anyone recreate this issue or help me to understand why it's happening?
To be explicit, the following code fails:
#include <ArduinoBLE.h>
#include <Arduino_HTS221.h>
int humX = 1;
int tempY = 1;
float x, y;
BLEService customService("1101");
BLEUnsignedIntCharacteristic customXChar("2101",
BLERead | BLENotify);
BLEUnsignedIntCharacteristic customYChar("2102",
BLERead | BLENotify);
void read_temphum() {
x = HTS.readHumidity();
y = HTS.readTemperature();
humX = x*100;
tempY = y*100;
}
void setup() {
HTS.begin();
Serial.begin(9600);
while (!Serial);
pinMode(LED_BUILTIN,OUTPUT); // Ready the onboard LED
// BLE error catching
if (!BLE.begin()){
Serial.println("BLE failed to initiate");
while(1);
}
BLE.setLocalName("arduinoTsensor");
BLE.setAdvertisedService(customService);
customService.addCharacteristic(customXChar);
customService.addCharacteristic(customYChar);
BLE.addService(customService);
customXChar.writeValue(humX);
customYChar.writeValue(tempY);
BLE.advertise();
Serial.println("Bluetooth device now active, waiting for connect...");
}
void loop() {
BLEDevice central = BLE.central();
if (central){
Serial.print("Connected to central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH);
while (central.connected()){
delay(1000);
read_temphum();
customXChar.writeValue(humX);
customYChar.writeValue(tempY);
Serial.print("At main function");
Serial.println("");
Serial.print(humX);
Serial.print(" - ");
Serial.println(tempY);
Serial.println("");
Serial.println("");
}
digitalWrite(LED_BUILTIN,LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
Cheers!