· 6 years ago · Mar 29, 2020, 05:18 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
21void setup() {
22 //Initialize serial and wait for port to open:
23 Serial.begin(9600);
24
25 // attempt to connect to Wifi network:
26 while (status != WL_CONNECTED) {
27 Serial.print("Attempting to connect to SSID: ");
28 Serial.println(ssid);
29
30 //use the line below if your network is protected by wpa password
31 status = WiFi.begin(ssid, pass);
32
33 // wait 10 seconds for connection:
34 delay(10000);
35 Serial.println(status);
36 }
37 Serial.println("Connected to wifi");
38}
39
40void loop() {
41 getWeather();
42 delay(10000);
43}
44
45
46void getWeather() {
47
48 Serial.println("\nStarting connection to server...");
49 // if you get a connection, report back via serial:
50 if (client.connect(server_name, 80)) {
51 Serial.println("connected to server");
52 // Make a HTTP request:
53 client.print("GET /data/2.5/forecast?");
54 client.print("q=" + location);
55 client.print("&appid=" + apiKey);
56 // number of forcast
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 City Name
84 String city = doc["city"]["name"];
85 // Get City Population
86 String population = doc["city"]["population"];
87
88 // Get FeelsLike from each forcast
89 String feelLike0 = doc["list"][0]["main"]["feels_like"];
90 String feelLike1 = doc["list"][1]["main"]["feels_like"];
91 String feelLike2 = doc["list"][2]["main"]["feels_like"];
92 String feelLike3 = doc["list"][3]["main"]["feels_like"];
93 String feelLike4 = doc["list"][4]["main"]["feels_like"];
94
95 // Get DateTime from each forcast
96 String dateTime0 = doc["list"][0]["dt_txt"];
97 String dateTime1 = doc["list"][1]["dt_txt"];
98 String dateTime2 = doc["list"][2]["dt_txt"];
99 String dateTime3 = doc["list"][3]["dt_txt"];
100 String dateTime4 = doc["list"][4]["dt_txt"];
101
102 // Get Temperature from each forcast
103 String temp0 = doc["list"][0]["main"]["temp"];
104 String temp1 = doc["list"][1]["main"]["temp"];
105 String temp2 = doc["list"][2]["main"]["temp"];
106 String temp3 = doc["list"][3]["main"]["temp"];
107 String temp4 = doc["list"][4]["main"]["temp"];
108
109 // Average Temp from the 5 temps
110 float avg = (temp0.toFloat() + temp1.toFloat() + temp2.toFloat() + temp3.toFloat() + temp4.toFloat()) / 5;
111
112 // Printing values
113 Serial.println("city: " + city);
114 Serial.println("population: " + population);
115 Serial.println("it will be " + feelLike0 + " degrees on " + dateTime0);
116 Serial.println("it will be " + feelLike1 + " degrees on " + dateTime1);
117 Serial.println("it will be " + feelLike2 + " degrees on " + dateTime2);
118 Serial.println("it will be " + feelLike3 + " degrees on " + dateTime3);
119 Serial.println("it will be " + feelLike4 + " degrees on " + dateTime4);
120
121 // Printing what you should wear
122 if (avg > 45){
123 Serial.println("Let's go to the beach");
124 } else {
125 Serial.println("Wear a coat");
126 }
127
128 }
129}