from machine import Pin, PWM
from time import sleep
from math import pi
import network
import time
import BlynkLib
# =========================
# BLYNK CONFIG (DO NOT SHARE TOKEN)
#define BLYNK_TEMPLATE_ID "TMPL26BB3hJs8"
#define BLYNK_TEMPLATE_NAME "Servo"
#define BLYNK_AUTH_TOKEN "l6qhrjCn2dF6oXU10Eutvbs7oaB3h18N"
# =========================
BLYNK_AUTH_TOKEN = "l6qhrjCn2dF6oXU10Eutvbs7oaB3h18N"
BLYNK_TEMPLATE_ID = "TMPL26BB3hJs8"
BLYNK_TEMPLATE_NAME = "Servo"
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
# =========================
# SERVO SETUP
# =========================
servo = PWM(Pin(14), freq=50)
def set_servo_angle(angle):
duty = int(((2 * angle / 180) + 0.5) / 20 * 1023)
servo.duty(duty)
set_servo_angle(0)
# =========================
# WIFI
# =========================
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
print("Connecting to WiFi....")
c = 0
while not wifi.isconnected():
print("Connecting..", c)
c += 1
time.sleep(1)
print("Connected to WiFi")
# =====================================
# BLYNK CONNECTION
# =====================================
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN, insecure=True)
# V0 = angle
@blynk.on("connected")
def blynk_connected():
print("Blynk is connected.")
@blynk.on("V1")
def V1(value):
angle = int(value[0])
print(f"Servo angle: {angle}° or {round(angle * pi / 180, 2)} radians")
set_servo_angle(angle)
while True:
blynk.run()
sleep(0.02)