· 6 years ago · Mar 29, 2020, 09:14 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 double nextWeather0 = doc["list"][0]["main"]["feels_like"];
89
90 String nextWeatherTime1 = doc["list"][1]["dt_txt"];
91 double nextWeather1 = doc["list"][1]["main"]["feels_like"];
92
93 String nextWeatherTime2 = doc["list"][2]["dt_txt"];
94 double nextWeather2 = doc["list"][2]["main"]["feels_like"];
95
96 String nextWeatherTime3 = doc["list"][3]["dt_txt"];
97 double nextWeather3 = doc["list"][3]["main"]["feels_like"];
98
99 String nextWeatherTime4 = doc["list"][4]["dt_txt"];
100 double nextWeather4 = doc["list"][4]["main"]["feels_like"];
101
102 String cityLat= doc["city"]["coord"]["lat"];
103
104
105 double avg = (nextWeather0 + nextWeather1 + nextWeather2 + nextWeather3 + nextWeather4) / 5;
106
107
108
109 // Print values.
110 Serial.println("city: " + cityName);
111 Serial.println("population: " + cityPop);
112 Serial.println(String("it will be ") + nextWeather0 + " degrees on " + nextWeatherTime0);
113 Serial.println(String("it will be ") + nextWeather1 + " degrees on " + nextWeatherTime1);
114 Serial.println(String("it will be ") + nextWeather2 + " degrees on " + nextWeatherTime2);
115 Serial.println(String("it will be ") + nextWeather3 + " degrees on " + nextWeatherTime3);
116 Serial.println(String("it will be ") + nextWeather4 + " degrees on " + nextWeatherTime4);
117
118 if (avg > 45) {
119 Serial.println("Let's go to the beach!");
120 } else {
121 Serial.println("Wear a coat");
122 }
123
124
125 }
126}