Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Justin Hill IST 402.001 SP20
- #include <SPI.h>
- #include <WiFiNINA.h>
- #include "arduino_secrets.h"
- #include <ArduinoJson.h>
- char ssid[] = SECRET_SSID; // your network SSID (name)
- char pass[] = SECRET_PASS;// your network PASSWORD ()
- //open weather map api key
- String apiKey= SECRET_APIKEY;
- //the city you want the weather for
- String location= "philadelphia,US";
- int status = WL_IDLE_STATUS;
- char server[] = "api.openweathermap.org";
- WiFiClient client;
- float addition;
- float result;
- void setup() {
- //Initialize serial and wait for port to open:
- Serial.begin(9600);
- // attempt to connect to Wifi network:
- while (status != WL_CONNECTED) {
- Serial.print("Attempting to connect to SSID: ");
- Serial.println(ssid);
- status = WiFi.begin(ssid, pass);
- //use the line below if your network is protected by wpa password
- //status = WiFi.begin(ssid, pass);
- // wait 10 seconds for connection:
- delay(1000);
- }
- Serial.println("Connected to wifi");
- printWifiStatus();
- }
- void loop() {
- getWeather();
- delay(60000);
- }
- void getWeather() {
- Serial.println("\nStarting connection to server...");
- // if you get a connection, report back via serial:
- if (client.connect(server, 80)) {
- Serial.println("connected to server");
- // Make a HTTP request:
- client.print("GET /data/2.5/forecast?");
- client.print("q="+location);
- client.print("&appid="+apiKey);
- client.print("&cnt=5");
- client.println("&units=metric");
- client.println("Host: api.openweathermap.org");
- client.println("Connection: close");
- client.println();
- } else {
- Serial.println("unable to connect");
- }
- delay(1000);
- String line = "";
- while (client.connected()) {
- line = client.readStringUntil('\n');
- Serial.print("parsingValues: ");
- Serial.println(line);
- DynamicJsonDocument doc(5000);
- DeserializationError error = deserializeJson(doc, line);
- if (error){
- return;
- }
- String nextWeatherTime0 = doc["list"][0]["dt_txt"];
- String nextWeather0 = doc["list"][0]["weather" ][0]["main" ];
- String nextWeatherTime1 = doc["list"][1]["dt_txt"];
- String nextWeather1 = doc["list"][1]["weather" ][0]["main" ];
- String nextWeatherTime2 = doc["list"][2]["dt_txt" ];
- String nextWeather2 = doc["list"][2]["weather" ][0]["main" ];
- String nextWeatherTime3 = doc["list"][3]["dt_txt" ];
- String nextWeather3 = doc["list"][3]["weather" ][0]["main" ];
- String nextWeatherTime4 = doc["list"][4]["dt_txt" ];
- String nextWeather4 = doc["list"][4]["weather" ][0]["main" ];
- String city = doc["city"]["name" ];
- String population = doc["city"]["population"];
- int feelsLike1 = doc["list"][0]["main"]["feels_like" ];
- int feelsLike2 = doc["list"][1]["main"]["feels_like" ];
- int feelsLike3 = doc["list"][2]["main"]["feels_like" ];
- int feelsLike4 = doc["list"][3]["main"]["feels_like" ];
- int feelsLike5 = doc["list"][4]["main"]["feels_like" ];
- String dt_text1 = doc["list"][0]["dt_txt" ];
- String dt_text2 = doc["list"][1]["dt_txt" ];
- String dt_text3 = doc["list"][2]["dt_txt" ];
- String dt_text4 = doc["list"][3]["dt_txt" ];
- String dt_text5 = doc["list"][4]["dt_txt" ];
- addition = feelsLike1 + feelsLike2 + feelsLike3 + feelsLike4 + feelsLike5;
- result = addition / 5;
- //Print Values
- Serial.print("City: ");
- Serial.println(city);
- Serial.print("Population: ");
- Serial.println(population);
- Serial.print("It will be ");
- Serial.print(feelsLike1);
- Serial.print(" degrees on ");
- Serial.println(dt_text1);
- Serial.print("It will be ");
- Serial.print(feelsLike2);
- Serial.print(" degrees on ");
- Serial.println(dt_text2);
- Serial.print("It will be ");
- Serial.print(feelsLike3);
- Serial.print(" degrees on ");
- Serial.println(dt_text3);
- Serial.print("It will be ");
- Serial.print(feelsLike4);
- Serial.print(" degrees on ");
- Serial.println(dt_text4);
- Serial.print("It will be ");
- Serial.print(feelsLike5);
- Serial.print(" degrees on ");
- Serial.println(dt_text5);
- if (result <= 45){
- Serial.print("Average temperature: ");
- Serial.print(result);
- Serial.print(" degrees.");
- Serial.println(" Wear a coat!");
- }
- else{
- Serial.print("Average temperature: ");
- Serial.print(result);
- Serial.print(" degrees.");
- Serial.println(" Let's go to the beach!");
- }
- }
- }
- void printWifiStatus() {
- // print the SSID of the network you're attached to:
- Serial.print("SSID: ");
- Serial.println(WiFi.SSID());
- // print your board's IP address:
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
- // print the received signal strength:
- long rssi = WiFi.RSSI();
- Serial.print("signal strength (RSSI):");
- Serial.print(rssi);
- Serial.println(" dBm");
- // print where to go in a browser:
- Serial.print("To see this page in action, open a browser to http://");
- Serial.println(ip);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement