#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String serverNameGet = "https://postman-echo.com/get";
LiquidCrystal_I2C lcd(0x27, 16, 2); // pin21 = SDA ; pin22 = SCL
DHTesp dhtSensor;
int dhtPin = 15;
int ledPin = 14;
int pirPin = 27;
int pirState = LOW;
int val = 0;
void setup() {
Serial.begin(9600);
// init lcd
lcd.init();
lcd.backlight();
// init dht
dhtSensor.setup(dhtPin, DHTesp::DHT22);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirPin, INPUT); // declare sensor as input
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Wait for WiFi... ");
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi ...");
delay(1000);
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
void loop() {
// get data from dht and print on lcd
TempAndHumidity data = dhtSensor.getTempAndHumidity();
lcd.setCursor(0, 0);
lcd.println("Temp: " + String(data.temperature, 2) + "C");
lcd.setCursor(0, 1);
lcd.println("Humidity: " + String(data.humidity, 1) + "%");
// get data (high/low) from pir and turn the light on/off
val = digitalRead(pirPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// send Get request
String serverPath = serverNameGet + "?temp=" + String(data.temperature, 2) + "&hum=" + String(data.humidity, 1) + "&motion=" + String(val);
http.begin(serverPath.c_str()); // Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code (get req): ");
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");
}
delay(2000);
}