· 6 years ago · Mar 29, 2020, 06:34 PM
1/* Adam Bilal
2 Team 2
3 Programming Lab - Wifi Weather
4*/
5
6
7
8#include <ArduinoJson.h>
9#include <SPI.h>
10#include <WiFiNINA.h>
11#include "arduino_secrets.h"
12
13char ssid[] = SECRET_SSID; // your network SSID (name)
14char pass[] = SECRET_PSW;// your network PASSWORD ()
15
16//open weather map api key
17String apiKey = SECRET_APIKEY;
18
19//the city you want the weather for
20String location = "philadelphia,US";
21
22int status = WL_IDLE_STATUS;
23char server_name[] = "api.openweathermap.org";
24
25WiFiClient client;
26 int AddingALL;
27 int Avg;
28
29void setup() {
30 //Initialize serial and wait for port to open:
31 Serial.begin(9600);
32
33 // attempt to connect to Wifi network:
34 while (status != WL_CONNECTED) {
35 Serial.print("Attempting to connect to SSID: ");
36 Serial.println(ssid);
37
38 //use the line below if your network is protected by wpa password
39 status = WiFi.begin(ssid, pass);
40
41 // wait 10 seconds for connection:
42 delay(10000);
43 Serial.println(status);
44
45 }
46 Serial.println("Connected to wifi");
47}
48
49void loop() {
50 getWeather();
51 delay(10000);
52}
53
54
55void getWeather() {
56
57 Serial.println("\nStarting connection to server...");
58 // if you get a connection, report back via serial:
59 if (client.connect(server_name, 80)) {
60 Serial.println("connected to server");
61 // Make a HTTP request:
62 client.print("GET /data/2.5/forecast?");
63 client.print("q=" + location);
64 client.print("&appid=" + apiKey);
65 client.print("&cnt=5"); //5 now intesd of 3
66 client.println("&units=imperial");
67 client.println("Host: api.openweathermap.org");
68 client.println("Connection: close");
69 client.println();
70 } else {
71 Serial.println("unable to connect");
72 }
73
74 delay(1000);
75 String line = "";
76
77 while (client.connected()) {
78 line = client.readStringUntil('\n');
79
80 //Serial.println(line);
81 Serial.print("parsingValues: ");
82 Serial.println(line);
83
84 //create a json buffer where to store the json data
85 DynamicJsonDocument doc(5000);
86 DeserializationError error = deserializeJson(doc, line);
87 if (error){
88 return;
89 }
90
91 //get the data from the json tree
92 String ItWillFellLike0 = doc["list"][0]["main"]["feels_like"];
93 String nextWeatherTime0 = doc["list"][0]["dt_txt"];
94 String ItWillFellLike1 = doc["list"][1]["main"]["feels_like"];
95 String nextWeatherTime1 = doc["list"][1]["dt_txt"];
96 String ItWillFellLike2 = doc["list"][2]["main"]["feels_like"];
97 String nextWeatherTime2 = doc["list"][2]["dt_txt"];
98 String ItWillFellLike3 = doc["list"][3]["main"]["feels_like"];
99 String nextWeatherTime3 = doc["list"][3]["dt_txt"];
100 String ItWillFellLike4 = doc["list"][4]["main"]["feels_like"];
101 String nextWeatherTime4 = doc["list"][4]["dt_txt"];
102 String cityName= doc["city"]["name"];
103 String cityPo= doc["city"]["population"];
104 int temp1 = ItWillFellLike0.toInt();
105 int temp2 = ItWillFellLike1.toInt();
106 int temp3 = ItWillFellLike2.toInt();
107 int temp4 = ItWillFellLike3.toInt();
108 int temp5 = ItWillFellLike4.toInt();
109
110 int AddingALL = (temp1 + temp2 + temp3+ temp4 + temp5);
111 int Avg = (AddingALL /5);
112 // Print values.
113 Serial.println("it will feels like "+ ItWillFellLike0+ " degrees on " +nextWeatherTime0);
114 Serial.println("it will feels like "+ ItWillFellLike1 +" degrees on " +nextWeatherTime1);
115 Serial.println("it will feels like "+ ItWillFellLike2 +" degrees on " +nextWeatherTime2);
116 Serial.println("it will feels like "+ ItWillFellLike3 +" degrees on " +nextWeatherTime3);
117 Serial.println("it will feels like "+ ItWillFellLike4+ " degrees on " +nextWeatherTime4);
118
119 Serial.print("City Name: ");
120 Serial.println(cityName);
121 Serial.print("City population: ");
122 Serial.println(cityPo);
123 Serial.print("Average temperature is: ");
124 Serial.println(Avg);
125 if (Avg < 45 ){
126 Serial.println("Wear a coat");
127 }
128 else{
129 Serial.println("Lets go to the beach!");
130 }
131 }
132 }