#include <string.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define DHT_PIN 15 // DHT connect to pin 15
#define LED_PIN 32 // LED connect to pin 32
#define PIR_SENSOR 12 // PIR connect to pin 12
DHTesp dhtSensor;
// Set the LCD address to 0x27,
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Use timestamp to print DHT
unsigned long previousMillis = 0;
const long interval = 2000;
// Server Address
String serverName = "https://postman-echo.com/";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// config led pin
pinMode(LED_PIN, OUTPUT);
// Config PIR
pinMode(PIR_SENSOR, INPUT);
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!");
Serial.print("ESP32 collecting sensors data\n");
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("ESP32 collecting data ...");
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
delay(dhtSensor.getMinimumSamplingPeriod()); // this speeds up the simulation
float humidity = dhtSensor.getHumidity();
float temperature = dhtSensor.getTemperature();
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("Temperature: ");
Serial.println(temperature);
const long currentMillis = millis();
if(currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
DHT_Handle(humidity, temperature);
Send_GET_URLENCODE(humidity, temperature);
Send_POST_URLENCODE(humidity, temperature);
Send_POST_Body(humidity, temperature);
}
int pir_value = digitalRead(PIR_SENSOR);
if(pir_value == 1){
digitalWrite(LED_PIN, HIGH);
Serial.println("Motion detected. LED is ON.");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("Motion ended. LED is OFF");
}
}
void DHT_Handle(float humidity, float temperature){
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.setCursor(0, 1);
lcd.print("Temperature: ");
lcd.print(temperature);
lcd.print("C");
}
// a. Send HTTP GET,
void Send_GET_URLENCODE(float humidity, float temperature){
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String serverPath = serverName + "get?temp=" + String(temperature) + "&humid=" + String(humidity);
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();
}
else {
Serial.println("WiFi Disconnected");
}
}
// b. Send HTTP POST Urlencode
void Send_POST_URLENCODE(float humidity, float temperature){
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String serverPath = serverName + "post";
http.begin(serverPath);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "&temp=" + String(temperature) + "&humid=" + String(humidity);
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
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();
}
else {
Serial.println("WiFi Disconnected");
}
}
// c. Send HTTP POST Body
void Send_POST_Body(float humidity, float temperature){
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String serverPath = serverName + "post";
http.begin(serverPath);
http.addHeader("Content-Type", "application/json");
String httpRequestData = "{\"temp\": " + String(temperature) + ", \"humid\": " + String(humidity) + "}";
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
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();
}
else {
Serial.println("WiFi Disconnected");
}
}