0

I'm a newbie with Raspberry Pi as well in python and node red.

So my goal with my project is to make a DIY irrigation systemm. I have an RPi, which reads value from the soil moisture sensor and controlling my relay.

I made a UI with Node Red, so I can see the sensor value real time. I also made 2 Buttons to control the irrigation system. Nr1: When I want to turn on the system independently from the sensor value, Nr2: When I want that the system is On, based on sensor value.

My Problem is that I can't quit and clean up my pins properly in my "autonomous mode". When I run my code in terminal that's not a problem as I quit with ctrl-c. But when I would run it in the UI the Relay Mode after the Autonomous Mode, the relay switches on and off simultaneously. It is because at this point both scipts are running and "Autonomous mode" is not terminated. I assume that the problem is with the GPIOs.

Code "Autonomous Mode" :

from time import sleep
import Adafruit_ADS1x15
adc = Adafruit_ADS1x15.ADS1015(address=0x48,busnum=1)
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
p_max = 1272
p_min = 642
one_perc = (p_max-p_min) /100
percentage =0
GAIN= 2/3
try:

while 1:
    value = [0]
    value[0] = adc.read_adc(0,gain=GAIN)
    percentage = 100 - (value[0] -p_min)/one_perc
    if (percentage > 100):
        percentage = 100
    if (percentage < 0) : 
        percentage = 0
    if percentage < 80 :
        GPIO.output(11, GPIO.LOW)
    else :
        GPIO.output(11, GPIO.HIGH)
    volts = value[0] / 2047.0 * 6.144
    print("{0:0.3f}V [{1}]  [{2:0.2f}] %".format(volts, value[0] , percentage))
    sleep(1)
except KeyboardInterrupt:
   print("Keyboardinterrupt caught")
   GPIO.cleanup()

Code "Relay Mode":

import time

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.LOW)

time.sleep(0.25)

GPIO.output(17, GPIO.HIGH)
GPIO.cleanup()

Node Red Flow: enter image description here

One of my ideas was to write a script that simulates the command ctrl-c, but I couldn't try it out yet, as I couldn't find anything that could help me.

1 Answer 1

2

I'm not sure if this is the best/cleanest answer but you could have annother button called Autonomos mode off which you would press before you press the sensor off relay on. That button would have go to a different script in that directory that you would call stop.py.

fileObject = open("isAutonumous.txt", "w")
fileObject.write(0)
fileObject.close()

You would then edit soil_moisture_v2.py to this:

from time import sleep
import Adafruit_ADS1x15
adc = Adafruit_ADS1x15.ADS1015(address=0x48,busnum=1)
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
p_max = 1272
p_min = 642
one_perc = (p_max-p_min) /100
percentage =0
GAIN= 2/3 

fileObject = open("isAutonumous.txt", "w")
fileObject.write(1)
fileObject.close()

while 1:
    fileObject=open("isAutonumous.txt", "r")
    if (fileObject.read() == "0"):
        fileObject.close()
        break
    fileObject.close()
    value = [0]
    value[0] = adc.read_adc(0,gain=GAIN)
    percentage = 100 - (value[0] -p_min)/one_perc
    if (percentage > 100):
        percentage = 100
    if (percentage < 0) : 
        percentage = 0
    if percentage < 80 :
        GPIO.output(11, GPIO.LOW)
    else :
        GPIO.output(11, GPIO.HIGH)
    volts = value[0] / 2047.0 * 6.144
    print("{0:0.3f}V [{1}]  [{2:0.2f}] %".format(volts, value[0] , percentage))
    sleep(1)

GPIO.cleanup()

This should work.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.