#importing the required libraries
import machine
import network
import time
import BlynkLib
from BlynkTimer import BlynkTimer
# Connecting with the Wokwi Wi-Fi and Connecting to my Blynk Cloud Console
SSID = 'Wokwi-GUEST'
PASSWORD = ''
BLYNK_AUTH_TOKEN = 'oQ3BrllUFSjoQuHLKIcv-GRYe6dKqVr5'
device1 = machine.Pin(14, machine.Pin.OUT)
device2 = machine.Pin(15, machine.Pin.OUT)
device3 = machine.Pin(20, machine.Pin.OUT)
device4 = machine.Pin(21, machine.Pin.OUT)
# Set initial device state to Low or Zero
device1.value(0)
device2.value(0)
device3.value(0)
device4.value(0)
# Making the board connection with the Wi-Fi
def connect_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
print('Connecting to network...')
while not wlan.isconnected():
print('Waiting for connection...')
time.sleep(1)
print('Network connected:', wlan.ifconfig())
connect_wifi(SSID, PASSWORD)
# Controlling the Device with the help of Blynk
def control_device(pin, value):
print(f'Control device {pin} with value {value}')
if pin == 'V0':
device1.value(int(value))
print('Device1', 'HIGH' if value == '1' else 'LOW')
blynk.log_event("lighton","Appliance one has been turned on!")
elif pin == 'V1':
device2.value(int(value))
print('Device2', 'HIGH' if value == '1' else 'LOW')
elif pin == 'V2':
device3.value(int(value))
print('Device3', 'HIGH' if value == '1' else 'LOW')
elif pin == 'V3':
device4.value(int(value))
print('Device4', 'HIGH' if value == '1' else 'LOW')
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN)
timer = BlynkTimer()
# Naming Pin values for connection and control
def blynk_connected():
print("Alert: Hi! Raspberry Pi Connected to Blynk")
@blynk.on("connected")
def blynk_connected_handler():
blynk_connected()
@blynk.on("V0")
def v0_write_handler(value):
control_device('V0', value[0])
blynk.log_event("lighton","Appliance one has been turned on!")
@blynk.on("V1")
def v1_write_handler(value):
control_device('V1', value[0])
@blynk.on("V2")
def v2_write_handler(value):
control_device('V2', value[0])
@blynk.on("V3")
def v3_write_handler(value):
control_device('V3', value[0])
while True:
blynk.run()
timer.run()
time.sleep(0.01) #reducing the usage of CPU