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