#include <WiFi.h>
#include "ThingSpeak.h"
#include <LiquidCrystal_I2C.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 m_p_u;
#define MPU_SDA 5 // Define the SDA pin for the MPU6050
#define MPU_SCL 18 // Define the SCL pin for the MPU6050
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 20, 4);
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WiFiClient client;
unsigned long channelNum = 2540707;
const char *API_Key = "S0UTEJW9N0CK1K6H";
int timer1;
int timer2;
float elapsedTime;
int flagStart = 0;
int flagEnd = 0;
float distance = 5.0;
float vehicleSpeed;
float nonZeroSpeed;
int carCount=0;
int sensorPin1 = 34;
int sensorPin2 = 35;
int buzzerPin = 2;
void setup() {
Serial.begin(115200);
Wire.begin(MPU_SDA, MPU_SCL);
while (!Serial);
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
Serial.print("\nVehicle Speed Detection System\n");
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
pinMode(buzzerPin, OUTPUT);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print(" Speed Detector");
delay(2000);
LCD.clear();
ThingSpeak.begin(client);
if (WiFi.status() != WL_CONNECTED) {
Serial.print("\nAttempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(3000);
}
}
LCD.setCursor(2, 1);
LCD.print("Wifi Connected!");
delay(1300);
LCD.clear();
Serial.println("\nConnected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (digitalRead(sensorPin1) == LOW && flagStart == 0) {
timer1 = millis();
flagStart = 1;
}
if (digitalRead(sensorPin2) == LOW && flagEnd == 0) {
timer2 = millis();
flagEnd = 1;
}
if (flagStart == 1 && flagEnd == 1) {
if (timer1 > timer2) {
elapsedTime = timer1 - timer2;
}
else if (timer2 > timer1) {
elapsedTime = timer2 - timer1;
}
elapsedTime = elapsedTime / 1000;
vehicleSpeed = (distance / elapsedTime);
vehicleSpeed = vehicleSpeed * 3600;
vehicleSpeed = vehicleSpeed / 1000;
}
if (vehicleSpeed == 0) {
LCD.setCursor(0, 3);
if (flagStart == 0 && flagEnd == 0) {
LCD.print("No car detected");
}
else {
LCD.print("Searching... ");
}
}
else {
carCount=carCount+1;
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Car Count: ");
LCD.print(carCount);
nonZeroSpeed=vehicleSpeed;
LCD.setCursor(0, 2);
LCD.print("Speed:");
LCD.print(vehicleSpeed, 1);
LCD.print("Km/Hr ");
LCD.setCursor(0, 3);
Serial.print("\nSpeed: ");
Serial.print(vehicleSpeed);
Serial.print(" Km/Hr");
if (vehicleSpeed > 50) {
LCD.print("Over Speeding! ");
Serial.println("\nOver Speeding");
tone(buzzerPin, 1000);
delay(1000);
noTone(buzzerPin);
}
else {
LCD.print("Normal Speed ");
Serial.println("\nNormal Speed");
}
ThingSpeak.setField(1, nonZeroSpeed);
int status = ThingSpeak.writeFields(channelNum, API_Key);
if (status == 200) {
Serial.println("\nChannel update successful.");
}
delay(1500);
digitalWrite(buzzerPin, LOW);
vehicleSpeed = 0;
flagStart = 0;
flagEnd = 0;
}
sensors_event_t acc, gyro, temp;
mpu.getEvent(&acc, &gyro, &temp);
Serial.print("Acceleration on x-axis: ");
Serial.println(acc.acceleration.x);
Serial.print("Acceleration on y-axis: ");
Serial.println(acc.acceleration.y);
Serial.print("Acceleration on z-axis: ");
Serial.println(acc.acceleration.z);
Serial.print("Rotation on x-axis: ");
Serial.println((gyro.gyro.x) * 180 / PI);
delay(1000);
}