martinius96

MRE - OPENWEATHER - ESP8266

Mar 1st, 2021
575
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ArduinoJson.h>
  3.  
  4. WiFiClient client;
  5. String weatherMain = "";
  6. String weatherDescription = "";
  7. String weatherLocation = "";
  8. String country;
  9. int humidity;
  10. int pressure;
  11. float temp;
  12. float tempMin, tempMax;
  13. int clouds;
  14. float windSpeed;
  15. String date;
  16. String weatherString;
  17.  
  18. // =======================================================================
  19. // CHANGE YOUR CONFIG HERE:
  20. // =======================================================================
  21. const char* ssid     = "huawei-2929";     // SSID of local network
  22. const char* password = "chefrolet";   // Password on network
  23. // read OpenWeather api description for more info
  24. // =======================================================================
  25.  
  26. void setup()
  27. {
  28.   Serial.begin(115200);
  29.  
  30.   Serial.print("Connecting WiFi ");
  31.   WiFi.begin(ssid, password);
  32.   // printStringWithShift("Connecting",15);
  33.   while (WiFi.status() != WL_CONNECTED) {
  34.     delay(500);
  35.     Serial.print(".");
  36.   }
  37.   Serial.println("");
  38.   Serial.print("Connected: "); Serial.println(WiFi.localIP());
  39.  
  40.   const char *weatherHost = "api.openweathermap.org";
  41.   String url = "/data/2.5/weather?id=3079508&appid=TU_VLOZIT_API_KEY&units=metric&lang=en";
  42.   Serial.print("connecting to "); Serial.println(weatherHost);
  43.   if (client.connect(weatherHost, 80)) {
  44.     client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + weatherHost + "\r\n" + "User-Agent: NodeMCU\r\n" + "Connection: close\r\n\r\n");
  45.     while (client.connected()) {
  46.       String line = client.readStringUntil('\n'); //HTTP HEADER
  47.       //Serial.println(line);
  48.       if (line == "\r") {
  49.         break;
  50.       }
  51.     }
  52.     DynamicJsonDocument doc(512);
  53.     String line = client.readString(); //PAYLOAD
  54.     Serial.println("PAYLOAD Z OPENWEATHER:");
  55.     Serial.println(line);
  56.     deserializeJson(doc, line);
  57.     JsonObject obj = doc.as<JsonObject>();
  58.     //weatherMain = obj["weather"]["main"].as<String>();
  59.     weatherDescription = obj["weather"]["description"].as<String>();
  60.     weatherDescription.toLowerCase();
  61.     //  weatherLocation = obj["name"].as<String>();
  62.     //  country = obj["sys"]["country"].as<String>();
  63.     temp = obj["main"]["temp"];
  64.     humidity = obj["main"]["humidity"];
  65.     pressure = obj["main"]["pressure"];
  66.     tempMin = obj["main"]["temp_min"];
  67.     tempMax = obj["main"]["temp_max"];
  68.     windSpeed = obj["wind"]["speed"];
  69.     clouds = obj["clouds"]["all"];
  70.     String deg = String(char('~' + 25));
  71.     weatherString = "         Temp: " + String(temp, 1) + deg + "C (" + String(tempMin, 1) + deg + "-" + String(tempMax, 1) + deg + ")  ";
  72.     weatherString += weatherDescription;
  73.     weatherString += "  Humidity: " + String(humidity) + "%  ";
  74.     weatherString += "  Pressure: " + String(pressure) + "hPa  ";
  75.     weatherString += "  Clouds: " + String(clouds) + "%  ";
  76.     weatherString += "  Wind: " + String(windSpeed, 1) + "m/s                 ";
  77.     Serial.println("Po parsingu....");
  78.     Serial.println(weatherString);
  79.   } else {
  80.     Serial.println("Problem s pripojenim na OpenWeather");
  81.   }
  82.   client.stop();
  83.  
  84.  
  85. }
  86.  
  87. void loop()
  88. {
  89.  
  90. }
  91.  
  92.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×