-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathGoodWeLogger.ino
177 lines (156 loc) · 4.95 KB
/
GoodWeLogger.ino
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <TimeLib.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "GoodWeCommunicator.h"
#include "SettingsManager.h"
#include "MQTTPublisher.h"
#include "PVOutputPublisher.h"
#include "Settings.h" //change and then rename Settings.example.h to Settings.h to compile
#include "Debug.h"
SettingsManager settingsManager;
GoodWeCommunicator goodweComms(&settingsManager);
MQTTPublisher mqqtPublisher(&settingsManager, &goodweComms);
PVOutputPublisher pvoutputPublisher(&settingsManager, &goodweComms);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_SERVER);
bool validTimeSet = false;
int reconnectCounter = 0;
#ifdef REMOTE_DEBUGGING_ENABLED
RemoteDebug Debug;
#endif
void setup()
{
//debug settings
auto settings = settingsManager.GetSettings();
//set settings from heade file
settings->mqttHostName = MQTT_HOST_NAME;
settings->mqttPort = MQTT_PORT;
settings->mqttUserName = MQTT_USER_NAME;
settings->mqttPassword = MQTT_PASSWORD;
settings->mqttQuickUpdateInterval = MQTT_QUICK_UPDATE_INTERVAL;
settings->mqttRegularUpdateInterval = MQTT_REGULAR_UPDATE_INTERVAL;
settings->pvoutputApiKey = PVOUTPUT_API_KEY;
settings->pvoutputSystemId = PVOUTPUT_SYSTEM_ID;
settings->pvoutputUpdateInterval = PVOUTPUT_UPDATE_INTERVAL;
settings->wifiHostname = WIFI_HOSTNAME;
settings->wifiSSID = WIFI_SSID;
settings->wifiPassword = WIFI_PASSWORD;
settings->timezone = TIMEZONE;
settings->RS485Rx = RS485_RX;
settings->RS485Tx = RS485_TX;
settings->wifiConnectTimeout = WIFI_CONNECT_TIMEOUT;
settings->ntpServer = NTP_SERVER;
settings->inverterOfflineDataResetTimeout = INVERTER_OFFLINE_RESET_VALUES_TIMEOUT;
//Init our compononents
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.hostname(settings->wifiHostname.c_str());
WiFi.begin(settings->wifiSSID.c_str(), settings->wifiPassword.c_str());
//check wifi connection
if (!checkConnectToWifi())
{
Serial.println("Cannot establish WiFi connection Please check the Wifi settings in the header file. Restarting ESP");
ESP.restart();
}
timeClient.setUpdateInterval(1000 * 60 * 60); //one hour updates
timeClient.begin();
ArduinoOTA.setHostname("GoodWeLogger");
ArduinoOTA.onStart([]() {
Serial.println("Start Ota");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd Ota");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("OTA Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
#ifdef REMOTE_DEBUGGING_ENABLED
Debug.begin("GoodweLogger");
Debug.setResetCmdEnabled(true);
Debug.setCallBackNewClient(&RemoteDebugClientConnected);
#endif
//ntp client
goodweComms.start();
mqqtPublisher.start();
validTimeSet = timeClient.update();
timeClient.setTimeOffset(settings->timezone * 60 * 60);
}
#ifdef REMOTE_DEBUGGING_ENABLED
void RemoteDebugClientConnected()
{
//Debug client connected. Print all our info
debugPrintln("===GoodWeLogger remote debug enabled===");
}
#endif
bool checkConnectToWifi()
{
//check for wifi connection
if (WiFi.status() != WL_CONNECTED || WiFi.localIP() == IPAddress(0, 0, 0, 0))
{
Serial.println("Connecting to WIFI...");
int totalWaitTime = 0;
while (WiFi.status() != WL_CONNECTED)
{ // Wait for the Wi-Fi to connect
delay(1000);
totalWaitTime += 1000;
Serial.print(".");
if (totalWaitTime > WIFI_CONNECT_TIMEOUT)
{
//no connection.
Serial.println("\nWiFi connect timed out");
return false;
}
}
}
return true;
}
void loop()
{
//update the time if set correctly
auto timeUpdate = timeClient.update();
//if the time is updated, we need to set it in the time lib so we can use the helper functions from timelib
if (timeUpdate)
{
if (!validTimeSet)
validTimeSet = true; //pvoutput is started after the time is set
//sync time to time lib
setTime(timeClient.getEpochTime());
}
//check wifi connection
if (!checkConnectToWifi())
{
Serial.println("Wifi connection lost for too long. Restarting ESP");
ESP.restart();
}
ArduinoOTA.handle();
yield();
goodweComms.handle();
yield();
mqqtPublisher.handle();
yield();
//start the pvoutput publisher after the time has been set if it is configured to start
if (validTimeSet && pvoutputPublisher.canStart() && !pvoutputPublisher.getIsStarted())
pvoutputPublisher.start();
pvoutputPublisher.handle();
yield(); //prevent wathcdog timeout
#ifdef REMOTE_DEBUGGING_ENABLED
Debug.handle();
#endif
}