fix: client — spec compliance, memory leak, LED order
This commit is contained in:
parent
97c79b9f4c
commit
bce5fc4107
1 changed files with 28 additions and 22 deletions
|
|
@ -3,6 +3,7 @@
|
|||
#include <FastLED.h>
|
||||
|
||||
#include <mbedtls/md.h>
|
||||
#include <mbedtls/sha256.h>
|
||||
#include <Preferences.h>
|
||||
|
||||
#include "version.h"
|
||||
|
|
@ -74,6 +75,8 @@ class MyClientCallbacks : public NimBLEClientCallbacks {
|
|||
}
|
||||
};
|
||||
|
||||
static MyClientCallbacks clientCallbacks;
|
||||
|
||||
static bool isHexKey(const String &hex) {
|
||||
for (int i = 0; i < hex.length(); i++) {
|
||||
char c = hex.charAt(i);
|
||||
|
|
@ -117,7 +120,7 @@ static void disconnectClient() {
|
|||
|
||||
static bool connectToServer() {
|
||||
pClient = NimBLEDevice::createClient();
|
||||
pClient->setClientCallbacks(new MyClientCallbacks());
|
||||
pClient->setClientCallbacks(&clientCallbacks);
|
||||
|
||||
if (!pClient->connect(serverAddress)) {
|
||||
Serial.println("BLE: connect failed");
|
||||
|
|
@ -167,7 +170,7 @@ void setup() {
|
|||
delay(2000);
|
||||
Serial.println("=== SylDa Client ===");
|
||||
|
||||
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
|
||||
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
|
||||
leds[0] = CRGB::Red;
|
||||
FastLED.show();
|
||||
|
||||
|
|
@ -266,7 +269,7 @@ void loop() {
|
|||
preferences.putBytes(NVS_KEY_NAME, hmacKey, HMAC_KEY_LEN);
|
||||
preferences.end();
|
||||
keyProvisioned = true;
|
||||
allProvisioned = keyProvisioned && serverProvisioned;
|
||||
allProvisioned = keyProvisioned && uuidProvisioned && serverProvisioned;
|
||||
Serial.println("OK");
|
||||
Serial.println("HMAC key stored in NVS");
|
||||
if (allProvisioned && !bleReady) {
|
||||
|
|
@ -291,7 +294,7 @@ void loop() {
|
|||
preferences.putBytes(NVS_UUID_NAME, deviceUuid, ESP_UUID_LEN);
|
||||
preferences.end();
|
||||
uuidProvisioned = true;
|
||||
allProvisioned = keyProvisioned && serverProvisioned;
|
||||
allProvisioned = keyProvisioned && uuidProvisioned && serverProvisioned;
|
||||
Serial.println("OK");
|
||||
Serial.println("UUID stored in NVS");
|
||||
} else {
|
||||
|
|
@ -306,7 +309,7 @@ void loop() {
|
|||
preferences.putBytes(NVS_SERVER_NAME, serverUuid, ESP_UUID_LEN);
|
||||
preferences.end();
|
||||
serverProvisioned = true;
|
||||
allProvisioned = keyProvisioned && serverProvisioned;
|
||||
allProvisioned = keyProvisioned && uuidProvisioned && serverProvisioned;
|
||||
Serial.println("OK");
|
||||
Serial.println("Server UUID stored in NVS");
|
||||
if (allProvisioned && !bleReady) {
|
||||
|
|
@ -349,14 +352,12 @@ void loop() {
|
|||
bool btnActive = (millis() - btnChange > DEBOUNCE_MS) && btnStable;
|
||||
|
||||
static bool prevBtnActive = false;
|
||||
static bool buttonToggle = false;
|
||||
if (btnActive && !prevBtnActive) {
|
||||
buttonToggle = !buttonToggle;
|
||||
Serial.printf("Button: toggle -> %s\n", buttonToggle ? "ON" : "OFF");
|
||||
Serial.printf("Button: pressed\n");
|
||||
}
|
||||
prevBtnActive = btnActive;
|
||||
|
||||
bool relay1State = t1Active || buttonToggle;
|
||||
bool relay1State = t1Active || btnActive;
|
||||
bool relay2State = t2Active;
|
||||
|
||||
if (relay1State || relay2State) {
|
||||
|
|
@ -369,19 +370,24 @@ void loop() {
|
|||
FastLED.show();
|
||||
|
||||
if (connected && pCmdChar != nullptr) {
|
||||
if (relay1State != relay1SendState) {
|
||||
uint8_t pkt[HMAC_PKT_SIZE];
|
||||
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);
|
||||
}
|
||||
if (relay2State != relay2SendState) {
|
||||
uint8_t pkt[HMAC_PKT_SIZE];
|
||||
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);
|
||||
static uint32_t lastCmd = 0;
|
||||
if (millis() - lastCmd > 200) {
|
||||
if (relay1State != relay1SendState) {
|
||||
uint8_t pkt[HMAC_PKT_SIZE];
|
||||
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);
|
||||
lastCmd = millis();
|
||||
}
|
||||
if (relay2State != relay2SendState) {
|
||||
uint8_t pkt[HMAC_PKT_SIZE];
|
||||
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);
|
||||
lastCmd = millis();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue