// BLYNK SECTION
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL5cLt8TUee"
#define BLYNK_TEMPLATE_NAME "Temperature and Humidity detector"
#define BLYNK_AUTH_TOKEN "RYb-Qa0flTTYCIEb2ClE7HCCqyuC9Ufx"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
// DHT22 SECTION
#include "DHTesp.h"
const int DHT_PIN = 27;
DHTesp dhtSensor;
// DEFINTING VARIABLES
int led = 14; // The pin that the LED is attached to
int sensor = 12; // The pin that the sensor is attached to
int state = LOW; // By default, no motion detected
int prevState = LOW; // Previous state of the sensor
int val = 0; // Variable to store the sensor status (value)
String message = "No Motion detected!"; // To show the message
// Setpoint and setHumi values (in degrees Celsius)
float setTemp = 0;
float setHumi = 0;
float currentTemp = 0;
float currentHumi = 0;
// define the GPIO connected with Relays and Buttons
#define RelayPin1 25 //D25
#define RelayPin2 26 //D26
// Push button
#define ButtonPin1 4 //D4
//Change the virtual pins according the rooms
#define VPIN_LED V3
#define VPIN_Mode V2
#define VPIN_setTemp V1
#define VPIN_setHumi V0
// Relay and Mode State
bool VPIN_LedState = led; //Define integer to remember the mode
BLYNK_CONNECTED(){
Blynk.syncVirtual(VPIN_LED);
// Blynk.syncVirtual(VPIN_setHumi);
}
BLYNK_WRITE(VPIN_LED) {
VPIN_LedState = param.asInt();
Blynk.virtualWrite(VPIN_LED, VPIN_LedState); // Send modeState to Blynk server
}
// BLYNK_WRITE(VPIN_setTemp) {
// setTemp = param.asFloat();
// Blynk.virtualWrite(VPIN_setTemp, setTemp);
// }
// BLYNK_WRITE(VPIN_setHumi) {
// setHumi = param.asFloat();
// Blynk.virtualWrite(VPIN_setHumi, setHumi);
// }
void setup() {
Serial.begin(115200); // Initialize serial
Blynk.begin(auth, ssid, pass);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); // Getting data from the sensor
pinMode(led, OUTPUT); // Initialize LED as an output
pinMode(sensor, INPUT); // Initialize sensor as an input
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(ButtonPin1, INPUT_PULLUP);
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH && prevState == LOW) { // check if the sensor went from LOW to HIGH
digitalWrite(led, HIGH); // turn LED ON
VPIN_LedState = HIGH; // update VPIN_LedState
message = "Motion detected!";
state = HIGH; // update variable state to HIGH
}
if (val == LOW && prevState == HIGH) { // check if the sensor went from HIGH to LOW
digitalWrite(led, LOW); // turn LED OFF
VPIN_LedState = LOW; // update VPIN_LedState
message = "No Motion detected!";
state = LOW; // update variable state to LOW
}
prevState = val; // update previous state of the sensor
// Update the Blynk LED widget to reflect the state of the physical LED
Blynk.virtualWrite(VPIN_LED, VPIN_LedState);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("Motion: " + message);
Serial.println("---");
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}