'''
from machine import Pin, ADC, I2C, time_pulse_us
from dht import DHT22
from utime import sleep
from pico_i2c_lcd import I2cLcd
# =========================
# LCD SETUP
# =========================
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# =========================
# DHT22
# =========================
dht = DHT22(Pin(15))
# =========================
# ULTRASONIC SENSOR
# =========================
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
# =========================
# LEDS
# =========================
l_green = Pin(4, Pin.OUT)
l_red = Pin(5, Pin.OUT)
# =========================
# BUZZER
# =========================
buzzer = Pin(6, Pin.OUT)
# =========================
# SOIL SENSOR (POTENTIOMETER)
# =========================
soil = ADC(26)
# =========================
# FUNCTION : DISTANCE
# =========================
def get_distance():
trig.low()
sleep(0.01)
trig.high()
sleep(0.00001)
trig.low()
duration = time_pulse_us(echo, 1, 30000)
if duration < 0:
return 999
distance = (duration * 0.0343) / 2
return round(distance, 1)
# =========================
# MAIN LOOP
# =========================
while True:
try:
# -------------------
# DHT22
# -------------------
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# -------------------
# SOIL MOISTURE
# -------------------
raw = soil.read_u16()
moisture = 100 - int((raw / 65535) * 100)
# -------------------
# ULTRASONIC
# -------------------
distance = get_distance()
# -------------------
# PLANT STATUS
# -------------------
if moisture < 20:
plant_status = "WATER NEEDED"
elif moisture < 40:
plant_status = "LOW MOISTURE"
else:
plant_status = "GOOD"
# -------------------
# SECURITY STATUS
# -------------------
if distance < 15:
security = "INTRUDER"
l_red.on()
l_green.off()
buzzer.on()
else:
security = "SAFE"
l_green.on()
l_red.off()
buzzer.off()
# -------------------
# SERIAL MONITOR
# -------------------
print("----------------------------------")
print("Temperature :", temp, "C")
print("Humidity :", hum, "%")
print("Moisture :", moisture, "%")
print("Distance :", distance, "cm")
print("Plant :", plant_status)
print("Security :", security)
print("----------------------------------")
# -------------------
# LCD DISPLAY
# -------------------
lcd.clear()
# First line always temperature & humidity
lcd.move_to(0, 0)
lcd.putstr("T:{}C H:{}%".format(int(temp), int(hum)))
# Second line shows meaningful status
lcd.move_to(0, 1)
if distance < 15:
lcd.putstr("INTRUDER ALERT")
elif moisture < 20:
lcd.putstr("WATER PLANT !")
elif moisture < 40:
lcd.putstr("LOW MOISTURE")
else:
lcd.putstr("PLANT IS GOOD")
sleep(2)
except Exception as e:
print("Error:", e)
lcd.clear()
lcd.putstr("Sensor Error")
sleep(2)'''
'''
from machine import Pin, ADC, I2C, time_pulse_us
from dht import DHT22
from utime import sleep
from pico_i2c_lcd import I2cLcd
# =========================
# LCD SETUP
# =========================
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# =========================
# DHT22
# =========================
dht = DHT22(Pin(15))
# =========================
# ULTRASONIC SENSOR
# =========================
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
# =========================
# LEDS
# =========================
l_green = Pin(4, Pin.OUT)
l_red = Pin(5, Pin.OUT)
# =========================
# BUZZER
# =========================
buzzer = Pin(6, Pin.OUT)
# =========================
# SOIL SENSOR
# =========================
soil = ADC(26)
# =========================
# ALARM STATUS
# =========================
alarm_triggered = False
# =========================
# FUNCTION : DISTANCE
# =========================
def get_distance():
trig.low()
sleep(0.01)
trig.high()
sleep(0.00001)
trig.low()
duration = time_pulse_us(echo, 1, 30000)
if duration < 0:
return 999
distance = (duration * 0.0343) / 2
return round(distance, 1)
# =========================
# MAIN LOOP
# =========================
while True:
try:
# -------------------
# DHT22
# -------------------
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# -------------------
# SOIL MOISTURE
# -------------------
raw = soil.read_u16()
moisture = 100 - int((raw / 65535) * 100)
# -------------------
# ULTRASONIC
# -------------------
distance = get_distance()
# -------------------
# PLANT STATUS
# -------------------
if moisture < 20:
plant_status = "WATER NEEDED"
elif moisture < 40:
plant_status = "LOW MOISTURE"
else:
plant_status = "GOOD"
# -------------------
# TRIGGER ALARM
# -------------------
if distance < 15:
alarm_triggered = True
# -------------------
# SECURITY STATUS
# -------------------
if alarm_triggered:
security = "INTRUDER"
l_red.on()
l_green.off()
buzzer.on()
else:
security = "SAFE"
l_green.on()
l_red.off()
buzzer.off()
# -------------------
# SERIAL MONITOR
# -------------------
print("----------------------------------")
print("Temperature :", temp, "C")
print("Humidity :", hum, "%")
print("Moisture :", moisture, "%")
print("Distance :", distance, "cm")
print("Plant :", plant_status)
print("Security :", security)
print("----------------------------------")
# -------------------
# LCD DISPLAY
# -------------------
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("T:{}C H:{}%".format(int(temp), int(hum)))
lcd.move_to(0, 1)
if alarm_triggered:
lcd.putstr("INTRUDER ALERT")
elif moisture < 20:
lcd.putstr("WATER PLANT !")
elif moisture < 40:
lcd.putstr("LOW MOISTURE")
else:
lcd.putstr("PLANT IS GOOD")
sleep(2)
except Exception as e:
print("Error:", e)
lcd.clear()
lcd.putstr("Sensor Error")
sleep(2)
'''
'''
from machine import Pin
from utime import sleep
buzzer = Pin(6, Pin.OUT)
alarm_triggered = False
while True:
if not alarm_triggered:
print("Alarm Triggered!")
alarm_triggered = True
if alarm_triggered:
buzzer.on()
sleep(1)'''
'''
#fine working code without ldr sensor
from machine import Pin, ADC, I2C, time_pulse_us
from dht import DHT22
from utime import sleep
from pico_i2c_lcd import I2cLcd
# =========================
# LCD SETUP
# =========================
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# =========================
# DHT22
# =========================
dht = DHT22(Pin(15))
# =========================
# ULTRASONIC SENSOR
# =========================
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
# =========================
# LEDS
# =========================
l_green = Pin(4, Pin.OUT)
l_red = Pin(5, Pin.OUT)
# =========================
# BUZZER
# =========================
buzzer = Pin(6, Pin.OUT)
# =========================
# SOIL SENSOR
# =========================
soil = ADC(26)
# =========================
# ALARM LATCH
# =========================
alarm_triggered = False
# =========================
# FUNCTION : DISTANCE
# =========================
def get_distance():
trig.low()
sleep(0.01)
trig.high()
sleep(0.00001)
trig.low()
duration = time_pulse_us(echo, 1, 30000)
if duration < 0:
return 999
distance = (duration * 0.0343) / 2
return round(distance, 1)
# =========================
# MAIN LOOP
# =========================
while True:
try:
# -------------------
# DHT22
# -------------------
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# -------------------
# SOIL MOISTURE
# -------------------
raw = soil.read_u16()
moisture = 100 - int((raw / 65535) * 100)
# -------------------
# ULTRASONIC
# -------------------
distance = get_distance()
# -------------------
# PLANT STATUS
# -------------------
if moisture < 20:
plant_status = "WATER NEEDED"
elif moisture < 40:
plant_status = "LOW MOISTURE"
else:
plant_status = "GOOD"
# -------------------
# ALARM TRIGGER
# -------------------
if distance < 15:
alarm_triggered = True
# -------------------
# SECURITY STATUS
# -------------------
if alarm_triggered:
security = "INTRUDER"
l_red.on()
l_green.off()
# Repeating buzzer pattern
for i in range(5):
buzzer.on()
sleep(0.1)
buzzer.off()
sleep(0.1)
else:
security = "SAFE"
l_green.on()
l_red.off()
buzzer.off()
# -------------------
# SERIAL MONITOR
# -------------------
print("----------------------------------")
print("Temperature :", temp, "C")
print("Humidity :", hum, "%")
print("Moisture :", moisture, "%")
print("Distance :", distance, "cm")
print("Plant :", plant_status)
print("Security :", security)
print("Alarm :", alarm_triggered)
print("----------------------------------")
# -------------------
# LCD DISPLAY
# -------------------
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("T:{}C H:{}%".format(int(temp), int(hum)))
lcd.move_to(0, 1)
if alarm_triggered:
lcd.putstr("INTRUDER ALERT")
elif moisture < 20:
lcd.putstr("WATER PLANT !")
elif moisture < 40:
lcd.putstr("LOW MOISTURE")
else:
lcd.putstr("PLANT IS GOOD")
sleep(2)
except Exception as e:
print("Error:", e)
lcd.clear()
lcd.putstr("Sensor Error")
sleep(2)
'''
'''
#this is a fine working code with ldr sensor
from machine import Pin, ADC, I2C, time_pulse_us
from dht import DHT22
from utime import sleep
from pico_i2c_lcd import I2cLcd
# =========================
# LCD
# =========================
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# =========================
# DHT22
# =========================
dht = DHT22(Pin(15))
# =========================
# ULTRASONIC
# =========================
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
# =========================
# LEDS
# =========================
l_green = Pin(4, Pin.OUT)
l_red = Pin(5, Pin.OUT)
# =========================
# BUZZER
# =========================
buzzer = Pin(6, Pin.OUT)
# =========================
# SOIL SENSOR
# =========================
soil = ADC(26)
# =========================
# LDR SENSOR
# =========================
ldr = ADC(27)
# =========================
# ALARM VARIABLES
# =========================
alarm_triggered = False
trigger_count = 0
# =========================
# DISTANCE FUNCTION
# =========================
def get_distance():
trig.low()
sleep(0.01)
trig.high()
sleep(0.00001)
trig.low()
duration = time_pulse_us(echo, 1, 30000)
if duration < 0:
return 999
distance = (duration * 0.0343) / 2
return round(distance, 1)
# =========================
# MAIN LOOP
# =========================
while True:
try:
# -------------------
# DHT22
# -------------------
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# -------------------
# SOIL
# -------------------
raw_soil = soil.read_u16()
moisture = 100 - int((raw_soil / 65535) * 100)
# -------------------
# LDR
# -------------------
raw_light = ldr.read_u16()
light = int((raw_light / 65535) * 100)
# -------------------
# DISTANCE
# -------------------
distance = get_distance()
# -------------------
# PLANT STATUS
# -------------------
if moisture < 20:
plant_status = "WATER NEEDED"
elif temp > 35 and light < 40:
plant_status = "HOT+LOW LIGHT"
elif temp > 35:
plant_status = "TOO HOT"
elif light < 30:
plant_status = "LOW LIGHT"
else:
plant_status = "GOOD"
# -------------------
# FALSE ALARM FILTER
# -------------------
if distance < 15:
trigger_count += 1
else:
trigger_count = 0
# -------------------
# ALARM TRIGGER
# -------------------
if trigger_count >= 3:
alarm_triggered = True
# -------------------
# SECURITY
# -------------------
if alarm_triggered:
security = "INTRUDER"
l_red.on()
l_green.off()
for i in range(5):
buzzer.on()
sleep(0.1)
buzzer.off()
sleep(0.1)
else:
security = "SAFE"
l_green.on()
l_red.off()
buzzer.off()
# -------------------
# SERIAL OUTPUT
# -------------------
print("--------------------------------")
print("Temp :", temp, "C")
print("Humidity :", hum, "%")
print("Moisture :", moisture, "%")
print("Light :", light, "%")
print("Distance :", distance, "cm")
print("Plant :", plant_status)
print("Security :", security)
print("Triggers :", trigger_count)
print("--------------------------------")
# -------------------
# LCD
# -------------------
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("T:{} H:{}%".format(int(temp), int(hum)))
lcd.move_to(0, 1)
if alarm_triggered:
lcd.putstr("INTRUDER ALERT")
elif moisture < 20:
lcd.putstr("WATER PLANT")
elif temp > 35 and light < 40:
lcd.putstr("HOT LOW LIGHT")
elif temp > 35:
lcd.putstr("TEMP TOO HIGH")
elif light < 30:
lcd.putstr("LOW SUNLIGHT")
else:
lcd.putstr("PLANT IS GOOD")
sleep(2)
except Exception as e:
print("Error:", e)
lcd.clear()
lcd.putstr("Sensor Error")
sleep(2)
'''
'''
#here is the code with updated button
from machine import Pin, ADC, I2C, time_pulse_us
from dht import DHT22
from utime import sleep
from pico_i2c_lcd import I2cLcd
# =========================
# LCD
# =========================
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# =========================
# DHT22
# =========================
dht = DHT22(Pin(15))
# =========================
# ULTRASONIC
# =========================
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
# =========================
# LEDS
# =========================
l_green = Pin(4, Pin.OUT)
l_red = Pin(5, Pin.OUT)
# =========================
# BUZZER
# =========================
buzzer = Pin(6, Pin.OUT)
# =========================
# RESET BUTTON
# Connect:
# GP7 ---- Button ---- GND
# =========================
reset_btn = Pin(7, Pin.IN, Pin.PULL_UP)
# =========================
# SOIL SENSOR
# =========================
soil = ADC(26)
# =========================
# LDR SENSOR
# =========================
ldr = ADC(27)
# =========================
# ALARM VARIABLES
# =========================
alarm_triggered = False
trigger_count = 0
# =========================
# DISTANCE FUNCTION
# =========================
def get_distance():
trig.low()
sleep(0.01)
trig.high()
sleep(0.00001)
trig.low()
duration = time_pulse_us(echo, 1, 30000)
if duration < 0:
return 999
distance = (duration * 0.0343) / 2
return round(distance, 1)
# =========================
# MAIN LOOP
# =========================
while True:
try:
# -------------------
# RESET BUTTON
# -------------------
if reset_btn.value() == 0:
alarm_triggered = False
trigger_count = 0
buzzer.off()
l_red.off()
l_green.on()
lcd.clear()
lcd.putstr("ALARM RESET")
sleep(1)
# -------------------
# DHT22
# -------------------
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# -------------------
# SOIL SENSOR
# -------------------
raw_soil = soil.read_u16()
moisture = 100 - int((raw_soil / 65535) * 100)
# -------------------
# LDR SENSOR
# -------------------
raw_light = ldr.read_u16()
if raw_light < 15000:
light_status = "LOW SUNLIGHT"
elif raw_light < 35000:
light_status = "MEDIUM LIGHT"
else:
light_status = "GOOD LIGHT"
# -------------------
# ULTRASONIC
# -------------------
distance = get_distance()
# -------------------
# PLANT STATUS
# -------------------
if moisture < 20:
plant_status = "WATER NEEDED"
elif temp > 35 and raw_light < 35000:
plant_status = "HOT+LOW LIGHT"
elif temp > 35:
plant_status = "TOO HOT"
elif raw_light < 15000:
plant_status = "LOW SUNLIGHT"
else:
plant_status = "PLANT GOOD"
# -------------------
# FALSE ALARM FILTER
# -------------------
if distance < 15:
trigger_count += 1
else:
trigger_count = 0
# -------------------
# ALARM TRIGGER
# -------------------
if trigger_count >= 3:
alarm_triggered = True
# -------------------
# SECURITY STATUS
# -------------------
if alarm_triggered:
security = "INTRUDER"
l_red.on()
l_green.off()
for i in range(5):
buzzer.on()
sleep(0.1)
buzzer.off()
sleep(0.1)
else:
security = "SAFE"
l_green.on()
l_red.off()
buzzer.off()
# -------------------
# SERIAL MONITOR
# -------------------
print("--------------------------------")
print("Temperature :", temp, "C")
print("Humidity :", hum, "%")
print("Moisture :", moisture, "%")
print("LDR Raw :", raw_light)
print("Light :", light_status)
print("Distance :", distance, "cm")
print("Plant :", plant_status)
print("Security :", security)
print("Triggers :", trigger_count)
print("--------------------------------")
# -------------------
# LCD DISPLAY
# -------------------
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("T:{}C H:{}%".format(int(temp), int(hum)))
lcd.move_to(0, 1)
if alarm_triggered:
lcd.putstr("INTRUDER ALERT")
elif moisture < 20:
lcd.putstr("WATER PLANT")
elif temp > 35 and raw_light < 35000:
lcd.putstr("HOT LOW LIGHT")
elif temp > 35:
lcd.putstr("TEMP TOO HIGH")
elif raw_light < 15000:
lcd.putstr("LOW SUNLIGHT")
else:
lcd.putstr("PLANT IS GOOD")
sleep(2)
except Exception as e:
print("Error:", e)
lcd.clear()
lcd.putstr("Sensor Error")
sleep(2)
'''
'''
#last perfect working code
from machine import Pin, ADC, I2C, time_pulse_us
from dht import DHT22
from utime import sleep
from pico_i2c_lcd import I2cLcd
# =========================
# LCD
# =========================
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# =========================
# DHT22
# =========================
dht = DHT22(Pin(15))
# =========================
# ULTRASONIC
# =========================
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
# =========================
# LEDS
# =========================
l_green = Pin(4, Pin.OUT)
l_red = Pin(5, Pin.OUT)
# =========================
# BUZZER
# =========================
buzzer = Pin(6, Pin.OUT)
# =========================
# RESET BUTTON
# GP7 ---- Button ---- GND
# =========================
reset_btn = Pin(7, Pin.IN, Pin.PULL_UP)
# =========================
# SOIL SENSOR
# =========================
soil = ADC(26)
# =========================
# LDR SENSOR
# =========================
ldr = ADC(27)
# =========================
# CALIBRATION VALUES
# =========================
SOIL_DRY = 60000
SOIL_WET = 20000
# =========================
# ALARM VARIABLES
# =========================
alarm_triggered = False
trigger_count = 0
# =========================
# DISTANCE FUNCTION
# =========================
def get_distance():
trig.low()
sleep(0.01)
trig.high()
sleep(0.00001)
trig.low()
duration = time_pulse_us(echo, 1, 30000)
if duration < 0:
return 999
distance = (duration * 0.0343) / 2
return round(distance, 1)
# =========================
# MAIN LOOP
# =========================
while True:
try:
# -------------------
# RESET BUTTON
# -------------------
if reset_btn.value() == 0:
alarm_triggered = False
trigger_count = 0
buzzer.off()
l_red.off()
l_green.on()
lcd.clear()
lcd.putstr("ALARM RESET")
sleep(0.5)
# -------------------
# DHT22
# -------------------
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# -------------------
# SOIL SENSOR
# -------------------
raw_soil = soil.read_u16()
moisture = int(
(SOIL_DRY - raw_soil) * 100 /
(SOIL_DRY - SOIL_WET)
)
if moisture < 0:
moisture = 0
if moisture > 100:
moisture = 100
# -------------------
# LDR SENSOR
# -------------------
raw_light = ldr.read_u16()
if raw_light < 15000:
light_status = "LOW SUNLIGHT"
elif raw_light < 35000:
light_status = "MEDIUM LIGHT"
else:
light_status = "GOOD LIGHT"
# -------------------
# ULTRASONIC
# -------------------
distance = get_distance()
# -------------------
# PLANT STATUS
# -------------------
if moisture < 20:
plant_status = "WATER NEEDED"
elif temp > 35 and raw_light < 35000:
plant_status = "HOT+LOW LIGHT"
elif temp > 35:
plant_status = "TEMP HIGH"
elif raw_light < 15000:
plant_status = "LOW SUNLIGHT"
else:
plant_status = "PLANT GOOD"
# -------------------
# FALSE ALARM FILTER
# -------------------
if distance < 15:
trigger_count += 1
else:
trigger_count = 0
# -------------------
# ALARM TRIGGER
# -------------------
if trigger_count >= 3:
alarm_triggered = True
# -------------------
# SECURITY
# -------------------
if alarm_triggered:
security = "INTRUDER"
l_red.on()
l_green.off()
for i in range(5):
if reset_btn.value() == 0:
alarm_triggered = False
trigger_count = 0
buzzer.off()
break
buzzer.on()
sleep(0.05)
buzzer.off()
sleep(0.05)
else:
security = "SAFE"
l_green.on()
l_red.off()
buzzer.off()
# -------------------
# SERIAL OUTPUT
# -------------------
print("--------------------------------")
print("Temp :", temp, "C")
print("Humidity :", hum, "%")
print("Moisture :", moisture, "%")
print("Light Raw :", raw_light)
print("Light :", light_status)
print("Distance :", distance, "cm")
print("Plant :", plant_status)
print("Security :", security)
print("Triggers :", trigger_count)
print("--------------------------------")
# -------------------
# LCD DISPLAY
# -------------------
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("T:{} H:{}%".format(int(temp), int(hum)))
lcd.move_to(0, 1)
if alarm_triggered:
lcd.putstr("INTRUDER ALERT")
elif moisture < 20:
lcd.putstr("WATER PLANT")
elif temp > 35 and raw_light < 35000:
lcd.putstr("HOT LOW LIGHT")
elif temp > 35:
lcd.putstr("TEMP TOO HIGH")
elif raw_light < 15000:
lcd.putstr("LOW SUNLIGHT")
else:
lcd.putstr("PLANT IS GOOD")
sleep(0.2)
except Exception as e:
print("Error:", e)
lcd.clear()
lcd.putstr("Sensor Error")
sleep(0.5)
'''
from machine import Pin, ADC, I2C, time_pulse_us
from dht import DHT22
from utime import sleep
from pico_i2c_lcd import I2cLcd
# =========================
# LCD
# =========================
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# =========================
# DHT22
# =========================
dht = DHT22(Pin(15))
# =========================
# HC-SR04
# =========================
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
# =========================
# LEDS
# =========================
green_led = Pin(4, Pin.OUT)
red_led = Pin(5, Pin.OUT)
# =========================
# BUZZER
# =========================
buzzer = Pin(6, Pin.OUT)
# =========================
# RESET BUTTON
# GP7 ---- Button ---- GND
# =========================
reset_btn = Pin(7, Pin.IN, Pin.PULL_UP)
# =========================
# SOIL SENSOR
# =========================
soil = ADC(26)
# =========================
# LDR SENSOR
# =========================
ldr = ADC(27)
# =========================
# SOIL CALIBRATION
# =========================
SOIL_DRY = 60000
SOIL_WET = 20000
# =========================
# SECURITY VARIABLES
# =========================
alarm_triggered = False
trigger_count = 0
# =========================
# DISTANCE FUNCTION
# =========================
def get_distance():
trig.low()
sleep(0.01)
trig.high()
sleep(0.00001)
trig.low()
duration = time_pulse_us(echo, 1, 30000)
if duration < 0:
return 999
distance = (duration * 0.0343) / 2
return round(distance, 1)
# =========================
# MAIN LOOP
# =========================
while True:
try:
# -------------------
# RESET ALARM
# -------------------
if reset_btn.value() == 0:
alarm_triggered = False
trigger_count = 0
buzzer.off()
red_led.off()
green_led.on()
lcd.clear()
lcd.putstr("ALARM RESET")
sleep(0.5)
# -------------------
# DHT22
# -------------------
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# -------------------
# SOIL MOISTURE
# -------------------
raw_soil = soil.read_u16()
moisture = int(
(SOIL_DRY - raw_soil) * 100 /
(SOIL_DRY - SOIL_WET)
)
if moisture < 0:
moisture = 0
if moisture > 100:
moisture = 100
# -------------------
# SUNLIGHT STATUS
# -------------------
sunlight = ldr.read_u16()
if sunlight < 15000:
sunlight_status = "LOW SUNLIGHT"
elif sunlight < 35000:
sunlight_status = "MODERATE"
else:
sunlight_status = "GOOD"
# -------------------
# DISTANCE
# -------------------
distance = get_distance()
# -------------------
# PLANT STATUS
# -------------------
if moisture < 20:
plant_status = "WATER NEEDED"
elif temp > 35 and sunlight < 35000:
plant_status = "HOT+LOW SUN"
elif temp > 35:
plant_status = "TEMP HIGH"
elif sunlight < 15000:
plant_status = "LOW SUNLIGHT"
else:
plant_status = "HEALTHY"
# -------------------
# FALSE ALARM FILTER
# -------------------
if distance < 15:
trigger_count += 1
else:
trigger_count = 0
# -------------------
# TRIGGER ALARM
# -------------------
if trigger_count >= 3:
alarm_triggered = True
# -------------------
# SECURITY STATUS
# -------------------
if alarm_triggered:
security_status = "INTRUDER"
red_led.on()
green_led.off()
for i in range(5):
if reset_btn.value() == 0:
alarm_triggered = False
trigger_count = 0
buzzer.off()
break
buzzer.on()
sleep(0.05)
buzzer.off()
sleep(0.05)
else:
security_status = "SAFE"
green_led.on()
red_led.off()
buzzer.off()
# -------------------
# SERIAL MONITOR
# -------------------
print("--------------------------------")
print("Temperature :", temp, "C")
print("Humidity :", hum, "%")
print("Moisture :", moisture, "%")
print("Sunlight Status :", sunlight_status)
print("Distance :", distance, "cm")
print("Plant Status :", plant_status)
print("Security Status :", security_status)
print("--------------------------------")
# -------------------
# LCD DISPLAY
# -------------------
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("T:{} H:{}%".format(int(temp), int(hum)))
lcd.move_to(0, 1)
if alarm_triggered:
lcd.putstr("INTRUDER ALERT")
elif moisture < 20:
lcd.putstr("WATER PLANT")
elif temp > 35 and sunlight < 35000:
lcd.putstr("HOT LOW SUN")
elif temp > 35:
lcd.putstr("TEMP TOO HIGH")
elif sunlight < 15000:
lcd.putstr("LOW SUNLIGHT")
else:
lcd.putstr("PLANT HEALTHY")
sleep(0.2)
except Exception as e:
print("Error:", e)
lcd.clear()
lcd.putstr("Sensor Error")
sleep(0.5)