#include "WiFi.h"
#include <WiFiClient.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
#define SSID "Wokwi-GUEST"
#define PASS ""
#define BTN 19
int mode = 0;
float coord_lon, coord_lat, main_temp, wind_speed;
int main_pressure, main_humidity;
const char * weather_0_main;
const char *sys_country;
const char * name;
LiquidCrystal_I2C lcd1(0x27, 16, 2);
void setup() {
// put your setup code here, to run once:
Serial.begin(115300);
Serial.println("Initializing WiFi...");
WiFi.mode(WIFI_STA);
Serial.println("Setup done!");
WiFi.begin(SSID, PASS);
Serial.print("Connecting to WIFI ...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("WIFI connected. LocalIP :");
Serial.println(WiFi.localIP());
Wire.begin();
lcd1.init();
lcd1.backlight();
getWeatherInfo("Hanoi");
pinMode(BTN, INPUT_PULLUP);
}
void loop() {
unsigned long now = millis();
if (Serial.available()) {
String location = Serial.readStringUntil('\n');
getWeatherInfo(location);
}
if (digitalRead(BTN) == LOW) {
delay(50);
while (digitalRead(BTN) == LOW) {
if (millis() - now > 1000) {
now = millis();
nextMode();
showInfo();
}
}
}
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
void getWeatherInfo (String location) {
// String location = "Hanoi";
String api_key = "9396f69fee320a0014ac3fa7ab35381d";
String api = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + api_key;
String output = "";
WiFiClient client;
HTTPClient http;
if (http.begin(client, api)) {
int httpCode = http.GET();
if (httpCode > 0) {
Serial.print("http code");
Serial.println(httpCode);
if (httpCode == HTTP_CODE_OK) {
output = http.getString();
Serial.print(output);
deSerialization(output);
showInfo();
}
} else {
Serial.println(httpCode);
Serial.println("Failed");
}
http.end();
} else {
Serial.println("unable to connect");
}
}
void showInfoLCD(LiquidCrystal_I2C lcd, float input, int col, int row) {
lcd.setCursor(col, row);
lcd.print(input);
}
void showInfoLCD(LiquidCrystal_I2C lcd, int input, int col, int row) {
lcd.setCursor(col, row);
lcd.print(input);
}
void showInfoLCD(LiquidCrystal_I2C lcd, const char * input, int col, int row) {
lcd.setCursor(col, row);
lcd.print(input);
}
void nextMode() {
mode ++;
if (mode == 3) mode = 0;
}
void deSerialization(String input) {
// String input;
JsonDocument doc;
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
coord_lon = doc["coord"]["lon"]; // 105.8412
coord_lat = doc["coord"]["lat"]; // 21.0245
JsonObject weather_0 = doc["weather"][0];
weather_0_main = weather_0["main"]; // "Clouds"
JsonObject main = doc["main"];
main_temp = main["temp"]; // 306.15
main_pressure = main["pressure"]; // 1005
main_humidity = main["humidity"]; // 71
JsonObject wind = doc["wind"];
wind_speed = wind["speed"]; // 5.56
JsonObject sys = doc["sys"];
sys_country = sys["country"]; // "VN"
name = doc["name"]; // "Hanoi"
}
void showInfo() {
switch (mode) {
case 0 :
lcd1.clear();
lcd1.print("Country: ");
showInfoLCD(lcd1, sys_country, 9, 0 );
lcd1.setCursor(0, 1);
lcd1.print("City: ");
showInfoLCD(lcd1, name, 6, 1 );
break;
case 1 :
lcd1.clear();
lcd1.print("Temp: ");
showInfoLCD(lcd1, main_temp, 6, 0 );
lcd1.setCursor(0, 1);
lcd1.print("Humid: ");
showInfoLCD(lcd1, main_humidity, 7, 1 );
break;
case 2:
lcd1.clear();
lcd1.print("Pressure: ");
showInfoLCD(lcd1, main_pressure, 10, 0 );
lcd1.setCursor(0, 1);
lcd1.print("windSpeed: ");
showInfoLCD(lcd1, wind_speed, 11, 1 );
break;
}
}