/*
Nama : Dafa Putra Rabbani
NIM : 220104004
Prodi : TK22A1
Judul : HTTP Request "GET" REST Countries API
API : 'https://restcountries.com/'
*/
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6O2WkawtA"
#define BLYNK_TEMPLATE_NAME "Label Response"
#define BLYNK_AUTH_TOKEN "x8PNzicfoS9f4-wxMhG6h537HS0Dxfiw"
#include <WiFi.h>
#include <Wire.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setupWiFi() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
Serial.println("WiFi Connected!");
}
void httpRequest() {
HTTPClient http;
http.begin("https://restcountries.com/v3.1/name/swisss1");
int httpResponCode = http.GET();
String payload = http.getString();
if (httpResponCode == 200) {
DynamicJsonDocument jsonDoc(1024);
deserializeJson(jsonDoc, payload);
String countryName = jsonDoc[0]["name"]["common"];
String capital = jsonDoc[0]["capital"][0];
String official = jsonDoc[0]["name"]["official"];
lcd.setCursor(0, 0);
lcd.print(countryName);
lcd.setCursor(0, 1);
lcd.print(capital);
lcd.setCursor(0, 2);
lcd.print(official);
Blynk.virtualWrite(V0, httpResponCode);
Blynk.virtualWrite(V1, HIGH);
Blynk.virtualWrite(V2, LOW);
} else {
Blynk.virtualWrite(V0, httpResponCode);
lcd.setCursor(7, 1);
lcd.print("ERROR");
lcd.setCursor(3, 2);
lcd.print(httpResponCode);
lcd.setCursor(7, 2);
lcd.print("NOT FOUND");
Blynk.virtualWrite(V2, HIGH);
Blynk.virtualWrite(V1, LOW);
}
http.end();
}
void setup() {
setupWiFi();
lcd.init();
lcd.backlight();
lcd.clear();
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
httpRequest();
delay(1000);
}