import BlynkLib
from BlynkTimer import BlynkTimer
import network
from machine import Pin,PWM
import time
blynk_timer = BlynkTimer()
# Store the current time when the system boots
bootTime = time.time()
# This will set a golbal pause time (needed for correct print out)
SLEEP_TIME = 1
# Blynk API token (needed to connect to the Blynk website)
BLYNK_TEMPLATE_ID = "TMPL2S32i5LU1"
BLYNK_TEMPLATE_NAME = "Pi Pico"
BLYNK_AUTH_TOKEN = "Kj7HVqH9o2zx5nJYwmSFjiqduxKOU2lX"
# Define an LED/SWITCH accessor
LED1 = Pin(0, Pin.OUT)
SWITCH1 = Pin(1, Pin.IN, Pin.PULL_UP)
# Set up PWM for the servo control
SERVO1 = PWM(Pin(15))
SERVO1.freq(50) # Period of 20ms
SERVO1.duty_u16(1638) # 0 degrees 180 Degrees 8191
# ===================================
# Connect to virtual WiFi
# This can take a while :(
# ===================================
print("Connecting to WiFi ", end="")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
while not wlan.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
time.sleep(SLEEP_TIME)
print(wlan.ifconfig())
# ====================================
# Starting Blynk
# =====================================
# Connect to the Blynk cloud (Note: Networking must be up first)
# use insecure = True?????
print("Connecting to Blynk server...")
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN)
# =======================================
# Event Handlers for incomming Blynk
# message interaction
# =======================================
# This function sends a ping to test the speed of the connection to the cloud server
# Seems really slow, maybe the full pay version may be better
@blynk.on("connected")
def blynk_connected(ping):
# We need this to check if things changed while offline
blynk.sync_virtual()
print('Blynk ready. Ping:', ping, 'ms')
blynk.virtual_write(3, "test6")
@blynk.on("disconnected")
def blynk_disconnected():
# Note: This will fire once during startup
print("Blynk has disconnected from the server")
# 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
LED1.value(int(value[0]))
# This recives the data from the Blynk slider to move
# the servo
@blynk.on("V2")
def v3_write_handler(value):
print(int(value[0]))
SERVO1.duty_u16(int(value[0]))
time.sleep(2)
# ============================================
# Functions for sending data to Blynk.Console
# ============================================
def poll_switch():
# Print out the current value on Pin 2 to the local console
print(SWITCH1.value())
# Send the current value on Pin 2 to Virtual pin 1 in Blynk.Console
# Can be 1 or 0
blynk.virtual_write(1, SWITCH1.value())
def send_uptime():
uptime = time.time() - bootTime
blynk.virtual_write(2, uptime)
# ===========================================
# Initial setup
# ===========================================
def setup():
# Run the poll_switch() function every 10sec
blynk_timer.set_interval(10, poll_switch)
blynk_timer.set_interval(10, send_uptime)
# Run the setup function once when app starts
setup()
# ============================================
# Main application loop
# ============================================
while True:
# This connects to the Blynk server on each iteration of the
# loop and sends 'blynk.virtual_write()' and recives '@blynk.on()'
# any changes
blynk.run()
# This will poll all of the timers
blynk_timer.run()
# sleep for 1 secound
time.sleep(1)
Loading
pi-pico-w
pi-pico-w