from machine import Pin, time_pulse_us
from time import sleep, sleep_us
import network
import urequests
import time
# -------------------------------
# WIFI + THINGSPEAK
# -------------------------------
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
WRITE_API_KEY = "QEHOL3OVDXGB4M2H"
THINGSPEAK_URL = "https://api.thingspeak.com/update"
last_upload_time = 0
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
print("Connecting to WiFi", end="")
while not wlan.isconnected():
print(".", end="")
sleep(0.3)
print("\nConnected!")
print("IP:", wlan.ifconfig()[0])
def send_to_thingspeak(value):
global last_upload_time
now = time.time()
if now - last_upload_time < 15:
print("ThingSpeak skipped - wait 15 seconds")
return
url = THINGSPEAK_URL + "?api_key=" + WRITE_API_KEY + "&field1=" + str(value)
try:
response = urequests.get(url)
print("Sent to ThingSpeak:", value)
print("ThingSpeak response:", response.text)
response.close()
last_upload_time = now
except Exception as e:
print("ThingSpeak upload failed:", e)
# -------------------------------
# LED SETUP (Traffic Lights)
# -------------------------------
red_a = Pin(0, Pin.OUT)
yellow_a = Pin(1, Pin.OUT)
green_a = Pin(2, Pin.OUT)
red_b = Pin(3, Pin.OUT)
yellow_b = Pin(4, Pin.OUT)
green_b = Pin(5, Pin.OUT)
# -------------------------------
# INPUTS (Buttons)
# -------------------------------
button_a = Pin(6, Pin.IN, Pin.PULL_UP)
button_b = Pin(12, Pin.IN, Pin.PULL_UP)
# -------------------------------
# OUTPUT (Buzzer)
# -------------------------------
buzzer = Pin(7, Pin.OUT)
# -------------------------------
# ULTRASONIC SENSORS
# -------------------------------
trig_a = Pin(8, Pin.OUT)
echo_a = Pin(9, Pin.IN)
trig_b = Pin(10, Pin.OUT)
echo_b = Pin(11, Pin.IN)
last_dist_a = 999
last_dist_b = 999
# -------------------------------
# TURN EVERYTHING OFF
# -------------------------------
def all_off():
red_a.off()
yellow_a.off()
green_a.off()
red_b.off()
yellow_b.off()
green_b.off()
buzzer.off()
# -------------------------------
# TRAFFIC LIGHT STATES
# -------------------------------
def lane_a_green():
red_a.off()
yellow_a.off()
green_a.on()
red_b.on()
yellow_b.off()
green_b.off()
def lane_a_yellow():
red_a.off()
yellow_a.on()
green_a.off()
red_b.on()
yellow_b.off()
green_b.off()
def lane_b_green():
red_a.on()
yellow_a.off()
green_a.off()
red_b.off()
yellow_b.off()
green_b.on()
def lane_b_yellow():
red_a.on()
yellow_a.off()
green_a.off()
red_b.off()
yellow_b.on()
green_b.off()
# -------------------------------
# ULTRASONIC DISTANCE FUNCTION
# -------------------------------
def get_distance(trig, echo):
trig.off()
sleep_us(2)
trig.on()
sleep_us(10)
trig.off()
duration = time_pulse_us(echo, 1, 30000)
if duration < 0:
return 999
return (duration * 0.0343) / 2
# -------------------------------
# SENSOR CHECK EVERY 5 SECONDS
# -------------------------------
def check_ultrasonic():
dist_a = get_distance(trig_a, echo_a)
dist_b = get_distance(trig_b, echo_b)
print("---- Sensor Check ----")
print("Lane A distance:", round(dist_a, 2), "cm")
print("Lane B distance:", round(dist_b, 2), "cm")
if dist_a < 25:
print("Car detected in Lane A")
if dist_b < 25:
print("Car detected in Lane B")
print("Next check in 5 seconds...\n")
return dist_a, dist_b
# -------------------------------
# EMERGENCY MODE
# -------------------------------
def emergency_lane_a():
print("\nEmergency button pressed in Lane A")
print("Switching light in Lane A to GREEN to ensure the emergency vehicle can go through")
print("ThingSpeak value: 100\n")
all_off()
green_a.on()
red_b.on()
send_to_thingspeak(100)
sleep(1)
for _ in range(6):
buzzer.on()
sleep(0.15)
buzzer.off()
sleep(0.15)
def emergency_lane_b():
print("\nEmergency button pressed in Lane B")
print("Switching light in Lane B to GREEN to ensure the emergency vehicle can go through")
print("ThingSpeak value: 200\n")
all_off()
green_b.on()
red_a.on()
send_to_thingspeak(200)
sleep(1)
for _ in range(6):
buzzer.on()
sleep(0.15)
buzzer.off()
sleep(0.15)
# -------------------------------
# WAIT FUNCTION WITH CHECKS
# -------------------------------
def wait_with_button_check(seconds):
global last_dist_a, last_dist_b
elapsed = 0.0
while elapsed < seconds:
if int(elapsed) % 5 == 0 and abs(elapsed - int(elapsed)) < 0.01:
last_dist_a, last_dist_b = check_ultrasonic()
if button_a.value() == 0:
emergency_lane_a()
return True
if button_b.value() == 0:
emergency_lane_b()
return True
sleep(0.1)
elapsed += 0.1
return False
# -------------------------------
# STARTUP
# -------------------------------
print("System starting...")
connect_wifi()
# -------------------------------
# MAIN LOOP
# -------------------------------
while True:
lane_a_green()
if wait_with_button_check(6):
continue
lane_a_yellow()
if wait_with_button_check(3):
continue
lane_b_green()
if wait_with_button_check(6):
continue
lane_b_yellow()
if wait_with_button_check(3):
continue