-
Notifications
You must be signed in to change notification settings - Fork 1
/
PowerMonitor-8266-OLED13.ino
235 lines (193 loc) · 5.07 KB
/
PowerMonitor-8266-OLED13.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
227
228
229
230
231
232
233
234
235
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <Arduino_JSON.h>
/***
*
* Arduino Power Monitor
* This sketch is for AZ-Delivery ESP8266 + 1.3 inches OLED display
* Author: Maurizio Giunti https://mauriziogiunti.it
*
***/
#ifndef STASSID
#define STASSID "---SSIDNAME---"
#define STAPSK "---SSIDPWD---"
#endif
// Shelly EM API URL
const char* shellyapiurl = "http://192.168.10.50/status";
#define NCYCLE 10 // Secondi tra le rilevazioni potenza
#define MAXPWP 4500 // Max power contatore 4.5kW
#define MAXFVP 5100 // Max power fotovoltaico 5.1kW
// Oled dimensions
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
const char* ssid = STASSID;
const char* password = STAPSK;
// INIT display
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
// Init other vars
int cycle=0;
int overpower=0;
void setup() {
Serial.begin(115200);
Serial.println("");
Serial.println("Startup");
// Setup display and clear it
u8g2.begin();
lcdPrepare();
// Clear the buffer
lcdClear();
// And startup
lcdPrintln(0,"Init WiFi net");
// Init WiFi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
for(int i=0;i<30;i++) {
if(WiFi.waitForConnectResult() == WL_CONNECTED) break;
delay(1000);
}
if(WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
lcdPrintln(2,"Connection Failed! Rebooting...");
delay(3000);
ESP.restart();
}
// Ready
Serial.println("Ready");
Serial.print("IP address: ");
String localIP = WiFi.localIP().toString().c_str();
Serial.println(localIP);
lcdPrintln(2,"Ready");
lcdPrintln(3,localIP);
}
void loop() {
// Ogni NCYCLE secondi chiama API Shelly per rilevare potenza
if( (cycle%(2*NCYCLE))==1 ) {
// Call API Shelly EM
String js = getShellyData();
JSONVar data = JSON.parse(js);
// Stampa sul display
String p0=JSON.stringify(data["emeters"][0]["power"]); // FV
String p1=JSON.stringify(data["emeters"][1]["power"]); // POW
// Verifico di aver ricevuto qualcosa da Shelly EM
if(p1!="null") {
double dp0=p0.toDouble(); // Inverto il segno perché dal sensore la produzione mi arriva in negativo
double dp1=p1.toDouble();
if(dp0<0) dp0=0; // FV non deve andare sotto zero
drawScreen(dp0,dp1);
// Blink if near overpower
if(dp1>MAXPWP) {
overpower++;
}
else {
overpower=0;
}
}
else {
Serial.println("Cannot connect to ShellyEM");
lcdClear();
lcdPrintln(1,"ERROR:");
lcdPrintln(2,"Cannot reach ShellyEM");
// Attendo 10 secondi e riprovo
delay(10000);
}
}
// Blink display overpower error
if(overpower>0) {
drawAlert(overpower);
overpower++;
}
cycle++;
delay(500);
}
/**
* Polls ShellyEM status API
*/
String getShellyData() {
String ret;
WiFiClient wifiClient;
HTTPClient http;
http.begin(wifiClient,shellyapiurl);
int statusCode = http.GET();
ret=http.getString();
//Serial.println(ret);
http.end();
return ret;
}
/**
* Handle display
*/
void lcdPrepare() {
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.setFontRefHeightExtendedText();
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
}
void lcdClear() {
u8g2.clearBuffer();
u8g2.sendBuffer();
}
/**
* Prints line of text on oled display
*/
void lcdPrintln(int posy, String txt) {
int py=posy*12;
u8g2.drawStr(0, py, txt.c_str());
u8g2.sendBuffer();
}
void drawProgressbar(int x,int y, int width,int height, int progress)
{
progress = progress > 100 ? 100 : progress; // set the progress value to 100
progress = progress < -100 ? -100 : progress; // set the progress value to 100
float bar = ((float)(width-1) / 100.0) * progress;
u8g2.drawFrame(x, y, width, height); // Cornice
if(bar >= 0.0) {
u8g2.drawBox(x+2, y+2, bar , height-4); // initailize the graphics fillRect(int x, int y, int width, int height)
}
else {
u8g2.drawBox(x+width+bar-1, y+2, -bar , height-4); // initailize the graphics fillRect(int x, int y, int width, int height)
}
}
void drawScreen(float fvP,float pwP) {
//
char buffer[128];
int y=0;
u8g2.clearBuffer();
u8g2.drawStr(0, y, "--| POWER MONITOR |--");
y+=14;
// FV
sprintf(buffer," fv: %.2fW",fvP);
u8g2.drawStr(0, y, buffer);
y+=12;
int fvPerc=(int)(100.0*fvP/MAXFVP);
drawProgressbar(2,y, 124, 10, fvPerc);
y+=12;
// Pad
y+=4;
// PW
sprintf(buffer," pow: %.2fW",pwP);
u8g2.drawStr(0, y, buffer);
y+=12;
int pwPerc=(int)(100.0*pwP/MAXPWP);
if(pwPerc<100) {
drawProgressbar(2, y, 124,10, pwPerc);
}
else {
u8g2.drawStr(0, y, " **** OVER POWER ****");
}
y+=12;
// OK print
u8g2.sendBuffer();
}
/**
* Draw blinking OVERPOWER alert
*/
void drawAlert(int c) {
u8g2.clearBuffer();
if((c%2)==1) {
u8g2.drawStr(0, 24, "**** OVER POWER ****");
}
u8g2.sendBuffer();
}