//
// Can ESP32 read data from Home Assistant? Reading "Inverter Active Power" from Home Assistant connected to Huawei SUN2000-5KTL-L1 inverter
//
// https://youtu.be/p7ZOHrjYuNU
//
#include <ArduinoJson.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YOUR WIFI SSID";
const char* password = "YOUR WIFI PASSWORD";
const char* haApiUrl = "http://XXX.XXX.XXX.XXX:8123/api/states/XXXXXXXXX_SENSOR_NAME_XXXXXXX";
// EXAMPLE
// const char* haApiUrl = "http://192.168.0.103:8123/api/states/sensor.inverter_active_power";
const char* haApiToken = "XXXXX HOME ASSISTANT TOKEN XXXXXXXXXXXXXXXXXXXXX";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectare la reteaua WIFI...");
}
Serial.println("Conectat la Wifi!");
}
void loop() {
HTTPClient http;
http.begin(haApiUrl);
http.addHeader("Authorization", "Bearer " + String(haApiToken));
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
float currentProduction = doc["state"].as<float>();
Serial.print("Productia curenta: ");
Serial.println(currentProduction);
} else {
Serial.println("Eroare la solicitarea HTTP");
}
http.end();
delay(5000);
}