from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import machine
import network
import time
from blynkLib import Blynk
BLYNK_AUTH_TOKEN = "4G0619BJ0MsknaF9ANg4QojWY1ur7RcP"
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect("Wokwi-GUEST", "")
# wifi.connect("testing","Satya2230")
while not wifi.isconnected():
pass
print("Wifi Connected Successfully")
blynk = Blynk(BLYNK_AUTH_TOKEN)
ULTRASONIC_TRIGGER_PIN = 19
ULTRASONIC_ECHO_PIN = 18
VIBRATION_SENSOR_PIN = 23
buzzer_pin = Pin(2, Pin.OUT)
# I2C LCD address and dimensions
I2C_LCD_ADDR = 0x27
I2C_LCD_COLS = 16
I2C_LCD_ROWS = 2
# Initialize I2C
i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
ultrasonic_trigger = machine.Pin(ULTRASONIC_TRIGGER_PIN, machine.Pin.OUT)
ultrasonic_echo = machine.Pin(ULTRASONIC_ECHO_PIN, machine.Pin.IN)
vibration_sensor = machine.Pin(VIBRATION_SENSOR_PIN, machine.Pin.IN)
# Initialize I2C LCD
lcd = I2cLcd(i2c, I2C_LCD_ADDR, I2C_LCD_ROWS, I2C_LCD_COLS) # Swap rows and cols
# MPU6050 constants
MPU6050_ADDR = 0x68
MPU6050_ACCEL_XOUT_H = 0x3B
MPU6050_ACCEL_XOUT_L = 0x3C
MPU6050_ACCEL_YOUT_H = 0x3D
MPU6050_ACCEL_YOUT_L = 0x3E
MPU6050_ACCEL_ZOUT_H = 0x3F
MPU6050_ACCEL_ZOUT_L = 0x40
MPU6050_GYRO_XOUT_H = 0x43
MPU6050_GYRO_XOUT_L = 0x44
MPU6050_GYRO_YOUT_H = 0x45
MPU6050_GYRO_YOUT_L = 0x46
MPU6050_GYRO_ZOUT_H = 0x47
MPU6050_GYRO_ZOUT_L = 0x48
i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
def read_word_2c(address):
high = i2c.readfrom_mem(MPU6050_ADDR, address, 1)[0]
low = i2c.readfrom_mem(MPU6050_ADDR, address+1, 1)[0]
value = (high << 8) + low
if (value >= 0x8000):
return -((65535 - value) + 1)
else:
return value
def read_accel_data():
accel_x = read_word_2c(MPU6050_ACCEL_XOUT_H)
accel_y = read_word_2c(MPU6050_ACCEL_YOUT_H)
accel_z = read_word_2c(MPU6050_ACCEL_ZOUT_H)
return accel_x, accel_y, accel_z
def read_gyro_data():
gyro_x = read_word_2c(MPU6050_GYRO_XOUT_H)
gyro_y = read_word_2c(MPU6050_GYRO_YOUT_H)
gyro_z = read_word_2c(MPU6050_GYRO_ZOUT_H)
return gyro_x, gyro_y, gyro_z
def measure_distance():
# Send a 10us pulse to trigger the ultrasonic sensor
ultrasonic_trigger.value(1)
time.sleep_us(10)
ultrasonic_trigger.value(0)
# Initialize pulse_start
pulse_start = 0
# Measure the duration of the echo pulse
while ultrasonic_echo.value() == 0:
pulse_start = time.ticks_us()
pulse_end = pulse_start # Initialize pulse_end with pulse_start
while ultrasonic_echo.value() == 1:
pulse_end = time.ticks_us()
# Calculate distance in centimeters
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 0.0343 / 2 # Speed of sound is approximately 343 m/s
return distance
while True:
distance = measure_distance()
vibration_status = vibration_sensor.value()
accel_x, accel_y, accel_z = read_accel_data()
gyro_x, gyro_y, gyro_z = read_gyro_data()
print('water distance in CM : ', distance)
print('vibration : ',vibration_status )
print('acceleration - X:', accel_x, ' Y:', accel_y, ' Z:', accel_z)
print('gyroscope - X:', gyro_x, ' Y:', gyro_y, ' Z:', gyro_z)
if distance<16 or vibration_status==True:
print("Warning..")
buzzer_pin.on()
time.sleep(1)
buzzer_pin.off()
blynk.virtual_write(6, distance)
blynk.virtual_write(7, vibration_status)
blynk.virtual_write(0, accel_x/1000)
blynk.virtual_write(1, accel_y/1000)
blynk.virtual_write(2, accel_z/1000)
blynk.virtual_write(3, gyro_x/100)
blynk.virtual_write(4, gyro_y/100)
blynk.virtual_write(5, gyro_z/100)
blynk.run()
time.sleep(1) # Add a delay to avoid flooding the Blynk server
# Display data on LCD
accel_str = "Accel: X={:.2f} Y={:.2f} Z={:.2f}".format(accel_x, accel_y, accel_z)
gyro_str = "Gyro: X={:.2f} Y={:.2f} Z={:.2f}".format(gyro_x, gyro_y, gyro_z)
lcd.clear()
lcd.putstr("Distance: " + "{:.2f} cm".format(distance))
time.sleep(1)
lcd.clear()
lcd.putstr(accel_str)
time.sleep(1)
lcd.clear()
lcd.putstr(gyro_str)
time.sleep(1)