· 6 years ago · Mar 29, 2020, 11:12 PM
1/*
2 * Modify Client functionality of weather program to print weather info into the serial monitor
3 * Show: name of city, population, and next 5 "feels-like" forecasts with date-times for city of choice
4 * Calculate average of the 5 temps
5 * If average is < 45 degrees, print "Wear a Coat"
6 * If average is > 45, print "Let's go to the Beach"
7 * 1 extra point if user can enter cite/country through serial monitor
8 *
9 */
10
11#include <ArduinoJson.h>
12#include <SPI.h>
13#include <WiFiNINA.h>
14#include "arduino_secrets.h"
15
16char ssid[] = SECRET_SSID; // your network SSID (name)
17char pass[] = SECRET_PASS;// your network PASSWORD () CHANGED to 'PASS' FROM 'PSW'
18
19//open weather map api key
20String apiKey = SECRET_APIKEY;
21
22//the city you want the weather for
23String location = "Fort Myers,US";
24
25int status = WL_IDLE_STATUS;
26char server_name[] = "api.openweathermap.org";
27
28WiFiClient client;
29
30
31void setup() {
32 //Initialize serial and wait for port to open:
33 Serial.begin(9600);
34
35 // attempt to connect to Wifi network:
36 while (status != WL_CONNECTED) {
37 Serial.print("Attempting to connect to SSID: ");
38 Serial.println(ssid);
39
40 //use the line below if your network is protected by wpa password
41 status = WiFi.begin(ssid, pass);
42
43 // wait 10 seconds for connection:
44 delay(10000);
45 Serial.println(status);
46 }
47 Serial.println("Connected to wifi");
48}
49
50void loop() {
51 getWeather();
52 delay(10000);
53}
54
55
56void getWeather() {
57
58 Serial.println("\nStarting connection to server...");
59 // if you get a connection, report back via serial:
60 if (client.connect(server_name, 80)) { //port 80 used
61 Serial.println("connected to server");
62
63 // Make a HTTP request:
64 client.print("GET /data/2.5/forecast?"); //GET Request
65 client.print("q=" + location);
66 client.print("&appid=" + apiKey); //where we send individual API Key
67
68 //CHANGE: add account 5 - we want 5 weather predictions
69 client.print("&cnt=5");
70 client.println("&units=imperial"); //set units to emperial
71 client.println("Host: api.openweathermap.org"); //this is who host should be
72 client.println("Connection: close"); //close the connection
73 client.println();
74 } else {
75 Serial.println("unable to connect");
76 }
77
78 delay(1000); //once GET request put in, wait 1 second
79 String line = "";
80
81 while (client.connected()) { //read API completely
82 line = client.readStringUntil('\n'); //line is the response from API after Get Call
83
84 //Serial.println(line);
85 Serial.print("parsingValues: "); //begin parsin values
86 Serial.println(line); //print raw document
87
88 //create a json buffer where to store the json data
89 DynamicJsonDocument doc(5000); //up to 5000 characters in size
90 DeserializationError error = deserializeJson(doc, line); //if error, store in doc variable
91 if (error){
92 return;
93 }
94
95
96 //get the data from the json tree - parsing json tree - cascading through json tree
97
98 //look through doc to find parsed info and key with value 'list'; while in list, find object under index 0; last request looking for 'dt_txt' (date/time) - successfully parsed out dt_txt
99
100 //looking for main in weather key
101 //here is where you can find specific location weather data
102 String cityName = doc["city"]["name"];
103 //add city population
104 String population = doc["city"]["population"];
105
106 //weather #0
107 String nextWeather0 = doc["list"][0]["main"]["feels_like"];
108 String nextWeatherTime0 = doc["list"][0]["dt_txt"];
109
110 //weather #1
111 String nextWeather1 = doc["list"][1]["main"]["feels_like"];
112 String nextWeatherTime1 = doc["list"][1]["dt_txt"];
113
114 //weather #2
115 String nextWeather2 = doc["list"][2]["main"]["feels_like"];
116 String nextWeatherTime2 = doc["list"][2]["dt_txt"];
117
118 //weather #3
119 String nextWeather3 = doc["list"][3]["main"]["feels_like"];
120 String nextWeatherTime3 = doc["list"][3]["dt_txt"];
121
122 //weather #4
123 String nextWeather4 = doc["list"][4]["main"]["feels_like"];
124 String nextWeatherTime4 = doc["list"][4]["dt_txt"];
125
126 //weather #5
127 // String nextWeather5 = doc["list"][5]["main"]["feels_like"];
128 // String nextWeatherTime5 = doc["list"][5]["dt_txt"];
129
130 // Print values
131
132 //CHANGE: print city name
133 Serial.println("City: " + cityName);
134 //print population of city
135 Serial.println("Population: " + population);
136
137 //ORIGINALLY --> Serial.println(nextWeather0);
138 //ORIGINALLY --> Serial.println(nextWeatherTime0);
139
140 //print "it will be [feels-like0] degrees on [dt_txt0]
141 Serial.println("It will be " + nextWeather0 + " degrees on " + nextWeatherTime0);
142 Serial.println("It will be " + nextWeather1 + " degrees on " + nextWeatherTime1);
143 Serial.println("It will be " + nextWeather2 + " degrees on " + nextWeatherTime2);
144 Serial.println("It will be " + nextWeather3 + " degrees on " + nextWeatherTime3);
145 Serial.println("It will be " + nextWeather4 + " degrees on " + nextWeatherTime4);
146
147 //average of weather predictions - must convert string to int
148 int weather0 = nextWeather0.toInt();
149 int weather1 = nextWeather1.toInt();
150 int weather2 = nextWeather2.toInt();
151 int weather3 = nextWeather3.toInt();
152 int weather4 = nextWeather4.toInt();
153
154 int total = weather0 + weather1 + weather2 + weather3 + weather4;
155 int count = 5;
156 int averageTemp = total / count;
157
158 if (averageTemp < 45.0) {
159 Serial.println("Wear a Coat.");
160 }
161 else {
162 Serial.println("Let's go to the Beach!");
163 }
164 }
165}