/*
* Bài 5b: ESP32 gửi dữ liệu HTTP POST request (url-encoded)
* Mô phỏng trên Wokwi
* Server: https://postman-echo.com/post
* Có LED báo motion và LCD hiển thị nhiệt độ/độ ẩm
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
// Cấu hình DHT sensor
#define DHTPIN 15
#define DHTTYPE DHT22
// Cấu hình PIR Motion sensor
#define PIRPIN 4
// Cấu hình LED
#define LEDPIN 2
// Cấu hình WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Server URL
String serverName = "https://postman-echo.com/post";
// Khởi tạo DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Khởi tạo LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(115200);
Serial.println("\n=== Bai 5b: HTTP POST Request (URL-Encoded) ===\n");
pinMode(PIRPIN, INPUT);
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW);
dht.begin();
// Khởi tạo LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Dang ket noi...");
// Kết nối WiFi
Serial.print("Dang ket noi WiFi");
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi da ket noi thanh cong!");
Serial.print("Dia chi IP: ");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Connected!");
delay(1000);
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int motion = digitalRead(PIRPIN);
// Điều khiển LED theo motion
digitalWrite(LEDPIN, motion == HIGH ? HIGH : LOW);
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Loi doc du lieu tu DHT sensor!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DHT Error!");
delay(2000);
return;
}
// Hiển thị lên LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature, 2);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity, 2);
lcd.print("%");
// Hiển thị trên Serial
Serial.println("--- Du lieu cam bien ---");
Serial.print("Nhiet do: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Do am: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Motion: ");
Serial.println(motion == HIGH ? "Co chuyen dong" : "Khong co chuyen dong");
HTTPClient http;
// Bắt đầu kết nối HTTP
http.begin(serverName);
// Header cho URL-encoded
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Đóng gói dữ liệu URL-encoded trong body
String httpRequestData = "temperature=" + String(temperature)
+ "&humidity=" + String(humidity)
+ "&motion=" + String(motion);
Serial.println("\n--- Gui HTTP POST Request (URL-Encoded) ---");
Serial.print("Server: ");
Serial.println(serverName);
Serial.print("Data: ");
Serial.println(httpRequestData);
// Gửi POST request
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println("Response tu server:");
Serial.println(payload);
}
else {
Serial.print("Loi ket noi! Error code: ");
Serial.println(httpResponseCode);
}
http.end();
Serial.println("\n========================================\n");
}
else {
Serial.println("WiFi bi ngat ket noi!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Error!");
WiFi.begin(ssid, password, 6);
}
delay(5000);
}