/ Compost_Friend / code.py
code.py
1 # SPDX-FileCopyrightText: 2018 Isaac Wellish for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # Author: Isaac Wellish 6 # Code adapted from Tony Dicola's CircuitPython code using the DS18x20 temp sensor 7 # as well as John Park's CircuitPython code determining soil moisture from nails 8 9 import time 10 from adafruit_onewire.bus import OneWireBus 11 from adafruit_ds18x20 import DS18X20 12 import board 13 import touchio 14 import neopixel 15 import analogio 16 from simpleio import map_range 17 18 # Initialize neopixels 19 pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.1) 20 21 # set variables for capacitive touch inputs, later used for soil moisture variables 22 touch = touchio.TouchIn(board.A1) 23 touch2 = touchio.TouchIn(board.A2) 24 25 DRY_VALUE = 3100 # calibrate this by hand! 26 WET_VALUE = 4000 # calibrate this by hand! 27 tempThreshhold = 43 #celius temperature of threshold for ideal compost temperature 28 29 # Initialize one-wire bus on board pin A3. 30 ow_bus = OneWireBus(board.A3) 31 32 # Scan for sensors and grab the first one found. 33 ds18 = DS18X20(ow_bus, ow_bus.scan()[0]) 34 35 # Initialize the light senor on board to use for neopixel brightness later 36 light = analogio.AnalogIn(board.LIGHT) 37 38 # Main loop 39 while True: 40 41 # SOIL MOISTURE READINGS 42 43 # set capacitive touch inputs for nails to take in soil moisture levels 44 value_A1 = touch.raw_value 45 value_A2 = touch2.raw_value 46 47 # take the average of both moisture levels 48 avgMoist = value_A1 + value_A2 / 2 49 print("Moisture level:",(avgMoist,)) 50 51 # TEMPERATURE READINGS 52 53 # variable for temperature 54 compostTemp = ds18.temperature 55 56 # print the temperature 57 print('Temperature: {0:0.3f}C'.format(compostTemp)) 58 59 # IF STATEMENTS TO DETERMINE STATE OF COMPOST 60 61 # RED & YELLOW = TOO COLD & TOO DRY 62 if((compostTemp<tempThreshhold) and (avgMoist<DRY_VALUE)): 63 pixels[0] = (255,0,0) # red 64 pixels[1] = (255,255,0) # yellow 65 pixels[2] = (255,0,0) 66 pixels[3] = (255,255,0) 67 pixels[4] = (255,0,0) 68 pixels[5] = (255,255,0) 69 pixels[6] = (255,0,0) 70 pixels[7] = (255,255,0) 71 pixels[8] = (255,0,0) 72 pixels[9] = (255,255,0) 73 74 print("Not hot enough, too dry") 75 76 # BLUE & YELLOW = TOO COLD & TOO WET 77 elif((compostTemp<tempThreshhold) and (avgMoist>WET_VALUE)): 78 pixels[0] = (0,0,255) # blue 79 pixels[1] = (255,255,0) # yellow 80 pixels[2] = (0,0,255) 81 pixels[3] = (255,255,0) 82 pixels[4] = (0,0,255) 83 pixels[5] = (255,255,0) 84 pixels[6] = (0,0,255) 85 pixels[7] = (255,255,0) 86 pixels[8] = (0,0,255) 87 pixels[9] = (255,255,0) 88 print("Not hot enough, too wet") 89 90 # GREEN & YELLOW = TOO COLD & MOISTURE LEVEL OPTIMUM 91 elif((compostTemp<tempThreshhold) and (avgMoist >DRY_VALUE and avgMoist<WET_VALUE)): 92 pixels[0] = (0,255,0) # green 93 pixels[1] = (255,255,0) # yellow 94 pixels[2] = (0,255,0) 95 pixels[3] = (255,255,0) 96 pixels[4] = (0,255,0) 97 pixels[5] = (255,255,0) 98 pixels[6] = (0,255,0) 99 pixels[7] = (255,255,0) 100 pixels[8] = (0,255,0) 101 pixels[9] = (255,255,0) 102 print("Not hot enough, right moisture level") 103 104 # ALL GREEN = COMPOST AT OPTIMUM TEMPERATURE & MOISTURE 105 elif compostTemp > tempThreshhold: 106 pixels.fill((0,255,0)) # green 107 print("Compost Ready") 108 109 # LIGHTING CONFIGURATION 110 111 # print value of light sensor 112 print((light.value,)) 113 114 # map light snesor range to neopixel brightness range 115 peak = map_range(light.value, 2000, 62000, 0.01, 0.3) 116 117 # print neopixel brightness levels 118 print(peak) 119 120 # show neopixels 121 pixels.show() 122 123 # update neopixel brightness based on level of exposed light 124 pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=peak) 125 126 # pause for three seconds 127 time.sleep(3) 128 129 # END PROGRAM