added moisture with test

Signed-off-by: Ebbe Baß <ebbe@ping-mee.de>
master
Ebbe Baß 2022-07-10 00:24:05 +02:00
parent 023a3987be
commit 134facf96b
2 changed files with 37 additions and 48 deletions

36
main.py
View File

@ -1,6 +1,8 @@
import time import time
import os import os
from datetime import datetime from datetime import datetime
import signal
import sys
try: try:
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import gpiozero import gpiozero
@ -9,15 +11,12 @@ try:
except ImportError: except ImportError:
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"') print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
exit() exit()
sys.path.append('./SDL_Adafruit_ADS1x15')
import SDL_Adafruit_ADS1x15
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
# setting up ultrasonic pins
GPIO_TRIGGER = 18
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def debug_print(message: str): def debug_print(message: str):
now = datetime.now() now = datetime.now()
current_time = now.strftime("%H:%M:%S") current_time = now.strftime("%H:%M:%S")
@ -28,6 +27,8 @@ def error_print(message: str):
current_time = now.strftime("%H:%M:%S") current_time = now.strftime("%H:%M:%S")
print('[ERROR]['+current_time+'] '+message) print('[ERROR]['+current_time+'] '+message)
#relay
def relay_controller(pin: int, status: str): def relay_controller(pin: int, status: str):
if status == "ON": if status == "ON":
relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False) relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False)
@ -38,6 +39,12 @@ def relay_controller(pin: int, status: str):
else: else:
error_print("Unknown status for relay ("+status+")") error_print("Unknown status for relay ("+status+")")
#ultrasonic
GPIO_TRIGGER = 18
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def get_water_level(): def get_water_level():
GPIO.output(GPIO_TRIGGER, True) GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001) time.sleep(0.00001)
@ -56,3 +63,18 @@ def get_water_level():
return distance return distance
#moisture
def signal_handler(signal, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def get_moisture():
ADS1115 = 0x01
gain = 6144
sps = 250
adc = SDL_Adafruit_ADS1x15.ADS1x15(ic=ADS1115)
voltsChArray = [adc.readADCSingleEnded(0, gain, sps) / 1000, adc.readADCSingleEnded(1, gain, sps) / 1000, adc.readADCSingleEnded(2, gain, sps) / 1000, adc.readADCSingleEnded(3, gain, sps) / 1000]
return sum(voltsChArray) / float(len(voltsChArray))

49
test.py
View File

@ -7,49 +7,16 @@ sys.path.append('./SDL_Adafruit_ADS1x15')
import SDL_Adafruit_ADS1x15 import SDL_Adafruit_ADS1x15
def signal_handler(signal, frame): def signal_handler(signal, frame):
print( 'You pressed Ctrl+C!')
sys.exit(0) sys.exit(0)
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)
ADS1115 = 0x01 # 16-bit ADC def get_moisture():
ADS1115 = 0x01
gain = 6144
sps = 250
adc = SDL_Adafruit_ADS1x15.ADS1x15(ic=ADS1115)
# Select the gain voltsChArray = [adc.readADCSingleEnded(0, gain, sps) / 1000, adc.readADCSingleEnded(1, gain, sps) / 1000, adc.readADCSingleEnded(2, gain, sps) / 1000, adc.readADCSingleEnded(3, gain, sps) / 1000]
# gain = 6144 # +/- 6.144V
gain = 4096 # +/- 4.096V
# gain = 2048 # +/- 2.048V
# gain = 1024 # +/- 1.024V
# gain = 512 # +/- 0.512V
# gain = 256 # +/- 0.256V
# Select the sample rate return sum(voltsChArray) / float(len(voltsChArray))
# sps = 8 # 8 samples per second
# sps = 16 # 16 samples per second
# sps = 32 # 32 samples per second
# sps = 64 # 64 samples per second
# sps = 128 # 128 samples per second
sps = 250 # 250 samples per second
# sps = 475 # 475 samples per second
# sps = 860 # 860 samples per second
# Initialise the ADC using the default mode (use default I2C address)
adc = SDL_Adafruit_ADS1x15.ADS1x15(ic=ADS1115)
while (1):
# Read channels in single-ended mode using the settings above
print ("--------------------")
voltsCh0 = adc.readADCSingleEnded(0, gain, sps) / 1000
rawCh0 = adc.readRaw(0, gain, sps)
print ("Channel 0 =%.6fV raw=0x%4X dec=%d" % (voltsCh0, rawCh0, rawCh0))
voltsCh1 = adc.readADCSingleEnded(1, gain, sps) / 1000
rawCh1 = adc.readRaw(1, gain, sps)
print ("Channel 1 =%.6fV raw=0x%4X dec=%d" % (voltsCh1, rawCh1, rawCh1))
voltsCh2 = adc.readADCSingleEnded(2, gain, sps) / 1000
rawCh2 = adc.readRaw(2, gain, sps)
print ("Channel 2 =%.6fV raw=0x%4X dec=%d" % (voltsCh2, rawCh2, rawCh2))
voltsCh3 = adc.readADCSingleEnded(3, gain, sps) / 1000
rawCh3 = adc.readRaw(3, gain, sps)
print ("Channel 3 =%.6fV raw=0x%4X dec=%d" % (voltsCh3, rawCh3, rawCh3))
print ("--------------------")
time.sleep(0.5)