from machine import Pin, SoftI2C
from machine import Pin, ADC
from i2c_lcd import I2cLcd
import time
import _thread
import network, BlynkLib
#network
wifi_ssid = "Wokwi-GUEST"
wifi_pass = ""
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(wifi_ssid, wifi_pass)
while not station.isconnected() :
pass
print("Connected", station.ifconfig())
auth = "5vziJ6zHCXpXikBPSJsY7yjEHFKkxlaD"
blynk = BlynkLib.Blynk(auth)
# PUMP
pump = Pin(33, Pin.OUT)
pump.off()
# LCD
i2c = SoftI2C(scl=Pin(22), sda=Pin(21),freq=100000)
lcd = I2cLcd(i2c, 0x27,2,16)
time.sleep(1)
lcd.clear()
# Soil Moisture
soil = ADC(Pin(35))
m = 50
min_moisture=0
max_moisture=4095
soil.atten(ADC.ATTN_11DB) #Full range: 3.3v
soil.width(ADC.WIDTH_12BIT) #range 0 to 4095
def check_moisture():
print('Check Moisture Starting...')
global m
while True:
try:
soil.read()
time.sleep(2)
m = (max_moisture-soil.read())*100/(max_moisture-min_moisture)
moisture = 'Soil Moisture: {:.1f} %'.format(m)
print("Soil Moisture: " + "%.1f" % m +"% (adc: "+str(soil.read())+")")
time.sleep(5)
except:
pass
# START
text = 'Starting...'
lcd.putstr(text)
# RUN
global pump_status
pump_status = 'OFF'
def show_lcd():
while True:
try:
moisture = 'Moisture: {:.1f} %'.format(m)
pump_s = 'PUMP: {:}'.format(pump_status)
lcd.clear()
lcd.putstr(pump_s)
time.sleep(2)
lcd.clear()
lcd.putstr(moisture)
read_moisture()
time.sleep(2)
except:
pass
def auto_pump():
global pump_status
while True:
if m < 50:
time.sleep(1)
pump.on()
pump_status = 'ON (AUTO)'
time.sleep(5)
elif t < 50: # TURN ON PUMP 5 sec
pump.off()
pump_status = 'OFF (AUTO)'
time.sleep(10)
else:
pass
def read_moisture():
global m
blynk.virtual_write(0, float(m))
_thread.start_new_thread(check_moisture,())
_thread.start_new_thread(show_lcd,())
_thread.start_new_thread(auto_pump,())