#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
//Import i2C LCD libraries
#include <LiquidCrystal_I2C.h>
int lcdColumns = 20;
int lcdRows = 4;
int hum1, wind1, temp1, pr1, pr2;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//Define API ke
String openWeatherMapApiKey = "29703c5d164dcdaa6be62345402106fc";
String city = "Dovol'noye"; //specify your city
String countryCode = "RU"; //specify your country code
unsigned long last_time = 0;
unsigned long timer_delay = 10000;
String jsonBuffer;
void setup() {
Serial.begin(115200);
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
WiFi.begin(ssid, password);
Serial.println("Connecting to WIFI...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("First set of readings will appear after 10 seconds");
}
void loop() {
// Send an HTTP GET request
if ((millis() - last_time) > timer_delay) {
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&units=metric" + "&APPID=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());
Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(myObject);
lcd.clear();
temp1 = myObject["main"]["temp"];
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(temp1);
lcd.print(" \xDF");
lcd.print("C");
hum1 = myObject["main"]["humidity"];
lcd.setCursor(0,1);
lcd.print("Hum: "); //LCD Printing Humidity
lcd.print(hum1);
lcd.print("%");
wind1 = myObject["wind"]["speed"];
lcd.setCursor(0,2);
lcd.print("Wid: ");
lcd.print(wind1);
lcd.print(" m/s");
pr1 = myObject["main"]["pressure"];
pr2 = pr1 - 256;
lcd.setCursor(0,3);
lcd.print("Atm: ");
lcd.print(pr2);
lcd.print(" mm.rts");
}
else {
Serial.println("WiFi Disconnected");
}
last_time = millis();
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}