pimoroni / enviro

MIT License
104 stars 83 forks source link

Demo code doesn't turn the pumps from the Enviro Grow kit on / off. Written custom code to resolve issue. #27

Closed RedDogUK closed 1 year ago

RedDogUK commented 2 years ago

The published Enviro Grow scripts doesn't have any code to control the three pumps. I've solved this by writing some custom code. Details as follows...

Created a new folder on the Pico called "custom". Created two new files within the custom folder: "pumps.py" and "water_plants.py".

pumps.py `import time from breakout_bme280 import BreakoutBME280 from breakout_ltr559 import BreakoutLTR559 from machine import Pin, PWM from enviro.board import i2c

bme280 = BreakoutBME280(i2c, 0x77) ltr559 = BreakoutLTR559(i2c)

Water pump GPIO settings

Pump_A = Pin(12, Pin.OUT) Pump_B = Pin(11, Pin.OUT) Pump_C = Pin(10, Pin.OUT)

def Pump_1(run): Pump_A.value(1) # Turns pump on print ('Pump A on') time.sleep(run) Pump_A.value(0) # Turns pump off print ('Pump A off')

def Pump_2(run): Pump_B.value(1) # Turns pump on print ('Pump B on') time.sleep(run) Pump_B.value(0) # Turns pump off print ('Pump B off')

def Pump_3(run): Pump_C.value(1) # Turns pump on print ('Pump C on') time.sleep(run) Pump_C.value(0) # Turns pump off print ('Pump C off')

Testing

Pump_1(run)

Pump_2(run)

Pump_3(run)

`

water_plants.py `import time import custom from custom import pumps import enviro from enviro.boards import grow

Pump running time (seconds)

run = 2 #For testing

run = 10 #For testing

Dry sensor value

dry = 70

Initiate sensor values for testing

sensor1 = 0

sensor2 = 0

sensor3 = 0

sensor2 = 100 # For testing. To stop pump 2 from running

sensor3 = 100 # For testing. To stop pump 2 from running

Initial sensor sensors

sensor_a = round(grow.moisture_readings(2000)[0],2) sensor_b = round(grow.moisture_readings(2000)[1],2) sensor_c = round(grow.moisture_readings(2000)[2],2)

Checking to see if there is a sensor attached

if sensor_a == 0: sensor_a_state = 'off' else: sensor_a_state = 'on'

if sensor_b == 0: sensor_b_state = 'off' else: sensor_b_state = 'on'

if sensor_c == 0: sensor_c_state = 'off' else: sensor_c_state = 'on'

print ('Sensor A state = ', sensor_a_state, '. Reading: ', sensor_a) print ('Sensor B state = ', sensor_b_state, '. Reading: ', sensor_b) print ('Sensor B state = ', sensor_c_state, '. Reading: ', sensor_c)

while (sensor_a_state == 'on' and sensor_a <= dry) or (sensor_b_state == 'on' and sensor_b <= dry) or (sensor_c_state == 'on' and sensor_c <= dry):

if sensor_a_state == 'on' and sensor_a <= dry:
    pumps.Pump_1(run)
    time.sleep(5)

if sensor_b_state == 'on' and sensor_b <= dry:
    pumps.Pump_2(run)
    time.sleep(5)

if sensor_c_state == 'on' and sensor_c <= dry:
    pumps.Pump_3(run)
    time.sleep(5)

#sensor = enviro.get_sensor_sensors()
sensor_a = round(grow.moisture_readings(2000)[0],2)
sensor_b = round(grow.moisture_readings(2000)[1],2)
sensor_c = round(grow.moisture_readings(2000)[2],2)

print ('Sensor A: ', sensor_a, ', ', sensor_a_state)
print ('Sensor B: ', sensor_b, ', ', sensor_b_state)
print ('Sensor C: ', sensor_c, ', ', sensor_c_state)

# For testing
#sensor_a = sensor_a+1
#sensor_b = sensor_b+1
#sensor_c = sensor_c+1

print ('All plants are well watered') `

I also amended the main.py file as follows: import custom from custom import water_plants

Inserted just above the enviro.sleep line. # water the plants custom.water_plants

I hope this helps.

sgtwilko commented 2 years ago

Nice, can you submit this as a pull request?

RedDogUK commented 1 year ago

Nice, can you submit this as a pull request?

@sgtwilko, what do you mean by "pull request"? My IT jargon dictionary isn't that great. Do you mean resubmit the above script via the github "pull request" menu?

sgtwilko commented 1 year ago

Hiya @RedDogUK ,

A pull request is where you start the process to get your code merged into a git repository.

There's many guides online as to how to do this, but I basically boils down to: You fork ( make a copy of the repo) here on GitHub.

You edit your copy to include the code you want to submit (normally this would be done on a separate branch, but if you're only submitting one thing you don't have to do this step)

You then open a pull request, this is basically you asking to have your code "pulled" and merged into the original repository.

Your pull request would then be reviewed by owner of the repo, and merged if they like it.

I hope that makes sense.

I was tempted to take some of your code and integrate it myself.

lowfatcode commented 1 year ago

Ahoy both! Thanks for picking this up. There is auto watering optionally alert tone in https://github.com/pimoroni/enviro/commit/2d722218b8c71d1b94cd9a0ccf19818affec70d4

You can set a target moisture level in config.py and either enable or disable auto watering. If auto watering is disabled then Grow will make noise when channels are below the target moisture level. It will beep once, twice, or three times depending on which channel (or if multiple channels need watering each of their beep tones will play one after another with a space between).

This will be rolled into v.0.0.7 later today or tomorrow.

sgtwilko commented 1 year ago

Hiya @lowfatcode

Damn, I was just about to submit a PR with auto-watering! I must have forked just before this was merged.

Looks I've taken a different approach where I check the sensors again after watering.

I've rebased my solution on the latest code here, not tested it yet, but will do so tomorrow and if it works, I'll open a PR.

Basically I saw you had a really nice separation of lower level code with few dependencies, being used by higher level code, abstracting the processing logic from the lower level board driver.

I carried on with that, passing in the thresholds for watering, and rechecking the sensors to see if we needed to keep watering.

branch with my changes is at https://github.com/sgtwilko/enviro/tree/PumpsRebased if you want a look before I open the PR.