· 6 years ago · Mar 29, 2020, 04:22 AM
1//Justin Hill IST 402.001 SP20
2
3#include <SPI.h>
4#include <WiFiNINA.h>
5#include "arduino_secrets.h"
6#include <ArduinoJson.h>
7
8char ssid[] = SECRET_SSID; // your network SSID (name)
9char pass[] = SECRET_PASS;// your network PASSWORD ()
10
11//open weather map api key
12String apiKey= SECRET_APIKEY;
13
14//the city you want the weather for
15String location= "philadelphia,US";
16
17int status = WL_IDLE_STATUS;
18char server[] = "api.openweathermap.org";
19
20WiFiClient client;
21
22float addition;
23float result;
24
25void setup() {
26 //Initialize serial and wait for port to open:
27 Serial.begin(9600);
28 // attempt to connect to Wifi network:
29 while (status != WL_CONNECTED) {
30 Serial.print("Attempting to connect to SSID: ");
31 Serial.println(ssid);
32
33 status = WiFi.begin(ssid, pass);
34 //use the line below if your network is protected by wpa password
35 //status = WiFi.begin(ssid, pass);
36 // wait 10 seconds for connection:
37 delay(1000);
38 }
39 Serial.println("Connected to wifi");
40 printWifiStatus();
41}
42void loop() {
43 getWeather();
44 delay(60000);
45}
46void getWeather() {
47 Serial.println("\nStarting connection to server...");
48 // if you get a connection, report back via serial:
49 if (client.connect(server, 80)) {
50 Serial.println("connected to server");
51 // Make a HTTP request:
52 client.print("GET /data/2.5/forecast?");
53 client.print("q="+location);
54 client.print("&appid="+apiKey);
55 client.print("&cnt=5");
56 client.println("&units=metric");
57 client.println("Host: api.openweathermap.org");
58 client.println("Connection: close");
59 client.println();
60 } else {
61 Serial.println("unable to connect");
62 }
63 delay(1000);
64 String line = "";
65 while (client.connected()) {
66 line = client.readStringUntil('\n');
67 Serial.print("parsingValues: ");
68 Serial.println(line);
69
70 DynamicJsonDocument doc(5000);
71 DeserializationError error = deserializeJson(doc, line);
72 if (error){
73 return;
74 }
75 String nextWeatherTime0 = doc["list"][0]["dt_txt"];
76 String nextWeather0 = doc["list"][0]["weather" ][0]["main" ];
77
78 String nextWeatherTime1 = doc["list"][1]["dt_txt"];
79 String nextWeather1 = doc["list"][1]["weather" ][0]["main" ];
80
81 String nextWeatherTime2 = doc["list"][2]["dt_txt" ];
82 String nextWeather2 = doc["list"][2]["weather" ][0]["main" ];
83
84 String nextWeatherTime3 = doc["list"][3]["dt_txt" ];
85 String nextWeather3 = doc["list"][3]["weather" ][0]["main" ];
86
87 String nextWeatherTime4 = doc["list"][4]["dt_txt" ];
88 String nextWeather4 = doc["list"][4]["weather" ][0]["main" ];
89
90 String city = doc["city"]["name" ];
91 String population = doc["city"]["population"];
92 int feelsLike1 = doc["list"][0]["main"]["feels_like" ];
93 int feelsLike2 = doc["list"][1]["main"]["feels_like" ];
94 int feelsLike3 = doc["list"][2]["main"]["feels_like" ];
95 int feelsLike4 = doc["list"][3]["main"]["feels_like" ];
96 int feelsLike5 = doc["list"][4]["main"]["feels_like" ];
97
98 String dt_text1 = doc["list"][0]["dt_txt" ];
99 String dt_text2 = doc["list"][1]["dt_txt" ];
100 String dt_text3 = doc["list"][2]["dt_txt" ];
101 String dt_text4 = doc["list"][3]["dt_txt" ];
102 String dt_text5 = doc["list"][4]["dt_txt" ];
103
104 addition = feelsLike1 + feelsLike2 + feelsLike3 + feelsLike4 + feelsLike5;
105 result = addition / 5;
106
107 //Print Values
108 Serial.print("City: ");
109 Serial.println(city);
110 Serial.print("Population: ");
111 Serial.println(population);
112
113 Serial.print("It will be ");
114 Serial.print(feelsLike1);
115 Serial.print(" degrees on ");
116 Serial.println(dt_text1);
117
118 Serial.print("It will be ");
119 Serial.print(feelsLike2);
120 Serial.print(" degrees on ");
121 Serial.println(dt_text2);
122
123 Serial.print("It will be ");
124 Serial.print(feelsLike3);
125 Serial.print(" degrees on ");
126 Serial.println(dt_text3);
127
128 Serial.print("It will be ");
129 Serial.print(feelsLike4);
130 Serial.print(" degrees on ");
131 Serial.println(dt_text4);
132
133 Serial.print("It will be ");
134 Serial.print(feelsLike5);
135 Serial.print(" degrees on ");
136 Serial.println(dt_text5);
137
138 if (result <= 45){
139 Serial.print("Average temperature: ");
140 Serial.print(result);
141 Serial.print(" degrees.");
142 Serial.println(" Wear a coat!");
143 }
144 else{
145 Serial.print("Average temperature: ");
146 Serial.print(result);
147 Serial.print(" degrees.");
148 Serial.println(" Let's go to the beach!");
149 }
150 }
151}
152
153void printWifiStatus() {
154 // print the SSID of the network you're attached to:
155 Serial.print("SSID: ");
156 Serial.println(WiFi.SSID());
157
158 // print your board's IP address:
159 IPAddress ip = WiFi.localIP();
160 Serial.print("IP Address: ");
161 Serial.println(ip);
162
163 // print the received signal strength:
164 long rssi = WiFi.RSSI();
165 Serial.print("signal strength (RSSI):");
166 Serial.print(rssi);
167 Serial.println(" dBm");
168 // print where to go in a browser:
169 Serial.print("To see this page in action, open a browser to http://");
170 Serial.println(ip);
171}