diff options
Diffstat (limited to 'moisture.py')
| -rw-r--r-- | moisture.py | 28 | 
1 files changed, 20 insertions, 8 deletions
| diff --git a/moisture.py b/moisture.py index c393905..31a683e 100644 --- a/moisture.py +++ b/moisture.py @@ -1,15 +1,27 @@  #!/usr/bin/python3 - +"""Reading moisture data from MCP3008."""  import time -import RPi.GPIO as GPIO +import Adafruit_GPIO.SPI as SPI +import Adafruit_MCP3008 -moisture_pin = 21 +# Make sure the spi are available `lsmod` +SPI_PORT   = 0 +SPI_DEVICE = 0 +mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE)) -GPIO.setmode(GPIO.BCM) -GPIO.setup(moisture_pin, GPIO.IN) +print('Reading MCP3008 values, press Ctrl-C to quit...') +# Print nice channel column headers. +print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8))) +print('-' * 57)  while True: -        print(GPIO.input(moisture_pin)) -            time.sleep(1) - +    # Read all the ADC channel values in a list. +    values = [0]*8 +    for i in range(8): +        # The read_adc function will get the value of the specified channel (0-7). +        values[i] = mcp.read_adc(i) +    # Print the ADC values. +    print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values)) +    # Pause for half a second. +    time.sleep(0.5) | 
