// Define the Blynk template ID and name
#define BLYNK_TEMPLATE_ID "TMPL5pa9-i7IL"
#define BLYNK_TEMPLATE_NAME "IOT Smart Trafficking"
// Define the pin numbers for various components
#define TRIG_PIN 5 // Ultrasonic sensor trigger pin
#define ECHO_PIN 18 // Ultrasonic sensor echo pin
#define GREEN_LED 22 // Green LED pin
#define RED_LED 19 // Red LED pin
#define YELLOW_LED 21 // Yellow LED pin
#define BUZZER_PIN 15 // Buzzer pin
#define DHT_PIN 4 // Pin where the DHT22 sensor is connected
#define DHTTYPE DHT22 // Type of DHT sensor used (DHT22)
// Include necessary libraries
#include <DHT.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Define network credentials
char ssid[] = "Wokwi-GUEST"; // WiFi SSID
char pass[] = ""; // WiFi password
// Define Blynk authentication token
char auth[] = "8HX4xf2X5PEolN8OWfkYQG7E5h0Vbthl"; // Blynk auth token
// Initialize the DHT sensor
DHT dht(DHT_PIN, DHTTYPE);
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud rate
// Set the pin modes for the ultrasonic sensor and LEDs
pinMode(TRIG_PIN, OUTPUT); // Set trigger pin as output
pinMode(ECHO_PIN, INPUT); // Set echo pin as input
pinMode(GREEN_LED, OUTPUT); // Set green LED pin as output
pinMode(RED_LED, OUTPUT); // Set red LED pin as output
pinMode(YELLOW_LED, OUTPUT); // Set yellow LED pin as output
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output
dht.begin(); // Initialize the DHT sensor
Blynk.begin(auth, ssid, pass); // Initialize Blynk with authentication and WiFi credentials
}
void loop() {
long duration, distance; // Variables for ultrasonic sensor reading
String status = "STOP"; // Default status
// Trigger the ultrasonic sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo pin and calculate the distance
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration / 2) / 29.1; // Convert duration to distance in cm
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Reset the buzzer
noTone(BUZZER_PIN);
// Set LED color and buzzer based on distance
if (distance < 50) {
setColor(0, 255, 0); // Green
tone(BUZZER_PIN, 3000); // Activate buzzer at 3000 Hz
status = "GO"; // Update status to "GO"
} else if (distance >= 50 && distance <= 200) {
setColor(0, 0, 255); // Yellow
tone(BUZZER_PIN, 2000); // Activate buzzer at 2000 Hz
status = "WAIT"; // Update status to "WAIT"
} else if (distance > 200) {
setColor(255, 0, 0); // Red
tone(BUZZER_PIN, 100); // Activate buzzer at 100 Hz
status = "STOP"; // Update status to "STOP"
}
// Read temperature and humidity from the DHT sensor
float h = dht.readHumidity();
float t = dht.readTemperature(); // Read temperature as Celsius (default)
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return; // Exit if reading fails
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
// Send temperature, humidity, and status to Blynk
Blynk.virtualWrite(V1, t); // Write temperature to virtual pin V1
Blynk.virtualWrite(V2, h); // Write humidity to virtual pin V2
Blynk.virtualWrite(V3, status); // Write status to virtual pin V3
Blynk.run(); // Run Blynk
delay(1000); // Wait for 1 second before next reading
}
// Function to set the color of the RGB LED
void setColor(int red, int green, int blue) {
analogWrite(RED_LED, red); // Set red LED brightness
analogWrite(GREEN_LED, green); // Set green LED brightness
analogWrite(YELLOW_LED, blue); // Set yellow LED brightness
delay(1000); // Delay for 1 second to allow color change to take effect
}