from machine import Pin
import time
import network
import BlynkLib
# Blynk Token
BLYNK_AUTH_TOKEN = "fIB4lyKHVAcWp5PziFcqHP7va3NsI__l"
# Wi-Fi settings
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
# Setup Pins
PIR1 = Pin(3, Pin.IN)
PIR2 = Pin(11, Pin.IN)
led_pins = [Pin(16, Pin.OUT), Pin(17, Pin.OUT), Pin(18, Pin.OUT), Pin(19, Pin.OUT)]
BUZZER1 = Pin(21, Pin.OUT)
BUZZER2 = Pin(22, Pin.OUT)
def connect_to_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
print("Connecting to Wi-Fi...")
while not wlan.isconnected():
print(".", end="")
time.sleep(1)
print("Wi-Fi connected")
# Initialize connections
connect_to_wifi()
# Initialize blynk
print("Connecting to Blynk server...")
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN)
print("Connected!")
# This seems to work but takes sometimes 30sec to toggle the light
# Note: the 'value' varible sent from the server is a python list
@blynk.on("V0")
def v0_write_handler(value):
print(value)
# The server switch will send a '1' or '0' string in the first
# element of a python list. We need to extract it from the list
# 'value[0]' and convert it to an integer so that we can set the
# pin value
led_pins[0].value(int(value[0]))
@blynk.on("V1")
def v1_write_handler(value):
print(value)
# The server switch will send a '1' or '0' string in the first
# element of a python list. We need to extract it from the list
# 'value[0]' and convert it to an integer so that we can set the
# pin value
led_pins[1].value(int(value[0]))
@blynk.on("V2")
def v2_write_handler(value):
print(value)
# The server switch will send a '1' or '0' string in the first
# element of a python list. We need to extract it from the list
# 'value[0]' and convert it to an integer so that we can set the
# pin value
led_pins[2].value(int(value[0]))
@blynk.on("V3")
def v3_write_handler(value):
print(value)
# The server switch will send a '1' or '0' string in the first
# element of a python list. We need to extract it from the list
# 'value[0]' and convert it to an integer so that we can set the
# pin value
led_pins[3].value(int(value[0]))
@blynk.on("V4")
def v4_write_handler(value):
state = int(value[0])
if state:
for pin in led_pins:
pin.value(state)
@blynk.on("V5")
def v5_write_handler(value):
state = int(value[0])
if state:
BUZZER1.value(state)
BUZZER2.value(state)
@blynk.on("V6")
def v4_write_handler(value):
state = int(value[0])
# Toggle all LEDs based on the state of V4
for pin in led_pins:
pin.value(state)
if state:
print("All LEDs turned ON")
else:
print("All LEDs turned OFF")
# Main loop
while True:
blynk.run()
time.sleep(0.1)
Loading
pi-pico-w
pi-pico-w