from machine import Pin, Timer
import time
trigpin1 = 5
echopin1 = 12
trigpin2 = 2
echopin2 = 14
# Define Wi-Fi credentials
ssid = "YourWiFiSSID"
passw = "YourWiFiPassword"
# Define Blynk authentication token
auth_token = "YourAuthToken"
# Define Blynk virtual pins
BLYNK_VIRTUAL_PIN_PUSH = 0
BLYNK_VIRTUAL_PIN_POWER = 1
BLYNK_VIRTUAL_PIN_LIGHT_DEFECT = 2
# Define constants
A0 = 0
HIGH = 1
LOW = 0
# Initialize Pins
pin16 = Pin(16, Pin.IN) # Push Button Input
pin15 = Pin(15, Pin.OUT) # Light
pin13 = Pin(13, Pin.IN) # Sun Light Detecting LDR Sensor
pin12 = Pin(12, Pin.IN) # PIR Sensor 1
pin14 = Pin(14, Pin.IN) # PIR Sensor 2
pin4 = Pin(4, Pin.IN) # Check for Light Fault
pin_trig1 = Pin(trigpin1, Pin.OUT) # Ultra 1
pin_echopin1 = Pin(echopin1, Pin.IN) # Ultra 1
pin_trig2 = Pin(trigpin2, Pin.OUT) # Ultra 2
pin_echopin2 = Pin(echopin2, Pin.IN) # Ultra 2
pin10 = Pin(10, Pin.IN) # Power Line Check
# Set initial states
pin_trig1.value(LOW)
pin_trig2.value(LOW)
# Define delay function for microseconds
def delay_microseconds(us):
time.sleep_us(us)
# Function to measure distance using ultrasonic sensor
def measure_distance(trigger_pin, echo_pin):
trigger_pin.value(HIGH)
delay_microseconds(10)
trigger_pin.value(LOW)
pulse_time = time.ticks_us()
while echo_pin.value() == 0:
pulse_time = time.ticks_us()
while echo_pin.value() == 1:
pulse_end = time.ticks_us()
pulse_duration = time.ticks_diff(pulse_end, pulse_time)
distance = (pulse_duration * 0.034) / 2
return distance
# Function to handle Blynk virtual writes
def handle_blynk_virtual_write(pin, value):
print("Virtual Pin {} wrote value: {}".format(pin, value))
# Function to handle Blynk virtual reads
def handle_blynk_virtual_read(pin):
print("Virtual Pin {} read".format(pin))
# You can perform necessary actions based on virtual read if needed
# Setup Blynk
# Currently, Raspberry Pi Pico with MicroPython doesn't have support for Blynk.
# You may need to use an alternative IoT platform or implement the communication protocol yourself.
# Setup Blynk Timer
blynk_timer = Timer(-1)
# Setup Blynk Virtual Write and Read callbacks
# Currently, Raspberry Pi Pico with MicroPython doesn't have support for Blynk.
# You may need to use an alternative IoT platform or implement the communication protocol yourself.
def setup():
# Connect to Wi-Fi (if applicable)
# Currently, Raspberry Pi Pico with MicroPython doesn't have built-in Wi-Fi.
# You may need to use an external Wi-Fi module or connect using a different method.
# Initialize serial communication for debugging (not applicable)
# Initialize pins (pins are already initialized)
print("Setup done")
def loop(timer):
push = pin16.value()
if push == 1:
print("Emergency Alert: Client on NHAI Highway Needs Immediate Assistance")
time.sleep(10)
# Blynk.virtualWrite(BLYNK_VIRTUAL_PIN_PUSH, push) # Uncomment when using Blynk
# time.sleep(1)
power = pin10.value()
if power == 1:
# Blynk.virtualWrite(BLYNK_VIRTUAL_PIN_POWER, power) # Uncomment when using Blynk
# time.sleep(1)
c = pin13.value()
if c == 1:
print("There is no SunLight!!!!!!!!!!!!")
print("Object Detection using ultrasonic Sensor")
# Ultrasonic 1
distance1 = measure_distance(pin_trig1, pin_echopin1)
# Ultrasonic 2
distance2 = measure_distance(pin_trig2, pin_echopin2)
if distance1 < 10:
pin15.value(HIGH)
print("Object is Detected!!!!!!!!!!")
print("1st Sensor Distance:", distance1)
kk = pin4.value()
if kk == 1:
print("Light is having Defect")
time.sleep(5)
elif distance2 < 10:
pin15.value(HIGH)
print("Object is Detected!!!!!!!!!!")
print("2nd Sensor Distance:", distance2)
k = pin4.value()
if k == 1:
print("Light is having Defect")
# Blynk.virtualWrite(BLYNK_VIRTUAL_PIN_LIGHT_DEFECT, k) # Uncomment when using Blynk
# time.sleep(1)
time.sleep(5)
else:
pin15.value(LOW)
else:
pin15.value(LOW)
print("It is Day Time So Sun Light is Available!!!!!!!!!!!!")
else:
print("Power is Not There")
# Setup
setup()
# Start looping
blynk_timer.init(period=1000, mode=Timer.PERIODIC, callback=loop)