· 6 years ago · Mar 29, 2020, 08:24 PM
1#include <ArduinoJson.h>
2#include <SPI.h>
3#include <WiFiNINA.h>
4#include "arduino_secrets.h"
5
6
7char ssid[] = SECRET_SSID; // your network SSID (name)
8char pass[] = SECRET_PASS;// your network PASSWORD ()
9
10//open weather map api key
11String apiKey = SECRET_APIKEY;
12
13
14String location = "philadelphia,US";
15
16int status = WL_IDLE_STATUS;
17char server_name[] = "api.openweathermap.org";
18
19WiFiClient client;
20
21
22void setup() {
23 //Initialize serial and wait for port to open:
24 Serial.begin(9600);
25
26 // attempt to connect to Wifi network:
27 while (status != WL_CONNECTED) {
28 Serial.print("Attempting to connect to SSID: ");
29 Serial.println(ssid);
30
31 //use the line below if your network is protected by wpa password
32 status = WiFi.begin(ssid, pass);
33
34 // wait 10 seconds for connection:
35 delay(10000);
36 Serial.println(status);
37 }
38 Serial.println("Connected to wifi");
39}
40
41void loop() {
42 getWeather();
43 delay(10000);
44}
45
46
47void getWeather() {
48
49 Serial.println("\nStarting connection to server...");
50 // if you get a connection, report back via serial:
51 if (client.connect(server_name, 80)) {
52 Serial.println("connected to server");
53 // Make a HTTP request:
54 client.print("GET /data/2.5/forecast?");
55 client.print("q=" + location);
56 client.print("&appid=" + apiKey);
57 client.print("&cnt=5");
58 client.println("&units=imperial");
59 client.println("Host: api.openweathermap.org");
60 client.println("Connection: close");
61 client.println();
62 } else {
63 Serial.println("unable to connect");
64 }
65
66 delay(1000);
67 String line = "";
68
69 while (client.connected()) {
70 line = client.readStringUntil('\n');
71
72 //Serial.println(line);
73 Serial.print("parsingValues: ");
74 Serial.println(line);
75
76 //create a json buffer where to store the json data
77 DynamicJsonDocument doc(5000);
78 DeserializationError error = deserializeJson(doc, line);
79 if (error){
80 return;
81 }
82
83 //get the data from the json tree
84 String cityName = doc["city"]["name"];
85 String cityPop = doc["city"]["population"];
86
87 String nextWeatherTime0 = doc["list"][0]["dt_txt"];
88 String nextWeather0 = doc["list"][0]["main"]["feels_like"];
89
90 String nextWeatherTime1 = doc["list"][1]["dt_txt"];
91 String nextWeather1 = doc["list"][1]["main"]["feels_like"];
92
93 String nextWeatherTime2 = doc["list"][2]["dt_txt"];
94 String nextWeather2 = doc["list"][2]["main"]["feels_like"];
95
96 String nextWeatherTime3 = doc["list"][3]["dt_txt"];
97 String nextWeather3 = doc["list"][3]["main"]["feels_like"];
98
99 String nextWeatherTime4 = doc["list"][4]["dt_txt"];
100 String nextWeather4 = doc["list"][4]["main"]["feels_like"];
101
102 String cityLat= doc["city"]["coord"]["lat"];
103
104 double temp0 = 0;
105 nextWeather0.toDouble(temp0);
106
107 double temp1 = 0;
108 nextWeather1.toDouble(temp1);
109
110 double temp2 = 0;
111 nextWeather2.toDouble(temp2);
112
113 double temp3 = 0;
114 nextWeather3.toDouble(temp3);
115
116 double temp4 = 0;
117 nextWeather4.toDouble(temp4);
118
119 double avg = (temp0 + temp1 + temp2 + temp3 + temp4)/5
120
121 while (avg != -1) {
122 if avg > 45
123 Serial.println("Let's go to the beach!");
124
125 { else {
126 Serial.println("Wear a coat");
127 }
128 }
129
130 // Print values.
131 Serial.println("city: " + cityName);
132 Serial.println("population: " + cityPop);
133 Serial.println("it will be " + nextWeather0 + " degrees on " + nextWeatherTime0);
134 Serial.println("it will be " + nextWeather1 + " degrees on " + nextWeatherTime1);
135 Serial.println("it will be " + nextWeather2 + " degrees on " + nextWeatherTime2);
136 Serial.println("it will be " + nextWeather3 + " degrees on " + nextWeatherTime3);
137 Serial.println("it will be " + nextWeather4 + " degrees on " + nextWeatherTime4);
138
139
140 }
141}