diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | boot_config.txt | 6 | ||||
-rw-r--r-- | main.py | 84 | ||||
-rw-r--r-- | moisture.py | 28 |
4 files changed, 111 insertions, 8 deletions
@@ -1 +1,2 @@ .mypy_cache +.vimcache diff --git a/boot_config.txt b/boot_config.txt new file mode 100644 index 0000000..ebcb638 --- /dev/null +++ b/boot_config.txt @@ -0,0 +1,6 @@ +# See /boot/overlays/README for all available options + +gpu_mem=64 +initramfs initramfs-linux.img followkernel +dtoverlay=w1-gpio,gpiopin=4 +dtparam=spi=on @@ -0,0 +1,84 @@ +import glob +import time +import Adafruit_CharLCD as LCD +import Adafruit_GPIO.SPI as SPI +import Adafruit_MCP3008 + +def initialize_lcd(): + """Initialize LCD for usage.""" + lcd_backlight = 4 + lcd_rs = 25 + lcd_en = 24 + lcd_d4 = 23 + lcd_d5 = 17 + lcd_d6 = 18 + lcd_d7 = 22 + lcd_columns = 16 + lcd_rows = 2 + return LCD.Adafruit_CharLCD( + lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, + lcd_columns, lcd_rows, lcd_backlight) + +def initialize_mcp3008(): + """Initialize mcp3008 for reading.""" + SPI_PORT = 0 + SPI_DEVICE = 0 + return Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE)) + +def get_device_file(): + """Get the path to the the data file.""" + base_dir = '/sys/bus/w1/devices/' + device_folder = glob.glob(base_dir + '28*')[0] + device_file = device_folder + '/w1_slave' + return device_file + + +def read_device_file(device_file): + """Read the raw temperature from the device.""" + f = open(device_file, 'r') + lines = f.readlines() + f.close() + return lines + + +def parse_raw_input(raw_input): + """Parse raw data into temperatures.""" + # Error reading, return nothing + if raw_input[0].strip()[-3:] != 'YES': + return None, None + equals_pos = raw_input[1].find('t=') + if equals_pos != -1: + temp_string = raw_input[1][equals_pos+2:] + temp_c = float(temp_string) / 1000.0 + temp_f = temp_c * 9.0 / 5.0 + 32.0 + return temp_c, temp_f + else: + return None, None + +def voltage_to_moisture(data): + """Turn adc value into percent + + The value from the YL-69 can read between 0-1023 where 1023 is dry. If we + want to display the moisture percent we need to do a bit of math. + """ + val = 1024 - data + return val / 1024 * 100 + +def main(): + """Main routine for reading values and printing to lcd.""" + lcd = initialize_lcd() + temp_device_file = get_device_file() + mcp3008 = initialize_mcp3008() + print('Writing to LCD.') + print('Press Ctrl-C to quit') + while True: + moisture = voltage_to_moisture(mcp3008.read_adc(0)) + raw_temp_input = read_device_file(temp_device_file) + degrees_c, _ = parse_raw_input(raw_temp_input) + msg = "Moisture: {:.2f}\nTemp: {:.2f}".format(moisture, degrees_c) + lcd.clear() + lcd.message(msg) + time.sleep(1) + +if __name__ == '__main__': + main() 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) |