-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespweather.ino
210 lines (184 loc) Β· 5.78 KB
/
espweather.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
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <JsonStreamingParser.h>
#include "WeatherListener.h"
#include "icons.h"
#include "constants.h"
// spin while connecting to WiFi
const char spinner[] = {'/', '-', 0b11111011, 0b11111110};
void load_icon(const uint8_t icon[]) {
for (uint8_t c = 0; c < 8; c++) {
Serial.printf("\031");
Serial.printf("%c", c);
for (uint8_t d = 0; d < 8; d++) {
Serial.printf("%c", icon[(c * 8) + d]);
}
}
}
void print_icon_pt1() {
// Position cursor to upper left of screen
Serial.printf("\014");
Serial.printf("\200\201\202\203");
}
void print_icon_pt2() {
Serial.printf("\204\205\206\207 ");
}
void scroll_string(String &daily_str, String &weekly_str) {
bool is_daily = true;
String *str = &daily_str;
unsigned long start_time = millis();
int len = str->length();
int pos = 0;
while (true) {
ArduinoOTA.handle();
if (pos == 0) {
// Beginning of string
// Position cursor to start of scroll area
Serial.printf("\021\004\001");
if (is_daily) {
Serial.print("Today: ");
} else {
Serial.print("Coming up: ");
}
delay(1500);
}
// Position cursor to start of scroll area
Serial.printf("\021\004\001");
Serial.print(str->substring(pos, pos+12));
++pos;
delay(200);
if (pos == 1) {
delay(2000);
} else if (pos >= len - 12) {
// End of string
pos = 0;
if (is_daily) {
str = &weekly_str;
} else {
str = &daily_str;
}
len = str->length();
is_daily = !is_daily;
delay(2000);
}
// Break after scrolling for 1 hour
// TODO handle millis() overflow (70 days)
if (millis() - start_time > 3600000) {
break;
}
}
}
void setup() {
WiFi.mode(WIFI_STA);
// FIXME -- WiFi.begin is buggy. Work around: https://github.com/esp8266/Arduino/issues/2186
if (WiFi.status() != WL_CONNECTED) { // FIX FOR USING 2.3.0 CORE (only .begin if not connected)
WiFi.begin(ssid, password); // connect to the network
}
uint8_t spinner_index = 0;
// Initialize Crystalfontz display
Serial.begin(19200, SERIAL_8N1);
delay(1000);
// Reset display
Serial.printf("\032\032");
delay(2000);
// Set display contrast
Serial.printf("\017F");
// Hide cursor
Serial.printf("\004");
// Scroll off
Serial.printf("\024");
// Wrap off
Serial.printf("\030");
delay(1000);
// Associate with wifi
Serial.printf("Connecting to\r\n%s... ", ssid);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf("\010%c", spinner[spinner_index++ % 4]);
delay(250);
}
Serial.printf("\014Connected! IP:\r\n");
IPAddress ip = WiFi.localIP();
Serial.printf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
delay(2000);
ArduinoOTA.setPassword("espweather");
ArduinoOTA.onStart([]() {
Serial.println("\014OTA Start");
});
ArduinoOTA.onEnd([]() {
Serial.print("\014OTA End");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("\014Progress: %u%%", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("\014OTA Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("\014Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("\014Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("\014Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("\014Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("\014End Failed");
});
ArduinoOTA.begin();
}
void loop() {
JsonStreamingParser parser;
WeatherListener listener;
ArduinoOTA.handle();
Serial.printf("\014Fetching url\r\n");
WiFiClientSecure client;
if (client.connect("api.darksky.net", 443)) {
Serial.printf("\014Connected\r\n");
} else {
Serial.printf("\014Error fetching");
delay(9999999);
}
client.printf("GET /forecast/%s/%s?exclude=currently,hourly,minutely,alerts,flags HTTP/1.1\r\n", api_key, latlong);
client.printf("Host: api.darksky.net\r\n");
client.printf("Connection: close\r\n");
client.printf("\r\n");
delay(10);
Serial.printf("\014Reading");
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.printf(".");
if (line == "\r") {
break;
}
}
Serial.printf("\014Parsing response\r\n");
parser.setListener(&listener);
while (client.connected()) {
parser.parse(client.read());
}
if (listener.icon == "snow") {
load_icon(snow);
} else if (listener.icon == "clear-day" || listener.icon == "clear-night") {
load_icon(clear_day); // TODO make icon for night
} else if (listener.icon == "party-cloudy-day" || listener.icon == "party-cloudy-night") {
load_icon(partly_cloudy_day); // TODO make icon for night
} else if (listener.icon == "fog") {
load_icon(cloudy); // TODO make icon for fog
} else if (listener.icon == "cloudy") {
load_icon(cloudy);
} else if (listener.icon == "wind") {
load_icon(wind);
} else if (listener.icon == "rain") {
load_icon(rain);
} else if (listener.icon == "sleet") {
load_icon(snow); // TODO make icon for sleet
} else {
load_icon(clear_day);
}
print_icon_pt1();
Serial.printf("%c %d\036\001\200 ", 0b11011110, listener.temperatureMax);
Serial.printf("%c %d\036\001\200\r\n", 0b11100000, listener.temperatureMin);
print_icon_pt2();
// Replace unicode degree symbol with blank. TODO handle multibyte degree symbol
// ("\036\001\200") while scrolling
listener.summary_daily.replace("Β°", "");
listener.summary_weekly.replace("Β°", "");
// Replace en dash with hyphen
listener.summary_daily.replace("β", "-");
listener.summary_weekly.replace("β", "-");
scroll_string(listener.summary_daily, listener.summary_weekly);
}