# Autor: David Marchena Martinez # Fecha: 12/09/2021 # Mail: davidmarxena@gmail.com # *********************************************************************************** # * Controla si llueve o no. Básicamente es un circuito eléctrico * # * donde el agua hace de interruptor cerrando el paso de electricidad * # *********************************************************************************** #!/usr/bin/env python3 # coding: utf-8 import writefile import busio import digitalio import board import adafruit_mcp3xxx.mcp3008 as MCP from adafruit_mcp3xxx.analog_in import AnalogIn def Control_lluvia(LLuvia_ant): lluvia_ant = LLuvia_ant # create the spi bus spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) # create the cs (chip select) cs = digitalio.DigitalInOut(board.D22) # create the mcp object mcp = MCP.MCP3008(spi, cs) try: # En teoría debería ser 0 si llueve. Pero al usar un converso Analógico Digital no es cero nunca. Pero se acerca. De ahí que sea mayor de 1 cuando no llueve lluviasensor = AnalogIn(mcp, MCP.P0).voltage if (lluviasensor > 1): print("------") print("NO Llueve") print("------") lluvia = False elif (lluviasensor >= 0.01): print("------") print("Llueve") print("------") lluvia = True if (lluvia_ant != lluvia): print("Cambio de Tiempo") writefile.setfile("Lluvia", lluvia) lluvia_ant = lluvia except (KeyboardInterrupt, SystemExit): raise return lluvia_ant