From 5bb87ed4a4c4e95265ff2cad355818ed5acbbf63 Mon Sep 17 00:00:00 2001 From: eddy Date: Sat, 1 Aug 2026 22:54:31 +0200 Subject: [PATCH] fix: remove pullups, invert touch logic for HS-S43A sensors --- src/client.cpp | 8 +++---- src/main.cpp | 60 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 5053873..11acbb5 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -217,8 +217,8 @@ void setup() { allProvisioned = keyProvisioned && uuidProvisioned && serverProvisioned; - pinMode(TOUCH1_PIN, INPUT_PULLUP); - pinMode(TOUCH2_PIN, INPUT_PULLUP); + pinMode(TOUCH1_PIN, INPUT); + pinMode(TOUCH2_PIN, INPUT); pinMode(BUTTON_PIN, INPUT); if (allProvisioned) { @@ -346,8 +346,8 @@ void loop() { static uint32_t t1Change = 0; static uint32_t t2Change = 0; static uint32_t btnChange = 0; - bool t1Raw = (digitalRead(TOUCH1_PIN) == LOW); - bool t2Raw = (digitalRead(TOUCH2_PIN) == LOW); + bool t1Raw = (digitalRead(TOUCH1_PIN) == HIGH); + bool t2Raw = (digitalRead(TOUCH2_PIN) == HIGH); bool btnRaw = (digitalRead(BUTTON_PIN) == LOW); if (t1Raw != t1Stable) { t1Change = millis(); t1Stable = t1Raw; } if (t2Raw != t2Stable) { t2Change = millis(); t2Stable = t2Raw; } diff --git a/src/main.cpp b/src/main.cpp index faa8f80..8fecd9c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,6 +63,9 @@ uint32_t otaWritten = 0; uint8_t otaExpectedSha[32]; mbedtls_sha256_context otaShaCtx; bool otaShaStarted = false; +bool otaWaitingForReboot = false; +bool otaConfirmed = false; +unsigned long otaRebootWaitStart = 0; uint8_t hmacKey[HMAC_KEY_LEN]; uint8_t deviceUuid[ESP_UUID_LEN]; @@ -197,7 +200,10 @@ static void sendOtaNotify(NimBLECharacteristic *otaChar, uint8_t status, uint32_ buf[3] = (seq >> 8) & 0xFF; buf[4] = seq & 0xFF; otaChar->setValue(buf, 5); - otaChar->notify(); + bool ok = otaChar->notify(); + if (!ok) { + Serial.printf("OTA: notify() returned false for status=0x%02X seq=%lu\n", status, seq); + } } static void abortOta(NimBLECharacteristic *otaChar, const char *reason) { @@ -216,6 +222,13 @@ class OtaCharacteristicCallbacks : public NimBLECharacteristicCallbacks { const uint8_t *data = (const uint8_t *)value.data(); size_t len = value.length(); + if (otaWaitingForReboot && len == 1 && data[0] == 0xFF) { + otaWaitingForReboot = false; + otaConfirmed = true; + Serial.println("OTA: reboot confirmation received"); + return; + } + if (len == 5 && data[0] == OTA_CMD_START) { if (otaInProgress) { esp_ota_end(otaHandle); @@ -271,19 +284,13 @@ class OtaCharacteristicCallbacks : public NimBLECharacteristicCallbacks { sendOtaNotify(pChar, OTA_STATUS_ERROR, 0); return; } - err = esp_ota_set_boot_partition(otaPartition); - if (err != ESP_OK) { - Serial.printf("OTA: set boot partition failed (%d)\n", err); - otaReset(); - sendOtaNotify(pChar, OTA_STATUS_ERROR, 0); - return; - } leds[0] = CRGB::Green; FastLED.show(); + otaInProgress = false; + otaWaitingForReboot = true; + otaRebootWaitStart = millis(); sendOtaNotify(pChar, OTA_STATUS_COMPLETE, 0); - Serial.println("OTA: SHA256 OK, rebooting..."); - delay(500); - esp_restart(); + Serial.println("OTA: SHA256 OK, waiting for reboot confirmation..."); return; } @@ -294,7 +301,10 @@ class OtaCharacteristicCallbacks : public NimBLECharacteristicCallbacks { ((uint32_t)data[2] << 8) | (uint32_t)data[3]; size_t dataLen = len - 4; mbedtls_sha256_update_ret(&otaShaCtx, data + 4, dataLen); + unsigned long t0 = micros(); esp_err_t err = esp_ota_write(otaHandle, data + 4, dataLen); + unsigned long elapsed = micros() - t0; + Serial.printf("OTA: chunk %lu write %d bytes in %lu us\n", seq, dataLen, elapsed); if (err != ESP_OK) { Serial.printf("OTA: write err at seq %lu (%d)\n", seq, err); sendOtaNotify(pChar, OTA_STATUS_NAK, seq); @@ -387,8 +397,8 @@ void setup() { Serial.println(); } - pinMode(TOUCH1_PIN, INPUT_PULLUP); - pinMode(TOUCH2_PIN, INPUT_PULLUP); + pinMode(TOUCH1_PIN, INPUT); + pinMode(TOUCH2_PIN, INPUT); pinMode(BUTTON_PIN, INPUT); pinMode(RELAY1_PIN, OUTPUT); pinMode(RELAY2_PIN, OUTPUT); @@ -464,6 +474,26 @@ void setup() { } void loop() { + if (otaWaitingForReboot) { + if (millis() - otaRebootWaitStart > 10000) { + Serial.println("OTA: no confirmation after 10s, rebooting anyway"); + otaWaitingForReboot = false; + otaConfirmed = true; + } + delay(10); + return; + } + if (otaConfirmed) { + otaConfirmed = false; + esp_err_t err = esp_ota_set_boot_partition(otaPartition); + if (err != ESP_OK) { + Serial.printf("OTA: set boot partition failed (%d)\n", err); + } + delay(100); + esp_restart(); + return; + } + if (!otaInProgress && Serial.available()) { static String serialBuf; char c = Serial.read(); @@ -528,8 +558,8 @@ void loop() { static uint32_t t2Change = 0; static uint32_t btnChange = 0; #define DEBOUNCE_MS 50 - bool t1Raw = (digitalRead(TOUCH1_PIN) == LOW); - bool t2Raw = (digitalRead(TOUCH2_PIN) == LOW); + bool t1Raw = (digitalRead(TOUCH1_PIN) == HIGH); + bool t2Raw = (digitalRead(TOUCH2_PIN) == HIGH); bool btnRaw = (digitalRead(BUTTON_PIN) == LOW); if (t1Raw != t1Stable) { t1Change = millis(); t1Stable = t1Raw; } if (t2Raw != t2Stable) { t2Change = millis(); t2Stable = t2Raw; }