· 5 years ago · Nov 04, 2020, 12:48 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 = "Pennsylvania, 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}
36void loop() {
37getWeather();
38delay(10000);
39}
40void getWeather() {
41
42Serial.println("\nStarting connection to server...");
43// if you get a connection, report back via serial:
44if (client.connect(server_name, 80)) {
45Serial.println("connected to server");
46// Make a HTTP request:
47client.print("GET /data/2.5/forecast?");
48client.print("q=" + location);
49client.print("&appid=" + apiKey);
50client.print("&cnt=5");
51client.println("&units=imperial");
52client.println("Host: api.openweathermap.org");
53client.println("Connection: close");
54client.println();
55} else {
56Serial.println("unable to connect");
57}
58delay(1000);
59String line = "";
60while (client.connected()) {
61line = client.readStringUntil('\n');
62
63Serial.print("parsingValues: " );
64Serial.println(line);
65
66//create a json buffer where to store the json data
67DynamicJsonDocument doc(5000);
68DeserializationError error = deserializeJson(doc, line);
69if (error) {
70return;
71}
72//get the data from the json tree
73String cityName = doc["city"]["name"];
74String pop0 = doc["city"]["population"];
75
76String city0 = doc["list"][0]["dt_txt"];
77String feelsLike0 = doc["list"][0]["main"]["feels_like"];
78
79String city1 = doc["list"][1]["dt_txt"];
80String feelsLike1 = doc["list"][1]["main"]["feels_like"];
81
82String city2 = doc["list"][2]["dt_txt"];
83String feelsLike2 = doc["list"][2]["main"]["feels_like"];
84
85String city3 = doc["list"][3]["dt_txt"];
86String feelsLike3 = doc["list"][3]["main"]["feels_like"];
87
88String city4 = doc["list"][4]["dt_txt"];
89String feelsLike4 = doc["list"][4]["main"]["feels_like"];
90
91// Print values.
92Serial.print("city: ");
93Serial.println (cityName);
94Serial.print("population: ");
95Serial.println(pop0);
96
97
98
99Serial.print("it will be ");
100Serial.print(feelsLike0);
101Serial.print(" degrees on ");
102Serial.println(city0);
103
104
105Serial.print("it will be ");
106Serial.print(feelsLike1);
107Serial.print(" degrees on ");
108Serial.println(city1);
109
110
111Serial.print("it will be ");
112Serial.print(feelsLike2);
113Serial.print(" degrees on ");
114Serial.println(city2);
115
116
117Serial.print("it will be ");
118Serial.print(feelsLike3);
119Serial.print(" degrees on ");
120Serial.println(city3);
121
122
123Serial.print("it will be ");
124Serial.print(feelsLike4);
125Serial.print(" degrees on ");
126Serial.println(city4);
127
128int result = 0;
129int average = 0;
130result = feelsLike0.toInt() + feelsLike1.toInt() + feelsLike2.toInt() + feelsLike3.toInt() + feelsLike4.toInt();
131average = result / 5;
132
133if (average > 45) {
134Serial.println("Let's go to the beach!");
135}
136else {
137Serial.println("Wear a coat.");
138}
139}
140}