· 6 years ago · Mar 30, 2020, 03: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";
14String population = "1.5 million";
15
16int status = WL_IDLE_STATUS;
17char server_name[] = "api.openweathermap.org";
18
19WiFiClient client;
20WiFiServer server(80);
21
22
23
24void setup() {
25 //Initialize serial and wait for port to open:
26 Serial.begin(9600);
27
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 //use the line below if your network is protected by wpa password
34 status = WiFi.begin(ssid, pass);
35
36 // wait 10 seconds for connection:
37 delay(10000);
38 Serial.println(status);
39 }
40 Serial.println("Connected to wifi");
41}
42
43void loop() {
44 getWeather();
45 delay(10000);
46 WiFiClient client = server.available(); // listen for incoming clients
47
48 if (client) { // if you get a client,
49 Serial.println("new client"); // print a message out the serial port
50 String currentLine = ""; // make a String to hold incoming data from the client
51 while (client.connected()) { // loop while the client's connected
52 if (client.available()) { // if there's bytes to read from the client,
53 char c = client.read(); // read a byte, then
54 Serial.write(c); // print it out the serial monitor
55 if (c == '\n') { // if the byte is a newline character
56
57 // if the current line is blank, you got two newline characters in a row.
58 // that's the end of the client HTTP request, so send a response:
59 if (currentLine.length() == 0) {
60 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
61 // and a content-type so the client knows what's coming, then a blank line:
62 client.println("HTTP/1.1 200 OK");
63 client.println("Content-type:text/html");
64 client.println();
65
66 // the content of the HTTP response follows the header:
67 client.print("Click <a href=\"/H\">here</a> turn the LED on pin 13 on<br>");
68 client.print("Click <a href=\"/L\">here</a> turn the LED on pin 13 off<br>");
69
70 // The HTTP response ends with another blank line:
71 client.println();
72 // break out of the while loop:
73 break;
74 } else { // if you got a newline, then clear currentLine:
75 currentLine = "";
76 }
77 } else if (c != '\r') { // if you got anything else but a carriage return character,
78 currentLine += c; // add it to the end of the currentLine
79 }
80
81 // Check to see if the client request was "GET /H" or "GET /L":
82 if (currentLine.endsWith("GET /H")) {
83 digitalWrite(13, HIGH); // GET /H turns the LED on
84 }
85 if (currentLine.endsWith("GET /L")) {
86 digitalWrite(13, LOW); // GET /L turns the LED off
87 }
88 }
89 }
90 // close the connection:
91 client.stop();
92 Serial.println("client disonnected");
93 }
94}
95
96
97void getWeather() {
98
99 Serial.println("\nStarting connection to server...");
100 // if you get a connection, report back via serial:
101 if (client.connect(server_name, 80)) {
102 Serial.println("connected to server");
103 // Make a HTTP request:
104 client.print("GET /data/2.5/forecast?");
105 client.print("q=" + location);
106 client.print("&appid=" + apiKey);
107 client.print("&cnt=3");
108 client.println("&units=imperial");
109 client.println("Host: api.openweathermap.org");
110 client.println("Connection: close");
111 client.println();
112
113 } else {
114 Serial.println("unable to connect");
115 }
116
117 delay(1000);
118 String line = "";
119
120 while (client.connected()) {
121 line = client.readStringUntil('\n');
122
123 //Serial.println(line);
124 Serial.print("parsingValues: ");
125 Serial.println(line);
126
127 //create a json buffer where to store the json data
128 DynamicJsonDocument doc(5000);
129 DeserializationError error = deserializeJson(doc, line);
130 if (error){
131 return;
132 }
133
134 //get the data from the json tree
135 String nextWeatherTime0 = doc["list"][0]["dt_txt"];
136 String nextWeather0 = doc["list"][0]["weather"][0]["main"];
137 //nextWeather0.toInt();
138
139 String nextWeatherTime1 = doc["list"][1]["dt_txt"];
140 String nextWeather1 = doc["list"][1]["weather"][0]["main"];
141 //nextWeather1.toInt();
142
143
144 String nextWeatherTime2 = doc["list"][2]["dt_txt"];
145 String nextWeather2 = doc["list"][2]["weather"][0]["main"];
146 //nextWeather2.toInt();
147
148 String nextWeatherTime3 = doc["list"][3]["dt_txt"];
149 String nextWeather3 = doc["list"][3]["weather"][0]["main"];
150 //nextWeather3.toInt();
151
152 String nextWeatherTime4 = doc["list"][4]["dt_txt"];
153 String nextWeather4 = doc["list"][4]["weather"][0]["main"];
154 //nextWeather4.toInt();
155
156 int temps[5] = {nextWeather0.toInt(), nextWeather1.toInt(), nextWeather2.toInt(), nextWeather3.toInt(), nextWeather4.toInt()};
157
158
159 String cityLat= doc["city"]["coord"]["lat"];
160
161 int average = 0;
162 for (int i=0; i < 5; i++) {
163 average = average + temps[i];
164 }
165 average = average/5;
166
167 // Print values
168
169 Serial.println("City: " + location);
170 Serial.println("Population: " + population);
171 Serial.println("It will be " + nextWeather0 + " degrees on " + nextWeatherTime0);
172 Serial.println("It will be " + nextWeather1 + " degrees on " + nextWeatherTime1);
173 Serial.println("It will be " + nextWeather2 + " degrees on " + nextWeatherTime2);
174 Serial.println("It will be " + nextWeather3 + " degrees on " + nextWeatherTime3);
175 Serial.println("It will be " + nextWeather4 + " degrees on " + nextWeatherTime4);
176
177 if (average > 45) {
178 Serial.println("Let's go to the beach.");
179 }
180 else {
181 Serial.println("Wear a coat.");
182 }
183
184
185 }
186}