import machine
import time
from blynk import Blynk
machine.Pin(10, machine.Pin.IN, machine.Pin.PULL_UP)
import network
# Replace with your WiFi network SSID and password
ssid = "Wokwi-GUEST"
password = ""
wlan = network.WLAN(network.STA_IF) # Create STA interface
wlan.active(True) # Activate the interface
# Attempt to connect to the WiFi network
if not wlan.isconnected():
print('Connecting to network...')
wlan.connect(ssid, password)
while not wlan.isconnected():
time.sleep(0.5)
print('.', end='')
print('Connected!')
# Print IP address
ip_address = wlan.ifconfig()[0]
print('IP address:', ip_address)
auth = 'lmwaPRjcxug7K0g-PG9OmigEa4R86NUr'
# Define servo pin connections
servo_pins = [machine.Pin(27), machine.Pin(26), machine.Pin(18)]
# Define servo pulse width range for min and max positions (in microseconds)
min_pulse = 500
max_pulse = 2500
#define BLYNK_TEMPLATE_ID "TMPL3TTWvCIV4"
#define BLYNK_TEMPLATE_NAME "window"
#define BLYNK_AUTH_TOKEN "lmwaPRjcxug7K0g-PG9OmigEa4R86NUr"
# Initialize Blynk connection (handle potential connection errors)
try:
blynk = Blynk(auth)
except Exception as e:
print("Error connecting to Blynk:", e)
while True:
pass # Infinite loop to prevent further execution if Blynk fails
@blynk.on(V0) # V1 corresponds to Blynk switch 1
def switch1(val):
# Check if switch is on (value 1)
if val == 0:
control_servo(servo_pins[0], 90) # Set servo 1 to 90 degrees (center)
else:
control_servo(servo_pins[0], 0) # Set servo 1 to 0 degrees
@blynk.on(V1) # V2 corresponds to Blynk switch 2
def switch2(val):
if val == 1:
control_servo(servo_pins[1], 90) # Set servo 2 to 90 degrees (center)
else:
control_servo(servo_pins[1], 0) # Set servo 2 to 0 degrees
@blynk.on(V2) # V3 corresponds to Blynk switch 3
def switch3(val):
if val == 1:
control_servo(servo_pins[2], 90) # Set servo 3 to 90 degrees (center)
else:
control_servo(servo_pins[2], 0) # Set servo 3 to 0 degrees
def control_servo(pin, angle):
pulse_width = map_value(angle, 0, 180, min_pulse, max_pulse) # Convert angle to pulse width
try:
pin.init(pin.PWM, freq=50, duty=pulse_width) # Configure pin for PWM
time.sleep_ms(20) # Delay to allow servo movement
except Exception as e:
print("Error controlling servo:", e)
finally:
pin.init(pin.IN) # Release pin
def map_value(x, in_min, in_max, out_min, out_max):
# Map a value from one range to another
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
while True:
try:
blynk.run()
except Exception as e:
print("Error running Blynk:", e)
time.sleep(0.1) # Debounce delay