-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
180 lines (147 loc) · 5.41 KB
/
main.cpp
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
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <GxEPD2_3C.h>
#include <GxEPD2_BW.h>
#include <Fonts/FreeSerif9pt7b.h> // fonts: https://github.com/adafruit/Adafruit-GFX-Library/tree/master/Fonts
#include "main.h"
#include "display.h"
#include "wifi.h"
#include "examples.h"
const String SPREADSHEET_URL = "https://sheets.googleapis.com/v4/spreadsheets/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/values/"
"Arduino?alt=json&valueRenderOption=FORMATTED_VALUE&key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const String WIFI_SSIDS[ARRAY_SIZE] = {"wifi1", "wifi2"};
const String WIFI_PASSWORDS[ARRAY_SIZE] = {"pass1", "pass2"};
int main()
{
//connectToWiFi(WIFI_SSIDS[0], WIFI_PASSWORDS[0]);
connectToMultiWiFi(WIFI_SSIDS, WIFI_PASSWORDS);
StaticJsonDocument<JSON_BUFFER_SIZE> data = loadData(SPREADSHEET_URL);
disconnectWiFi();
DataHolder dataHolder = parseData(data);
displayData(dataHolder);
return 0;
}
StaticJsonDocument<JSON_BUFFER_SIZE> loadData(String url)
{
Serial.println("Loading data from: " + url);
WiFiClientSecure wifiClient;
wifiClient.setInsecure();
wifiClient.connect(url, 80);
HTTPClient httpClient;
httpClient.begin(wifiClient, url);
int httpStatus = httpClient.GET();
String payload = httpClient.getString();
httpClient.end();
Serial.print("HTTP status code: ");
Serial.println(httpStatus);
Serial.print("Payload: ");
Serial.println(payload);
if(httpStatus == 200)
{
StaticJsonDocument<JSON_BUFFER_SIZE> jsonBuffer;
DeserializationError parseError = deserializeJson(jsonBuffer, payload);
if(parseError)
{
Serial.println("JSON parse error");
}
else
{
Serial.println("Parsing JSON data success");
return jsonBuffer;
}
}
else
{
Serial.println("HTTP response error");
}
return StaticJsonDocument<JSON_BUFFER_SIZE>();
}
DataHolder parseData(StaticJsonDocument<JSON_BUFFER_SIZE>& jsonBuffer)
{
String header = jsonBuffer["values"][0][0].as<String>();
String symbol1 = jsonBuffer["values"][1][0].as<String>();
String value1 = jsonBuffer["values"][1][1].as<String>();
String change1 = jsonBuffer["values"][1][2].as<String>();
String item1 = alignText(symbol1, 7, true) + alignText(value1, 7, false) + alignText(change1, 9, false);
String symbol2 = jsonBuffer["values"][2][0].as<String>();
String value2 = jsonBuffer["values"][2][1].as<String>();
String change2 = jsonBuffer["values"][2][2].as<String>();
String item2 = alignText(symbol2, 7, true) + alignText(value2, 7, false) + alignText(change2, 9, false);
String symbol3 = jsonBuffer["values"][3][0].as<String>();
String value3 = jsonBuffer["values"][3][1].as<String>();
String change3 = jsonBuffer["values"][3][2].as<String>();
String item3 = alignText(symbol3, 7, true) + alignText(value3, 7, false) + alignText(change3, 9, false);
String portfolioValue = jsonBuffer["values"][4][0].as<String>();
String portfolioReturn = jsonBuffer["values"][4][1].as<String>();
String portfolioGain = jsonBuffer["values"][4][2].as<String>();
String footer = portfolioValue + " " + portfolioReturn + " " + portfolioGain;
return DataHolder(header, item1, item2, item3, footer);
}
void displayData(DataHolder data)
{
display.init(115200);
display.setRotation(3); // landscape
display.setFullWindow();
display.firstPage();
int16_t textBoundsX, textBoundsY;
uint16_t textBoundsWidth, textBoundsHeight;
uint16_t displayWidth = display.width() - 1;
uint16_t displayHeight = display.height() - 1;
uint16_t paddingHorizontal = 8;
uint16_t paddingTop = 21;
uint16_t lineHeight = 24;
uint16_t linePadding = 4;
do
{
display.fillScreen(GxEPD_WHITE);
display.setFont(&FreeSerif9pt7b);
// header text
display.setTextColor(GxEPD_RED);
display.getTextBounds(data.header, 0, 0, &textBoundsX, &textBoundsY, &textBoundsWidth, &textBoundsHeight);
uint16_t headerX = ((display.width() - textBoundsWidth) / 2) - textBoundsX;
display.setCursor(headerX, paddingTop);
display.print(data.header);
// header line
uint16_t headerLineY = lineHeight + linePadding;
display.drawLine(paddingHorizontal, headerLineY, displayWidth - paddingHorizontal, headerLineY, GxEPD_RED);
// text 1
display.setTextColor(GxEPD_BLACK);
display.setCursor(paddingHorizontal, paddingTop + lineHeight);
display.print(data.item1);
// text 2
display.setCursor(paddingHorizontal, paddingTop + lineHeight * 2);
display.print(data.item2);
// text 3
display.setCursor(paddingHorizontal, paddingTop + lineHeight * 3);
display.print(data.item3);
// footer line
uint16_t footerLineY = (lineHeight * 4) + linePadding;
display.drawLine(paddingHorizontal, footerLineY, displayWidth - paddingHorizontal, footerLineY, GxEPD_RED);
// footer text
display.setTextColor(GxEPD_RED);
display.getTextBounds(data.footer, 0, 0, &textBoundsX, &textBoundsY, &textBoundsWidth, &textBoundsHeight);
uint16_t footerX = ((display.width() - textBoundsWidth) / 2) - textBoundsX;
display.setCursor(footerX, paddingTop + lineHeight * 4);
display.print(data.footer);
// rectangle
display.drawLine(0, 0, displayWidth, 0, GxEPD_BLACK);
display.drawLine(0, 0, 0, displayHeight, GxEPD_BLACK);
display.drawLine(displayWidth, 0, displayWidth, displayHeight, GxEPD_BLACK);
display.drawLine(0, displayHeight, displayWidth, displayHeight, GxEPD_BLACK);
} while(display.nextPage());
}
String alignText(String text, int length, bool toLeft)
{
while(text.length() < length)
{
if(toLeft)
{
text = text + " ";
}
else
{
text = " " + text;
}
}
return text;
}