#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define LED1 26
#define LED2 27
#define LED3 14
#define LED4 12
const String serverURL = "http://rizkyproject.com/getdata.php";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
}
void loop() {
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
http.begin(serverURL);
int httpResponseCode = http.GET();
Serial.print("Response Code: ");
Serial.println(httpResponseCode);
if (httpResponseCode == 200) {
char input[500];
String payload = http.getString();
payload.toCharArray(input, 500);
StaticJsonDocument<192> doc;
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
//Serial.println(payload);
// Sesuaikan dengan nama kategori yang ada di web
String Sains = doc["Sains"];
String Teknik = doc["Teknik"];
String Ilmu = doc["Ilmu Komputer"];
String Matematika = doc["Matematika"];
digitalWrite(LED1, Sains.toInt());
digitalWrite(LED2, Teknik.toInt());
digitalWrite(LED3, Ilmu.toInt());
digitalWrite(LED4, Matematika.toInt());
}
else {
Serial.print("Request Failed!");
Serial.println(http.errorToString(httpResponseCode));
Serial.println("");
}
}
else{
Serial.println("WiFi Disconnected");
}
}