Replace Serial.* logging with ESP_LOG* macros (issue #8)
- Convert all Serial.println/printf debug logging to ESP_LOGE/W/I/D - Add TAG-based log identification (server/client) - Lower CORE_DEBUG_LEVEL from 5 to 3 for production - Keep Serial.printf for USB serial protocol commands (KEY?, VERSION?, etc.)
This commit is contained in:
parent
0261c73746
commit
32489fbabb
3 changed files with 85 additions and 107 deletions
|
|
@ -6,8 +6,11 @@
|
|||
#include <mbedtls/sha256.h>
|
||||
#include <Preferences.h>
|
||||
|
||||
#include <esp_log.h>
|
||||
#include "version.h"
|
||||
|
||||
static const char *TAG = "client";
|
||||
|
||||
#define TOUCH1_PIN 8
|
||||
#define TOUCH2_PIN 6
|
||||
#define BUTTON_PIN 41
|
||||
|
|
@ -70,14 +73,14 @@ class MyScanCallbacks : public NimBLEScanCallbacks {
|
|||
class MyClientCallbacks : public NimBLEClientCallbacks {
|
||||
void onConnect(NimBLEClient *client) override {
|
||||
connected = true;
|
||||
Serial.println("BLE: connected to server");
|
||||
ESP_LOGI(TAG, "BLE: connected to server");
|
||||
}
|
||||
void onDisconnect(NimBLEClient *client, int reason) override {
|
||||
connected = false;
|
||||
relay1SendState = false;
|
||||
relay2SendState = false;
|
||||
pCmdChar = nullptr;
|
||||
Serial.printf("BLE: disconnected (reason=%d)\n", reason);
|
||||
ESP_LOGW(TAG, "BLE: disconnected (reason=%d)", reason);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -129,7 +132,7 @@ static bool connectToServer() {
|
|||
pClient->setClientCallbacks(&clientCallbacks);
|
||||
|
||||
if (!pClient->connect(serverAddress)) {
|
||||
Serial.println("BLE: connect failed");
|
||||
ESP_LOGE(TAG, "BLE: connect failed");
|
||||
NimBLEDevice::deleteClient(pClient);
|
||||
pClient = nullptr;
|
||||
return false;
|
||||
|
|
@ -137,14 +140,14 @@ static bool connectToServer() {
|
|||
|
||||
NimBLERemoteService *pSvc = pClient->getService(SERVICE_UUID);
|
||||
if (pSvc == nullptr) {
|
||||
Serial.println("BLE: service not found");
|
||||
ESP_LOGW(TAG, "BLE: service not found");
|
||||
disconnectClient();
|
||||
return false;
|
||||
}
|
||||
|
||||
NimBLERemoteCharacteristic *pUuidChar = pSvc->getCharacteristic(UUID_CHAR_UUID);
|
||||
if (pUuidChar == nullptr) {
|
||||
Serial.println("BLE: UUID characteristic not found");
|
||||
ESP_LOGW(TAG, "BLE: UUID characteristic not found");
|
||||
disconnectClient();
|
||||
return false;
|
||||
}
|
||||
|
|
@ -152,14 +155,14 @@ static bool connectToServer() {
|
|||
std::string uuidVal = pUuidChar->readValue();
|
||||
if (uuidVal.length() != ESP_UUID_LEN ||
|
||||
memcmp(uuidVal.data(), serverUuid, ESP_UUID_LEN) != 0) {
|
||||
Serial.println("BLE: server UUID mismatch — disconnecting");
|
||||
ESP_LOGW(TAG, "BLE: server UUID mismatch — disconnecting");
|
||||
disconnectClient();
|
||||
return false;
|
||||
}
|
||||
|
||||
pCmdChar = pSvc->getCharacteristic(CHARACTERISTIC_UUID);
|
||||
if (pCmdChar == nullptr) {
|
||||
Serial.println("BLE: command characteristic not found");
|
||||
ESP_LOGW(TAG, "BLE: command characteristic not found");
|
||||
disconnectClient();
|
||||
return false;
|
||||
}
|
||||
|
|
@ -167,14 +170,14 @@ static bool connectToServer() {
|
|||
seq = 0;
|
||||
relay1SendState = false;
|
||||
relay2SendState = false;
|
||||
Serial.println("BLE: server UUID verified, ready");
|
||||
ESP_LOGI(TAG, "BLE: server UUID verified, ready");
|
||||
return true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(2000);
|
||||
Serial.println("=== SylDa Client ===");
|
||||
ESP_LOGI(TAG, "=== SylDa Client v%s ===", FIRMWARE_VERSION_CLIENT);
|
||||
|
||||
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
leds[0] = CRGB::Red;
|
||||
|
|
@ -188,17 +191,15 @@ void setup() {
|
|||
|
||||
if (len == HMAC_KEY_LEN) {
|
||||
keyProvisioned = true;
|
||||
Serial.println("HMAC key loaded from NVS");
|
||||
ESP_LOGI(TAG, "HMAC key loaded from NVS");
|
||||
} else {
|
||||
keyProvisioned = false;
|
||||
Serial.println("No HMAC key — use USB SET_KEY to provision");
|
||||
ESP_LOGW(TAG, "No HMAC key — use USB SET_KEY to provision");
|
||||
}
|
||||
|
||||
if (uuidLen == ESP_UUID_LEN) {
|
||||
uuidProvisioned = true;
|
||||
Serial.print("UUID loaded from NVS: ");
|
||||
for (int i = 0; i < ESP_UUID_LEN; i++) Serial.printf("%02X", deviceUuid[i]);
|
||||
Serial.println();
|
||||
ESP_LOGD(TAG, "UUID loaded from NVS");
|
||||
} else {
|
||||
uuidProvisioned = false;
|
||||
for (int i = 0; i < ESP_UUID_LEN; i++) deviceUuid[i] = (uint8_t)(esp_random() & 0xFF);
|
||||
|
|
@ -206,19 +207,15 @@ void setup() {
|
|||
preferences.putBytes(NVS_UUID_NAME, deviceUuid, ESP_UUID_LEN);
|
||||
preferences.end();
|
||||
uuidProvisioned = true;
|
||||
Serial.print("Generated new UUID: ");
|
||||
for (int i = 0; i < ESP_UUID_LEN; i++) Serial.printf("%02X", deviceUuid[i]);
|
||||
Serial.println();
|
||||
ESP_LOGD(TAG, "Generated new UUID");
|
||||
}
|
||||
|
||||
if (serverLen == ESP_UUID_LEN) {
|
||||
serverProvisioned = true;
|
||||
Serial.print("Server UUID loaded from NVS: ");
|
||||
for (int i = 0; i < ESP_UUID_LEN; i++) Serial.printf("%02X", serverUuid[i]);
|
||||
Serial.println();
|
||||
ESP_LOGD(TAG, "Server UUID loaded from NVS");
|
||||
} else {
|
||||
serverProvisioned = false;
|
||||
Serial.println("No server UUID — use USB SET_SERVER to provision");
|
||||
ESP_LOGW(TAG, "No server UUID — use USB SET_SERVER to provision");
|
||||
}
|
||||
|
||||
allProvisioned = keyProvisioned && uuidProvisioned && serverProvisioned;
|
||||
|
|
@ -237,10 +234,10 @@ void setup() {
|
|||
pScan->setActiveScan(false);
|
||||
pScan->setMaxResults(0);
|
||||
bleReady = true;
|
||||
Serial.println("BLE: initialized, will scan for SylDa_Server");
|
||||
ESP_LOGI(TAG, "BLE: initialized, will scan for SylDa_Server");
|
||||
}
|
||||
|
||||
Serial.println("Setup done.");
|
||||
ESP_LOGI(TAG, "Setup done.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
|
@ -290,7 +287,7 @@ void loop() {
|
|||
pScan->setActiveScan(false);
|
||||
pScan->setMaxResults(0);
|
||||
bleReady = true;
|
||||
Serial.println("BLE: initialized after provisioning");
|
||||
ESP_LOGI(TAG, "BLE: initialized after key provisioning");
|
||||
}
|
||||
} else {
|
||||
Serial.println("ERROR: invalid key format");
|
||||
|
|
@ -331,7 +328,7 @@ void loop() {
|
|||
pScan->setActiveScan(false);
|
||||
pScan->setMaxResults(0);
|
||||
bleReady = true;
|
||||
Serial.println("BLE: initialized after provisioning");
|
||||
ESP_LOGI(TAG, "BLE: initialized after server provisioning");
|
||||
}
|
||||
} else {
|
||||
Serial.println("ERROR: invalid server UUID format");
|
||||
|
|
@ -363,9 +360,6 @@ void loop() {
|
|||
bool btnActive = (millis() - btnChange > DEBOUNCE_MS) && btnStable;
|
||||
|
||||
static bool prevBtnActive = false;
|
||||
if (btnActive && !prevBtnActive) {
|
||||
Serial.printf("Button: pressed\n");
|
||||
}
|
||||
prevBtnActive = btnActive;
|
||||
|
||||
bool relay1State = !t1Active || btnActive;
|
||||
|
|
@ -415,7 +409,7 @@ void loop() {
|
|||
buildHmacPacket(relay1State ? 0x01 : 0x00, pkt);
|
||||
pCmdChar->writeValue(pkt, HMAC_PKT_SIZE, false);
|
||||
relay1SendState = relay1State;
|
||||
Serial.printf("TX: relay1 %s (seq=%lu)\n", relay1State ? "ON" : "OFF", seq);
|
||||
ESP_LOGD(TAG, "TX: relay1 %s (seq=%lu)", relay1State ? "ON" : "OFF", seq);
|
||||
lastCmd = millis();
|
||||
}
|
||||
if (relay2State != relay2SendState) {
|
||||
|
|
@ -423,7 +417,7 @@ void loop() {
|
|||
buildHmacPacket(relay2State ? 0x02 : 0x03, pkt);
|
||||
pCmdChar->writeValue(pkt, HMAC_PKT_SIZE, false);
|
||||
relay2SendState = relay2State;
|
||||
Serial.printf("TX: relay2 %s (seq=%lu)\n", relay2State ? "ON" : "OFF", seq);
|
||||
ESP_LOGD(TAG, "TX: relay2 %s (seq=%lu)", relay2State ? "ON" : "OFF", seq);
|
||||
lastCmd = millis();
|
||||
}
|
||||
}
|
||||
|
|
@ -444,21 +438,20 @@ void loop() {
|
|||
if (!pScan->isScanning() && scanRunnable) {
|
||||
scanRunnable = false;
|
||||
if (serverFound) {
|
||||
Serial.printf("BLE: found SylDa_Server (%s), connecting...\n",
|
||||
serverAddress.toString().c_str());
|
||||
ESP_LOGI(TAG, "BLE: found SylDa_Server, connecting...");
|
||||
connectToServer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (connected && pClient != nullptr && !pClient->isConnected()) {
|
||||
Serial.println("BLE: lost connection");
|
||||
ESP_LOGW(TAG, "BLE: lost connection");
|
||||
disconnectClient();
|
||||
}
|
||||
|
||||
static uint32_t heartbeat = 0;
|
||||
if (millis() - heartbeat > 60000) {
|
||||
Serial.printf("[heartbeat] t1=%s t2=%s btn=%s r1=%s r2=%s connected=%s\n",
|
||||
ESP_LOGI(TAG, "[heartbeat] t1=%s t2=%s btn=%s r1=%s r2=%s connected=%s",
|
||||
!t1Active ? "YES" : "no",
|
||||
!t2Active ? "YES" : "no",
|
||||
btnActive ? "YES" : "no",
|
||||
|
|
|
|||
Loading…
Reference in a new issue