#include <Wire.h>
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHTesp.h"
#define PPM_PIN 16
#define PIR_PIN 2 // Pin connected to the PIR sensor for motion detection
#define ULTRASONIC_TRIG_PIN 5 // Pin connected to the trigger of the ultrasonic sensor
#define ULTRASONIC_ECHO_PIN 13 // Pin connected to the echo of the ultrasonic sensor
#define LED_RED 12 // Pin for the red LED (traffic light)
#define LED_YELLOW 4 // Pin for the yellow LED (traffic light)
#define LED_GREEN 27 // Pin for the green LED (traffic light)
const char *ssid = "Wokwi-GUEST"; // Wi-Fi SSID (network name)
const char *password = ""; // Wi-Fi password (empty for guest network)
const char *webhookURL = "https://webhookwizard.com/api/webhook/in?key=sk_384fc8b2-402d-4151-9b6f-a82ddb67b8ef"; // Webhook URL for sending sensor data
const int DHT_PIN = 15; // Pin connected to the DHT22 sensor for temperature and humidity measurements
long distance; // Variable to store distance measured by the ultrasonic sensor
DHTesp dhtSensor; // Create a DHT sensor object
// Function to connect to Wi-Fi
void setup_wifi() {
WiFi.mode(WIFI_STA); // Set Wi-Fi mode to station (client)
WiFi.begin(ssid, password); // Connect to the Wi-Fi network
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) { // Wait until the Wi-Fi is connected
Serial.print('.'); // Print dots to indicate connection progress
delay(1000);
}
Serial.println("Connected"); // Print when the connection is successful
}
// Function to send sensor data to a server via a webhook
void sendWebhook(String data) {
HTTPClient http; // Create HTTP client object
http.begin(webhookURL); // Set the webhook URL
http.addHeader("Content-Type", "application/json"); // Specify that data is sent in JSON format
http.POST(data); // Send POST request with sensor data
http.end(); // Close the connection
}
// Function to process the distance data and control the traffic light
void processSensorData(long distance) {
// Traffic light control based on the distance measured by the ultrasonic sensor
if (distance < 20) {
// If distance is between 20 cm and 50 cm, caution (yellow light)
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, LOW);
Serial.println("Moderate Distance - Use Caution");
} else if (distance > 20 && distance <= 50) {
// If distance is between 20 cm and 50 cm, caution (yellow light)
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, LOW);
Serial.println("Moderate Distance - Use Caution");
} else if (distance > 50 && distance < 400) {
// If distance is between 20 cm and 50 cm, caution (yellow light)
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_RED, LOW);
Serial.println("Moderate Distance - Use Caution");
}
}
void setup() {
Wire.begin(23, 22); // Start I2C communication for some sensors (if used)
Serial.begin(115200); // Start serial communication at 115200 baud rate
pinMode(PIR_PIN, INPUT); // Set PIR sensor pin as input
pinMode(ULTRASONIC_TRIG_PIN, OUTPUT); // Set ultrasonic sensor trigger pin as output
pinMode(ULTRASONIC_ECHO_PIN, INPUT); // Set ultrasonic sensor echo pin as input
pinMode(LED_RED, OUTPUT); // Set red LED pin as output
pinMode(LED_YELLOW, OUTPUT);// Set yellow LED pin as output
pinMode(LED_GREEN, OUTPUT); // Set green LED pin as output
// Initialize DHT sensor for temperature and humidity
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// Initialize Wi-Fi connection
setup_wifi();
}
void loop() {
// Read the PIR sensor to detect motion
int pirValue = digitalRead(PIR_PIN);
if (pirValue == HIGH) {
// If motion is detected, stop traffic (turn on red light)
digitalWrite(LED_RED, LOW);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_GREEN, LOW);
Serial.println("Motion detected - Stop Traffic");
} else {
// Read the ultrasonic sensor to measure the distance of vehicles
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH); // Measure the time for the pulse to return
distance = duration * 0.034 / 2; // Convert time to distance in cm
// Process sensor data and control the traffic lights accordingly
processSensorData(distance);
// Read PPM value from the MQ135 gas sensor (for pollution monitoring)
int16_t ppmValue = analogRead(PPM_PIN);
int mappedppmValue = ppmValue / 4.095; // Map the raw value to the correct scale for the sensor
Serial.print("PPM: "); // Print the PPM (pollution level) value to the serial monitor
Serial.println(mappedppmValue);
// Send sensor data (distance and PPM) via webhook to a server
String sensorData = "{\"event\": \"SensorData\", \"distance\": " + String(distance) + ", \"ppm\": " + String(mappedppmValue) + "}";
sendWebhook(sensorData);
}
delay(1000); // Delay for 1 second before the next loop iteration
// Read temperature and humidity from the DHT22 sensor
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temperature = data.temperature; // Get temperature in Celsius
float humidity = data.humidity; // Get humidity percentage
// Send sensor data (distance, temperature, and humidity) via webhook to a server
String sensorData = "{\"event\": \"SensorData\", \"distance\": " + String(distance) + ", \"temperature\": " + String(temperature) + ", \"humidity\": " + String(humidity) + "}";
sendWebhook(sensorData);
delay(1000); // Delay for 1 second before the next loop iteration
}