#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
// --------------------------
// DHT22 setup
// --------------------------
#define DHTPIN 9 // Connect DATA pin of DHT22 to GPIO15
#define DHTTYPE DHT22 // Sensor type
DHT dht(DHTPIN, DHTTYPE);
// --------------------------
// Wi-Fi setup (Wokwi simulator)
// --------------------------
const char* ssid = "Wokwi-GUEST"; // Wokwi default Wi-Fi
const char* password = ""; // No password
// --------------------------
// Server URL (replace with your ngrok HTTPS URL + /upload)
// --------------------------
const char* serverUrl = "https://4f463da9d9a7.ngrok-free.app/upload";
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Start DHT22
dht.begin();
delay(2000); // wait 2s before first read
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (!isnan(temp) && !isnan(hum)) {
HTTPClient http;
// HTTPS bypass for ngrok (no SSL verification)
http.begin(serverUrl, "");
http.addHeader("Content-Type", "application/json");
String payload = "{\"temperature\": " + String(temp, 1) +
", \"humidity\": " + String(hum, 1) + "}";
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.print("✅ Data sent! Server response: ");
Serial.println(http.getString());
} else {
Serial.print("❌ Error sending POST. Code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("⚠️ Failed to read from DHT sensor!");
}
} else {
Serial.println("⚠️ WiFi not connected!");
}
delay(10000); // send every 10s
}