#include <WiFi.h>
#include <HTTPClient.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST"; //your wifi ssid
const char* password = ""; //your wifi password
void setup() {
Serial.begin(115200);
delay(100);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("Enter the name of the Pokemon:");
}
void loop() {
if (Serial.available()) {
String pokemonName = Serial.readStringUntil('\n');
Serial.print("Proses mendapatkan data pokemon : ");
Serial.println(pokemonName);
pokemonName.trim();
if (pokemonName.length() > 0) {
getAndPrintPokemonData(pokemonName);
}
}
}
void getAndPrintPokemonData(String pokemonName) {
HTTPClient http;
String url = "https://pokeapi.co/api/v2/pokemon/" + pokemonName;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Berhasil Mendapatkan Data!!");
String payload = http.getString();
Serial.println("Response:");
Serial.println(payload);
Serial.print("Data pokemon '");
Serial.print(pokemonName);
Serial.println("' sudah didapatkan ");
} else {
Serial.println("Gagal mendapatkan data");
Serial.print("Error: ");
Serial.println(httpResponseCode);
}
http.end();
Serial.println("Enter the name of the Pokemon:");
}