It is working fine only when Arduino Nano BLE Sense is connected to a PC through usb cable to load the sketh using Arduino Web Editor. So basically it is working when connected to Web Editor and I was able to see the Device( arduino ble sense) published over bluetooth ble and I was able see it using Nordic nRF Connect app and also I was able to connect and get data using my mobile App..
"It is NOT working when Arduino Nano BLE Sense device is not connected to pc/ arduino web editor.. ie in the following cases :
/*
Nano 33 BLE Sense Getting Started
A BLE peripheral broadcasting temperatature and humidity readings that can be viewed
on a mobile phone. On-board LED indicates a BLE connection.
Adapted from Arduino BatteryMonitor example by Peter Milne
*/
#include <ArduinoBLE.h>
#include <Arduino_HTS221.h>
#include <Arduino_LPS22HB.h>
#include <Arduino_APDS9960.h>
const int UPDATE_FREQUENCY = 1000; // Update frequency in ms
const float CALIBRATION_FACTOR = -4.0; // Temperature calibration factor (celcius)
// Declare the variable for irradiance reading.
float irradiance;
long previousMillis = 0; // last time readings were checked, in ms
BLEService environmentService("181A"); // Standard Environmental Sensing service,Declare Bluetooth service names, and irradiance characteristics.
// BLE Battery Service
BLEService batteryService("180F");
// BLE Battery Level Characteristic
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
BLEIntCharacteristic tempCharacteristic("2A6E", // Standard 16-bit Temperature characteristic
BLERead | BLENotify); // Remote clients can read and get updates
BLEUnsignedIntCharacteristic humidCharacteristic("2A6F", // Unsigned 16-bit Humidity characteristic
BLERead | BLENotify);
BLEUnsignedIntCharacteristic pressureCharacteristic("2A6D", // Unsigned 32-bit Pressure characteristic
BLERead | BLENotify); // Remote clients can read and get updates
BLEUnsignedIntCharacteristic irradianceCharacteristic("2A77", BLERead | BLENotify); // 16-bit unsigned, 1 decimal place.
int oldBatteryLevel = 0; // last battery level reading from analog input
void setup() {
Serial.begin(9600); // Initialize serial communication
//while (!Serial);
if (!HTS.begin()) { // Initialize HTS22 sensor
Serial.println("Failed to initialize humidity temperature sensor!");
while (1);
}
if (!APDS.begin()) {
Serial.println("Failed.");
while (1);
}
pinMode(LED_BUILTIN, OUTPUT); // Initialize the built-in LED pin
if (!BLE.begin()) { // Initialize BLE
Serial.println("starting BLE failed!");
while (1);
}
BLE.setLocalName("RichoNano33BLESENSE"); // Set name for connection
BLE.setAdvertisedService(environmentService); // Advertise environment service
environmentService.addCharacteristic(tempCharacteristic); // Add temperature characteristic
environmentService.addCharacteristic(humidCharacteristic); // Add humidity chararacteristic
environmentService.addCharacteristic(pressureCharacteristic); // Add pressure chararacteristic
// Add characteristic for irradiance.
environmentService.addCharacteristic(irradianceCharacteristic);
BLE.addService(environmentService); // Add environment service
tempCharacteristic.setValue(0); // Set initial temperature value
humidCharacteristic.setValue(0); // Set initial humidity value
pressureCharacteristic.setValue(0); // Set initial humidity value
irradianceCharacteristic.setValue(0); // Set initial irradiance value
BLE.setAdvertisedService(batteryService); // add the service UUID
batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic
BLE.addService(batteryService); // Add the battery service
batteryLevelChar.setValue(oldBatteryLevel); // set initial value for this characteristic
BLE.advertise(); // Start advertising
Serial.print("Peripheral device MAC: ");
Serial.println(BLE.address());
Serial.println("Waiting for connections...");
}
void loop() {
BLEDevice central = BLE.central(); // Wait for a BLE central to connect
// If central is connected to peripheral
if (central) {
Serial.print("Connected to central MAC: ");
Serial.println(central.address()); // Central's BT address:
// Turn on the LED to indicate the connection:
digitalWrite(LED_BUILTIN, HIGH);
while (central.connected()) {
// Get reading from sensor and update the charcteristic value.
if (APDS.colorAvailable()) {
int red, green, blue, ambient;
APDS.readColor(red, green, blue, ambient);
irradiance = (float) ambient/ 2360; // irradiance is converted to Watts per square meter. See datasheet for typical irradiance responsivity. https://docs.broadcom.com/doc/AV02-4191EN
//Serial.println("Ambient: ");
//Serial.println(ambient);
}
// Delay between updates. (Don't make too long or connections start to timeout.)
delay(1000);
long currentMillis = millis();
// After UPDATE_FREQUENCY ms have passed, check temperature & humidity
if (currentMillis - previousMillis >= UPDATE_FREQUENCY) {
previousMillis = currentMillis;
updateReadings();
}
}
// When the central disconnects, turn off the LED
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central MAC: ");
Serial.println(central.address());
}
}
int getTemperature(float calibration) {
// Get calibrated temperature as signed 16-bit int for BLE characteristic
return (int) (HTS.readTemperature() * 100) + (int) (calibration * 100);
}
unsigned int getHumidity() {
// Get humidity as unsigned 16-bit int for BLE characteristic
return (unsigned int) (HTS.readHumidity() * 100);
}
unsigned int getPressure() {
// Get humidity as unsigned 32-bit int for BLE characteristic
return (unsigned int) (BARO.readPressure() * 1000 * 10);
}
void updateReadings() {
// Read the HTS22 temperature and humidity
int temperature = getTemperature(CALIBRATION_FACTOR);
unsigned int humidity = getHumidity();
unsigned int pressure = getPressure();
int batterylevel = 0;
int hallsensor = 0;
float battery = 0;
float hallsensor_0 = 0;
batterylevel = analogRead(A0);
//int batteryLevel = map(battery, 0, 1023, 0, 2.5);
hallsensor = analogRead(A1);
hallsensor_0 = hallsensor * (1.46/805) * 10;
//battery=analogRead(0);//This divider module will divide the measured voltage by 5, the maximum voltage it can measure is 25V.
battery = (batterylevel * (2.52/778) * 5 * 10) + 5;
//int sensitivity = (5.1 - 4.9)/10; // 0.08
//int current = (Vout-4.9)/0.08;
Serial.print("Temperature: ");
Serial.println(temperature);
tempCharacteristic.writeValue(temperature); // Update characteristic
Serial.print("Humidity: ");
Serial.println(humidity);
humidCharacteristic.writeValue(humidity);
Serial.print("Pressure: ");
Serial.println(pressure);
pressureCharacteristic.writeValue(pressure);
Serial.print("Battery Level is: "); // print it
Serial.println(battery);
batteryLevelChar.writeValue(battery); // and update the battery level characteristic
//oldBatteryLevel = batteryLevel; // save the level for next comparison
Serial.println("Irradiance: ");
Serial.println(irradiance);
Serial.println("W/sq. meter");
// Update Bluetooth characteristic with new value.
irradianceCharacteristic.writeValue((uint16_t) round(irradiance * 10)); // Shift for one decimal place.
Serial.print("current is: "); // print it
Serial.println(hallsensor_0);
Serial.println(hallsensor);
}
(1) when connected to only power supply using usb cable
(2) battery using usb cable or
(3) connected to pc using usb cable ( with not connecting/logging on to arduino web editor)
The intent is to make it work remotely, so it should be able to work on a battery or some power supply."
Originally posted by Sree, I am also experiencing the same issue. I have removed "while(!Serial)", but the nano BLE SENSE is still not discoverable.