#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define DHTPIN 13
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *serverURL = "http://cyodk661lp.sharedwithexpose.com/nangcao/add.php";
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
dht.begin();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
float nhietdo = dht.readTemperature();
float doam = dht.readHumidity();
Serial.println("Nhiệt độ: ");
Serial.print(nhietdo);
Serial.print(" °C, Độ ẩm: ");
Serial.println(doam);
Serial.println(" %");
if (isnan(nhietdo) || isnan(doam)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
String url = String(serverURL) + "?nhietdo=" + String(nhietdo) + "&doam=" + String(doam);
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("HTTP status code: " + String(httpCode));
Serial.println("Server response: " + payload);
} else {
Serial.println("Error on HTTP request");
}
http.end();
}
delay(2000);
}