Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ArduinoJson.h>
- WiFiClient client;
- String weatherMain = "";
- String weatherDescription = "";
- String weatherLocation = "";
- String country;
- int humidity;
- int pressure;
- float temp;
- float tempMin, tempMax;
- int clouds;
- float windSpeed;
- String date;
- String weatherString;
- // =======================================================================
- // CHANGE YOUR CONFIG HERE:
- // =======================================================================
- const char* ssid = "huawei-2929"; // SSID of local network
- const char* password = "chefrolet"; // Password on network
- // read OpenWeather api description for more info
- // =======================================================================
- void setup()
- {
- Serial.begin(115200);
- Serial.print("Connecting WiFi ");
- WiFi.begin(ssid, password);
- // printStringWithShift("Connecting",15);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected: "); Serial.println(WiFi.localIP());
- const char *weatherHost = "api.openweathermap.org";
- String url = "/data/2.5/weather?id=3079508&appid=TU_VLOZIT_API_KEY&units=metric&lang=en";
- Serial.print("connecting to "); Serial.println(weatherHost);
- if (client.connect(weatherHost, 80)) {
- 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");
- while (client.connected()) {
- String line = client.readStringUntil('\n'); //HTTP HEADER
- //Serial.println(line);
- if (line == "\r") {
- break;
- }
- }
- DynamicJsonDocument doc(512);
- String line = client.readString(); //PAYLOAD
- Serial.println("PAYLOAD Z OPENWEATHER:");
- Serial.println(line);
- deserializeJson(doc, line);
- JsonObject obj = doc.as<JsonObject>();
- //weatherMain = obj["weather"]["main"].as<String>();
- weatherDescription = obj["weather"]["description"].as<String>();
- weatherDescription.toLowerCase();
- // weatherLocation = obj["name"].as<String>();
- // country = obj["sys"]["country"].as<String>();
- temp = obj["main"]["temp"];
- humidity = obj["main"]["humidity"];
- pressure = obj["main"]["pressure"];
- tempMin = obj["main"]["temp_min"];
- tempMax = obj["main"]["temp_max"];
- windSpeed = obj["wind"]["speed"];
- clouds = obj["clouds"]["all"];
- String deg = String(char('~' + 25));
- weatherString = " Temp: " + String(temp, 1) + deg + "C (" + String(tempMin, 1) + deg + "-" + String(tempMax, 1) + deg + ") ";
- weatherString += weatherDescription;
- weatherString += " Humidity: " + String(humidity) + "% ";
- weatherString += " Pressure: " + String(pressure) + "hPa ";
- weatherString += " Clouds: " + String(clouds) + "% ";
- weatherString += " Wind: " + String(windSpeed, 1) + "m/s ";
- Serial.println("Po parsingu....");
- Serial.println(weatherString);
- } else {
- Serial.println("Problem s pripojenim na OpenWeather");
- }
- client.stop();
- }
- void loop()
- {
- }
RAW Paste Data