Created
January 3, 2021 10:13
-
-
Save Abathargh/bff1aaa94b1c7fab600654a5a9061f72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <PDM.h> | |
#include <arm_math.h> | |
#include <Arduino_HTS221.h> | |
#include <Arduino_LPS22HB.h> | |
#include <Arduino_APDS9960.h> | |
#include <ArduinoBLE.h> | |
#define BLE_NAME "Moody-ble" | |
#define MAX_NAME_SIZE 256 | |
#define BUFFER_SIZE 256 | |
#define SERVICES 5 | |
#define DARK 0 | |
#define LIGHT 1 | |
#define LAMP 2 | |
static const long randSuff = random(100, 1000); | |
static char bleName[10]; | |
BLEService pressureService("c3796395-a8d5-4f17-8c2e-a85ebd16f6f1"); | |
BLEService temperatureService("9352b3a5-abeb-4ef6-8ba1-eba4a57926d1"); | |
BLEService humidityService("e6c13adf-d41b-42b1-adc7-c78c947a01e4"); | |
BLEService proximityService("5110b7cf-1c48-4435-ab04-133a3e2cba69"); | |
BLEService colorService("9059c260-5136-4794-a1f7-34eae684fc9d"); | |
BLEService gestureService("c02c3582-5538-447f-895e-b02ff08097f3"); | |
BLEService micService("f6675d4c-cde6-4006-992c-11e6e2bbfad5"); | |
BLEStringCharacteristic pressureName("f05b2077-779e-4bfc-ba0b-cf14e804d6fa", BLERead, MAX_NAME_SIZE); | |
BLEFloatCharacteristic pressureReading("5c23a789-c501-4e59-9547-238a924ee363", BLERead | BLENotify); | |
#if defined(TEMP_HUM) | |
BLEStringCharacteristic temperatureName("f05b2077-779e-4bfc-ba0b-cf14e804d6fa", BLERead, MAX_NAME_SIZE); | |
BLEFloatCharacteristic temperatureReading("5c23a789-c501-4e59-9547-238a924ee363", BLERead | BLENotify); | |
BLEStringCharacteristic humidityName("f05b2077-779e-4bfc-ba0b-cf14e804d6fa", BLERead, MAX_NAME_SIZE); | |
BLEFloatCharacteristic humidityReading("5c23a789-c501-4e59-9547-238a924ee363", BLERead | BLENotify); | |
#endif | |
BLEStringCharacteristic proximityName("f05b2077-779e-4bfc-ba0b-cf14e804d6fa", BLERead, MAX_NAME_SIZE); | |
BLEFloatCharacteristic proximityReading("5c23a789-c501-4e59-9547-238a924ee363", BLERead | BLENotify); | |
BLEStringCharacteristic colorName("f05b2077-779e-4bfc-ba0b-cf14e804d6fa", BLERead, MAX_NAME_SIZE); | |
BLEFloatCharacteristic colorReading("5c23a789-c501-4e59-9547-238a924ee363", BLERead | BLENotify); | |
BLEStringCharacteristic gestureName("f05b2077-779e-4bfc-ba0b-cf14e804d6fa", BLERead, MAX_NAME_SIZE); | |
BLEFloatCharacteristic gestureReading("5c23a789-c501-4e59-9547-238a924ee363", BLERead | BLENotify); | |
BLEStringCharacteristic micName("f05b2077-779e-4bfc-ba0b-cf14e804d6fa", BLERead, MAX_NAME_SIZE); | |
BLEFloatCharacteristic micReading("5c23a789-c501-4e59-9547-238a924ee363", BLERead | BLENotify); | |
int16_t microphoneBuffer[BUFFER_SIZE]; | |
bool microphoneBufferReadyFlag; | |
int16_t microphoneRMSval; | |
float fval; | |
void Microphone_availablePDMDataCallback() | |
{ | |
int bytesAvailable = PDM.available(); | |
PDM.read(microphoneBuffer, bytesAvailable); | |
microphoneBufferReadyFlag = true; | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
pinMode(LED_BUILTIN, OUTPUT); | |
BLE.begin(); | |
BLE.setLocalName(BLE_NAME); | |
BLE.advertise(); | |
pressureService.addCharacteristic(pressureName); | |
pressureService.addCharacteristic(pressureReading); | |
BLE.addService(pressureService); | |
pressureName.writeValue("pressure"); | |
#if defined(TEMP_HUM) | |
temperatureService.addCharacteristic(temperatureName); | |
temperatureService.addCharacteristic(temperatureReading); | |
BLE.addService(temperatureService); | |
temperatureName.writeValue("temperature"); | |
humidityService.addCharacteristic(humidityName); | |
humidityService.addCharacteristic(humidityReading); | |
BLE.addService(humidityService); | |
humidityName.writeValue("humidity"); | |
#endif | |
proximityService.addCharacteristic(proximityName); | |
proximityService.addCharacteristic(proximityReading); | |
BLE.addService(proximityService); | |
proximityName.writeValue("proximity"); | |
colorService.addCharacteristic(colorName); | |
colorService.addCharacteristic(colorReading); | |
BLE.addService(colorService); | |
colorName.writeValue("color"); | |
gestureService.addCharacteristic(gestureName); | |
gestureService.addCharacteristic(gestureReading); | |
BLE.addService(gestureService); | |
gestureName.writeValue("gesture"); | |
micService.addCharacteristic(micName); | |
micService.addCharacteristic(micReading); | |
BLE.addService(micService); | |
micName.writeValue("mic"); | |
// PDM setup | |
PDM.onReceive(Microphone_availablePDMDataCallback); | |
PDM.begin(1, 16000); | |
PDM.setGain(50); | |
microphoneBufferReadyFlag = false; | |
// APDS setup | |
APDS.setGestureSensitivity(50); | |
APDS.begin(); | |
APDS.setLEDBoost(0); | |
// BAR and HTS setup | |
BARO.begin(); | |
#if defined(TEMP_HUM) | |
HTS.begin(); | |
#endif | |
BLE.advertise(); | |
} | |
void loop() | |
{ | |
BLEDevice central = BLE.central(); | |
if (central) | |
{ | |
digitalWrite(LED_BUILTIN, HIGH); | |
while (central.connected()) | |
{ | |
fval = BARO.readPressure(); | |
pressureReading.writeValue(fval); | |
Serial.printf("%f ", fval); | |
#if defined(TEMP_HUM) | |
fval = HTS.readTemperature(); | |
temperatureReading.writeValue(fval); | |
Serial.printf("%f ", fval); | |
fval = HTS.readHumidity(); | |
humidityReading.writeValue(fval); | |
Serial.printf("%f ", fval); | |
#endif | |
if (APDS.proximityAvailable()) | |
{ | |
fval = (float)APDS.readProximity(); | |
proximityReading.writeValue(fval); | |
Serial.printf("%f ", fval); | |
} | |
if (APDS.colorAvailable()) | |
{ | |
int r, g, b; | |
APDS.readColor(r, g, b); | |
fval = (float) ((r < 5 && g < 5 && b < 5) ? DARK : (r < 15 && g < 15 && b < 15) ? LIGHT : LAMP); | |
colorReading.writeValue(fval); | |
Serial.printf("%f ", fval); | |
} | |
if (APDS.gestureAvailable()) | |
{ | |
fval = (float) APDS.readGesture(); | |
gestureReading.writeValue(fval); | |
Serial.printf("%f ", fval); | |
} | |
if (microphoneBufferReadyFlag) | |
{ | |
arm_rms_q15((q15_t *)microphoneBuffer, BUFFER_SIZE * sizeof(int16_t), (q15_t *)µphoneRMSval); | |
microphoneBufferReadyFlag = false; | |
micReading.writeValue((float)microphoneRMSval); | |
Serial.printf("%f ", fval); | |
} | |
Serial.println(); | |
} | |
digitalWrite(LED_BUILTIN, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment