diff --git a/README.de.md b/README.de.md index 5218575..d074a2c 100644 --- a/README.de.md +++ b/README.de.md @@ -111,17 +111,21 @@ Wenn Sie einen Hersteller-NFC-Tag zum ersten Mal scannen: ### Pin-Konfiguration -| Komponente | ESP32 Pin | -|-------------------|-----------| -| HX711 DOUT | 16 | -| HX711 SCK | 17 | -| OLED SDA | 21 | -| OLED SCL | 22 | -| PN532 IRQ | 32 | -| PN532 RESET | 33 | -| PN532 SDA | 21 | -| PN532 SCL | 22 | -| TTP223 I/O | 25 | +| Komponente | ESP32 Pin | ESP32-S3 Pin | +|-------------------|-----------|--------------| +| HX711 DOUT | 16 | 5 | +| HX711 SCK | 17 | 6 | +| OLED SDA | 21 | 8 | +| OLED SCL | 22 | 9 | +| PN532 IRQ | 32 | 15 | +| PN532 RESET | 33 | 16 | +| PN532 SDA | 21 | 8 | +| PN532 SCL | 22 | 9 | +| TTP223 I/O | 25 | 7 | + +Die I2C-Pins sind die Arduino-Standardwerte des jeweiligen Chips und werden im Code nicht explizit gesetzt. + +**!! ESP32-S3: GPIO19/GPIO20 nicht für den HX711 verwenden.** Das sind die nativen USB-Datenleitungen (D-/D+), die auf dem DevKitC-1 an der "USB"-Buchse liegen. Der HX711 wird dort nur unzuverlässig erkannt. **!! Stellen Sie sicher, dass die DIP-Schalter am PN532 auf I2C eingestellt sind.** **Verwenden Sie den 3V-Pin des ESP für den Touch-Sensor.** diff --git a/README.md b/README.md index 134f386..da5ed9f 100644 --- a/README.md +++ b/README.md @@ -111,17 +111,21 @@ When you scan a manufacturer NFC tag for the first time: ### Pin Configuration -| Component | ESP32 Pin | -|-------------------|-----------| -| HX711 DOUT | 16 | -| HX711 SCK | 17 | -| OLED SDA | 21 | -| OLED SCL | 22 | -| PN532 IRQ | 32 | -| PN532 RESET | 33 | -| PN532 SDA | 21 | -| PN532 SCL | 22 | -| TTP223 I/O | 25 | +| Component | ESP32 Pin | ESP32-S3 Pin | +|-------------------|-----------|--------------| +| HX711 DOUT | 16 | 5 | +| HX711 SCK | 17 | 6 | +| OLED SDA | 21 | 8 | +| OLED SCL | 22 | 9 | +| PN532 IRQ | 32 | 15 | +| PN532 RESET | 33 | 16 | +| PN532 SDA | 21 | 8 | +| PN532 SCL | 22 | 9 | +| TTP223 I/O | 25 | 7 | + +The I2C pins are the Arduino defaults for each chip and are not set explicitly in the code. + +**!! ESP32-S3: do not use GPIO19/GPIO20 for the HX711.** These are the native USB data lines (D-/D+) and are wired to the "USB" connector on the DevKitC-1, which makes the HX711 detection unreliable. **!! Make sure that the DIP switches on the PN532 are set to I2C** **Use the 3V pin from the ESP for the touch sensor** diff --git a/data/setup.html b/data/setup.html index e863280..b9bcaf3 100644 --- a/data/setup.html +++ b/data/setup.html @@ -87,6 +87,24 @@
+ +
+

WiFi Settings

+ +
+ + +

Only lower this if your router keeps dropping the device, or if it sits right next to the access point. Applies immediately, no restart needed.

+
+
+
@@ -160,6 +178,45 @@ }); }); + // WiFi transmit power setting + fetch('/api/wifi-power') + .then(response => response.json()) + .then(data => { + const sel = document.getElementById('txPowerSelect'); + const val = String(data.txPower); + let matched = false; + for (let i = 0; i < sel.options.length; i++) { + if (sel.options[i].value === val) { sel.value = val; matched = true; break; } + } + if (!matched) sel.value = '195'; + }) + .catch(error => console.error('Error fetching wifi power:', error)); + + document.getElementById('txPowerSelect').addEventListener('change', function() { + const msg = document.getElementById('txPowerStatusMessage'); + fetch('/api/wifi-power', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ txPower: parseInt(this.value) }) + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + msg.textContent = 'Saved.'; + msg.style.color = 'var(--success-text)'; + setTimeout(() => { msg.textContent = ''; }, 2000); + } else { + msg.textContent = 'Error saving setting.'; + msg.style.color = 'var(--error-text)'; + } + }) + .catch(error => { + msg.textContent = 'Error saving setting.'; + msg.style.color = 'var(--error-text)'; + console.error('Error saving wifi power:', error); + }); + }); + document.getElementById('registerBtn').addEventListener('click', () => { const url = document.getElementById('filamanUrl').value; const code = document.getElementById('deviceCode').value; diff --git a/src/config.cpp b/src/config.cpp index 959c312..2d69fdd 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -17,6 +17,9 @@ const uint8_t PN532_RESET = 33; // ***** HX711 (Waage) // HX711 circuit wiring +// ACHTUNG ESP32-S3: GPIO19/20 sind die nativen USB-Leitungen (D-/D+) und liegen auf +// dem DevKitC-1 an der "USB"-Buchse. Der HX711 wird dort unzuverlaessig erkannt. +// Pins hier nur zusammen mit den Tabellen in README.md / README.de.md aendern. #if CONFIG_IDF_TARGET_ESP32S3 const uint8_t LOADCELL_DOUT_PIN = 5; const uint8_t LOADCELL_SCK_PIN = 6; @@ -60,6 +63,12 @@ bool filamanRegistered = false; uint16_t oledSleepTimeout = 60; // Default 60 seconds (0 = disabled) // ***** Display Sleep +// ***** WiFi +// Sendeleistung in 0.1 dBm. Default 19.5dBm = Maximum; nur senken, wenn der AP +// das Geraet rauswirft (reason=2/4) oder es direkt neben dem AP steht. +int16_t wifiTxPowerDeciDbm = 195; +// ***** WiFi + // ***** Task Prios uint8_t rfidTaskCore = 1; uint8_t rfidTaskPrio = 1; diff --git a/src/config.h b/src/config.h index 847579a..83eec05 100644 --- a/src/config.h +++ b/src/config.h @@ -15,6 +15,7 @@ #define NVS_NAMESPACE_SETTINGS "settings" #define NVS_KEY_LANGUAGE "language" #define NVS_KEY_OLED_SLEEP "oled_sleep" +#define NVS_KEY_WIFI_TXPOWER "wifi_txpwr" #define SCALE_DEFAULT_CALIBRATION_VALUE 430.0f; #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) @@ -74,4 +75,7 @@ extern uint8_t scaleTaskPrio; extern uint16_t defaultScaleCalibrationValue; extern uint16_t oledSleepTimeout; + +// WLAN-Sendeleistung in 0.1 dBm (195 = 19.5dBm). Siehe txPowerFromDeciDbm() in wlan.cpp. +extern int16_t wifiTxPowerDeciDbm; #endif diff --git a/src/website.cpp b/src/website.cpp index 67234ff..6c34589 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -12,6 +12,7 @@ #include "config.h" #include "debug.h" #include "lang.h" +#include "wlan.h" #ifndef VERSION #define VERSION "1.2.0" @@ -295,6 +296,35 @@ void setupWebserver(AsyncWebServer &server) { request->send(200, "application/json", "{\"success\": true}"); }); + // WiFi TX Power API (Wert in 0.1 dBm, z.B. 195 = 19.5dBm) + server.on("/api/wifi-power", HTTP_GET, [](AsyncWebServerRequest *request){ + JsonDocument doc; + doc["txPower"] = wifiTxPowerDeciDbm; + String response; + serializeJson(doc, response); + request->send(200, "application/json", response); + }); + + server.on("/api/wifi-power", HTTP_POST, [](AsyncWebServerRequest *request){}, NULL, [](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){ + JsonDocument doc; + DeserializationError error = deserializeJson(doc, (const uint8_t*)data, len); + if (error) { + request->send(400, "application/json", "{\"success\": false, \"error\": \"Invalid JSON\"}"); + return; + } + if (!doc["txPower"].is()) { + request->send(400, "application/json", "{\"success\": false, \"error\": \"Missing txPower\"}"); + return; + } + int16_t txPower = (int16_t)doc["txPower"].as(); + if (!isValidTxPowerDeciDbm(txPower)) { + request->send(400, "application/json", "{\"success\": false, \"error\": \"Invalid txPower\"}"); + return; + } + saveWifiTxPower(txPower); + request->send(200, "application/json", "{\"success\": true}"); + }); + server.on("/api/v1/rfid/scan-request", HTTP_POST, [](AsyncWebServerRequest *request){}, NULL, [](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){ setScanRequest(true); request->send(200, "application/json", "{\"success\": true}"); diff --git a/src/wlan.cpp b/src/wlan.cpp index ba53956..5e06129 100644 --- a/src/wlan.cpp +++ b/src/wlan.cpp @@ -7,11 +7,53 @@ #include "display.h" #include "config.h" #include "lang.h" +#include WiFiManager wm; bool wm_nonblocking = false; uint8_t wifiErrorCounter = 0; +// Erlaubte Stufen in 0.1 dBm. Der ESP32-Treiber kennt nur diese festen Werte; +// alles andere fällt auf das Maximum (19.5dBm) zurück. +wifi_power_t txPowerFromDeciDbm(int16_t deciDbm) { + switch (deciDbm) { + case 85: return WIFI_POWER_8_5dBm; + case 110: return WIFI_POWER_11dBm; + case 130: return WIFI_POWER_13dBm; + case 150: return WIFI_POWER_15dBm; + case 170: return WIFI_POWER_17dBm; + case 195: return WIFI_POWER_19_5dBm; + default: return WIFI_POWER_19_5dBm; + } +} + +bool isValidTxPowerDeciDbm(int16_t deciDbm) { + return deciDbm == 85 || deciDbm == 110 || deciDbm == 130 || + deciDbm == 150 || deciDbm == 170 || deciDbm == 195; +} + +void loadWifiTxPower() { + Preferences preferences; + preferences.begin(NVS_NAMESPACE_SETTINGS, true); + int16_t stored = preferences.getShort(NVS_KEY_WIFI_TXPOWER, 195); + preferences.end(); + wifiTxPowerDeciDbm = isValidTxPowerDeciDbm(stored) ? stored : 195; + Serial.print("WiFi TX power loaded: "); + Serial.print(wifiTxPowerDeciDbm / 10.0, 1); + Serial.println(" dBm"); +} + +void saveWifiTxPower(int16_t deciDbm) { + if (!isValidTxPowerDeciDbm(deciDbm)) return; + wifiTxPowerDeciDbm = deciDbm; + Preferences preferences; + preferences.begin(NVS_NAMESPACE_SETTINGS, false); + preferences.putShort(NVS_KEY_WIFI_TXPOWER, deciDbm); + preferences.end(); + // Sofort anwenden, ein Neustart ist nicht noetig. + WiFi.setTxPower(txPowerFromDeciDbm(deciDbm)); +} + void wifiSettings() { // Standard WiFi-Einstellungen für höchste Stabilität mit ESPAsyncWebServer WiFi.mode(WIFI_STA); @@ -29,10 +71,13 @@ void wifiSettings() { WiFi.setSleep(false); esp_wifi_set_ps(WIFI_PS_NONE); - // Maximale Sendeleistung. 17dBm war ein Kompromiss gegen Hitzeprobleme, - // führte bei marginalem Empfang aber zu AP-seitigen Auth-/Assoc-Kicks - // (reason=2/4/9/202). Bei Verdacht auf Überhitzung wieder auf 17dBm zurück. Bei Problemen mit dem ESP32-S3 können 8.5dBm getestet werden. - WiFi.setTxPower(WIFI_POWER_19_5dBm); + // Sendeleistung, einstellbar unter Setup (Default 19.5dBm = Maximum). + // 17dBm war früher ein Kompromiss gegen Hitzeprobleme, führte bei marginalem + // Empfang aber zu AP-seitigen Auth-/Assoc-Kicks (reason=2/4/9/202). Umgekehrt + // kann volle Leistung direkt neben dem AP dessen Empfaenger uebersteuern - + // dann hilft ein niedrigerer Wert (beim ESP32-S3 z.B. 8.5dBm). + loadWifiTxPower(); + WiFi.setTxPower(txPowerFromDeciDbm(wifiTxPowerDeciDbm)); // Hinweis: Kein esp_wifi_set_rssi_threshold() mehr. // Der vorherige Wert (-80 dBm) löste in Single-AP-Heimnetzen häufige @@ -73,6 +118,8 @@ void onWiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { // gelegentlich zurückgesetzt). WiFi.setSleep(false); esp_wifi_set_ps(WIFI_PS_NONE); + // Gleiches gilt für die Sendeleistung: nach einem Reconnect erneut setzen. + WiFi.setTxPower(txPowerFromDeciDbm(wifiTxPowerDeciDbm)); oledShowTopRow(); break; default: diff --git a/src/wlan.h b/src/wlan.h index 7c22caf..be6cd39 100644 --- a/src/wlan.h +++ b/src/wlan.h @@ -6,4 +6,9 @@ void initWiFi(); void checkWiFiConnection(); +// Sendeleistung in 0.1 dBm (85/110/130/150/170/195). Ungueltige Werte werden ignoriert. +bool isValidTxPowerDeciDbm(int16_t deciDbm); +void loadWifiTxPower(); +void saveWifiTxPower(int16_t deciDbm); + #endif \ No newline at end of file