from machine import Pin, I2C
from imu import MPU6050
import time
import math
import urequests
import network
# =============================
# BLYNK & WiFi Configuration
# =============================
#BLYNK_AUTH_TOKEN = "Kxd7tjVXDHFFxcl3-U8DS53gC_jC-DXV"
SSID = "Wokwi-GUEST"
PASS = ""
# =============================
# MPU6050 Setup
# =============================
i2c = I2C(1, sda=Pin(21), scl=Pin(22), freq=400000)
mpu = MPU6050(i2c)
# =============================
# PIR Sensors Setup
# =============================
pir_entry = Pin(14, Pin.IN) # Entrance PIR sensor
pir_exit = Pin(27, Pin.IN) # Exit PIR sensor
# =============================
# Parameters
# =============================
VIBRATION_THRESHOLD = 1.5 # g units
WINDOW_SIZE = 5
SCALE_FACTOR = 10
PERSON_THRESHOLD = 20 # Max safe persons on bridge
# =============================
# WiFi Connection
# =============================
def connect_wifi():
sta_if = network.WLAN(network.STA_IF)
print('Connecting to network...')
sta_if.active(True)
sta_if.connect(SSID, PASS)
while not sta_if.isconnected():
time.sleep(0.5)
print('Network config:', sta_if.ifconfig())
# =============================
# Vibration Calculation
# =============================
def calculate_vibration():
ax = mpu.accel.x
ay = mpu.accel.y
az = mpu.accel.z - 1.0 # remove gravity effect
return math.sqrt(ax * ax + ay * ay + az * az)
def moving_average(new_value, prev_avg, window_size):
return (prev_avg * (window_size - 1) + new_value) / window_size
# =============================
# Send Data to Blynk
# =============================
def send_to_blynk(vibration, temperature, person_count):
try:
scaled_vibration = vibration * SCALE_FACTOR
# V1 → vibration
urequests.get("https://blr1.blynk.cloud/external/api/update?token={}&V1={}".format(BLYNK_AUTH_TOKEN, scaled_vibration))
# V0 → temperature
urequests.get("https://blr1.blynk.cloud/external/api/update?token={}&V0={}".format(BLYNK_AUTH_TOKEN, temperature))
# V2 → number of persons
urequests.get("https://blr1.blynk.cloud/external/api/update?token={}&V2={}".format(BLYNK_AUTH_TOKEN, person_count))
print("📡 Sent to Blynk - Vib: {:.2f}, Temp: {:.2f}, Persons: {}".format(
scaled_vibration, temperature, person_count))
except Exception as e:
print("Error sending to Blynk:", e)
# =============================
# MAIN PROGRAM
# =============================
vibration_avg = 0.0
person_count = 0
pir_entry_last = 0
pir_exit_last = 0
connect_wifi()
print("🚨 Bridge Safety Monitoring Started...")
while True:
try:
# -------- Vibration ----------
vibration = calculate_vibration()
vibration_avg = moving_average(vibration, vibration_avg, WINDOW_SIZE)
temperature = mpu.temperature
# -------- People Counting ----------
entry_state = pir_entry.value()
exit_state = pir_exit.value()
# Detect new entry
if entry_state == 1 and pir_entry_last == 0:
person_count += 1
print("🚶 Person ENTERED. Total:", person_count)
# Detect new exit
if exit_state == 1 and pir_exit_last == 0:
if person_count > 0:
person_count -= 1
print("🚶 Person LEFT. Total:", person_count)
pir_entry_last = entry_state
pir_exit_last = exit_state
print("-" * 50)
print("Accel: X={:.3f}, Y={:.3f}, Z={:.3f}".format(mpu.accel.x, mpu.accel.y, mpu.accel.z))
print("Temperature: {:.2f} °C".format(temperature))
print("Vibration (avg): {:.3f} g".format(vibration_avg))
print("Persons on Bridge: {}".format(person_count))
# -------- Bridge Safety Alerts ----------
if vibration > VIBRATION_THRESHOLD:
print("⚠ ALERT: Excessive Vibration on Bridge!")
if person_count > PERSON_THRESHOLD:
print("🚨 ALERT: Bridge Overloaded! More than {} persons detected!".format(PERSON_THRESHOLD))
# -------- Send to Blynk ----------
send_to_blynk(vibration_avg, temperature, person_count)
except Exception as e:
print("Error:", e)
time.sleep(1)