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'
Components and supplies
1
Nano 33 BLE Sense
Apps and platforms
1
Visual Studio Code Extension for Arduino
1
Arduino IDE
1
VS Code
Project description
Code
Sketch
arduino
Upload this sketch to your board to get started!
1#include <ArduinoBLE.h> 2#include <Arduino_APDS9960.h> 3#include <Arduino_HTS221.h> 4#include <Arduino_LPS22HB.h> 5#include <Arduino_LSM9DS1.h> 6 7 8// Device name 9const char* nameOfPeripheral = "Nano BLE Sense"; 10const char* uuidOfSenseService = "00001101-0000-1000-8000-00805f9b34fb"; 11const char* uuidOfSensorCharacteristic = "00001141-0000-1000-8000-00805f9b34fb"; 12 13// BLE Service 14BLEService senseService(uuidOfSenseService); 15 16// Characteristics 17// =================================================================================================================== 18// | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 19// |gesture|proximity|color r|color g|color b|light intensity|temperature_0|temperature_1|temperature_2|temperature_3| 20// ========================================================================================= 21// | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 22// |humidity_0|humidity_1|humidity_2|humidity_3|pressure_0|pressure_1|pressure_2|pressure_3| 23// ================================================================================================= 24// | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 25// |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| 26// ================================================================================================= 27// | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 28// |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| 29// ================================================================================================= 30// | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 31// |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| 32// ================================================================================================= 33BLECharacteristic sensorCharacteristic(uuidOfSensorCharacteristic, BLERead | BLENotify | BLEBroadcast, 54, true); 34 35void setup() { 36 37 pinMode(LED_BUILTIN, OUTPUT); 38 pinMode(LEDB, OUTPUT); 39 pinMode(LEDR, OUTPUT); 40 pinMode(LEDG, OUTPUT); 41 42 digitalWrite(LED_BUILTIN, LOW); 43 digitalWrite(LEDB, HIGH); 44 digitalWrite(LEDR, HIGH); 45 digitalWrite(LEDG, HIGH); 46 47 if (!BLE.begin()) { 48 digitalWrite(LEDR, LOW); 49 while (true); 50 } 51 52 if (!APDS.begin()) { 53 digitalWrite(LEDR, LOW); 54 while (true); 55 } 56 57 if (!HTS.begin()) { 58 digitalWrite(LEDR, LOW); 59 while (true); 60 } 61 62 if (!BARO.begin()) { 63 digitalWrite(LEDR, LOW); 64 while (true); 65 } 66 67 if (!IMU.begin()) { 68 digitalWrite(LEDR, LOW); 69 while (true); 70 } 71 72 // Create BLE service and characteristics. 73 BLE.setLocalName(nameOfPeripheral); 74 BLE.setAdvertisedService(senseService); 75 senseService.addCharacteristic(sensorCharacteristic); 76 BLE.addService(senseService); 77 78 // Bluetooth LE connection handlers. 79 BLE.setEventHandler(BLEConnected, onBLEConnected); 80 BLE.setEventHandler(BLEDisconnected, onBLEDisconnected); 81 82 // Let's tell devices about us. 83 BLE.advertise(); 84} 85 86uint8_t characteristicBuffer[54]; 87 88int proximity = 0; 89uint8_t gesture = 0x0; 90int r = 0, g = 0, b = 0, a = 0; 91float temperature = 0.0f; 92float humidity = 0.0f; 93float atmosphericPressure = 0.0f; 94 95float accelerometerX = 0.0f; 96float accelerometerY = 0.0f; 97float accelerometerZ = 0.0f; 98 99float gyroscopeX = 0.0f; 100float gyroscopeY = 0.0f; 101float gyroscopeZ = 0.0f; 102 103float magnetometerX = 0.0f; 104float magnetometerY = 0.0f; 105float magnetometerZ = 0.0f; 106 107unsigned long lastLedReset = 0; 108unsigned long lastHtsUpdate = 0; 109unsigned long lastBaroUpdate = 0; 110 111void loop() { 112 113 BLEDevice central = BLE.central(); 114 115 if(millis() - lastLedReset > 50) { 116 lastLedReset = millis(); 117 digitalWrite(LEDR, HIGH); 118 digitalWrite(LED_BUILTIN, LOW); 119 } 120 121 // Check if a proximity reading is available. 122 if (APDS.proximityAvailable()) { 123 proximity = APDS.readProximity(); 124 } 125 126 // Check if a gesture reading is available 127 if (APDS.gestureAvailable()) { 128 gesture = APDS.readGesture(); 129 } 130 131 // Check if a color reading is available 132 if (APDS.colorAvailable()) { 133 APDS.readColor(r, g, b, a); 134 } 135 136 // Check if a accelerometer, gyroscope or magnetometer reading is available 137 if (IMU.accelerationAvailable()) { 138 IMU.readAcceleration(accelerometerX, accelerometerY, accelerometerZ); 139 } 140 if (IMU.gyroscopeAvailable()){ 141 IMU.readGyroscope(gyroscopeX, gyroscopeY, gyroscopeZ); 142 } 143 if (IMU.magneticFieldAvailable()) { 144 IMU.readMagneticField(magnetometerX, magnetometerY, magnetometerZ); 145 } 146 147 // Read temperature and humidity from sensor 148 if (millis() - lastHtsUpdate > 1000) { 149 lastHtsUpdate = millis(); 150 temperature = HTS.readTemperature(); 151 humidity = HTS.readHumidity(); 152 } 153 154 // Read atmospheric pressure from sensor 155 if (millis() - lastBaroUpdate > 1000) { 156 lastBaroUpdate = millis(); 157 atmosphericPressure = BARO.readPressure(); 158 } 159 160 updateSensorCharacteristic(); 161 162 // Check BLE connection 163 if (central && central.connected()) { 164 digitalWrite(LEDB, LOW); 165 } 166} 167 168void onBLEConnected(BLEDevice central) { 169 digitalWrite(LEDB, LOW); 170} 171 172void onBLEDisconnected(BLEDevice central) { 173 digitalWrite(LEDB, HIGH); 174} 175 176void updateSensorCharacteristic() { 177 memcpy(&characteristicBuffer[0], &gesture, 1); 178 179 memcpy(&characteristicBuffer[1], &proximity, 1); 180 181 memcpy(&characteristicBuffer[2], &r, 4); 182 memcpy(&characteristicBuffer[3], &g, 4); 183 memcpy(&characteristicBuffer[4], &b, 4); 184 memcpy(&characteristicBuffer[5], &a, 4); 185 186 memcpy(&characteristicBuffer[6], &temperature, 4); 187 memcpy(&characteristicBuffer[10], &humidity, 4); 188 memcpy(&characteristicBuffer[14], &atmosphericPressure, 4); 189 190 memcpy(&characteristicBuffer[18], &accelerometerX, 4); 191 memcpy(&characteristicBuffer[22], &accelerometerY, 4); 192 memcpy(&characteristicBuffer[26], &accelerometerZ, 4); 193 194 memcpy(&characteristicBuffer[30], &gyroscopeX, 4); 195 memcpy(&characteristicBuffer[34], &gyroscopeY, 4); 196 memcpy(&characteristicBuffer[38], &gyroscopeZ, 4); 197 198 memcpy(&characteristicBuffer[42], &magnetometerX, 4); 199 memcpy(&characteristicBuffer[46], &magnetometerY, 4); 200 memcpy(&characteristicBuffer[50], &magnetometerZ, 4); 201 202 if(sensorCharacteristic.writeValue(characteristicBuffer, 54, true)) { 203 digitalWrite(LED_BUILTIN, HIGH); 204 } else { 205 digitalWrite(LEDR, LOW); 206 } 207} 208
Comments
Only logged in users can leave comments