#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <DHT.h>
// WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// URL POST ke WordPress
const char* serverName =
"https://gerakpinasti.com/wp-admin/admin-post.php?action=esp_post_data";
// API Key
String apiKeyValue = "tPmAT5Ab3j7F9";
// Sensor
String sensorName = "DHT22";
String sensorLocation = "Site Gugel";
// DHT22
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// LDR
#define LDR_PIN 32
int lightLevel = 0;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected, IP: ");
Serial.println(WiFi.localIP());
dht.begin();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure *client = new WiFiClientSecure;
client->setInsecure(); // Abaikan SSL
HTTPClient https;
https.begin(*client, serverName);
https.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Baca sensor
float suhu = dht.readTemperature();
float kelembapan = dht.readHumidity();
lightLevel = analogRead(LDR_PIN); // 0-4095
if (isnan(suhu) || isnan(kelembapan)) {
Serial.println("Failed to read from DHT sensor!");
delay(30000);
return;
}
// Siapkan data POST sesuai database
String httpRequestData = "api_key=" + apiKeyValue
+ "&sensor=" + sensorName
+ "&location=" + sensorLocation
+ "&value1=" + String(suhu)
+ "&value2=" + String(kelembapan)
+ "&value3=" + String(lightLevel);
Serial.println("POST Data: " + httpRequestData);
int httpResponseCode = https.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = https.getString();
Serial.println("Server response: " + payload);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
https.end();
} else {
Serial.println("WiFi Disconnected");
}
delay(300); // Kirim data tiap 30 detik
}