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
|
#include <user_settings.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ezTime.h>
#include <ArduinoJson.h>
Timezone CopenhagenTime;
WiFiClient client; // or WiFiClientSecure for HTTPS
HTTPClient http;
unsigned long aPIPreviousMillis = 0;
const long aPIInterval = 600000; // call API every 10 minutes
String weatherText = "";
String previousOutputText = "";
String weatherDescription(int code, bool isDay) {
switch(code) {
case 0:
if (isDay){
return "Sunny";
} else {
return "Clear";
}
break;
case 1:
if (isDay){
return "Mainly Sunny";
} else {
return "Mainly Clear";
}
break;
case 2:
return "Partly Cloudy";
break;
case 3:
return "Cloudy";
break;
case 45:
return "Foggy";
break;
case 48:
return "Rime Fog";
break;
case 51:
return "Light Drizzle";
break;
case 53:
return "Drizzle";
break;
case 55:
return "Heavy Drizzle";
break;
case 56:
return "Light Freezing Drizzle";
break;
case 57:
return "Freezing Drizzle";
break;
case 61:
return "Light Rain";
break;
case 63:
return "Rain";
break;
case 65:
return "Heavy Rain";
break;
case 66:
return "Light Freezing Rain";
break;
case 67:
return "Freezing Rain";
break;
case 71:
return "Light Snow";
break;
case 73:
return "Snow";
break;
case 75:
return "Heavy Snow";
break;
case 77:
return "Snow Grains";
break;
case 80:
return "Light Showers";
break;
case 81:
return "Showers";
break;
case 82:
return "Heavy Showers";
break;
case 85:
return "Light Snow Showers";
break;
case 86:
return "Snow Showers";
break;
case 95:
return "Thunderstorm";
break;
case 96:
return "Light Thunderstorms With Hail";
break;
case 99:
return "Thunderstorm With Hail";
break;
default:
return "Unkown";
break;
}
}
void callAPI(){
// Send request
http.useHTTP10(true);
// https://open-meteo.com/en/docs?hourly=&timezone=Europe%2FBerlin&latitude=55.6759&longitude=12.5655¤t=is_day,temperature_2m,wind_speed_10m&forecast_days=1&daily=weather_code
http.begin(client, "http://api.open-meteo.com/v1/forecast?latitude=55.6759&longitude=12.5655&daily=weather_code¤t=is_day,temperature_2m,wind_speed_10m&timezone=Europe%2FBerlin&forecast_days=1");
http.GET();
// Parse response
DynamicJsonDocument weather(2048);
DeserializationError error = deserializeJson(weather, http.getStream());
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
}
// Temperature
String temperature = weather["current"]["temperature_2m"].as<String>();
int tempDotIndex = temperature.indexOf('.');
if (tempDotIndex > -1) {
temperature = temperature.substring(0, tempDotIndex); // Take the part before the decimal point
}
weatherText = temperature;
weatherText += "C ";
// Weather description
int weather_code = weather["daily"]["weather_code"][0];
bool weather_is_day = weather["current"]["is_day"].as<bool>();
weatherText += weatherDescription(weather_code, weather_is_day) + " ";
// Wind speed
String windSpeed = weather["current"]["wind_speed_10m"].as<String>();
int windDotIndex = windSpeed.indexOf('.');
if (windDotIndex > -1) {
windSpeed = windSpeed.substring(0, windDotIndex); // Take the part before the decimal point
}
weatherText += windSpeed;
weatherText += "km/h";
// Disconnect
http.end();
}
void setup() {
Serial.begin(9600);
Serial.write(0x0C); // Clear display
Serial.print("VFD display\n");
Serial.print("Starting\n");
WiFi.setHostname(HOSTNAME);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.write(0x0C); // Clear display
Serial.printf("Hostname: %s\n", HOSTNAME);
Serial.printf("Connecting to: %s\n", WIFI_SSID);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print('.');
}
Serial.write(0x0C); // Clear display
Serial.println("Connected, IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
waitForSync();
CopenhagenTime.setLocation("Europe/Copenhagen");
Serial.write(0x0C); // Clear display
callAPI();
}
void loop() {
String outputText = "";
if (millis() - aPIPreviousMillis >= aPIInterval) {
aPIPreviousMillis = millis();
callAPI();
}
outputText = weatherText + "\n\r";
// Get Unix epoch timestamp
/*char buffer [3];
sprintf(buffer,"%03d",ms());
String milliEpoch = String(now()) + buffer;
String truncatedEpoch = milliEpoch.substring(0, 10);
Serial.print(truncatedEpoch);
Serial.print(" ");*/
// https://github.com/ropg/ezTime#getting-date-and-time
// 16:36:15 Sat 16 Aug
String time_formatted = CopenhagenTime.dateTime("H:i:s D d M");
outputText += time_formatted;
if (outputText != previousOutputText)
{
Serial.println(outputText);
previousOutputText = outputText;
}
events(); // Run NTP
}
|