forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
113 lines (102 loc) · 3.1 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
*/
#include "Configuration.h"
#include "Hoymiles.h"
#include "MqttHassPublishing.h"
#include "MqttPublishing.h"
#include "MqttSettings.h"
#include "NtpSettings.h"
#include "WebApi.h"
#include "NetworkSettings.h"
#include "defaults.h"
#include <Arduino.h>
#include <LittleFS.h>
void setup()
{
// 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... "));
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");
}
} else {
Serial.println(F("done"));
}
// 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 Network... "));
NetworkSettings.init();
Serial.println(F("done"));
NetworkSettings.applyConfig();
// Initialize NTP
Serial.print(F("Initialize NTP... "));
NtpSettings.init();
Serial.println(F("done"));
// Initialize MqTT
Serial.print(F("Initialize MqTT... "));
MqttSettings.init();
MqttPublishing.init();
MqttHassPublishing.init();
Serial.println(F("done"));
// Initialize WebApi
Serial.print(F("Initialize WebApi... "));
WebApi.init();
Serial.println(F("done"));
// 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) {
auto inv = Hoymiles.addInverter(
config.Inverter[i].Name,
config.Inverter[i].Serial);
for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) {
inv->Statistics()->setChannelMaxPower(c, config.Inverter[i].MaxChannelPower[c]);
}
}
}
Serial.println(F("done"));
}
void loop()
{
NetworkSettings.loop();
yield();
Hoymiles.loop();
yield();
MqttPublishing.loop();
yield();
MqttHassPublishing.loop();
yield();
WebApi.loop();
yield();
}