· 5 years ago · Nov 02, 2020, 04:22 AM
1#include <ArduinoJson.h>
2#include <SPI.h>
3#include <WiFiNINA.h>
4#include "arduino_secrets.h"
5
6
7
8char ssid[] = SECRET_SSID; // your network SSID (name)
9char pass[] = SECRET_PSW;// your network PASSWORD ()
10
11
12
13//open weather map api key
14String apiKey = SECRET_APIKEY;
15
16
17
18//the city you want the weather for
19String location = "brooklyn,US";
20
21
22
23int status = WL_IDLE_STATUS;
24char server_name[] = "api.openweathermap.org";
25
26
27
28WiFiClient client;
29
30
31
32
33void setup() {
34 //Initialize serial and wait for port to open:
35 Serial.begin(9600);
36
37
38
39 // attempt to connect to Wifi network:
40 while (status != WL_CONNECTED) {
41 Serial.print("Attempting to connect to SSID: ");
42 Serial.println(ssid);
43
44
45
46 //use the line below if your network is protected by wpa password
47 status = WiFi.begin(ssid, pass);
48
49
50
51 // wait 10 seconds for connection:
52 delay(10000);
53 Serial.println(status);
54 }
55 Serial.println("Connected to wifi");
56}
57
58
59
60void loop() {
61 getWeather();
62 delay(10000);
63}
64
65
66
67void getWeather() {
68
69
70
71 Serial.println("\nStarting connection to server...");
72 // if you get a connection, report back via serial:
73 if (client.connect(server_name, 80)) {
74 Serial.println("connected to server");
75 // Make a HTTP request:
76 client.print("GET /data/2.5/forecast?");
77 client.print("q=" + location);
78 client.print("&appid=" + apiKey);
79 client.print("&cnt=5");
80 client.println("&units=imperial");
81 client.println("Host: api.openweathermap.org");
82 client.println("Connection: close");
83 client.println();
84 } else {
85 Serial.println("unable to connect");
86 }
87
88
89
90 delay(1000);
91 String line = "";
92
93
94
95 while (client.connected()) {
96 line = client.readStringUntil('\n');
97
98
99
100 Serial.print("parsingValues: " );
101 Serial.println(line);
102
103
104
105 //create a json buffer where to store the json data
106 DynamicJsonDocument doc(5000);
107 DeserializationError error = deserializeJson(doc, line);
108 if (error) {
109 return;
110 }
111
112
113
114 //get the data from the json tree
115 String cityName = doc["city"]["name"];
116 String pop0 = doc["city"]["population"];
117
118 String city0 = doc["list"][0]["dt_txt"];
119 String feelsLike0 = doc["list"][0]["main"]["feels_like"];
120
121 String city1 = doc["list"][1]["dt_txt"];
122 String feelsLike1 = doc["list"][1]["main"]["feels_like"];
123
124 String city2 = doc["list"][2]["dt_txt"];
125 String feelsLike2 = doc["list"][2]["main"]["feels_like"];
126
127 String city3 = doc["list"][3]["dt_txt"];
128 String feelsLike3 = doc["list"][3]["main"]["feels_like"];
129
130 String city4 = doc["list"][4]["dt_txt"];
131 String feelsLike4 = doc["list"][4]["main"]["feels_like"];
132
133
134
135 // Print values.
136 Serial.print("city: ");
137 Serial.println (cityName);
138 Serial.print("population: ");
139 Serial.println(pop0);
140
141
142 Serial.print("it will be ");
143 Serial.print(feelsLike0);
144 Serial.print(" degrees on ");
145 Serial.println(city0);
146
147 Serial.print("it will be ");
148 Serial.print(feelsLike1);
149 Serial.print(" degrees on ");
150 Serial.println(city1);
151
152 Serial.print("it will be ");
153 Serial.print(feelsLike2);
154 Serial.print(" degrees on ");
155 Serial.println(city2);
156
157 Serial.print("it will be ");
158 Serial.print(feelsLike3);
159 Serial.print(" degrees on ");
160 Serial.println(city3);
161
162 Serial.print("it will be ");
163 Serial.print(feelsLike4);
164 Serial.print(" degrees on ");
165 Serial.println(city4);
166
167
168 int result = 0;
169 int average = 0;
170 result = feelsLike0.toInt() + feelsLike1.toInt() + feelsLike2.toInt() + feelsLike3.toInt() + feelsLike4.toInt();
171 average = result / 5;
172
173
174 if (average > 45) {
175 Serial.println("Let's go to the beach!");
176 }
177 else {
178 Serial.println("Wear a coat.");
179 }
180 }
181}