#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include "WiFi.h"
#include "ArduinoJson.h"
#include "HTTPClient.h"
// Fronius Inverter
const char *HOST = "192.168.86.23";
const char *SSID = "XXXXXX";
const char *PASSWORD = "XXXXXX";
const long kMaxPower = 3300; // maximum watt for the bar graph
const int16_t displayWidth = 128;
const int16_t displayHeight = 64;
// Initialize the OLED display using Wire library
SSD1306 display(0x3c, 5, 4);
void connectToWiFi(const char * ssid, const char * pwd);
void setup() {
// put your setup code here, to run once:
//Serial.begin(115200);
display.init();
// display.flipScreenVertically();
display.setContrast(255);
display.setFont(ArialMT_Plain_24);
display.setTextAlignment(TEXT_ALIGN_CENTER);
connectToWiFi(SSID, PASSWORD);
}
void connectToWiFi(const char * ssid, const char * pwd)
{
WiFi.begin(ssid, pwd);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
//Serial.print(".");
}
//Serial.print("Wifi connected");
}
void displayThis(String text, long power)
{
display.clear();
display.drawString(displayWidth / 2, 16, text); // x,y
const int16_t barHeight = 8;
display.drawRect(0, displayHeight - barHeight, displayWidth, barHeight);
long barWidth = power * displayWidth / kMaxPower;
display.fillRect(0, displayHeight - barHeight, barWidth, barHeight);
display.display();
}
void loop() {
if((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
String url = "http://" + String(HOST) + "/solar_api/v1/GetInverterRealtimeData.cgi?Scope=System";
http.begin(url);
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// Allocate the JSON document
// Use arduinojson.org/v6/assistant to compute the capacity.
//const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonDocument doc(900);
// Parse JSON object
DeserializationError error = deserializeJson(doc, payload);
if (error) {
//Serial.print(F("deserializeJson() failed: "));
//Serial.println(error.c_str());
return;
}
// Extract current power generated from the Fronius inverter
long generatedPower = doc["Body"]["Data"]["PAC"]["Values"]["1"];
String displayedPower = String(generatedPower) + " W";
displayThis(displayedPower, generatedPower);
}
} else {
displayThis("HTTP Error", 0);
//Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
displayThis("Wifi Error", 0);
connectToWiFi(SSID, PASSWORD);
}
delay(5000);
}