#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define DHT_PIN 15
#define PIR_SENSOR 12
#define LED_PIN 32
String serverName1 = "https://postman-echo.com/get";
String serverName2 = "https://postman-echo.com/post";
DHTesp dhtSensor;
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
//config LED_PIN output
pinMode(LED_PIN, OUTPUT);
//setup for serial communication
Serial.begin(9600);
Serial.print("Connecting to WiFi");
//setup for WiFi connection
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("WiFi Connected!");
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("ESP32 collecting data ...");
delay(1000);
//setup for dht sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(PIR_SENSOR, INPUT);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
int temp = data.temperature;
int humid = data.humidity;
String stemp = String(temp) + "C";
String shumid = String(humid) + "%";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: " + stemp);
lcd.setCursor(0,1);
lcd.print("Humidity: " + shumid);
delay(1000);
if (WiFi.status() == WL_CONNECTED) {
//HTTP get request, dữ liệu đóng gói url-encoded
HTTPClient http;
String serverPath = serverName1 + "?temp=24.7&humid=30";
http.begin(serverPath.c_str()); // Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: "); Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: "); Serial.println(httpResponseCode);
}
http.end();
//HTTP post request, dữ liệu đóng gói url-encoded
http.begin(serverName2);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "&temp=" + String(temp) + "&humid=" + String(humid);
Serial.println(httpRequestData);
int httpResponseCode2 = http.POST(httpRequestData);
if (httpResponseCode2 > 0) {
Serial.print("HTTP Response code: "); Serial.println(httpResponseCode2);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: "); Serial.println(httpResponseCode2);
}
http.end();
//HTTP post request, dữ liệu đóng gói json (trong request body)
http.begin(serverName2);
http.addHeader("Content-Type", "application/json");
DynamicJsonDocument doc(1024);
String jsonstr;
JsonObject root = doc.to<JsonObject>();
root["temperature"] = temp;
root["humidity"] = humid;
serializeJson(doc, jsonstr);
String httpRequestData3 = jsonstr;
Serial.println(httpRequestData3);
int httpResponseCode3 = http.POST(httpRequestData3);
if (httpResponseCode3 > 0) {
Serial.print("HTTP Response code: "); Serial.println(httpResponseCode3);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: "); Serial.println(httpResponseCode3);
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
delay(3000);
}