#include <string.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define LED_PIN 32
#define PIR_PIN 34
#define DHT_PIN 33
LiquidCrystal_I2C lcd(0x27, 20, 4);
DHTesp dhtSensor;
String serverAddress = "https://postman-echo.com/";
// a) Gửi dữ liệu HTTP get request, dữ liệu đóng gói url-encoded
void sendData_HttpGet_UrlEncoded(float temp, float humid)
{
Serial.println("Sending data to server using HTTP GET request and url-encoded data");
// Url to send data to server
String url = serverAddress + "get?temp=" + String(temp) + "&humid=" + String(humid);
Serial.println("Url: " + url);
HTTPClient http;
http.begin(url);
// Specify content-type header: application/x-www-form-urlencoded
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.GET();
if (httpCode == 200)
{
// If request success
// Get the response payload
String payload = http.getString();
Serial.println("Response Payload: " + payload);
}
else
{
// If request failed
Serial.println("Send data failed");
}
http.end();
}
// b) Gửi dữ liệu HTTP post request, dữ liệu đóng gói url-encoded
void sendData_HttpPost_UrlEncoded(float temp, float humid)
{
Serial.println("Sending data to server using HTTP POST request and url-encoded data");
// Url to send data to server
String url = serverAddress + "post";
Serial.println("Url: " + url);
HTTPClient http;
http.begin(url);
// Specify content-type header: application/x-www-form-urlencoded
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare the payload
String payload = "temp=" + String(temp) + "&humid=" + String(humid);
Serial.println("Data: " + payload);
int httpCode = http.POST(payload);
if (httpCode == 200)
{
// If request success
// Get the response payload
String responsePayload = http.getString();
Serial.println("Response Payload: " + responsePayload);
}
else
{
// If request failed
Serial.println("Send data failed");
}
http.end();
}
// c) Gửi dữ liệu HTTP post request, dữ liệu đóng gói json (trong request body)
void sendData_HttpPost_Json(float temp, float humid)
{
Serial.println("Sending data to server using HTTP POST request and json data");
// Url to send data to server
String url = serverAddress + "post";
Serial.println("Url: " + url);
HTTPClient http;
http.begin(url);
// Specify content-type header: application/json
http.addHeader("Content-Type", "application/json");
// Prepare the json payload
StaticJsonDocument<200> doc;
doc["temp"] = temp;
doc["humid"] = humid;
// Serialize the json payload to String
String payload;
serializeJson(doc, payload);
Serial.println("Data: " + payload);
int httpCode = http.POST(payload);
if (httpCode == 200)
{
// If request success
// Get the response payload
String responsePayload = http.getString();
Serial.println("Response Payload: " + responsePayload);
}
else
{
// If request failed
Serial.println("Send data failed");
}
http.end();
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// Set LED_PIN as OUTPUT
pinMode(LED_PIN, OUTPUT);
// Set PIR_PIN as INPUT
pinMode(PIR_PIN, INPUT);
// Initialize the lcd
lcd.init();
// Setup temperature sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// Connect to Wifi
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
}
// int a = 0;
void loop()
{
// put your main code here, to run repeatedly:
// Get temperature and humidity
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temp = data.temperature;
float humid = data.humidity;
// Print Temperature and humidity to LCD
lcd.backlight();
lcd.setCursor(0, 0);
String tempS = "Temp: " + String(temp) + "C";
lcd.print(tempS);
Serial.println(tempS.c_str());
lcd.setCursor(0, 1);
String humidS = "Humidity: " + String(humid) + "%";
lcd.print(humidS);
Serial.println(humidS.c_str());
// Send data to server via HTTP request
if (WiFi.status() == WL_CONNECTED)
{
sendData_HttpGet_UrlEncoded(temp, humid);
sendData_HttpPost_UrlEncoded(temp, humid);
sendData_HttpPost_Json(temp, humid);
}
else
{
Serial.println("WiFi Disconnected");
}
// Check motion sensor whether a motion appears
int motion = digitalRead(PIR_PIN);
if (motion == 1)
{
// Turn the led on
digitalWrite(LED_PIN, LOW);
Serial.println("LED ON");
}
else
{
// Turn the led off
digitalWrite(LED_PIN, HIGH);
Serial.println("LED OFF");
}
// this speeds up the simulation
delay(2000);
}