-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathe_paper_logo.ino
375 lines (316 loc) · 8.97 KB
/
e_paper_logo.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <GxEPD2_BW.h> //https://github.com/eziosoft/GxEPD2
#include <GxEPD2_3C.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeMonoBold24pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSans18pt7b.h>
#include <Fonts/FreeSans24pt7b.h>
#include <Fonts/FreeSans9pt7b.h>
#include <StreamString.h>
#define PrintString StreamString
#include <GxEPD2_3C.h>
#include <ArduinoJson.h>
#include "ESP8266WiFi.h"
#include "icons.h" //weather icons from OpenWeather
#include "logo.h"
#define FW_VERSION 13
bool firmwareUpdating = false;
#define myID 16493833
uint32_t ID = ESP.getChipId();
// GxEPD2_BW<GxEPD2_260, GxEPD2_260::HEIGHT> display(GxEPD2_260(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); //BW - faster refresh
GxEPD2_3C<GxEPD2_260c, GxEPD2_260c::HEIGHT> display(GxEPD2_260c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); //BRW - slow refresh
static const uint16_t WIDTH = 152;
static const uint16_t HEIGHT = 296;
ADC_MODE(ADC_VCC); //enable VCC measurement
#define VDD_offset -0.17
#define ENABLE_GxEPD2_GFX 1 //enable Adafruit GFX library
//milliseconds to sleep
static const uint16_t minute = 60;
long interval = minute; //deep sleep duration
PrintString vddString;
PrintString rssiString;
float vdd, rssi;
void setup()
{
bool firmwareUpdate = digitalRead(D6) == LOW;
Serial.begin(115200);
display.init(115200);
display.setFullWindow();
WiFi.forceSleepWake();
delay(1);
setupWifi();
vdd = (ESP.getVcc() / 1000.0) + VDD_offset;
vddString.print(vdd, 2);
rssi = dBmtoPercentage(WiFi.RSSI());
rssiString.print(rssi, 0);
//put D6 low to force firmware update
pinMode(D6, INPUT_PULLUP);
if (firmwareUpdate)
{
updateFirmware();
}
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
loopMQTT();
}
if (!firmwareUpdating && millis() > 30000) //if this runs more than 20sek something was wrong. usually takes 8sek
{
printTextCenter("timeout. next try in 5min");
delay(100);
interval = 5 * minute;
deepSleep();
}
}
void json(char *json) //from mqttCallback
{
const size_t capacity = JSON_ARRAY_SIZE(4) + JSON_ARRAY_SIZE(20) + 24 * JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 550;
DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, json);
if (error)
{
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
printTextCenter("json error");
interval = minute;
deepSleep();
return;
}
int version = doc["v"];
if (version > FW_VERSION)
{
updateFirmware();
}
sendTelemetry("");
delay(100);
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
Serial.println("\n\nWIFI OFF");
interval = doc["NR"]; // in seconds
int icon = doc["i"];
int iconColor = GxEPD_BLACK;
int xi = doc["xi"];
int yi = doc["yi"];
if (doc["ic"] == 1)
{
iconColor = GxEPD_RED;
}
JsonArray data = doc["d"];
JsonArray lines = doc["l"];
display.setRotation(1);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(GxEPD_BLACK);
display.setFullWindow();
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
printStatic();
switch (icon)
{
case 0:
displayFullScreenImage();
deepSleep();
break;
case 1:
display.drawInvertedBitmap(xi, yi, i01n, 100, 100, iconColor);
break;
case 2:
display.drawInvertedBitmap(xi, yi, i02n, 100, 100, iconColor);
break;
case 3:
display.drawInvertedBitmap(xi, yi, i03n, 100, 100, iconColor);
break;
case 4:
display.drawInvertedBitmap(xi, yi, i04n, 100, 100, iconColor);
break;
case 9:
display.drawInvertedBitmap(xi, yi, i09n, 100, 100, iconColor);
break;
case 10:
display.drawInvertedBitmap(xi, yi, i10n, 100, 100, iconColor);
break;
case 11:
display.drawInvertedBitmap(xi, yi, i11n, 100, 100, iconColor);
break;
case 13:
display.drawInvertedBitmap(xi, yi, i13n, 100, 100, iconColor);
break;
case 50:
display.drawInvertedBitmap(xi, yi, i50n, 100, 100, iconColor);
break;
default:
break;
}
for (int i = 0; i < 20; i++)
{
JsonObject datai = data[i];
int data_x = datai["x"]; // 10
int data_y = datai["y"]; // 10
int font = datai["f"]; //font number
int color = datai["c"]; // 0 or 1 - black or red
const char *data_text = datai["t"]; // "text"
switch (font)
{
case 0:
display.setFont(&FreeMonoBold9pt7b);
break;
case 1:
display.setFont(&FreeMonoBold24pt7b);
break;
case 2:
display.setFont(&FreeSans9pt7b);
break;
case 3:
display.setFont(&FreeSans12pt7b);
break;
case 4:
display.setFont(&FreeSans18pt7b);
break;
case 5:
display.setFont(&FreeSans24pt7b);
break;
default:
display.setFont(&FreeMonoBold9pt7b);
break;
}
if (color == 1)
display.setTextColor(GxEPD_RED);
else
display.setTextColor(GxEPD_BLACK);
display.setCursor(data_x, data_y);
display.print(data_text);
}
for (int i = 0; i < 10; i++)
{
JsonObject line = lines[i];
int x1 = line["x1"]; // 10
int x2 = line["x2"]; // 10
int y1 = line["y1"]; // 10
int y2 = line["y2"]; // 10
int color = line["c"]; // 0 or 1 - black or red
if (color == 1)
display.drawLine(x1, y1, x2, y2, GxEPD_RED);
else
display.drawLine(x1, y1, x2, y2, GxEPD_BLACK);
}
} while (display.nextPage());
deepSleep();
}
void deepSleep()
{
display.hibernate();
delay(10);
// https: //github.com/esp8266/Arduino/issues/644
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
delay(1);
ESP.deepSleep(interval * 1e6 /*, WAKE_RF_DISABLED*/); //in seconds
}
void printStatic() //prints wifi signal strength and VCC in top right corner
{
display.fillRect(208, 0, 88, 13, GxEPD_BLACK);
display.setTextColor(GxEPD_WHITE);
display.setCursor(250, 11);
display.print(vddString);
display.setCursor(210, 11);
display.print(rssiString);
display.setTextColor(GxEPD_BLACK);
// display.drawLine(0, 13, HEIGHT, 13, GxEPD_RED);
}
void printTextCenter(char *text) // prints text in center of the screen. Used to display errors
{
display.setRotation(1);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(GxEPD_BLACK);
int16_t tbx, tby;
uint16_t tbw, tbh;
display.getTextBounds(text, 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t x = ((display.width() - tbw) / 2) - tbx;
uint16_t y = ((display.height() - tbh) / 2) - tby;
display.setFullWindow();
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
printStatic();
display.setCursor(x, y);
display.print(text);
} while (display.nextPage());
}
int dBmtoPercentage(int dBm)
{
const int RSSI_MAX = -50; // define maximum strength of signal in dBm
const int RSSI_MIN = -100; // define minimum strength of signal in dBm
int quality;
if (dBm <= RSSI_MIN)
{
quality = 0;
}
else if (dBm >= RSSI_MAX)
{
quality = 100;
}
else
{
quality = 2 * (dBm + 100);
}
return quality;
}
/////////////////////////////////////////
void update_started()
{
Serial.println("CALLBACK: HTTP update process started");
printTextCenter("Firmware update...");
sendTelemetry("Firmware update started");
}
void update_finished()
{
Serial.println("CALLBACK: HTTP update process finished");
printTextCenter("Firmware update DONE!");
sendTelemetry("Firmware update finished");
}
#include "ESP8266httpUpdate.h"
void updateFirmware()
{
firmwareUpdating = true;
// https://raw.githubusercontent.com/eziosoft/weatherEpaperDisplay/e_paper_logo.ino.d1.bin
// const char *host = "raw.githubusercontent.com";
// const int httpsPort = 443;
// const char *url = "/eziosoft/weatherEpaperDisplay/master/e_paper_logo.ino.d1.bin";
ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);
ESPhttpUpdate.onStart(update_started);
ESPhttpUpdate.onEnd(update_finished);
// ESPhttpUpdate.onProgress(update_progress);
// ESPhttpUpdate.onError(update_error);
auto ret = ESPhttpUpdate.update("http://eziosoft.com/api/weatherDisplayFW/e_paper_logo.ino.d1.bin");
switch (ret)
{
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
printTextCenter("FW update failed");
sendTelemetry("Firmware update FAILED");
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
void displayFullScreenImage()
{
display.setRotation(0);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(GxEPD_BLACK);
display.setFullWindow();
display.firstPage();
do
{
display.drawInvertedBitmap(0, 0, logoB, WIDTH, HEIGHT, GxEPD_RED);
display.drawInvertedBitmap(0, 0, logoR, WIDTH, HEIGHT, GxEPD_BLACK);
} while (display.nextPage());
}