-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyWeatherProject.ino
More file actions
420 lines (328 loc) · 11.6 KB
/
myWeatherProject.ino
File metadata and controls
420 lines (328 loc) · 11.6 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Stephanie Wyckoff
* CSc 4110 - Embedded Systems
* Final Project - Running Clothes Barometer
* Due: 24 Apr 2018
*
* Description: This project sources real time weather data from OpenWeatherMap.org via an API key, is translated into
* a useable format via the JSON parser, and that information is relayed to the user via lit up LEDs and LCD display.
* The bulk of this code came from the ESP32 Weather Station Project v1.00, which can be found at
* http://educ8s.tv/esp32-weather-station. I hava annotated which parts were wholy my own creation, which parts were
* modified from the oritginal code and which parts are wholy the original code.
*
* Input: OpenWeatherMap.org weather information
*
* Output: Light up correct LED which is associated with the current weather (rain, wind, sun/clear, clouds) and also
* update the LCD display from its default "Wifi Connecting..." to the coresponding image of running clothes or
* incliment weather condition.
*
*/
/*BEGIN ALL CODE*/
/*BEGIN CODE MODIFIED BY ME*/
#define ARDUINOJSON_ENABLE_PROGMEM 0
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <WiFi.h>
/*YOU WILL NOTICE THAT THE SSID, PASSWORD AND APIKEY HAVE BEEN REDACTED FOR SECURITY PURPOSES*/
const char* ssid = "****INFORMATION REDACTED****";
const char* password = "****INFORMATION REDACTED****";
String CityID = "4225309"; //suwanee, Ga, USA
String APIKEY = "****INFORMATION REDACTED****";
/*END CODE MODIFIED BY ME*/
/*BEGIN CODE BY ME*/
#define red 14
#define white 27
#define yellow 26
#define green 25
#define pink 33
#define blue 32
float temperature = 0;
int weatherID = 0;
/*END CODE BY ME*/
/*BEGIN CODE BY EDUC8S.TV*/
WiFiClient client;
const char* servername ="api.openweathermap.org"; // remote server we will connect to
String result;
int iterations = 1800;
/*END CODE BY EDUC8S.TV*/
void setup()
{
/*BEGIN CODE BY ME*/
//this is to make the LEDs light up
pinMode(red, OUTPUT); //sun
pinMode(yellow, OUTPUT); //rain
pinMode(green, OUTPUT); //cloudy
pinMode(blue, OUTPUT); //windy
pinMode(white, OUTPUT); //incliment weather
pinMode(pink, OUTPUT); //Jacket
/*END CODE BY ME*/
/*BEGIN CODE BY EDUC8S.TV*/
Serial.begin(9600);
connectToWifi();
/*END CODE BY EDUC8S.TV*/
}
/*ALL OF LOOP WRITTEN BY EDUC8S.TV*/
void loop() {
delay(2000);
if(iterations == 1800)//We check for updated weather forecast once every hour
{
getWeatherData();
iterations = 0;
}
iterations++;
}
/*CONNECT_TO_WIFI() WRITTEN BY EDUC8S.TV*/
void connectToWifi()
{
WiFi.enableSTA(true);
delay(2000);
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("Connecting to WiFi...\n");
}
Serial.print("connected to wifi\n"); //ADDED BY ME
}
/*BULK OF GET_WEATHER_DATA() WRITTEN BY EDUC8S.TV... THIS SECTION OF CODE CONTAINS THE JSON PARSER CALLS WHICH HAVE STOPPED
* WORKING FOR MY CODE, AS SUCH I ONLY HAVE 0 OR NULL VALUES AT THE END.
*/
void getWeatherData() //client function to send/receive GET request data.
{
String result ="";
WiFiClient client;
const int httpPort = 80;
if (!client.connect(servername, httpPort)) {
return;
}
// We now create a URI for the request
String url = "/data/2.5/weather?zip=30024,us&units=imperial&cnt=1&APPID="+APIKEY; //METRIC CHANGED TO IMPERIAL BY ME
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + servername + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
client.stop();
return;
}
}
// Read all the lines of the reply from server
while(client.available()) {
result = client.readStringUntil('\r');
}
result.replace('[', ' ');
result.replace(']', ' ');
char jsonArray [result.length()+1];
result.toCharArray(jsonArray,sizeof(jsonArray));
jsonArray[result.length() + 1] = '\0';
StaticJsonBuffer<1024> json_buf;
JsonObject &root = json_buf.parseObject(jsonArray);
if (!root.success())
{
Serial.println("parseObject() failed");
}
/*BEGIN CODE HEAVILY MODIFIED OR CREATED ENTIRELY BY ME*/
//this gives us the current temperature outside
String idTemp = root["main"]["temp"];
//Serial.println("String temp is " + idTemp + "\n");
float temp = idTemp.toFloat();
Serial.print("\nThe current temperature is: ");
Serial.println(temp);
//this gives us the weatherID so we know what to display
String idString = root["weather"]["id"];
weatherID = idString.toInt();
Serial.print("\nThe weather code is: ");
Serial.println(idString);
weatherLED(weatherID);
getClothes(temp);
wearJacket(temp, weatherID);
/*END CODE HEAVILY MODIFIED OR CREATED ENTIRELY BY ME*/
endNextionCommand(); //We need that in order the nextion to recognise the first command after the serial print
}
/*BEGIN CODE BY ME*/
/*WEATHER_LED() IS WHOLY MY OWN CREATION*/
void weatherLED(int weather_condition){
if(weather_condition == 800){
//light up sun LED... also clear sky
//if temp < 60, light up jacket led
digitalWrite(red, HIGH);
Serial.println("\nThe LED is RED\n");
Serial.println("\nThe weather is clear ");
}
else if(weather_condition >= 801 && weather_condition <= 804){
//light up clouds LED
//if temp < 60, light up jacket led
digitalWrite(yellow, HIGH);
Serial.println("\nThe LED is YELLOW\n");
Serial.println("\nThe weather is cloudy");
}
else if((weather_condition >= 300 && weather_condition <= 321) || (weather_condition >= 500 && weather_condition <= 503) || (weather_condition >= 520 && weather_condition <= 531)){
//light up rain LED
digitalWrite(green, HIGH);
Serial.println("\nThe LED is GREEN\n");
Serial.println("\nThe weather is rainy");
}
else if(weather_condition >= 952 && weather_condition <= 956){
//light up wind LED
digitalWrite(blue, HIGH);
Serial.println("\nThe LED is BLUE\n");
Serial.println("\nThe weather is windy");
}
else{//incliment weather assumed
digitalWrite(white, HIGH);
Serial.println("\nThe LED is WHITE\n");
Serial.println("\nThe weather is incliment");
incliment_weather(weather_condition);
}
}
/*INCLIMENT_WEATHER() IS WHOLY MY OWN CREATION*/
void incliment_weather(int weather_condition){
if(weather_condition == 900){
//display torando
sendTornadoToNextion();
}
else if(weather_condition == 901 || weather_condition == 902){
//display hurricane
sendHurricaneToNextion();
}
else if(weather_condition >= 957 && weather_condition <= 959){
//extremem wind
sendExtremeWindToNextion();
}
else if(weather_condition== 906){
//hail
sendHailToNextion();
}
else if((weather_condition == 504 || weather_condition == 511) || (weather_condition == 960 || weather_condition == 961)){
//extreme rain
sendExtremeRainToNextion();
}
else if((weather_condition >= 200 && weather_condition <= 232) || (weather_condition >= 960 && weather_condition <= 961)){
//thunder storm
sendThunderStormToNextion();
}
else if(weather_condition >= 600 && weather_condition <= 622){
//display snow
sendSnowToNextion();
}
}
/*GET_CLOTHES() IS WHOLY MY OWN CREATION*/
void getClothes(int temp){
Serial.print("\nBecause the temperature is: ");
Serial.print(temp);
Serial.println(" I should wear...");
if(temp <= 32){
Serial.println("Flannel Jammies with Feet, its too cold to run today!");
sendStayInsideToNextion();
}
else if(temp > 32 && temp <= 40){
//show hat, long sleeves, pants
Serial.println("... a hat, long sleeves, and pants.\n");
sendHatLongSleevesPantsToNextion();
}
else if(temp > 40 && temp <= 60){
//show long sleeves, shorts
Serial.println("... long sleeves, and shorts.\n");
sendLongSleevesShortsToNextion();
}
else if(temp > 60 && temp <= 80){
//show short sleeves, shorts
Serial.println("... short sleeves, and shorts.\n");
sendShortSleevesShortsToNextion();
}
else if(temp > 80){
//show sleeveless, shorts
Serial.println("... a sleeveless top, and shorts.\n");
sendSleevelessShortsToNextion();
}
}
/*WEAR_JACKET() IS WHOLY MY OWN CREATION*/
void wearJacket(float temp, int weather_condition){
//jacket LED lights up when its raining w/o thunder/lightning, and when its windy
if((weather_condition >= 300 && weather_condition <= 321) || /* drizzle */
(weather_condition >= 500 && weather_condition <= 503) || /* rain w/o lightning */
(weather_condition >= 520 && weather_condition <= 521) || /* more rain w/o lightning */
(weather_condition >= 952 && weather_condition <= 956)){ /* wind */
if(temp <= 60 && temp >= 32){
digitalWrite(pink, HIGH);
Serial.println("\nlight up pink jacket led\n");
}
} Serial.println("\n\nDONT light up pink jacket led\n");
}
/*END CODE BY ME*/
/*THE FOLLOWING CODE WAS MODIFIED FROM THE ORIGINAL TO USE WITH MY OWN IMAGES... THIS IS
* EXACTLY HOW THE CODE IS FORMATED IN THE ORIGINAL, BUT FOR SOME REASON IT DOES NOT WORK
* FOR MY CODE. I BELIEVE THIS TO BE A BAD CONNECTION IN THE HARDWARE AS THE SERIAL MONITOR
* OUTPUTS THE PROPER INFORMATION.
*/
//incliment weather diplays
void sendTornadoToNextion(){
String command = "weatherIcon.pic=5";
Serial.print(command);
endNextionCommand();
}
void sendHurricaneToNextion(){
String command = "weatherIcon.pic=4";
Serial.print(command);
endNextionCommand();
}
void sendExtremeWindToNextion(){
String command = "weatherIcon.pic=3";
Serial.print(command);
endNextionCommand();
}
void sendHailToNextion(){
String command = "weatherIcon.pic=8";
Serial.print(command);
endNextionCommand();
}
void sendExtremeRainToNextion(){
String command = "weatherIcon.pic=2";
Serial.print(command);
endNextionCommand();
}
void sendThunderStormToNextion(){
String command = "weatherIcon.pic=1";
Serial.print(command);
endNextionCommand();
}
void sendSnowToNextion(){
String command = "weatherIcon.pic=0";
Serial.print(command);
endNextionCommand();
}
//stay inside its too cold display
void sendStayInsideToNextion(){
String command = "weatherIcon.pic=9";
Serial.print(command);
endNextionCommand();
}
//begin clothing displays
void sendHatLongSleevesPantsToNextion(){
String command = "weatherIcon.pic=10";
Serial.print(command);
endNextionCommand();
}
void sendLongSleevesShortsToNextion(){
String command = "weatherIcon.pic=11";
Serial.print(command);
endNextionCommand();
}
void sendShortSleevesShortsToNextion(){
String command = "weatherIcon.pic=12";
Serial.print(command);
endNextionCommand();
}
void sendSleevelessShortsToNextion(){
String command = "weatherIcon.pic=13";
Serial.print(command);
endNextionCommand();
}
//end nextion commands
void endNextionCommand(){
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
/*END ALL CODE*/