2022-07-15 18:05:58 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022 Thomas Basler and others
|
|
|
|
|
*/
|
2022-04-10 16:57:24 +02:00
|
|
|
#include "Configuration.h"
|
2022-05-30 21:57:21 +02:00
|
|
|
#include "Hoymiles.h"
|
2022-07-18 22:28:03 +02:00
|
|
|
#include "MqttHassPublishing.h"
|
2022-06-15 21:37:25 +02:00
|
|
|
#include "MqttPublishing.h"
|
2022-04-18 15:19:26 +02:00
|
|
|
#include "MqttSettings.h"
|
2022-04-17 23:13:36 +02:00
|
|
|
#include "NtpSettings.h"
|
2022-04-11 19:44:51 +02:00
|
|
|
#include "WebApi.h"
|
2022-07-13 21:30:53 +02:00
|
|
|
#include "NetworkSettings.h"
|
2022-04-09 13:08:51 +02:00
|
|
|
#include "defaults.h"
|
2022-04-09 11:05:14 +02:00
|
|
|
#include <Arduino.h>
|
2022-04-18 03:38:07 +02:00
|
|
|
#include <LittleFS.h>
|
2022-04-09 11:05:14 +02:00
|
|
|
|
2022-04-09 11:05:58 +02:00
|
|
|
void setup()
|
|
|
|
|
{
|
2022-04-09 13:08:51 +02:00
|
|
|
// Initialize serial output
|
|
|
|
|
Serial.begin(SERIAL_BAUDRATE);
|
|
|
|
|
while (!Serial)
|
|
|
|
|
yield();
|
|
|
|
|
Serial.println();
|
|
|
|
|
Serial.println(F("Starting OpenDTU"));
|
|
|
|
|
|
|
|
|
|
// Initialize file system
|
|
|
|
|
Serial.print(F("Initialize FS... "));
|
2022-06-15 22:07:30 +02:00
|
|
|
if (!LittleFS.begin(false)) { // Do not format if mount failed
|
|
|
|
|
Serial.print(F("failed... trying to format..."));
|
|
|
|
|
if (!LittleFS.begin(true)) {
|
|
|
|
|
Serial.print("success");
|
|
|
|
|
} else {
|
|
|
|
|
Serial.print("failed");
|
|
|
|
|
}
|
2022-04-09 13:08:51 +02:00
|
|
|
} else {
|
|
|
|
|
Serial.println(F("done"));
|
|
|
|
|
}
|
2022-04-10 16:57:24 +02:00
|
|
|
|
|
|
|
|
// Read configuration values
|
|
|
|
|
Serial.print(F("Reading configuration... "));
|
|
|
|
|
if (!Configuration.read()) {
|
|
|
|
|
Serial.print(F("initializing... "));
|
|
|
|
|
Configuration.init();
|
|
|
|
|
if (Configuration.write()) {
|
|
|
|
|
Serial.print(F("written... "));
|
|
|
|
|
} else {
|
|
|
|
|
Serial.print(F("failed... "));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (Configuration.get().Cfg_Version != CONFIG_VERSION) {
|
|
|
|
|
Serial.print(F("migrated... "));
|
|
|
|
|
Configuration.migrate();
|
|
|
|
|
}
|
|
|
|
|
Serial.println(F("done"));
|
|
|
|
|
|
|
|
|
|
// Initialize WiFi
|
|
|
|
|
Serial.print(F("Initialize WiFi... "));
|
2022-07-13 21:30:53 +02:00
|
|
|
NetworkSettings.init();
|
2022-04-10 17:37:54 +02:00
|
|
|
Serial.println(F("done"));
|
2022-07-13 21:30:53 +02:00
|
|
|
NetworkSettings.applyConfig();
|
2022-04-11 19:44:51 +02:00
|
|
|
|
2022-04-17 23:13:36 +02:00
|
|
|
// Initialize NTP
|
|
|
|
|
Serial.print(F("Initialize NTP... "));
|
|
|
|
|
NtpSettings.init();
|
|
|
|
|
Serial.println(F("done"));
|
|
|
|
|
|
2022-04-18 15:19:26 +02:00
|
|
|
// Initialize MqTT
|
|
|
|
|
Serial.print(F("Initialize MqTT... "));
|
|
|
|
|
MqttSettings.init();
|
2022-06-15 21:37:25 +02:00
|
|
|
MqttPublishing.init();
|
2022-07-18 22:28:03 +02:00
|
|
|
MqttHassPublishing.init();
|
2022-04-18 15:19:26 +02:00
|
|
|
Serial.println(F("done"));
|
|
|
|
|
|
2022-04-11 19:44:51 +02:00
|
|
|
// Initialize WebApi
|
|
|
|
|
Serial.print(F("Initialize WebApi... "));
|
|
|
|
|
WebApi.init();
|
|
|
|
|
Serial.println(F("done"));
|
2022-05-30 21:57:21 +02:00
|
|
|
|
|
|
|
|
// Initialize inverter communication
|
|
|
|
|
Serial.print(F("Initialize Hoymiles interface... "));
|
|
|
|
|
CONFIG_T& config = Configuration.get();
|
|
|
|
|
Hoymiles.init();
|
|
|
|
|
Hoymiles.getRadio()->setPALevel((rf24_pa_dbm_e)config.Dtu_PaLevel);
|
|
|
|
|
Hoymiles.getRadio()->setDtuSerial(config.Dtu_Serial);
|
|
|
|
|
Hoymiles.setPollInterval(config.Dtu_PollInterval);
|
|
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < INV_MAX_COUNT; i++) {
|
|
|
|
|
if (config.Inverter[i].Serial > 0) {
|
2022-06-22 21:12:45 +02:00
|
|
|
auto inv = Hoymiles.addInverter(
|
2022-05-30 21:57:21 +02:00
|
|
|
config.Inverter[i].Name,
|
|
|
|
|
config.Inverter[i].Serial);
|
2022-06-22 21:12:45 +02:00
|
|
|
|
|
|
|
|
for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) {
|
2022-07-12 18:27:56 +02:00
|
|
|
inv->Statistics()->setChannelMaxPower(c, config.Inverter[i].MaxChannelPower[c]);
|
2022-06-22 21:12:45 +02:00
|
|
|
}
|
2022-05-30 21:57:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Serial.println(F("done"));
|
2022-04-09 11:05:14 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-09 11:05:58 +02:00
|
|
|
void loop()
|
|
|
|
|
{
|
2022-07-13 21:30:53 +02:00
|
|
|
NetworkSettings.loop();
|
2022-06-15 21:37:25 +02:00
|
|
|
yield();
|
2022-05-30 21:57:21 +02:00
|
|
|
Hoymiles.loop();
|
2022-06-15 21:37:25 +02:00
|
|
|
yield();
|
|
|
|
|
MqttPublishing.loop();
|
|
|
|
|
yield();
|
2022-07-18 22:28:03 +02:00
|
|
|
MqttHassPublishing.loop();
|
|
|
|
|
yield();
|
2022-06-15 22:53:05 +02:00
|
|
|
WebApi.loop();
|
|
|
|
|
yield();
|
2022-04-09 11:05:14 +02:00
|
|
|
}
|