bool notifySuccess = pTxCharacteristic->notify(); は、pTxCharacteristic->notify() を呼び出した直後に追加します。具体的には、notify() を呼び出している箇所すべてにこのチェックを追加します。
以下に、修正が必要な箇所を示します。
1. デバイス名の変更処理
NAME: メッセージを処理する部分に追加します。
if (newDeviceName.length() > 0 && newDeviceName.length() <= 32) {
Serial.printf("newDeviceName.length: %d\n", newDeviceName.length());
// EEPROMに保存
EEPROM.writeString(NAME_ADDR, newDeviceName);
if (EEPROM.commit()) {
// アドバタイジングデータを更新
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->stop();
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // 推奨設定
pAdvertising->setMinPreferred(0x12);
pAdvertising->start();
// 成功メッセージを送信
String response = "NAME_SET_SUCCESS:" + newDeviceName;
pTxCharacteristic->setValue(response.c_str());
bool notifySuccess = pTxCharacteristic->notify(); // 通知の成功を確認
if (notifySuccess) {
Serial.println("Notification sent successfully.");
} else {
Serial.println("Failed to send notification.");
}
Serial.println("Response sent: " + response);
} else {
// EEPROMへの保存に失敗した場合
String errorMessage = "ERROR: Failed to save to EEPROM";
pTxCharacteristic->setValue(errorMessage.c_str());
bool notifySuccess = pTxCharacteristic->notify(); // 通知の成功を確認
if (notifySuccess) {
Serial.println("Notification sent successfully.");
} else {
Serial.println("Failed to send notification.");
}
Serial.println(errorMessage);
}
}
2. ポンプ設定の変更処理
PUMP: メッセージを処理する部分に追加します。
if (newHourDivision >= 1 && newHourDivision <= 1440 &&
newPumpOnPercentage >= 0 && newPumpOnPercentage <= 100) {
// EEPROMに保存
EEPROM.writeInt(HOUR_DIVISION_ADDR, newHourDivision);
EEPROM.writeInt(PUMP_ON_PERCENTAGE_ADDR, newPumpOnPercentage);
EEPROM.commit();
// メモリ上の変数を更新
hour_devision = newHourDivision;
pump_on_percentage = newPumpOnPercentage;
// 応答を送信
String response = "PUMP_SET:" + String(hour_devision) + "," + String(pump_on_percentage);
pTxCharacteristic->setValue(response.c_str());
bool notifySuccess = pTxCharacteristic->notify(); // 通知の成功を確認
if (notifySuccess) {
Serial.println("Notification sent successfully.");
} else {
Serial.println("Failed to send notification.");
}
Serial.println("Response sent: " + response);
Serial.printf("Updated hour_devision: %d, pump_on_percentage: %d\n", hour_devision, pump_on_percentage);
}
3. パラメータの取得処理
GET_PARAMS メッセージを処理する部分に追加します。
String params = "PARAMS:" + String(hour_devision) + "," + String(pump_on_percentage);
pTxCharacteristic->setValue(params.c_str());
bool notifySuccess = pTxCharacteristic->notify(); // 通知の成功を確認
if (notifySuccess) {
Serial.println("Notification sent successfully.");
} else {
Serial.println("Failed to send notification.");
}
Serial.println("Parameters sent: " + params);
4. センサーデータの送信処理
measureAndSendSensorData() 関数内に追加します。
if (deviceConnected) {
pTxCharacteristic->setValue(sensorData.c_str());
bool notifySuccess = pTxCharacteristic->notify(); // 通知の成功を確認
if (notifySuccess) {
Serial.println("Notification sent successfully.");
} else {
Serial.println("Failed to send notification.");
}
}
動作確認
- 通知の成功確認
各通知が成功した場合、Notification sent successfully.がログに出力されます。 - 通知の失敗確認
通知が失敗した場合、Failed to send notification.がログに出力されます。
例: ログ出力
Received string: NAME:minIUSB04
New device name: minIUSB04
newDeviceName.length: 9
Notification sent successfully.
Response sent: NAME_SET_SUCCESS:minIUSB04
