#define BLYNK_TEMPLATE_ID "TMPL6K-2SY3Nj"
#define BLYNK_TEMPLATE_NAME "IOT"
#define BLYNK_AUTH_TOKEN "3GPdg1DNOZv-RwOcjdrrbGpwHjNPZ8Nf"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <ESP32Servo.h>
#define BLYNK_PRINT Serial
// Blynk authentication token and WiFi credentials
char auth[] = "3GPdg1DNOZv-RwOcjdrrbGpwHjNPZ8Nf";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// DHT sensor configuration
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Servo motor configuration
Servo servo1;
// Pins for PIR and buzzer
const int pirPin = 25;
const int buzzerPin = 14;
// Virtual pins for Blynk
const int virtualPinPIR = V5;
const int virtualPinTemperature = V3;
const int virtualPinHumidity = V4;
// Task handles
TaskHandle_t TaskHandle_ReadDHT22 = NULL;
TaskHandle_t TaskHandle_CheckPIR = NULL;
// Shared variables
volatile bool motionDetected = false;
// Task to read temperature and humidity from DHT22 sensor
void readDHT22(void *pvParameters) {
(void) pvParameters;
for (;;) {
float t = dht.readTemperature();
float h = dht.readHumidity();
if (!isnan(t) && !isnan(h)) {
Blynk.virtualWrite(virtualPinTemperature, t);
Blynk.virtualWrite(virtualPinHumidity, h);
Serial.print("Temperature = ");
Serial.print(t);
Serial.print(" °C, Humidity = ");
Serial.print(h);
Serial.println(" %");
} else {
Serial.println("Failed to read from DHT sensor!");
}
vTaskDelay(2000 / portTICK_PERIOD_MS); // Delay for 2 seconds
}
}
// Task to check PIR sensor for motion
void checkPIR(void *pvParameters) {
(void) pvParameters;
for (;;) {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
if (!motionDetected) {
motionDetected = true;
digitalWrite(buzzerPin, HIGH); // Activate buzzer
Blynk.virtualWrite(virtualPinPIR, 1); // Update Blynk (motion detected)
Serial.println("Motion detected!");
delay(100); // Wait briefly to avoid multiple triggers
}
} else {
if (motionDetected) {
motionDetected = false;
digitalWrite(buzzerPin, LOW); // Deactivate buzzer
Blynk.virtualWrite(virtualPinPIR, 0); // Update Blynk (no motion detected)
Serial.println("No motion detected.");
}
}
vTaskDelay(100 / portTICK_PERIOD_MS); // Check every 100ms
}
}
// Blynk write function for controlling the LED
BLYNK_WRITE(V2) {
int pinValue = param.asInt();
if (pinValue == 1) {
digitalWrite(12, HIGH); // Turn the LED on
} else {
digitalWrite(12, LOW); // Turn the LED off
}
}
// Blynk write function for controlling the servo
BLYNK_WRITE(V1) {
int angle = param.asInt();
servo1.write(angle); // Set servo position
Serial.print("Servo angle set to: ");
Serial.println(angle);
}
void setup() {
Serial.begin(115200);
Serial.println("Temperature and Motion Monitoring System");
pinMode(buzzerPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(12, OUTPUT); // LED control pin
servo1.attach(16); // Servo control pin
dht.begin(); // Initialize DHT sensor
Blynk.begin(auth, ssid, pass); // Connect to Blynk
// Create FreeRTOS tasks
xTaskCreatePinnedToCore(
readDHT22, // Task function
"ReadDHT22", // Task name
10000, // Stack size in words
NULL, // Task input parameter
1, // Task priority
&TaskHandle_ReadDHT22, // Task handle
0); // Core where the task should run
xTaskCreatePinnedToCore(
checkPIR, // Task function
"CheckPIR", // Task name
10000, // Stack size in words
NULL, // Task input parameter
1, // Task priority
&TaskHandle_CheckPIR, // Task handle
1); // Core where the task should run
}
void loop() {
Blynk.run(); // Required in the main loop for Blynk to operate
}