|
| 1 | +#include "LowPower.h" |
| 2 | +#include <arm_math.h> |
| 3 | + |
| 4 | +#include "config.h" |
| 5 | +#include <PDM.h> |
| 6 | + |
| 7 | +#include <ArduinoBLE.h> |
| 8 | + |
| 9 | +const int VERSION = 0x00000001; |
| 10 | + |
| 11 | +#define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8") |
| 12 | +#define RESISTANCE_PIN A0 |
| 13 | +#define VOLTAGE_BUFFER_SIZE 16 |
| 14 | +#define DEBUG 0 |
| 15 | + |
| 16 | +BLEService service(SCIENCE_KIT_UUID("0000")); |
| 17 | +BLEUnsignedIntCharacteristic versionCharacteristic(SCIENCE_KIT_UUID("0001"), BLERead); |
| 18 | +BLECharacteristic accelerationCharacteristic(SCIENCE_KIT_UUID("0011"), BLENotify, 3 * sizeof(float)); |
| 19 | +BLECharacteristic gyroscopeCharacteristic(SCIENCE_KIT_UUID("0012"), BLENotify, 3 * sizeof(float)); |
| 20 | +BLECharacteristic magneticFieldCharacteristic(SCIENCE_KIT_UUID("0013"), BLENotify, 3 * sizeof(float)); |
| 21 | +BLEFloatCharacteristic temperatureCharacteristic(SCIENCE_KIT_UUID("0014"), BLENotify); |
| 22 | +BLEFloatCharacteristic pressureCharacteristic(SCIENCE_KIT_UUID("0015"), BLENotify); |
| 23 | +BLEFloatCharacteristic humidityCharacteristic(SCIENCE_KIT_UUID("0016"), BLENotify); |
| 24 | +BLEUnsignedIntCharacteristic proximityCharacteristic(SCIENCE_KIT_UUID("0017"), BLENotify); |
| 25 | +BLECharacteristic colorCharacteristic(SCIENCE_KIT_UUID("0018"), BLENotify, 4 * sizeof(int)); |
| 26 | +BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify); |
| 27 | +BLEFloatCharacteristic resistanceCharacteristic(SCIENCE_KIT_UUID("0020"), BLENotify); |
| 28 | + |
| 29 | +byte voltageBufferIndex = 0; |
| 30 | +bool voltageBufferFilled = false; |
| 31 | +short soundSampleBuffer[256]; |
| 32 | +short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE]; |
| 33 | + |
| 34 | +void updateSubscribedCharacteristics(); |
| 35 | + |
| 36 | +void onPDMdata() { |
| 37 | + // query the number of bytes available |
| 38 | + int bytesAvailable = PDM.available(); |
| 39 | + |
| 40 | + // read into the sample buffer |
| 41 | + PDM.read(soundSampleBuffer, bytesAvailable); |
| 42 | +} |
| 43 | + |
| 44 | +uint16_t getSoundAverage() { |
| 45 | + uint32_t avg = 0; |
| 46 | + for (int i = 0; i < sizeof(soundSampleBuffer) / sizeof(soundSampleBuffer[0]); i++) { |
| 47 | + avg += soundSampleBuffer[i] * soundSampleBuffer[i]; |
| 48 | + } |
| 49 | + return sqrt(avg); |
| 50 | +} |
| 51 | + |
| 52 | +void readVoltage() { |
| 53 | + voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN); |
| 54 | + if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE - 1) { |
| 55 | + voltageBufferFilled = true; |
| 56 | + } |
| 57 | + voltageBufferIndex = (++voltageBufferIndex) % VOLTAGE_BUFFER_SIZE; |
| 58 | +} |
| 59 | + |
| 60 | +uint16_t getVoltageAverage() { |
| 61 | + uint16_t avg = 0; |
| 62 | + byte upperBound = voltageBufferFilled ? VOLTAGE_BUFFER_SIZE : voltageBufferIndex; |
| 63 | + for (int i = 0; i < upperBound; i++) { |
| 64 | + avg += voltageSampleBuffer[i]; |
| 65 | + } |
| 66 | + return avg / upperBound; |
| 67 | +} |
| 68 | + |
| 69 | +// String to calculate the local and device name |
| 70 | +String name; |
| 71 | +unsigned long lastNotify = 0; |
| 72 | + |
| 73 | +void printSerialMsg(const char* msg) { |
| 74 | +#ifdef DEBUG |
| 75 | + if (Serial) { |
| 76 | + Serial.println(msg); |
| 77 | + } |
| 78 | +#endif |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +void setup() { |
| 84 | +#ifdef DEBUG |
| 85 | + Serial.begin(9600); |
| 86 | + while (!Serial) |
| 87 | + ; |
| 88 | + Serial.println("Started"); |
| 89 | +#endif |
| 90 | + |
| 91 | + delay(2000); |
| 92 | + sensorsInit(); |
| 93 | + |
| 94 | + pinMode(RESISTANCE_PIN, INPUT); // Used for reading resistance |
| 95 | + |
| 96 | + PDM.onReceive(onPDMdata); |
| 97 | + if (!PDM.begin(1, 16000)) { |
| 98 | + printSerialMsg("Failed to start PDM!"); |
| 99 | + blinkLoop(); |
| 100 | + } |
| 101 | + |
| 102 | + if (!BLE.begin()) { |
| 103 | + printSerialMsg("Failed to initialized BLE!"); |
| 104 | + blinkLoop(); |
| 105 | + } |
| 106 | + |
| 107 | + String address = BLE.address(); |
| 108 | +#ifdef DEBUG |
| 109 | + if (Serial) { |
| 110 | + Serial.print("address = "); |
| 111 | + Serial.println(address); |
| 112 | + } |
| 113 | +#endif |
| 114 | + address.toUpperCase(); |
| 115 | + |
| 116 | + name = "BLE Sense - "; |
| 117 | + name += address[address.length() - 5]; |
| 118 | + name += address[address.length() - 4]; |
| 119 | + name += address[address.length() - 2]; |
| 120 | + name += address[address.length() - 1]; |
| 121 | + |
| 122 | +#ifdef DEBUG |
| 123 | + if (Serial) { |
| 124 | + Serial.print("name = "); |
| 125 | + Serial.println(name); |
| 126 | + } |
| 127 | +#endif |
| 128 | + |
| 129 | + BLE.setLocalName(name.c_str()); |
| 130 | + BLE.setDeviceName(name.c_str()); |
| 131 | + BLE.setAdvertisedService(service); |
| 132 | + |
| 133 | + service.addCharacteristic(versionCharacteristic); |
| 134 | + service.addCharacteristic(accelerationCharacteristic); |
| 135 | + service.addCharacteristic(gyroscopeCharacteristic); |
| 136 | + service.addCharacteristic(magneticFieldCharacteristic); |
| 137 | + service.addCharacteristic(temperatureCharacteristic); |
| 138 | + service.addCharacteristic(pressureCharacteristic); |
| 139 | + service.addCharacteristic(humidityCharacteristic); |
| 140 | + service.addCharacteristic(proximityCharacteristic); |
| 141 | + service.addCharacteristic(colorCharacteristic); |
| 142 | + service.addCharacteristic(soundPressureCharacteristic); |
| 143 | + service.addCharacteristic(resistanceCharacteristic); |
| 144 | + |
| 145 | + versionCharacteristic.setValue(VERSION); |
| 146 | + |
| 147 | + BLE.addService(service); |
| 148 | + BLE.advertise(); |
| 149 | + |
| 150 | + lowPower(); |
| 151 | +} |
| 152 | + |
| 153 | +void loop() { |
| 154 | + BLE.poll(1000); |
| 155 | + while (BLE.connected()) { |
| 156 | + lowPowerBleWait(100); |
| 157 | + updateSubscribedCharacteristics(); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +void updateSubscribedCharacteristics() { |
| 162 | + if (accelerationCharacteristic.subscribed()) { |
| 163 | + if (IMU.accelerationAvailable()) { |
| 164 | + float x, y, z; |
| 165 | + IMU.readAcceleration(x, y, z); |
| 166 | + float acceleration[3]; |
| 167 | + |
| 168 | + acceleration[0] = x; |
| 169 | + acceleration[1] = y; |
| 170 | + acceleration[2] = z; |
| 171 | + accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration)); |
| 172 | + } |
| 173 | + } |
| 174 | + if (gyroscopeCharacteristic.subscribed()) { |
| 175 | + if (IMU.gyroscopeAvailable()) { |
| 176 | + float x, y, z; |
| 177 | + IMU.readGyroscope(x, y, z); |
| 178 | + float gyroscope[3]; |
| 179 | + |
| 180 | + gyroscope[0] = x; |
| 181 | + gyroscope[1] = y; |
| 182 | + gyroscope[2] = z; |
| 183 | + gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope)); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + if (magneticFieldCharacteristic.subscribed()) { |
| 188 | + float x, y, z; |
| 189 | + if (IMU.magneticFieldAvailable()) { |
| 190 | + IMU.readMagneticField(x, y, z); |
| 191 | + float magneticField[3]; |
| 192 | + magneticField[0] = x; |
| 193 | + magneticField[1] = y; |
| 194 | + magneticField[2] = z; |
| 195 | + magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField)); |
| 196 | + } |
| 197 | + } |
| 198 | + if (soundPressureCharacteristic.subscribed()) { |
| 199 | + uint16_t sound = getSoundAverage(); |
| 200 | + soundPressureCharacteristic.writeValue(sound); |
| 201 | + } |
| 202 | + |
| 203 | + bool doTemperature = temperatureCharacteristic.subscribed(); |
| 204 | + bool doHumidity = humidityCharacteristic.subscribed(); |
| 205 | + if (doTemperature || doHumidity) { |
| 206 | + float temperature = HS300x.readTemperature(); |
| 207 | + if (doTemperature) { |
| 208 | + temperatureCharacteristic.writeValue(temperature); |
| 209 | + } |
| 210 | + if (doHumidity) { |
| 211 | + float humidity = HS300x.readHumidity(); |
| 212 | + float dp = temperature - ((100.0 - humidity) / 5.0); |
| 213 | + float humidityCalibrated = 100.0 - (5.0 * (temperature - dp)); |
| 214 | + humidityCharacteristic.writeValue(humidityCalibrated); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + if (resistanceCharacteristic.subscribed()) { |
| 219 | + readVoltage(); |
| 220 | + uint16_t measuredValue = getVoltageAverage(); |
| 221 | + float voltageRatio = 1024.0f / measuredValue; |
| 222 | + resistanceCharacteristic.writeValue(voltageRatio); |
| 223 | + } |
| 224 | + |
| 225 | + if (proximityCharacteristic.subscribed() && APDS.proximityAvailable()) { |
| 226 | + uint32_t proximity = APDS.readProximity(); |
| 227 | + proximityCharacteristic.writeValue(proximity); |
| 228 | + } |
| 229 | + if (colorCharacteristic.subscribed() && APDS.colorAvailable()) { |
| 230 | + int color[4]; |
| 231 | + APDS.readColor(color[0], color[1], color[2], color[3]); |
| 232 | + colorCharacteristic.writeValue((byte*)color, sizeof(color)); |
| 233 | + } |
| 234 | + |
| 235 | + if (pressureCharacteristic.subscribed()) { |
| 236 | + float pressure = BARO.readPressure(); |
| 237 | + pressureCharacteristic.writeValue(pressure); |
| 238 | + } |
| 239 | +} |
0 commit comments