-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSolar.ino
168 lines (135 loc) · 5.54 KB
/
Solar.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
struct SolarPwrSystems {
bool Available;
String Url;
String Token;
uint64_t TokenExpire;
uint32_t SiteID;
uint32_t Wp;
uint32_t Actual;
uint32_t Daily;
uint32_t Interval;
time_t LastRefresh;
char file_name[20];
};
SolarPwrSystems Enphase = { false, "https://envoy/api/v1/production", "", 0, 0, 0, 0, 0, 60, 0, "/enphase.json" }, SolarEdge = { false, "", "", 0, 0, 0, 0, 0, 300, 0, "/solaredge.json" };
void ReadSolarConfig( SolarSource src ){
SolarPwrSystems* solarSystem = (src == ENPHASE ? &Enphase : &SolarEdge);
if ( !FSmounted || !LittleFS.exists( solarSystem->file_name ) ) return;
DebugT("ReadSolarConfig: ");Debugln(solarSystem->file_name);
StaticJsonDocument<800> doc;
File SolarFile = LittleFS.open( solarSystem->file_name, "r");
if ( !SolarFile ) {
DebugTln("Error reading SolarFile");
return;
}
DeserializationError error = deserializeJson(doc, SolarFile);
if (error) {
Debugln("Error deserialize SolarFile");
SolarFile.close();
return;
}
solarSystem->Available = true;
solarSystem->Url = doc["gateway-url"].as<String>();
solarSystem->Token = doc["token"].as<String>();
solarSystem->Wp = doc["wp"].as<int>();
solarSystem->SiteID = doc["siteid"].as<int>();
if ( doc["refresh-interval"].as<int>() > (src==ENPHASE?60:300) ) solarSystem->Interval = doc["refresh-interval"].as<int>();
#ifdef DEBUG
Debug("url > "); Debugln(solarSystem->Url);
Debug("token > "); Debugln(solarSystem->Token);
Debug("wp > "); Debugln(solarSystem->Wp);
Debug("interval > "); Debugln(solarSystem->Interval);
Debug("siteid > "); Debugln(solarSystem->SiteID);
#endif
SolarFile.close();
GetSolarData( src, true );
}
void ReadSolarConfigs(){
ReadSolarConfig( ENPHASE );
ReadSolarConfig( SOLAR_EDGE );
}
void GetSolarDataN(){
GetSolarData( ENPHASE, false );
GetSolarData( SOLAR_EDGE, false );
}
void GetSolarData( SolarSource src, bool forceUpdate ){
SolarPwrSystems* solarSystem = (src == ENPHASE ? &Enphase : &SolarEdge);
if ( !solarSystem->Available ) return;
if ( !forceUpdate && ((uptime() - solarSystem->LastRefresh) < solarSystem->Interval) ) return;
solarSystem->LastRefresh = uptime();
HTTPClient http;
if (src == SOLAR_EDGE) {
solarSystem->Url = "https://monitoringapi.solaredge.com/site/" + String(SolarEdge.SiteID) + "/overview";
Debug("url > "); Debugln(solarSystem->Url);
}
String urlcheck = solarSystem->Url;
bool bSolis = false;
if(urlcheck.indexOf("CMD=inv_query") > 0) {
DebugTln("Solis Omvormer");
bSolis = true;
}
http.begin( solarSystem->Url.c_str() );
http.addHeader("Accept", "application/json");
if (src == ENPHASE) {
if ( !bSolis ) http.addHeader("Authorization", "Bearer " + solarSystem->Token);
} else {
http.addHeader("X-API-Key", solarSystem->Token);
}
int httpResponseCode = http.GET();
DebugT(F("HTTP Response code: "));Debugln(httpResponseCode);
if ( httpResponseCode <= 0 ) {
return; //leave on error
}
String payload = http.getString();
Debugln(payload);
http.end();
// payload = "{\"g_sn\":1902457183,\"g_ver\":\"ME-121001-V1.0.6(201704012008)\",\"time\":1723404273,\"g_time\":9168,\"g_err\":0,\"mac\":\"bc-54-f9-f5-73-bc\",\"auto_ip\":1,\"ip\":\"192.168.0.38\",\"sub\":\"255.255.255.0\",\"gate\":\"192.168.0.1\",\"auto_dns\":1,\"dns\":\"208.67.222.222\",\"dns_bak\":\"208.67.222.222\",\"i_status\":16,\"i_type\":1283,\"i_num\":1,\"i_sn\":\"110E301842200080\",\"i_ver_m\":\"\",\"i_ver_s\":\"\",\"i_modle\":\"\",\"i_pow\":\"\",\"i_pow_n\":88,\"i_eday\":\"8.451\",\"i_eall\":\"8012.0\",\"i_alarm\":\"F22F23\",\"i_last_t\":0,\"l_a_agr\":2,\"l_a_ip\":\"8.211.53.112\",\"l_a_port\":10000}";
// Parse JSON object in response
DynamicJsonDocument solarDoc(1024);
DeserializationError error = deserializeJson(solarDoc, payload);
if (error) {
Debugln("Error deserialisation solarDoc");
return;
}
if ( bSolis ) {
solarSystem->Actual = solarDoc["i_pow_n"].as<int>();
solarSystem->Daily = (int) ((float)solarDoc["i_eday"] * 1000);
} else {
solarSystem->Actual = (src == ENPHASE) ? solarDoc["wattsNow"].as<int>() : solarDoc["overview"]["currentPower"]["power"].as<int>();
solarSystem->Daily = (src == ENPHASE) ? solarDoc["wattHoursToday"].as<int>() : solarDoc["overview"]["lastDayData"]["energy"].as<int>();
}
#ifdef DEBUG
Debug("Daily > "); Debugln(solarSystem->Daily);
Debug("Actual > "); Debugln(solarSystem->Actual);
#endif
}
void SendSolarJson(){
if ( !Enphase.Available && !SolarEdge.Available ) {
httpServer.send(200, "application/json", "{\"active\":false}");
return;
}
/*
SCR = Eigengebruik = ( Productie - Teruglevering ) / Productie
SEUE = Zelf Voorzienendheid = ( Productie - Teruglevering ) / ( Afname + Productie - Teruglevering )
Prestatie = kWh/kWp
*/
String Json = "{\"active\":true,\"total\":{\"daily\":";
Json += String(Enphase.Daily + SolarEdge.Daily);
Json += ",\"actual\":";
Json += String(Enphase.Actual + SolarEdge.Actual);
Json += "}, \"Wp\":";
Json += String( Enphase.Wp + SolarEdge.Wp );
Json += ", \"perc\":{\"seue\":";
Json += String( 0 /* self consumed solar engery : totale energy consumptie */ );
Json += ",\"scr\":";
Json += String( 0 /* self consumed solar engery : opwekking */);
Json += ",\"pres\":";
Json += String( 0 /* kWh/kWp */);
Json += "}";
Json += "}";
#ifdef DEBUG
DebugTln("SendSolarJson");
DebugT("Solar Json: ");Debugln(Json);
#endif
httpServer.send(200, "application/json", Json.c_str() );
}