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