Initial commit
This commit is contained in:
commit
3e456c4f1a
56
testing.py
Normal file
56
testing.py
Normal file
@ -0,0 +1,56 @@
|
||||
import time
|
||||
|
||||
import adafruit_mcp3xxx.mcp3008 as MCP
|
||||
import board
|
||||
import busio
|
||||
import digitalio
|
||||
import RPi.GPIO as gp
|
||||
|
||||
from adafruit_mcp3xxx.analog_in import AnalogIn
|
||||
|
||||
|
||||
def test_relay_on_off(rly=None):
|
||||
# use GPIO numbering
|
||||
gp.setmode(gp.BCM)
|
||||
|
||||
gp.setup(rly, gp.OUT)
|
||||
gp.output(rly, gp.HIGH)
|
||||
|
||||
try:
|
||||
while True:
|
||||
gp.output(rly, gp.LOW)
|
||||
print("Relay 1 ON")
|
||||
time.sleep(1)
|
||||
gp.output(rly, gp.HIGH)
|
||||
print("Relay 1 OFF")
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
gp.cleanup()
|
||||
|
||||
|
||||
def test_sensor_in_out_water():
|
||||
# create the spi bus
|
||||
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
|
||||
|
||||
# create the chip select
|
||||
cs = digitalio.DigitalInOut(board.D5)
|
||||
|
||||
# create the mcp object
|
||||
mcp = MCP.MCP3008(spi, cs)
|
||||
|
||||
# create an analog input channel on pin 5
|
||||
analog_ic = AnalogIn(mcp, MCP.P5)
|
||||
|
||||
try:
|
||||
while True:
|
||||
print(f"Raw ADC value: {analog_ic.value:.2f}")
|
||||
print(f"ADC Voltage value: {analog_ic.voltage:.3f}")
|
||||
time.sleep(.5)
|
||||
except KeyboardInterrupt:
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Relay 1 on GPIO21
|
||||
#test_relay_on_off(21)
|
||||
test_sensor_in_out_water()
|
68
water_plants.py
Normal file
68
water_plants.py
Normal file
@ -0,0 +1,68 @@
|
||||
import time
|
||||
|
||||
import adafruit_mcp3xxx.mcp3008 as MCP
|
||||
import board
|
||||
import busio
|
||||
import digitalio
|
||||
import RPi.GPIO as gp
|
||||
|
||||
from adafruit_mcp3xxx.analog_in import AnalogIn
|
||||
|
||||
def _set_up_capacitive_sensor():
|
||||
# spi bus
|
||||
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
|
||||
# chip
|
||||
chip = digitalio.DigitalInOut(board.D5)
|
||||
mcp = MCP.MCP3008(spi, chip)
|
||||
|
||||
return mcp
|
||||
|
||||
def _get_sensor_reading():
|
||||
mcp = _set_up_capacitive_sensor()
|
||||
analog_input_channel = AnalogIn(mcp, MCP.P5)
|
||||
|
||||
return analog_input_channel
|
||||
|
||||
def _set_up_pump(rly=None):
|
||||
# use GPIO numbering
|
||||
gp.setmode(gp.BCM)
|
||||
gp.setup(rly, gp.OUT)
|
||||
|
||||
|
||||
def _pump_on_off(rly=None):
|
||||
_set_up_pump(rly)
|
||||
try:
|
||||
# on
|
||||
gp.output(rly, gp.LOW)
|
||||
time.sleep(1)
|
||||
# off
|
||||
gp.output(rly, gp.HIGH)
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
gp.cleanup()
|
||||
# else:
|
||||
# gp.cleanup()
|
||||
|
||||
|
||||
def read_moisture_level(verbose=False):
|
||||
sensor = _get_sensor_reading()
|
||||
if verbose:
|
||||
print(f"Voltage is {sensor.voltage:.3f} V")
|
||||
return sensor.voltage
|
||||
|
||||
def water_plants():
|
||||
moisture = read_moisture_level()
|
||||
|
||||
try:
|
||||
while True:
|
||||
if moisture > 1.2:
|
||||
_pump_on_off(rly=21)
|
||||
moisture = read_moisture_level()
|
||||
else:
|
||||
time.sleep(1)
|
||||
moisture = read_moisture_level()
|
||||
except KeyboardInterrupt:
|
||||
exit
|
||||
|
||||
if __name__ == "__main__":
|
||||
water_plants()
|
Loading…
Reference in New Issue
Block a user