print("Project: Goal 9 (Industries, Inovation & Infrastructure)")
print("Dateline: 22/11/2024")
print("Created By: Jd")
from machine import Pin, ADC
import dht
import time
# GPIO Pin Configuration
dht_pin = Pin(14) # GPIO14 for DHT22
soil_sensor_pin = ADC(Pin(35)) # GPIO35 for Soil Moisture Sensor (ADC)
pump_pin = Pin(27, Pin.OUT) # GPIO27 for Water Pump (LED as indicator)
# Configure the ADC for the soil moisture sensor
soil_sensor_pin.atten(ADC.ATTN_11DB) # Full-scale voltage range (0-3.3V)
# Soil Moisture Threshold (calibrated)
SOIL_MOISTURE_THRESHOLD = 1500 # Adjust based on sensor calibration
# Initialize DHT22 Sensor
dht_sensor = dht.DHT22(dht_pin)
# Function to read soil moisture
def read_soil_moisture():
moisture_value = soil_sensor_pin.read() # ADC value (0-4095)
print(f"Soil Moisture: {moisture_value}")
return moisture_value
# Function to read temperature and humidity
def read_temperature_and_humidity():
try:
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print(f"Temperature: {temperature}°C, Humidity: {humidity}%")
return temperature, humidity
except Exception as e:
print(f"Error reading DHT22 sensor: {e}")
return None, None
# Function to control the water pump
def control_pump(state):
if state:
print("Turning ON the water pump.")
pump_pin.on() # GPIO HIGH to activate the pump
else:
print("Turning OFF the water pump.")
pump_pin.off() # GPIO LOW to deactivate the pump
# Main Function
def smart_garden():
while True:
# Read Sensor Data
soil_moisture = read_soil_moisture()
temperature, humidity = read_temperature_and_humidity()
# Check soil moisture and control the pump
if soil_moisture < SOIL_MOISTURE_THRESHOLD:
print("Soil is dry. Activating water pump.")
control_pump(True)
else:
print("Soil moisture level is adequate. Deactivating water pump.")
control_pump(False)
# Optional: Log data or send to IoT platform
# e.g., send_to_cloud(temperature, humidity, soil_moisture)
# Delay for next reading
time.sleep(10) # Adjust the interval as needed
# Run the Smart Garden System
if __name__ == "__main__":
smart_garden()