from machine import Pin
import time
import network
import BlynkLib
from BlynkTimer import BlynkTimer
# choose one
# import randomint
# import random
import dht
# import time
dhts=dht.DHT22(Pin((16)));
#AP : SSID and PASS --> WOKWi default parameters
WIFI_SSID = 'CJ_Smart farm-5G'
WIFI_PASS = '1234567890'
BLYNK_AUTH = "Bs3lI80IDXqNwrsPrOkB2hNE_R8USAbu"
led01 = Pin( 14, Pin.OUT) # GPIO14
led02 = Pin( 15, Pin.OUT) # GPIO15
wifi = network.WLAN(network.STA_IF)
if not wifi.isconnected():
print("Connecting to WiFi...")
wifi.active(True)
wifi.connect( WIFI_SSID, WIFI_PASS)
while not wifi.isconnected():
print(".", end="_")
time.sleep(1)
print('\nWifi connected, IP:', wifi.ifconfig()[0])
blynk = BlynkLib.Blynk(BLYNK_AUTH, insecure=True) # for ESP8266
timer = BlynkTimer()
def upload_temp_and_humi():
dhts.measure();
tempX = dhts.temperature() + random.randint( -3 , 3 ) # randomint.rand( 6)
humiX = dhts.humidity() + random.randint( -5, 5 ) # randomint.rand( 10)
blynk.virtual_write( 0, tempX)
time.sleep(0.1)
blynk.virtual_write( 1, humiX)
print("Publish... temp:{}, humi:{}".format(tempX, humiX))
timer.set_interval( 3, upload_temp_and_humi) # Interval : 3s
@blynk.on("connected") # 주어진 인자의 값이 달라질 때, 그 때 함수 실행.
# 이 전과 같은 값이 유지되면, 실행되지 않는다는 뜻이다.
# def blynk_connected(ping): # 함수 이름은 아무렇게나 해도 된다. 어차피 아무데서도 쓰여지지 않는다.
def connected(ping):
print('Blynk Server is Connected, ready. Ping:', ping, 'ms')
@blynk.on("disconnected")
# def blynk_disconnected():
def disconnected():
print('Blynk Server is Disconnected')
# LED01
@blynk.on("V2") # 연결된 virtual Pin 번호가 인자로 주어진다.
# def blynk_handle_vpin2(value):
def vpin2(value):
print("pin: V2 value: {}".format(value))
if value[0] == '1': # 값이 문자임에 주의!
led01.on()
else:
led01.off()
# LED02
@blynk.on("V3")
# def blynk_handle_vpin3(value):
def vpin3(value):
print("pin: V3 value: {}".format(value))
if value[0] == '1':
led02.on()
else:
led02.off()
# Main program
while True:
blynk.run()
timer.run()
time.sleep(1)