#define BLYNK_TEMPLATE_ID "TMPL3WkOZR8_c"
#define BLYNK_TEMPLATE_NAME "NM VI IOT002"
#define BLYNK_AUTH_TOKEN "EBM1-rJjBYIMn2Owyj3OL8hGQDMhXpaY"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BlynkSimpleEsp32.h>
// WiFi credentials
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pin definitions
#define TRIG_PIN 18
#define ECHO_PIN 19
#define BUZZER_PIN 21
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Display objects
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Function declarations
double readDistance();
void setup() {
Serial.begin(115200);
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize OLED
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true); // Halt on failure
}
oled.clearDisplay();
oled.display();
// Initialize ultrasonic sensor
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize buzzer
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
Blynk.run();
// Read distance from ultrasonic sensor
double distance = readDistance();
// Update LCD display
lcd.clear();
lcd.setCursor(0, 0);
if (distance < 50) {
lcd.print("Vehicle is");
lcd.setCursor(0, 1);
lcd.print("too near");
tone(BUZZER_PIN, 1000); // Activate the buzzer
} else if (distance > 200) {
lcd.print("Vehicle is");
lcd.setCursor(0, 1);
lcd.print("too far");
noTone(BUZZER_PIN); // Deactivate the buzzer
} else {
lcd.print("You are at");
lcd.setCursor(0, 1);
lcd.print("a safe dist");
noTone(BUZZER_PIN); // Deactivate the buzzer
}
// Update OLED display
oled.clearDisplay();
oled.setTextSize(2); // Adjust text size if needed
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.print("Distance:");
oled.setCursor(0, 24);
oled.print(distance);
oled.print(" cm");
oled.display();
// Send data to Blynk
Blynk.virtualWrite(V1, distance); // Measured distance
delay(1000); // Adjust the delay as needed
}
double readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
double distance = duration * 0.034 / 2;
return distance;
}
BLYNK_WRITE(V1) {
// Here, you can add functionality to set the setpoint if needed
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306