· 5 years ago · Nov 02, 2020, 02:22 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 = "philadelphia,US";
14
15int status = WL_IDLE_STATUS;
16char server_name[] = "api.openweathermap.org";
17
18WiFiClient client;
19
20
21int average;
22
23void setup() {
24 //Initialize serial and wait for port to open:
25 Serial.begin(9600);
26
27 // attempt to connect to Wifi network:
28 while (status != WL_CONNECTED) {
29 Serial.print("Attempting to connect to SSID: ");
30 Serial.println(ssid);
31
32 //use the line below if your network is protected by wpa password
33 status = WiFi.begin(ssid, pass);
34
35 // wait 10 seconds for connection:
36 delay(10000);
37 Serial.println(status);
38 }
39 Serial.println("Connected to wifi");
40}
41
42void loop() {
43 getWeather();
44 delay(10000);
45}
46
47
48void getWeather() {
49
50 Serial.println("\nStarting connection to server...");
51 // if you get a connection, report back via serial:
52 if (client.connect(server_name, 80)) {
53 Serial.println("connected to server");
54 // Make a HTTP request:
55 client.print("GET /data/2.5/forecast?");
56 client.print("q=" + location);
57 client.print("&appid=" + apiKey);
58 client.print("&cnt=5");
59 client.println("&units=imperial");
60 client.println("Host: api.openweathermap.org");
61 client.println("Connection: close");
62 client.println();
63 } else {
64 Serial.println("unable to connect");
65 }
66
67 delay(1000);
68 String line = "";
69
70 while (client.connected()) {
71 line = client.readStringUntil('\n');
72
73 //Serial.println(line);
74 Serial.print("parsingValues: ");
75 Serial.println(line);
76
77 //create a json buffer where to store the json data
78 DynamicJsonDocument doc(5000);
79 DeserializationError error = deserializeJson(doc, line);
80 if (error){
81 return;
82 }
83
84 //get the data from the json tree
85 String cityName = doc["city"]["name"];
86 String cityPop = doc["city"]["population"];
87
88 String feels_like0 = doc["list"][0]["main"]["feels_like"];
89 String dt_txt0 = doc["list"][0]["dt_txt"];
90
91 String feels_like1 = doc["list"][1]["main"]["feels_like"];
92 String dt_txt1 = doc["list"][1]["dt_txt"];
93
94 String feels_like2 = doc["list"][2]["main"]["feels_like"];
95 String dt_txt2 = doc["list"][2]["dt_txt"];
96
97 String feels_like3 = doc["list"][3]["main"]["feels_like"];
98 String dt_txt3 = doc["list"][3]["dt_txt"];
99
100 String feels_like4 = doc["list"][4]["main"]["feels_like"];
101 String dt_txt4 = doc["list"][4]["dt_txt"];
102
103 average = (feels_like0.toInt() + feels_like1.toInt() + feels_like2.toInt() + feels_like3.toInt() + feels_like4.toInt()) / 5;
104
105 // Print values.
106 Serial.println("City: " + cityName);
107 Serial.println("Population: " + cityPop);
108 Serial.println("it will be " + feels_like0 + " degrees on " + dt_txt0);
109 Serial.println("it will be " + feels_like1 + " degrees on " + dt_txt1);
110 Serial.println("it will be " + feels_like2 + " degrees on " + dt_txt2);
111 Serial.println("it will be " + feels_like3 + " degrees on " + dt_txt3);
112 Serial.println("it will be " + feels_like4 + " degrees on " + dt_txt4);
113 if (average <= 45) {
114 Serial.println("Wear a coat");
115 } else {
116 Serial.println("Let's go to the beach");
117 }
118
119
120
121 }
122}