#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <DHT.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const 22 = dht22.INPUT
// 1. PASTIKAN URL MENGGUNAKAN HTTPS
const char* serverName = "https://aerologic-natalee-nostalgically.ngrok-free.dev/";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
// 1. Inisialisasi client secure
WiFiClientSecure client;
// 2. WAJIB: Abaikan validasi sertifikat agar tidak error -1
client.setInsecure();
HTTPClient http;
// 3. Gunakan HTTPS
http.begin(client, "https://aerologic-natalee-nostalgically.ngrok-free.dev/api/sensor/push");
// 4. Pastikan redirect tetap aktif
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http.addHeader("Content-Type", "application/json");
http.addHeader("ngrok-skip-browser-warning", "true");
String jsonPayload = "{\"device_id\":\"WOKWI-001\",\"ph\":7.2,\"temp\":28.5,\"power_status\":\"ON\",\"battery_pct\":95}";
Serial.println("Mengirim ke Ngrok (HTTPS)...");
int httpResponseCode = http.POST(jsonPayload);
Serial.print("Hasil Akhir: ");
Serial.println(httpResponseCode);
if (httpResponseCode > 0) {
if (httpResponseCode == 201) Serial.println("BERHASIL! Cek MongoDB kamu, Ziz.");
} else {
// Jika masih -1, ini akan memberi tahu alasannya
Serial.printf("Error detail: %s\n", http.errorToString(httpResponseCode).c_str());
}
http.end();
}
delay(10000);
}