from machine import Pin
import time, network, random
import BlynkLib
from BlynkTimer import BlynkTimer
#AP : SSID and PASS --> WOKWi default parameters
# WIFI_SSID = 'CJ_Smart farm-5G'
# WIFI_PASS = '1234567890'
#wifi 연결
ssid = 'CJ_Smart farm-5G'
password= '1234567890'
wifi = network.WLAN(network.STA_IF)
if not wifi.isconnected():
print("Connecting to WiFi...")
wifi.active(True)
wifi.connect( ssid, password)
while not wifi.isconnected():
print(".", end="_")
time.sleep(1)
print('\nWifi connected, IP:', wifi.ifconfig()[0])
import dht
dhts=dht.DHT22(Pin((16)));
red_led = Pin( 14, Pin.OUT) # GPIO14
blue_led = Pin( 15, Pin.OUT) # GPIO15
# Blynk 연결
BLYNK_AUTH = "R0-WaEsABBTA3q6Y1mkYGQ8lueo94JZ4"
blynk = BlynkLib.Blynk(BLYNK_AUTH, insecure=True)
timer = BlynkTimer()
def upload_temp_and_humi():
dhts.measure();
temp = dhts.temperature() + random.randint( -3 , 3 )
humi = dhts.humidity() + random.randint( -5, 5 )
blynk.virtual_write( 2, temp)
time.sleep(0.1)
blynk.virtual_write( 3, humi)
print("Publish... temp:{temp}, humi:{humi}")
timer.set_interval( 3, upload_temp_and_humi) # Interval : 3s
@blynk.on("connected") # 주어진 인자의 값이 달라질 때, 그 때 함수 실행.
# 이 전과 같은 값이 유지되면, 실행되지 않는다는 뜻이다.
def connected(ping): # 함수 이름은 아무렇게나 해도 된다. 어차피 아무데서도 쓰여지지 않는다.
print('Blynk Server is Connected, ready. Ping:', ping, 'ms')
@blynk.on("disconnected")
def disconnected():
print('Blynk Server is Disconnected')
# Red LED
@blynk.on("V0") # 연결된 virtual Pin 번호가 인자로 주어진다.
def vpin0(value):
if value[0] == '1': # 값이 문자임에 주의!
red_led.on()
status = 'ON'
else:
red_led.off()
status = 'OFF'
print(f"Red_Led is {status}")
# Blue LED
@blynk.on("V1")
def vpin3(value):
if value[0] == '1':
blue_led.on()
status = 'ON'
else:
blue_led.off()
status = 'OFF'
print(f"Blue_Led is {status}")
# Main program
while True:
blynk.run()
timer.run()
time.sleep(1)