extractTasmotaValues bug

This commit is contained in:
2024-12-28 17:52:36 +01:00
parent 2e66dfd940
commit f55d7c1eff
2 changed files with 20 additions and 17 deletions

View File

@@ -1,7 +1,9 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#include <LiquidCrystal_I2C.h> #include <LiquidCrystal_I2C.h>
#include <settings.h> //#include <settings.h>
#include <settings-my-ignore.h>
#define BUTTON_PIN D3 // GPIO-Pin verbunden mit Taster (D3 entspricht GPIO0) #define BUTTON_PIN D3 // GPIO-Pin verbunden mit Taster (D3 entspricht GPIO0)
@@ -114,7 +116,6 @@ void extractTasmotaValues(const String& url, String values[6], int decimalPlaces
String response = http.getString(); String response = http.getString();
http.end(); http.end();
// Array mit den Schlüsseln der gewünschten Werte
const String keys[6] = { const String keys[6] = {
tasmota_plug_json_prefix_power, tasmota_plug_json_prefix_power,
tasmota_smr_json_prefix_power, tasmota_smr_json_prefix_power,
@@ -124,39 +125,37 @@ void extractTasmotaValues(const String& url, String values[6], int decimalPlaces
tasmota_smr_json_prefix_2_8_0 tasmota_smr_json_prefix_2_8_0
}; };
// Werte extrahieren
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
values[i] = "?"; // Standardwert, falls Key nicht gefunden wird values[i] = "?";
int keyIndex = response.indexOf("\"" + keys[i] + "\":"); int keyIndex = response.indexOf("\"" + keys[i] + "\":");
if (keyIndex != -1) { if (keyIndex != -1) {
int valueStart = keyIndex + keys[i].length() + 3; // Position nach dem ":" int valueStart = keyIndex + keys[i].length() + 3;
int valueEnd = response.indexOf(",", valueStart); // Ende der Zahl oder des Wertes int valueEnd = response.indexOf(",", valueStart);
if (valueEnd == -1) { if (valueEnd == -1) {
valueEnd = response.indexOf("}", valueStart); // Falls am Ende der JSON-Objekte valueEnd = response.indexOf("}", valueStart);
} }
if (valueEnd != -1) { if (valueEnd != -1) {
values[i] = response.substring(valueStart, valueEnd); values[i] = response.substring(valueStart, valueEnd);
values[i].trim(); // Entfernt überflüssige Leerzeichen values[i].trim();
// Anzahl der Nachkommastellen begrenzen // Begrenzung der Nachkommastellen
if (decimalPlaces >= 0) { if (decimalPlaces >= 0) {
int dotIndex = values[i].indexOf('.'); int dotIndex = values[i].indexOf('.');
if (dotIndex != -1) { if (dotIndex != -1) {
// Berechnung des Truncate-Index
int truncateIndex = dotIndex + 1 + decimalPlaces; int truncateIndex = dotIndex + 1 + decimalPlaces;
if (truncateIndex < values[i].length()) {
values[i] = values[i].substring(0, truncateIndex); // Sicherstellen, dass truncateIndex nicht negativ ist und im gültigen Bereich liegt
if (truncateIndex >= 0 &&
static_cast<size_t>(truncateIndex) < values[i].length()) {
values[i] = values[i].substring(0, static_cast<size_t>(truncateIndex));
} }
} }
} }
} }
} }
} }
// for (int i = 0; i < 6; i++) {
// Serial.println(String(values[i]));
// }
} else { } else {
Serial.println("HTTP Fehler: " + String(httpResponseCode)); Serial.println("HTTP Fehler: " + String(httpResponseCode));
} }
@@ -170,6 +169,9 @@ void extractTasmotaValues(const String& url, String values[6], int decimalPlaces
void connectToWiFi() { void connectToWiFi() {
Serial.println("WLAN Verbindungsaufbau zu: " + String(ssidList[current_ap])); Serial.println("WLAN Verbindungsaufbau zu: " + String(ssidList[current_ap]));
Serial.println("Passwort: " + String(passwordList[current_ap])); Serial.println("Passwort: " + String(passwordList[current_ap]));

View File

@@ -1,3 +1,4 @@
#include <Arduino.h>
#ifndef SETTINGS_H #ifndef SETTINGS_H
#define SETTINGS_H #define SETTINGS_H