77 lines
2.1 KiB
Python
77 lines
2.1 KiB
Python
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, watering_time=None):
|
|
# use GPIO numbering
|
|
gp.setmode(gp.BCM)
|
|
|
|
gp.setup(rly, gp.OUT)
|
|
gp.output(rly, gp.HIGH)
|
|
|
|
try:
|
|
gp.output(rly, gp.LOW)
|
|
#print(f"Relay 1 ON pin {rly}")
|
|
time.sleep(watering_time)
|
|
gp.output(rly, gp.HIGH)
|
|
#print(f"Relay 1 OFF pin {rly}")
|
|
time.sleep(2)
|
|
except KeyboardInterrupt:
|
|
gp.cleanup()
|
|
|
|
|
|
def test_sensor_in_out_water(channel=None):
|
|
channels = {"5": board.D5}
|
|
# create the spi bus
|
|
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
|
|
|
|
# print(dir(board.pin))
|
|
# create the chip select
|
|
# ch = ast.literal_eval(channel)
|
|
# ch = eval(channel)
|
|
# print(ch)
|
|
cs_1 = digitalio.DigitalInOut(channels[channel])
|
|
# cs_2 = digitalio.DigitalInOut(board.D5)
|
|
|
|
# create the mcp object
|
|
mcp_1 = MCP.MCP3008(spi, cs_1)
|
|
# mcp_2 = MCP.MCP3008(spi, cs_2)
|
|
|
|
#https://docs.circuitpython.org/en/latest/shared-bindings/analogio/index.htm
|
|
# create an analog input channel on pin 5
|
|
analog_ic_1 = AnalogIn(mcp_1, MCP.P5)
|
|
# analog_ic_2 = AnalogIn(mcp_2, MCP.P6)
|
|
# print(dir(analog_ic_1._mcp._spi_device))
|
|
#print(analog_ic_1._pin_setting)
|
|
try:
|
|
while True:
|
|
# print(f"Raw ADC value: {analog_ic.value:.2f}")
|
|
print(f"ADC Voltage value sensor 1: {analog_ic_1.voltage:.3f}")
|
|
# print(f"ADC Voltage value sensor 2: {analog_ic_2.voltage:.3f}\n")
|
|
time.sleep(.5)
|
|
except KeyboardInterrupt:
|
|
return
|
|
|
|
|
|
if __name__ == "__main__":
|
|
from datetime import date
|
|
# Relay 1 on GPIO21
|
|
for _ in range(2):
|
|
# plantas colgantes
|
|
test_relay_on_off(rly=16, watering_time=5)
|
|
time.sleep(2)
|
|
# plantas estanteria
|
|
test_relay_on_off(rly=26, watering_time=5)
|
|
time.sleep(2)
|
|
# limonero
|
|
test_relay_on_off(rly=23, watering_time=4)
|
|
time.sleep(2)
|
|
#test_sensor_in_out_water("5")
|
|
print(f"Ha regado el {date.today()}") |