haraldholzner
Published © GPL3+

Angnano - Visualize Nano BLE Sense data within the Browser

A single page web application which can connect to the Nano BLE Sense Board via a bluetooth connection and visualize sensors in 'real time'

BeginnerShowcase (no instructions)1,228
Angnano - Visualize Nano BLE Sense data within the Browser

Things used in this project

Hardware components

Nano 33 BLE Sense
Arduino Nano 33 BLE Sense
×1

Software apps and online services

Arduino IDE
Arduino IDE
VS Code
Microsoft VS Code
optional
Visual Studio Code Extension for Arduino
Microsoft Visual Studio Code Extension for Arduino
optional

Story

Read more

Code

Sketch

Arduino
Upload this sketch to your board to get started!
#include <ArduinoBLE.h>
#include <Arduino_APDS9960.h>
#include <Arduino_HTS221.h>
#include <Arduino_LPS22HB.h>
#include <Arduino_LSM9DS1.h>


// Device name
const char* nameOfPeripheral = "Nano BLE Sense";
const char* uuidOfSenseService = "00001101-0000-1000-8000-00805f9b34fb";
const char* uuidOfSensorCharacteristic = "00001141-0000-1000-8000-00805f9b34fb";

// BLE Service
BLEService senseService(uuidOfSenseService);

// Characteristics
// ===================================================================================================================
// |   0   |    1    |   2   |   3   |   4   |       5       |      6      |      7      |      8      |      9      |
// |gesture|proximity|color r|color g|color b|light intensity|temperature_0|temperature_1|temperature_2|temperature_3|
// =========================================================================================
// |    10    |    11    |    12    |    13    |    14    |    15    |    16    |    17    |
// |humidity_0|humidity_1|humidity_2|humidity_3|pressure_0|pressure_1|pressure_2|pressure_3|
// =================================================================================================
// |   18  |   19  |   20  |   21  |   22  |   23  |   24  |   25  |   26  |   27  |   28  |   29  |
// |acc_x_0|acc_x_1|acc_x_2|acc_x_3|acc_y_0|acc_y_1|acc_y_2|acc_y_3|acc_z_0|acc_z_1|acc_z_2|acc_z_3|
// =================================================================================================
// |   30  |   31  |   32  |   33  |   34  |   35  |   36  |   37  |   38  |   39  |   40  |   41  |
// |gyr_x_0|gyr_x_1|gyr_x_2|gyr_x_3|gyr_y_0|gyr_y_1|gyr_y_2|gyr_y_3|gyr_z_0|gyr_z_1|gyr_z_2|gyr_z_3|
// =================================================================================================
// |   42  |   43  |   44  |   45  |   46  |   47  |   48  |   49  |   50  |   51  |   52  |   53  |
// |mag_x_0|mag_x_1|mag_x_2|mag_x_3|mag_y_0|mag_y_1|mag_y_2|mag_y_3|mag_z_0|mag_z_1|mag_z_2|mag_z_3|
// =================================================================================================
BLECharacteristic sensorCharacteristic(uuidOfSensorCharacteristic, BLERead | BLENotify | BLEBroadcast, 54, true);

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);

  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(LEDB, HIGH);
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  
  if (!BLE.begin()) {
    digitalWrite(LEDR, LOW);
    while (true);
  }  

  if (!APDS.begin()) {
    digitalWrite(LEDR, LOW);
    while (true);
  }
  
  if (!HTS.begin()) {
    digitalWrite(LEDR, LOW);
    while (true);
  }  

  if (!BARO.begin()) {
    digitalWrite(LEDR, LOW);
    while (true);
  }

  if (!IMU.begin()) {
    digitalWrite(LEDR, LOW);
    while (true);
  }

  // Create BLE service and characteristics.
  BLE.setLocalName(nameOfPeripheral);
  BLE.setAdvertisedService(senseService);
  senseService.addCharacteristic(sensorCharacteristic);
  BLE.addService(senseService);

  // Bluetooth LE connection handlers.
  BLE.setEventHandler(BLEConnected, onBLEConnected);
  BLE.setEventHandler(BLEDisconnected, onBLEDisconnected);
    
  // Let's tell devices about us.
  BLE.advertise();
}

uint8_t characteristicBuffer[54];

int proximity = 0;
uint8_t gesture = 0x0;
int r = 0, g = 0, b = 0, a = 0;
float temperature = 0.0f;
float humidity = 0.0f;
float atmosphericPressure = 0.0f;

float accelerometerX = 0.0f;
float accelerometerY = 0.0f;
float accelerometerZ = 0.0f;

float gyroscopeX = 0.0f;
float gyroscopeY = 0.0f;
float gyroscopeZ = 0.0f;

float magnetometerX = 0.0f;
float magnetometerY = 0.0f;
float magnetometerZ = 0.0f;

unsigned long lastLedReset = 0;
unsigned long lastHtsUpdate = 0;
unsigned long lastBaroUpdate = 0;

void loop() {

  BLEDevice central = BLE.central();

  if(millis() - lastLedReset > 50) {
    lastLedReset = millis();
    digitalWrite(LEDR, HIGH);
    digitalWrite(LED_BUILTIN, LOW);
  }  

  // Check if a proximity reading is available.
  if (APDS.proximityAvailable()) {
    proximity = APDS.readProximity();
  }

  // Check if a gesture reading is available
  if (APDS.gestureAvailable()) {
    gesture = APDS.readGesture();
  }

  // Check if a color reading is available
  if (APDS.colorAvailable()) {
    APDS.readColor(r, g, b, a);
  }

  // Check if a accelerometer, gyroscope or magnetometer reading is available
  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(accelerometerX, accelerometerY, accelerometerZ);
  }
  if (IMU.gyroscopeAvailable()){
    IMU.readGyroscope(gyroscopeX, gyroscopeY, gyroscopeZ);
  }
  if (IMU.magneticFieldAvailable()) {
    IMU.readMagneticField(magnetometerX, magnetometerY, magnetometerZ);
  }
    
  // Read temperature and humidity from sensor
  if (millis() - lastHtsUpdate > 1000) {
    lastHtsUpdate = millis();
    temperature = HTS.readTemperature();
    humidity    = HTS.readHumidity();
  }

  // Read atmospheric pressure from sensor
  if (millis() - lastBaroUpdate > 1000) {
    lastBaroUpdate = millis();
    atmosphericPressure = BARO.readPressure();
  }

  updateSensorCharacteristic();

  // Check BLE connection  
  if (central && central.connected()) {
    digitalWrite(LEDB, LOW);
  }
}

void onBLEConnected(BLEDevice central) {
  digitalWrite(LEDB, LOW);
}

void onBLEDisconnected(BLEDevice central) {
  digitalWrite(LEDB, HIGH);
}

void updateSensorCharacteristic() {
  memcpy(&characteristicBuffer[0], &gesture, 1);

  memcpy(&characteristicBuffer[1], &proximity, 1);

  memcpy(&characteristicBuffer[2], &r, 4);
  memcpy(&characteristicBuffer[3], &g, 4);
  memcpy(&characteristicBuffer[4], &b, 4);
  memcpy(&characteristicBuffer[5], &a, 4);

  memcpy(&characteristicBuffer[6], &temperature, 4);
  memcpy(&characteristicBuffer[10], &humidity, 4);
  memcpy(&characteristicBuffer[14], &atmosphericPressure, 4);

  memcpy(&characteristicBuffer[18], &accelerometerX, 4);
  memcpy(&characteristicBuffer[22], &accelerometerY, 4);
  memcpy(&characteristicBuffer[26], &accelerometerZ, 4);

  memcpy(&characteristicBuffer[30], &gyroscopeX, 4);
  memcpy(&characteristicBuffer[34], &gyroscopeY, 4);
  memcpy(&characteristicBuffer[38], &gyroscopeZ, 4);

  memcpy(&characteristicBuffer[42], &magnetometerX, 4);
  memcpy(&characteristicBuffer[46], &magnetometerY, 4);
  memcpy(&characteristicBuffer[50], &magnetometerZ, 4);

  if(sensorCharacteristic.writeValue(characteristicBuffer, 54, true)) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    digitalWrite(LEDR, LOW); 
  }  
}

Credits

haraldholzner
2 projects • 5 followers

Comments