GROVE's soil moisture sensor and AD converter (MCP3002) measure the amount of water contained in the soil every 5 minutes, and when the amount of water falls below the threshold, operate the kerosene pump to pump water from the water tank and use it as a planter. I tried to make water.
The moisture sensor outputs a voltage (analog value) according to the amount of moisture. Since the Raspberry Pi can only input digitally, it needs to be converted to digital data and read by an AD converter. This time, I used MCP3002 for the AD converter. For the connection between Raspberry Pi and MCP3002, I referred to the following article. Connect the A / D converter MCP3002 to the Raspberry Pi
The following materials were used for the soil moisture sensor. https://www.switch-science.com/catalog/814/ The sensor is inserted by making a hole in the planter.
For the watering part, the kerosene pump is operated by connecting the electric kerosene pump and GPIO and turning on GPIO. However, the motor operates the pump, but if you try to operate the motor with GPIO of Raspberry Pi, you will run out of power, so make sure to operate the kerosene pump with the power supply of the battery box of the kerosene pump that is attached from the beginning. Then, the relay switch is controlled from the Raspberry Pi. In order to be able to control GPIO of Raspberry Pi with Python, I referred to the following article. [LED blinking in GPIO (Python)](http://make.bcde.jp/raspberry-pi/gpio%E3%81%A7led%E3%81%AE%E7%82%B9%E6%BB%85python /)
Measure the water content in the soil every 5 minutes, and when it falls below the threshold, operate the kerosene pump for 30 seconds. If the water tank is empty, the water content will not recover even if the kerosene pump is operated, and after 5 minutes, it will try to water, which will waste the battery of the kerosene pump. Once I water it, I try not to water it for a while.
water.py
import spidev
import time
import datetime
import subprocess
import RPi.GPIO as GPIO
wet_low = 450
water_check_enable = True
water_wait_count = 0
check_reset = 10
def write_log(log_text):
f = open("/var/log/water","a")
d = datetime.datetime.today()
f.write (d.strftime("%Y-%m-%d %H:%M:%S" + "," + log_text + "\n" ))
#print log_text
f.close()
def read_wet_level():
spi=spidev.SpiDev()
spi.open(0,0)
resp=spi.xfer2([0x68,0x00])
value=(resp[0] * 256 + resp[1]) & 0x3ff
write_log("wet_level:" + str(value))
spi.close()
return value
def water():
write_log("***** water start *****")
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.OUT)
GPIO.output(16,True)
time.sleep(30)
GPIO.output(16,False)
GPIO.cleanup()
write_log("***** water end *****")
while True:
wet_level =read_wet_level()
if water_check_enable == False:
water_wait_count += 1
if water_wait_count > check_reset:
water_check_enable = True
water_wait_count = 0
if wet_level < wet_low and water_check_enable == True:
water_check_enable = False
water()
time.sleep(300)
Recommended Posts