-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesp-fronius-monitor.ino
227 lines (191 loc) · 6.76 KB
/
esp-fronius-monitor.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Fronius Solar generation and Load monitor
Niklas Casaril 2016
This code is in the public domain.
*/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>
#include <ArduinoJson.h>
extern "C" {
#include <oled.h>
#include "user_interface.h"
}
typedef struct {
String ssid;
String pass;
} wifi_nets_t;
#include "networkinfo.h"
// Solarpv generated and usage
IPAddress solarpv_ip(192,168,10,6);
WiFiClient client;
float _generate_w = 0;
float _load_w = 0;
unsigned long _inverter_time = 0;
unsigned long _last_inverter_time = 0;
unsigned long _last_plottime = 0;
solargraph_t _solar_log[128];
int _solar_log_i = 0;
char _jsonbuffer[2048];
char *_jp = _jsonbuffer;
bool _skipHeader = true;
void getCurrentKW()
{
while (client.available())
{
char c = client.read();
if (_skipHeader && c!='{') continue;
_skipHeader = false;
if (c == ' ' || c == '\t' || c=='\n' || c=='\r') continue;
*(_jp++) = c;
//Serial.print(c);
}
if (!client.connected())
{
if (_jp != _jsonbuffer && _jsonbuffer[0]!=0)
{
// Remove trailing 0
if (_jp > _jsonbuffer && *(_jp-1) == '0') *(_jp-1) = 0;
Serial.println("\nStarting json conversion...");
Serial.println((int)(_jp - _jsonbuffer));
Serial.println(_jsonbuffer);
StaticJsonBuffer<2048> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(_jsonbuffer);
if (root.success())
{
Serial.println(" json success");
_generate_w = root["Body"]["Data"]["Power_P_Generate"]["value"];
_load_w = root["Body"]["Data"]["Power_P_Load"]["value"];
_inverter_time = root["Body"]["Data"]["TimeStamp"]["value"];
_inverter_time += 10*3600; // Brisbane
}
Serial.println("complete");
}
Serial.println("\nStarting connection to inverter...");
for (int i=0;i<sizeof(_jsonbuffer);i++) _jsonbuffer[i]=0;
_jp = _jsonbuffer;
_skipHeader = true;
char buffer[32];
// if you get a connection, report back via serial:
if (client.connect(solarpv_ip, 80))
{
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /components/5/0/?print=names HTTP/1.1");
String hostip = String("Host: ") + String(solarpv_ip[0]) + '.' + String(solarpv_ip[1]) + '.' + String(solarpv_ip[2]) + '.' + String(solarpv_ip[3]);
hostip.toCharArray(buffer, 20);
client.println(buffer);
client.println("Connection: close");
client.println();
}
}
}
unsigned int localPort = 2390; // local port to listen for UDP packets
void setup()
{
Serial.begin(115200);
delay(2000); // wait for uart to settle and print Espressif blurb..
Serial.println();
Serial.println();
// print out all system information
Serial.print("Heap: "); Serial.println(system_get_free_heap_size());
Serial.print("Boot Vers: "); Serial.println(system_get_boot_version());
Serial.print("CPU: "); Serial.println(system_get_cpu_freq());
#if defined(ARDUINO_ESP8266_ESP12E)
Wire.begin(2, 14); //on ESP12E.
#else
Wire.begin(0, 2); //on ESP-01.
#endif
StartUp_OLED(); // Init Oled and fire up!
Serial.println("OLED Init...");
clear_display();
sendStrXY(" SOLAR POWER ", 0, 1); // 16 Character max per line with font set
sendStrXY(" MONITOR ", 2, 1);
sendStrXY(" CONNECTING... ", 4, 1);
// We start by connecting to a WiFi network
int i=0;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print("Connecting to:");
Serial.println(networks[i].ssid.c_str());
WiFi.begin(networks[i].ssid.c_str(), networks[i].pass.c_str());
int timeout = 0;
while (WiFi.status() != WL_CONNECTED && timeout++<20)
{
delay(500);
Serial.print(".");
}
Serial.println("");
if (WiFi.status() == WL_CONNECTED) break;
if (networks[++i].ssid=="") i=0;
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
clear_display(); // Clear OLED
sendStrXY("NETWORK DETAILS", 0, 1);
sendStrXY("NET: ", 3, 1);
IPAddress ip = WiFi.localIP(); // Convert IP Here
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
char buffer[32];
networks[i].ssid.toCharArray(buffer, 20);
sendStrXY((buffer), 3, 6);
ipStr.toCharArray(buffer, 20);
sendStrXY((buffer), 6, 1); // Print IP
// Clearing json buffer
for (int i=0;i<sizeof(_jsonbuffer);i++) _jsonbuffer[i]=0;
for (int i=0;i<128;i++)
{
_solar_log[i].g = 0;//5000*((i*4)%128)/128.0;
_solar_log[i].l = 0;//1500+sin(i/8.0)*1000;
}
}
unsigned long _last_check_t=0;
void loop()
{
unsigned long t=millis();
if (t - _last_check_t > 200)
{
getCurrentKW();
_last_check_t = t;
}
if (_inverter_time > 0 && _last_inverter_time == 0)
clear_display();
if (_inverter_time && _inverter_time != _last_inverter_time)
{
// Current 24h time (from inverter) up the top
String timestr = "";
char buffer[32];
timestr += String((_inverter_time % 86400L) / 3600) + ":";
if ( ((_inverter_time % 3600) / 60) < 10 ) timestr += "0";
timestr += String(((_inverter_time % 3600) / 60)) + ":";
if ( (_inverter_time % 60) < 10 ) timestr += "0";
timestr += String(_inverter_time % 60);
timestr.toCharArray(buffer, 20);
sendStrXY((buffer), 0, 1);
// Solar - Load = Net
dtostrf(_generate_w/1000.0,3,2,buffer);
String kwstring = String(buffer);
dtostrf(_load_w/1000.0,3,2,buffer);
kwstring += String(buffer) + "=";
dtostrf((_generate_w+_load_w)/1000.0,3,2,buffer);
kwstring += String(buffer);
sendStrXY("Sol Load Net ", 2, 0);
kwstring.toCharArray(buffer, 20);
sendStrXY((buffer), 3, 0);
// Graph at the bottom
if (_inverter_time - _last_plottime > 10)
{
_last_plottime = _inverter_time;
_solar_log[_solar_log_i].g = _generate_w;
_solar_log[_solar_log_i].l = -_load_w;
Serial.print("storling gen :");Serial.println(_solar_log[_solar_log_i].g);
Serial.print("storling load:");Serial.println(_solar_log[_solar_log_i].l);
if (++_solar_log_i > 127) _solar_log_i=0;
Serial.println("Plotting data...");
Draw_Plot(_solar_log, _solar_log_i, 128);
}
}
_last_inverter_time = _inverter_time;
delay(10);
}