Created
September 25, 2024 22:20
-
-
Save jscatena88/ee2522da96fa93db7b40c708e668b82f to your computer and use it in GitHub Desktop.
BLE Manometer
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 <BLEDevice.h> | |
#include <BLEUtils.h> | |
#include <BLEServer.h> | |
#include <BLE2904.h> | |
#include <BLE2902.h> | |
#include <Wire.h> // Must include Wire library for I2C | |
#include <SparkFun_MicroPressure.h> | |
#include <SparkFun_I2C_Mux_Arduino_Library.h> | |
QWIICMUX myMux; | |
#define NumberOfPressureSensors (2) | |
static auto deviceConnected = false; | |
class MyServerCallbacks: public BLEServerCallbacks { | |
void onConnect(BLEServer* pServer) { | |
deviceConnected = true; | |
}; | |
void onDisconnect(BLEServer* pServer) { | |
deviceConnected = false; | |
BLEDevice::startAdvertising(); | |
} | |
}; | |
SparkFun_MicroPressure Mprs[NumberOfPressureSensors]; | |
// See the following for generating UUIDs: | |
// https://www.uuidgenerator.net/ | |
#define SERVICE_UUID "b9e51711-5665-4cca-adf8-cfbef91a97c4" | |
#define CHARACTERISTIC_PRESSURE_1_UUID "49d190d5-a199-4850-956d-74be95d4dea2" | |
#define CHARACTERISTIC_PRESSURE_2_UUID "3144fe5c-9e80-4bbc-8c1f-6baaa4c50b49" | |
static const uint16_t UNIT_PSI = 0x27A5; | |
// makes the chracteristic globlal | |
static BLECharacteristic *pCharacteristicPressure1; | |
static BLECharacteristic *pCharacteristicPressure2; | |
static BLE2904 DescriptorPressure1; | |
static BLE2904 DescriptorPressure2; | |
static MyServerCallbacks server_callbacks; | |
void setup() { | |
Serial.begin(115200); | |
Wire.begin(); | |
if (myMux.begin() == false) { | |
Serial.println("Error with Mux, aborting"); | |
while (1); | |
} | |
uint8_t port_state = myMux.getPortState(); | |
Serial.printf("Mux Detected, Port State: %x\n", port_state); | |
myMux.setPortState(0); | |
port_state = myMux.getPortState(); | |
Serial.printf("New Port State: %x\n", port_state); | |
bool sensor_init_success = true; | |
for (int i = 0; i < NumberOfPressureSensors; i++) { | |
myMux.enablePort(i+6); | |
if (!Mprs[i].begin()) { | |
sensor_init_success = false; | |
break; | |
} | |
myMux.disablePort(i+6); | |
} | |
if (!sensor_init_success) { | |
Serial.println("Failed to init sensors"); | |
while(1); | |
} | |
Serial.println("Sensors init'd"); | |
Serial.println("Starting BLE work!"); | |
BLEDevice::init("Nexus Vacuum Sensor"); | |
BLEServer *pServer = BLEDevice::createServer(); | |
pServer->setCallbacks(&server_callbacks); | |
BLEService *pService = pServer->createService(SERVICE_UUID); | |
pCharacteristicPressure1 = pService->createCharacteristic( | |
CHARACTERISTIC_PRESSURE_1_UUID, | |
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_READ | |
); | |
DescriptorPressure1.setFormat(BLE2904::FORMAT_FLOAT32); | |
DescriptorPressure1.setUnit(UNIT_PSI); | |
pCharacteristicPressure1->addDescriptor(&DescriptorPressure1); | |
pCharacteristicPressure1->addDescriptor(new BLE2902()); | |
pCharacteristicPressure2 = pService->createCharacteristic( | |
CHARACTERISTIC_PRESSURE_2_UUID, | |
BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_READ | |
); | |
DescriptorPressure2.setFormat(BLE2904::FORMAT_FLOAT32); | |
DescriptorPressure2.setUnit(UNIT_PSI); | |
pCharacteristicPressure2->addDescriptor(&DescriptorPressure2); | |
pCharacteristicPressure2->addDescriptor(new BLE2902()); | |
Serial.println("Starting Service"); | |
pService->start(); | |
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility | |
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); | |
pAdvertising->addServiceUUID(SERVICE_UUID); | |
pAdvertising->setScanResponse(true); | |
pAdvertising->setMinPreferred(0x06); | |
pAdvertising->setMaxPreferred(0x12); | |
BLEDevice::startAdvertising(); | |
Serial.println("Characteristics defined!"); | |
} | |
void loop() { | |
if (!deviceConnected) { | |
// Loop endlessly if no one is connected | |
delay(10); | |
return; | |
} | |
// put your main code here, to run repeatedly: | |
float pressure_reading_0; | |
float pressure_reading_1; | |
myMux.enablePort(6); | |
pressure_reading_0 = Mprs[0].readPressure(); | |
pCharacteristicPressure1->setValue(pressure_reading_0);//setValue takes uint8_t, uint16_t, uint32_t, int, float, double and string | |
pCharacteristicPressure1->notify(); | |
myMux.disablePort(6); | |
myMux.enablePort(7); | |
pressure_reading_1 = Mprs[1].readPressure(); | |
pCharacteristicPressure2->setValue( pressure_reading_1);//setValue takes uint8_t, uint16_t, uint32_t, int, float, double and string | |
pCharacteristicPressure2->notify(); | |
myMux.disablePort(7); | |
//Serial.print("PSI: "); | |
Serial.printf("%f :: %f\n", pressure_reading_0, pressure_reading_1); | |
delay(10);// 100 ms | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment