from machine import Pin, ADC, I2C
from time import sleep
import dht
from hx711 import HX711
from mpu6050 import MPU6050
import network
import BlynkLib
# ==============================
# WiFi and Blynk Setup
# ==============================
# #define BLYNK_TEMPLATE_ID "TMPL3O1Rxot3D"
# #define BLYNK_TEMPLATE_NAME "bridge monitoring system"
# #define BLYNK_AUTH_TOKEN "CLh_zVhFn9yiPfIcxz9DXkEbV4UkImV9"
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
BLYNK_AUTH = "CLh_zVhFn9yiPfIcxz9DXkEbV4UkImV9" # Replace with your own token
# Connect to WiFi
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
if not wifi.isconnected():
print("Connecting to WiFi...", end="")
wifi.connect(WIFI_SSID, WIFI_PASS)
while not wifi.isconnected():
print(".", end="")
sleep(1)
print()
print("WiFi Connected! IP:", wifi.ifconfig()[0])
# Connect to Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)
print("Connected to Blynk Cloud!")
# ==============================
# Sensor Setup
# ==============================
# Vibration (Potentiometer) - ADC1 channel (GPIO34)
vibration_sensor = ADC(Pin(34))
vibration_sensor.atten(ADC.ATTN_11DB)
# Load Cell (HX711)
hx = HX711(dout=Pin(19), pd_sck=Pin(18))
hx.set_scale(1)
hx.tare()
# Tilt (MPU6050) - I2C on pins 21 (SDA) / 22 (SCL)
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
mpu = MPU6050(i2c)
# Temperature & Humidity (DHT22)
dht_sensor = dht.DHT22(Pin(4))
# Crack Sensor (Pushbutton)
crack_sensor = Pin(25, Pin.IN, Pin.PULL_DOWN)
# Alert System (LED & Buzzer)
led = Pin(2, Pin.OUT)
buzzer = Pin(5, Pin.OUT)
# ==============================
# Thresholds
# ==============================
THRESHOLDS = {
"vibration": 2500, # ADC (0-4095)
"load": 500, # grams (example)
"tilt": 15, # degrees
"temperature": 50, # Celsius
"crack": 1
}
# ==============================
# Main Loop
# ==============================
print("\nSmart Bridge Health Monitoring System Started\n")
while True:
# Keep Blynk connection alive
blynk.run()
alert = False
reasons = []
# --- 1. Vibration ---
try:
vibration = vibration_sensor.read()
except Exception:
vibration = 0
if vibration > THRESHOLDS["vibration"]:
alert = True
reasons.append("High Vibration")
# --- 2. Load Cell ---
try:
weight = max(0, int(hx.get_units(5)))
except Exception:
weight = 0
if weight > THRESHOLDS["load"]:
alert = True
reasons.append("Overload")
# --- 3. Tilt ---
try:
accel = mpu.get_accel_data()
tilt_x = accel.get('x', 0) * 90
tilt_y = accel.get('y', 0) * 90
tilt_angle = max(abs(tilt_x), abs(tilt_y))
except Exception:
tilt_angle = 0
if tilt_angle > THRESHOLDS["tilt"]:
alert = True
reasons.append("Bridge Tilted")
# --- 4. Temperature & Humidity ---
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
except Exception:
temp = 0
hum = 0
if temp > THRESHOLDS["temperature"]:
alert = True
reasons.append("High Temperature")
# --- 5. Crack Detection ---
try:
crack = crack_sensor.value()
except Exception:
crack = 0
if crack == THRESHOLDS["crack"]:
alert = True
reasons.append("Crack Detected")
# --- 6. Alert Action ---
if alert:
led.on()
buzzer.on()
status = "ALERT: " + ", ".join(reasons)
else:
led.off()
buzzer.off()
status = "Bridge Stable"
# --- Print Readings ---
print("=================================")
print("Vibration :", vibration)
print("Load (g) :", weight)
print("Tilt (°) :", "{:.2f}".format(tilt_angle))
print("Temp (°C) :", temp)
print("Humidity (%) :", hum)
print("Crack Detected :", "YES" if crack else "NO")
print("Status:", status)
# --- Send Data to Blynk ---
try:
blynk.virtual_write(0, vibration)
blynk.virtual_write(8, weight)
blynk.virtual_write(9, tilt_angle)
blynk.virtual_write(10, temp)
blynk.virtual_write(11, hum)
blynk.virtual_write(7, "YES" if crack else "NO")
blynk.virtual_write(6, status)
except Exception as e:
# ignore Blynk send errors (network may drop)
print("Blynk write error:", e)
sleep(2)