import network
from machine import Pin
import blynklib
from machine import Pin, time_pulse_us,PWM
import time
WIFI_SSID = "Infinix NOTE 30 5G"
WIFI_PASSWORD = "pranav......252005"
BLYNK_AUTH_TOKEN = "-5RXdgudLxF6GrgehepPaKmA9ZoUEX-S"
# Define pins for Ultrasonic Sensor
TRIGGER_PIN = 4
ECHO_PIN = 5
# Define pin for LED
LED_PIN3 = 19
LED_PIN1 = 18
LED_PIN2 = 2
LED_PIN2s = 21
LED_PIN3s = 33
LED_PIN4s = 25
LED_PINo = 14
T_COLOR = '#f5b041'
H_COLOR = '#85c1e9'
ERR_COLOR = '#444444'
# Set up the trigger and echo pins
trigger = Pin(TRIGGER_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
# Set up the LED pin
led1 = Pin(LED_PIN1, Pin.OUT)
led2 = Pin(LED_PIN2, Pin.OUT)
led3 = Pin(LED_PIN3, Pin.OUT)
# Array of LED's
led2s = Pin(LED_PIN2s, Pin.OUT)
led3s = Pin(LED_PIN3s, Pin.OUT)
led4s = Pin(LED_PIN4s, Pin.OUT)
ledo = Pin(LED_PINo, Pin.OUT)
def measure_distance():
# Send a 10us pulse to trigger the measurement
trigger.off()
time.sleep_us(2)
trigger.on()
time.sleep_us(10)
trigger.off()
# Measure the time it takes for the echo to return
duration = time_pulse_us(echo, 1)
# Calculate the distance in centimeters
distance = (duration / 2) / 29.1
return distance
distance = measure_distance()
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# Connect to WiFi
blynk = blynklib.Blynk(BLYNK_AUTH_TOKEN)
@blynk.handle_event('read V5')
def read_handler(vpin):
distance=0.0
blynk.virtual_write(ECHO_PIN, distance)
# Send input values to Blynk
# Assuming V2 is the virtual pin you want to send data to
# Example of reading a sensor value and sending it to Blynk
# Replace this with your actual sensor reading code
def main():
while True:
ledo.on()
distance = measure_distance()
print('Distance: {:.2f} cm'.format(distance))
time.sleep(1)
if(75 < distance <= 100):
led1.on()
time.sleep(1)
led1.off()
time.sleep(1)
led2s.on()
time.sleep(1)
led2s.off()
time.sleep(1)
if(50 < distance <= 75):
led2.on()
time.sleep(1)
led2.off()
time.sleep(1)
led2s.on()
led3s.on()
time.sleep(1)
led2s.off()
led3s.off()
time.sleep(1)
if(distance <= 50):
led3.on()
time.sleep(1)
led3.off()
time.sleep(1)
led2s.on()
led3s.on()
led4s.on()
time.sleep(1)
led2s.off()
led3s.off()
led4s.off()
time.sleep(1)
blynk.run()
if __name__ == '__main__':
main()