From 134facf96bbcf5a942982039995adac8a992a5b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Sun, 10 Jul 2022 00:24:05 +0200 Subject: [PATCH] added moisture with test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- main.py | 36 +++++++++++++++++++++++++++++------- test.py | 49 ++++++++----------------------------------------- 2 files changed, 37 insertions(+), 48 deletions(-) diff --git a/main.py b/main.py index ad6c9c8..464d983 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ import time import os from datetime import datetime +import signal +import sys try: import paho.mqtt.client as mqtt import gpiozero @@ -9,15 +11,12 @@ try: except ImportError: print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"') exit() + +sys.path.append('./SDL_Adafruit_ADS1x15') +import SDL_Adafruit_ADS1x15 + 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): now = datetime.now() current_time = now.strftime("%H:%M:%S") @@ -28,6 +27,8 @@ def error_print(message: str): current_time = now.strftime("%H:%M:%S") print('[ERROR]['+current_time+'] '+message) +#relay + def relay_controller(pin: int, status: str): if status == "ON": relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False) @@ -38,6 +39,12 @@ def relay_controller(pin: int, status: str): else: 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(): GPIO.output(GPIO_TRIGGER, True) time.sleep(0.00001) @@ -56,3 +63,18 @@ def get_water_level(): 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)) \ No newline at end of file diff --git a/test.py b/test.py index 6f33ec2..f3af3b5 100644 --- a/test.py +++ b/test.py @@ -7,49 +7,16 @@ sys.path.append('./SDL_Adafruit_ADS1x15') import SDL_Adafruit_ADS1x15 def signal_handler(signal, frame): - print( 'You pressed Ctrl+C!') sys.exit(0) + 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 -# gain = 6144 # +/- 6.144V -gain = 4096 # +/- 4.096V -# gain = 2048 # +/- 2.048V -# gain = 1024 # +/- 1.024V -# gain = 512 # +/- 0.512V -# gain = 256 # +/- 0.256V + 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] -# Select the sample rate -# 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) \ No newline at end of file + return sum(voltsChArray) / float(len(voltsChArray)) \ No newline at end of file