Update 'testing.py'

This commit is contained in:
David Doblas Jiménez 2023-08-05 19:05:36 +02:00
parent 00dd649873
commit d546d5fb3c

View File

@ -9,7 +9,7 @@ import RPi.GPIO as gp
from adafruit_mcp3xxx.analog_in import AnalogIn from adafruit_mcp3xxx.analog_in import AnalogIn
def test_relay_on_off(rly=None): def test_relay_on_off(rly=None, watering_time=None):
# use GPIO numbering # use GPIO numbering
gp.setmode(gp.BCM) gp.setmode(gp.BCM)
@ -17,40 +17,61 @@ def test_relay_on_off(rly=None):
gp.output(rly, gp.HIGH) gp.output(rly, gp.HIGH)
try: try:
while True: gp.output(rly, gp.LOW)
gp.output(rly, gp.LOW) #print(f"Relay 1 ON pin {rly}")
print("Relay 1 ON") time.sleep(watering_time)
time.sleep(1) gp.output(rly, gp.HIGH)
gp.output(rly, gp.HIGH) #print(f"Relay 1 OFF pin {rly}")
print("Relay 1 OFF") time.sleep(2)
time.sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
gp.cleanup() gp.cleanup()
def test_sensor_in_out_water(): def test_sensor_in_out_water(channel=None):
channels = {"5": board.D5}
# create the spi bus # create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# print(dir(board.pin))
# create the chip select # create the chip select
cs = digitalio.DigitalInOut(board.D5) # 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 # create the mcp object
mcp = MCP.MCP3008(spi, cs) 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 # create an analog input channel on pin 5
analog_ic = AnalogIn(mcp, MCP.P5) 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: try:
while True: while True:
print(f"Raw ADC value: {analog_ic.value:.2f}") # print(f"Raw ADC value: {analog_ic.value:.2f}")
print(f"ADC Voltage value: {analog_ic.voltage:.3f}") 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) time.sleep(.5)
except KeyboardInterrupt: except KeyboardInterrupt:
return return
if __name__ == "__main__": if __name__ == "__main__":
from datetime import date
# Relay 1 on GPIO21 # Relay 1 on GPIO21
#test_relay_on_off(21) for _ in range(2):
test_sensor_in_out_water() # 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()}")