#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST"; // Nama jaringan WiFi yang akan dihubungkan
const char* password = "";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
getPokemonData();
}
void getPokemonData() {
HTTPClient http;
http.begin("https://pokeapi.co/api/v2/type/1"); // URL to fetch data from type 1 Pokémon
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
parseJSON(payload);
}
} else {
Serial.printf("HTTP GET failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void parseJSON(String payload) {
DynamicJsonDocument doc(2048);
deserializeJson(doc, payload);
const char* name = doc["name"]; // Get the type name
Serial.print("Type Name: ");
Serial.println(name);
JsonArray pokemon = doc["pokemon"];
for (JsonObject elem : pokemon) {
JsonObject pokemon_data = elem["pokemon"];
const char* pokemon_name = pokemon_data["name"]; // Get Pokémon names of this type
Serial.println(pokemon_name);
}
}
void loop() {
// Nothing to do here
}