#import machine
from machine import I2C
from machine import Pin
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from time import sleep
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
S_v=0 # soil sensor value
V_v=0 # valve sensor value
M_v=0 # Motion sensor value
# pin decleration
Relay = Pin(3, Pin.OUT) # Motor for water tank relay (yellow LED)
Valve = Pin(2, Pin.OUT) # Valve for field relay (red LED)
Speaker = Pin(21, Pin.OUT) # speaker pin
Soil_sensor = Pin(10, Pin.IN,Pin.PULL_UP) # sensor inpur
Valve_sensor = Pin(11, Pin.IN,Pin.PULL_UP)
Motion_sensor = Pin(12, Pin.IN)
Valve.value(1)
Relay.value(1)
i2c = I2C(0, sda=machine.Pin(16), scl=machine.Pin(17)) #freq=50000 #I2c pins declair
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
def read_sensor():
global S_v,V_v,M_v
S_v = Soil_sensor.value() # reading soil sensor value
V_v= Valve_sensor.value()
M_v= Motion_sensor.value()
while True: # continious loop system flow
#lcd.clear()
lcd.move_to(0,0) # (Colum,row)
lcd.putstr("Field Irrigation")
lcd.move_to(0,1)
lcd.putstr("System ")
read_sensor() # reading sensor data
if S_v == 1: # soil moisture low
lcd.clear()
lcd.putstr("Activating ")
lcd.move_to(0,1)
lcd.putstr("Valve ")
Valve.value(0)
sleep(1)
while True:
#lcd.clear()
lcd.move_to(0,0)
lcd.putstr("Valve is Open ")
lcd.move_to(0,1)
lcd.putstr("Watering Field ")
sleep(1)
# print("in while loop")
read_sensor()
if S_v == 0:
Valve.value(1) # LED OFF/Relay OFF
#lcd.clear()
lcd.move_to(0,0)
lcd.putstr("Valve is OFF ")
lcd.move_to(0,1)
lcd.putstr("Field fully moist")
sleep(1)
break
#print("out of while")
if V_v == 1:
#lcd.clear()
lcd.putstr("Activating ")
lcd.move_to(0,1)
lcd.putstr("Water motor ")
Relay.value(0)
sleep(1)
while True:
#lcd.clear()
lcd.move_to(0,0)
lcd.putstr("Filling ")
lcd.move_to(0,1)
lcd.putstr("Water tank ")
sleep(1)
# print("in while loop")
read_sensor()
if V_v == 0:
Relay.value(1)
#lcd.clear()
lcd.move_to(0,0)
lcd.putstr("Motor is off ")
lcd.move_to(0,1)
lcd.putstr("Tank is full ")
sleep(1)
break
#print("outof while")
if M_v == 1: # motion detected
Speaker.value(1)
sleep(1)
elif M_v == 0:
Speaker.value(0)
# Relay.value(1)
# lcd.putstr("Lets Count 0-10!")
# sleep(2)
# lcd.clear()
# for i in range(10):
# lcd.putstr(str(i))
# sleep(1)
# lcd.clear()