# import machine
from machine import Pin
import time
import network
WIFI_SSID = 'aaa '
WIFI_PASS = '111'
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])
# import BlynkLib
# from BlynkTimer import BlynkTimer
# import dht
# dhts=dht.DHT11(Pin((16)));
# dhts=dht.DHT22(Pin((16)));
###########################################################################
# Wifi AP : SSID and PASS --> WOKWi default parameters
###########################################################################
# Wifi AP : SSID and PASS --> Real environment
# WIFI_SSID = 'U+NetF424_5G'
# WIFI_PASS = 'D0E77#HF81'
###########################################################################
# Token of Device in blynk.cloud Platform --> MUST modify the value !
# http://blynk.cloud
BLYNK_AUTH = "Bs3lI80IDXqNwrsPrOkB2hNE_R8USAbu"
###########################################################################
# Relationships of datastream and vPin#
# temp --> Virtual Pin 0, double, .C, -20 ~ 50, #.##
# humi --> Virtual Pin 1, integer, %, 0 ~ 100,
# led01 --> Virtual Pin 2, integer, , 0/1,
# led02 --> Virtual Pin 3, integer, , 0/1,
###########################################################################
led01 = Pin( 14, Pin.OUT) # GPIO14
led02 = Pin( 15, Pin.OUT) # GPIO15
###########################################################################
# Action as Wifi Station Mode (Client)
#
# Initialize Blynk Client and BlynkTimer
# blynk = BlynkLib.Blynk(BLYNK_AUTH)
blynk = BlynkLib.Blynk(BLYNK_AUTH, insecure=True) # for ESP8266
'''
# blynk = BlynkLib.Blynk(BLYNK_AUTH,
# insecure=True, # disable SSL/TLS
# server='blynk.cloud', # set server address
# port=80, # set server port
# heartbeat=30, # set heartbeat to 30 secs
# log=print # use print function for debug logging
# )
'''
########################################################
# Example for BlynkTimer
timer = BlynkTimer()
# Will be executed Every 3 Seconds
# def upload_temp_and_humi():
# #
# dhts.measure();
# #print('TEMP:' + str(dhts.temperature()))
# tempX = dhts.temperature() + random.randint( -3 , 3 ) # randomint.rand( 6)
# humiX = dhts.humidity() + random.randint( -5, 5 ) # randomint.rand( 10)
# # send value to Virtual Pin0 & Pin1 and store it in Blynk Cloud
# blynk.virtual_write( 0, tempX)
# time.sleep(0.1)
# blynk.virtual_write( 1, humiX)
# #
# print("Publish... temp:{}, humi:{}".format(tempX, humiX))
# Add Timers
# 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)