33constexpr size_t MAX_CONNECT_RETRIES = 3 ;
44constexpr size_t BLE_SCAN_DURATION_SECONDS = 10 ;
55
6- NimBLEClientController::NimBLEClientController ()
7- : client(nullptr ), tempControlChar(nullptr ), pumpControlChar(nullptr ), valveControlChar(nullptr ), altControlChar(nullptr ),
8- tempReadChar(nullptr ), pingChar(nullptr ), pidControlChar(nullptr ), errorChar(nullptr ), autotuneChar(nullptr ),
9- brewBtnChar(nullptr ), steamBtnChar(nullptr ), serverDevice(nullptr ) {}
6+ NimBLEClientController::NimBLEClientController () : client(nullptr ) {}
107
118void NimBLEClientController::initClient () {
129 NimBLEDevice::init (" GPBLC" );
@@ -29,16 +26,13 @@ void NimBLEClientController::scan() {
2926 pBLEScan->start (BLE_SCAN_DURATION_SECONDS, nullptr , false );
3027}
3128
32- void NimBLEClientController::registerTempReadCallback (const temp_read_callback_t &callback) { tempReadCallback = callback; }
33-
3429void NimBLEClientController::registerRemoteErrorCallback (const remote_err_callback_t &callback) {
3530 remoteErrorCallback = callback;
3631}
37-
3832void NimBLEClientController::registerBrewBtnCallback (const brew_callback_t &callback) { brewBtnCallback = callback; }
3933void NimBLEClientController::registerSteamBtnCallback (const brew_callback_t &callback) { steamBtnCallback = callback; }
4034
41- void NimBLEClientController::registerPressureCallback (const pressure_read_callback_t &callback) { pressureCallback = callback; }
35+ void NimBLEClientController::registerSensorCallback (const sensor_read_callback_t &callback) { sensorCallback = callback; }
4236
4337void NimBLEClientController::registerAutotuneResultCallback (const pid_control_callback_t &callback) {
4438 autotuneResultCallback = callback;
@@ -81,21 +75,15 @@ bool NimBLEClientController::connectToServer() {
8175 }
8276
8377 // Obtain the remote write characteristics
84- tempControlChar = pRemoteService->getCharacteristic (NimBLEUUID (TEMP_CONTROL_CHAR_UUID));
85- pumpControlChar = pRemoteService->getCharacteristic (NimBLEUUID (PUMP_CONTROL_CHAR_UUID));
86- valveControlChar = pRemoteService->getCharacteristic (NimBLEUUID (VALVE_CONTROL_CHAR_UUID));
78+ outputControlChar = pRemoteService->getCharacteristic (NimBLEUUID (OUTPUT_CONTROL_UUID));
8779 altControlChar = pRemoteService->getCharacteristic (NimBLEUUID (ALT_CONTROL_CHAR_UUID));
8880 autotuneChar = pRemoteService->getCharacteristic (NimBLEUUID (AUTOTUNE_CHAR_UUID));
8981 pingChar = pRemoteService->getCharacteristic (NimBLEUUID (PING_CHAR_UUID));
9082 pidControlChar = pRemoteService->getCharacteristic (NimBLEUUID (PID_CONTROL_CHAR_UUID));
9183 infoChar = pRemoteService->getCharacteristic (NimBLEUUID (INFO_UUID));
84+ pressureScaleChar = pRemoteService->getCharacteristic (NimBLEUUID (PRESSURE_SCALE_UUID));
9285
9386 // Obtain the remote notify characteristic and subscribe to it
94- tempReadChar = pRemoteService->getCharacteristic (NimBLEUUID (TEMP_READ_CHAR_UUID));
95- if (tempReadChar->canNotify ()) {
96- tempReadChar->subscribe (true , std::bind (&NimBLEClientController::notifyCallback, this , std::placeholders::_1,
97- std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
98- }
9987
10088 errorChar = pRemoteService->getCharacteristic (NimBLEUUID (ERROR_CHAR_UUID));
10189 if (errorChar->canNotify ()) {
@@ -121,17 +109,25 @@ bool NimBLEClientController::connectToServer() {
121109 std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
122110 }
123111
112+ sensorChar = pRemoteService->getCharacteristic (NimBLEUUID (SENSOR_DATA_UUID));
113+ if (sensorChar != nullptr && sensorChar->canNotify ()) {
114+ sensorChar->subscribe (true , std::bind (&NimBLEClientController::notifyCallback, this , std::placeholders::_1,
115+ std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
116+ }
117+
124118 delay (500 );
125119
126120 readyForConnection = false ;
127121 return true ;
128122}
129123
130- void NimBLEClientController::sendTemperatureControl (float setpoint) {
131- if (tempControlChar != nullptr && client->isConnected ()) {
132- char tempStr[8 ];
133- snprintf (tempStr, sizeof (tempStr), " %.2f" , setpoint);
134- tempControlChar->writeValue (tempStr);
124+ void NimBLEClientController::sendOutputControl (bool valve, float pumpSetpoint, float boilerSetpoint) {
125+ if (client->isConnected () && outputControlChar != nullptr ) {
126+ char str[30 ];
127+ snprintf (str, sizeof (str), " %d,%d,%.1f,%.1f" , 0 , valve ? 1 : 0 , pumpSetpoint, boilerSetpoint);
128+ if (outputControlChar->getValue ().c_str () != str) {
129+ outputControlChar->writeValue (str, false );
130+ }
135131 }
136132}
137133
@@ -141,17 +137,12 @@ void NimBLEClientController::sendPidSettings(const String &pid) {
141137 }
142138}
143139
144- void NimBLEClientController::sendPumpControl (float setpoint) {
145- if (pumpControlChar != nullptr && client->isConnected ()) {
146- char pumpStr[8 ];
147- snprintf (pumpStr, sizeof (pumpStr), " %.2f" , setpoint);
148- pumpControlChar->writeValue (pumpStr);
149- }
150- }
151-
152- void NimBLEClientController::sendValveControl (bool pinState) {
153- if (valveControlChar != nullptr && client->isConnected ()) {
154- valveControlChar->writeValue (pinState ? " 1" : " 0" );
140+ void NimBLEClientController::setPressureScale (float scale) {
141+ if (client->isConnected () && pressureScaleChar != nullptr ) {
142+ constexpr size_t bufferSize = sizeof (float );
143+ char buffer[bufferSize];
144+ std::memcpy (buffer + 0 , &scale, sizeof (scale));
145+ pressureScaleChar->writeValue (buffer);
155146 }
156147}
157148
@@ -203,20 +194,6 @@ void NimBLEClientController::onDisconnect(NimBLEClient *pServer) {
203194// Notification callback
204195void NimBLEClientController::notifyCallback (NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t ,
205196 bool ) const {
206- if (pRemoteCharacteristic->getUUID ().equals (NimBLEUUID (TEMP_READ_CHAR_UUID))) {
207- float temperature = atof ((char *)pData);
208- ESP_LOGV (LOG_TAG, " Temperature read: %.2f" , temperature);
209- if (tempReadCallback != nullptr ) {
210- tempReadCallback (temperature);
211- }
212- }
213- if (pRemoteCharacteristic->getUUID ().equals (NimBLEUUID (PRESSURE_UUID))) {
214- float pressure = atof ((char *)pData);
215- ESP_LOGV (LOG_TAG, " Pressure read: %.2f" , pressure);
216- if (pressureCallback != nullptr ) {
217- pressureCallback (pressure);
218- }
219- }
220197 if (pRemoteCharacteristic->getUUID ().equals (NimBLEUUID (ERROR_CHAR_UUID))) {
221198 int errorCode = atoi ((char *)pData);
222199 ESP_LOGV (LOG_TAG, " Error read: %d" , errorCode);
@@ -238,6 +215,16 @@ void NimBLEClientController::notifyCallback(NimBLERemoteCharacteristic *pRemoteC
238215 steamBtnCallback (steamButtonStatus);
239216 }
240217 }
218+ if (pRemoteCharacteristic->getUUID ().equals (NimBLEUUID (SENSOR_DATA_UUID))) {
219+ String data = String ((char *)pData);
220+ float temperature = get_token (data, 0 , ' ,' ).toFloat ();
221+ float pressure = get_token (data, 1 , ' ,' ).toFloat ();
222+
223+ ESP_LOGV (LOG_TAG, " Received sensor data: temperature=%.1f, pressure=%.1f" , temperature, pressure);
224+ if (sensorCallback != nullptr ) {
225+ sensorCallback (temperature, pressure);
226+ }
227+ }
241228 if (pRemoteCharacteristic->getUUID ().equals (NimBLEUUID (AUTOTUNE_RESULT_UUID))) {
242229 String settings = String ((char *)pData);
243230 ESP_LOGV (LOG_TAG, " autotune result: %s" , settings.c_str ());
0 commit comments