#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h> // Tambahkan pustaka ArduinoJSON
const char* ssid = "Wokwi-GUEST";
const char* password = "";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
DynamicJsonDocument doc(200); // Buat objek JSON
// Tambahkan data ke objek JSON
doc["suhu"] = 40;
// Buat string JSON dari objek JSON
String jsonString;
serializeJson(doc, jsonString);
HTTPClient http;
http.begin("https://api.thingspeak.com/update?api_key=BTGZHXSUYP61QA8J&field1=0"); // Ganti dengan alamat IP server Anda
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(jsonString);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.println("Error on sending POST request");
}
http.end();
}
delay(5000);
}