extractTasmotaValues bug
This commit is contained in:
38
src/main.cpp
38
src/main.cpp
@@ -1,7 +1,9 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266HTTPClient.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)
|
||||
@@ -114,7 +116,6 @@ void extractTasmotaValues(const String& url, String values[6], int decimalPlaces
|
||||
String response = http.getString();
|
||||
http.end();
|
||||
|
||||
// Array mit den Schlüsseln der gewünschten Werte
|
||||
const String keys[6] = {
|
||||
tasmota_plug_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
|
||||
};
|
||||
|
||||
// Werte extrahieren
|
||||
for (int i = 0; i < 6; i++) {
|
||||
values[i] = "?"; // Standardwert, falls Key nicht gefunden wird
|
||||
values[i] = "?";
|
||||
int keyIndex = response.indexOf("\"" + keys[i] + "\":");
|
||||
if (keyIndex != -1) {
|
||||
int valueStart = keyIndex + keys[i].length() + 3; // Position nach dem ":"
|
||||
int valueEnd = response.indexOf(",", valueStart); // Ende der Zahl oder des Wertes
|
||||
int valueStart = keyIndex + keys[i].length() + 3;
|
||||
int valueEnd = response.indexOf(",", valueStart);
|
||||
if (valueEnd == -1) {
|
||||
valueEnd = response.indexOf("}", valueStart); // Falls am Ende der JSON-Objekte
|
||||
valueEnd = response.indexOf("}", valueStart);
|
||||
}
|
||||
if (valueEnd != -1) {
|
||||
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) {
|
||||
int dotIndex = values[i].indexOf('.');
|
||||
if (dotIndex != -1) {
|
||||
// Berechnung des Truncate-Index
|
||||
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 {
|
||||
Serial.println("HTTP Fehler: " + String(httpResponseCode));
|
||||
}
|
||||
@@ -170,6 +169,9 @@ void extractTasmotaValues(const String& url, String values[6], int decimalPlaces
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void connectToWiFi() {
|
||||
Serial.println("WLAN Verbindungsaufbau zu: " + String(ssidList[current_ap]));
|
||||
Serial.println("Passwort: " + String(passwordList[current_ap]));
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <Arduino.h>
|
||||
#ifndef SETTINGS_H
|
||||
#define SETTINGS_H
|
||||
|
||||
|
||||
Reference in New Issue
Block a user