import dht
import machine
from machine import Pin, ADC
import time
import network
import BlynkLib
from BlynkTimer import BlynkTimer
import random
dhts=dht.DHT22(Pin((16)));
WIFI_SSID = 'Wokwi-GUEST'
WIFI_PASS = ''
BLYNK_AUTH = 'jM0qdYPtiZdAUBT9yt7NpDe5ciZexNkA'
# Action as Wifi Station Mode (Client)
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
'''
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
)
'''
timer = BlynkTimer()
# Will be executed Every 3 Seconds
def upload():
dhts.measure();
#print('TEMP:' + str(dhts.temperature()))
tempX = dhts.temperature() + random.randint( -3 , 3 ) # randomint.rand( 6)
#generate random value for soil moisture and crop health
soil_moisture = random.uniform(0,100)
crop_health = random.uniform(0.0,1.0)
# 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, soil_moisture)
time.sleep(0.1)
blynk.virtual_write( 3, crop_health)
print("Publish... temp:{}, soil moisture: {}, crop health:{}".format(tempX, soil_moisture, crop_health))
# Add Timers
timer.set_interval( 3, upload) # Interval : 3s
@blynk.on("connected")
def blynk_connected(ping):
print('Blynk Server is connected, ready. Ping:', ping, 'ms')
@blynk.on("disconnected")
def blynk_disconnected():
print('Blynk Server is disconnected')
# Main program
while True:
blynk.run()
timer.run()
#
time.sleep(5)