I'm very new at using Python/MicroPython and currently have a Raspberry Pi Pico connected to a 3 channel relay. This will be used as a indoor plant watering system with an LED lamp. The code I pieced together is set to cycle every 12 hours checking for dryness before turning on the water pump. I have a DS3231 clock set and code to tell the lamp when to turn on or off. Unfortunately the lamp is also under the same loop so it won't check to see if it needs to be turned on or off until the next cycle. I was hoping to figure out a way to separate the lamp from the rest of the code so if can turn on and off at specific times. The way I have it set now is as a window of times for the lamp to turn on and off. As far as I understand, everything needs to be on a loop to repeat the process indefinitely. Any help or suggestions would be appreciated.
Code: Select all
# Import libraries
import time, utime
from machine import Pin, I2C
from ds3231_i2c import DS3231_I2C
# Set DS I2C ID, SDA, SCL respective pins and uses default frequency (freq=400000)
ds_i2c = I2C(0,sda=Pin(16), scl=Pin(17))
ds = DS3231_I2C(ds_i2c)
# Uncomment the two lines below to set time 0 1 2 3 4 5 6
#current_time = b'\x00\x11\x13\x07\x24\x07\x21' # sec\min\hour\week(Sunday = 1 - Saturday = 7)\day\month\year
#ds.set_time(current_time)
# Once time is set, you can delete or recomment the 2 lines above.
# Define the name of week days list. Uncomment if you want to change what days the External LED turns on. Otherwise the current code will turn on every day
#w = ["Sunday","Monday","Tuesday","Wednesday","Thurday","Friday","Saturday"];
# Set GP25 onboard LED output
led = Pin(25, Pin.OUT)
# Set GP22 as moisture sensor input
moisture = Pin(22,Pin.IN)
# Set GP1 as moisture sensor power relay 2 output
moisturepower = Pin(1,Pin.OUT)
# Set GP0 for water pump relay 1 output
pump = Pin(0,Pin.OUT)
# Set GP2 for external LED relay 3 output
extled = Pin(2, Pin.OUT)
# Interval time in minutes and Watering time in seconds
interval = 720
water = 10
while True:
led(True)
t = ds.read_time()
# Replace numbers inside green quotation marks with military time to set External LED on and off times
hour_on = int("7", 16)
minute_on = int("00", 16)
hour_off = int("19", 16)
minute_off = int("00", 16)
extled(False)
if (t[2]) >= hour_on and (t[2]) < hour_off:
extled(True)
elif (t[2]) >= hour_off and (t[2]) < hour_on:
extled(False)
moisturepower(True)
time.sleep(1)
if moisture.value() == 1:
pump(True)
time.sleep(water)
pump(False)
moisturepower(False)
#Wait for interval period, flashing LED every 30 seconds
count_AA = 0
while count_AA < (interval * 2):
count_BB = 0
while count_BB < 5:
led(True)
time.sleep(0.5)
led(False)
time.sleep(0.5)
count_BB = count_BB + 1
time.sleep(25)
count_AA = count_AA + 1