hidayahramli27
Posts: 3
Joined: Wed Oct 09, 2019 5:42 pm

Help me please,my due date is next week.....

Wed Oct 09, 2019 6:05 pm

`Hye, Iam new users and currently I am doing a project about irrigation,it is possible for me to combine this two coding,,Im trying to make a coding when the moisture dry,the pump will trigger water and will automatically stop when the moisture wet.I am also trying to use Thing Speak to display the data but I don't know how...... :| :| :|

Relay coding

Code: Select all

import RPi.GPIO as GPIO
import time

channel = 21

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)


def motor_on(pin):
    GPIO.output(pin, GPIO.HIGH)  # Turn motor on


def motor_off(pin):
    GPIO.output(pin, GPIO.LOW)  # Turn motor off


if __name__ == '__main__':
    try:
        motor_on(channel)
        time.sleep(1)
        motor_off(channel)
        time.sleep(1)
        GPIO.cleanup()
    except KeyboardInterrupt:
        GPIO.cleanup()

Soil coding
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
 
#GPIO SETUP
channel = 14
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel,GPIO.IN)
 
def callback(channel):
        if GPIO.input(channel):
                print ("DRY SOIL")
        else:
                print ("WATERY SOIL")
 
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)  # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback)  # assign function to GPIO PIN, Run function on change
 
# infinite loop
while True:
        time.sleep(1)
THANK YOU for your help seniorr

pcmanbob
Posts: 7073
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 12:43 pm

Hi.

So you have left things late and now you want us to do your home work for you :roll:

well I wont do it for you be I will give you some pointers.

In the first portion of your code, you define you gpio pin using channel but you then go on to try and use pin but you never define pin , I suggest you keep it simple just use the gpio number in each gpio line.

in the second portion of you code you again use channel to define the gpio to use , you can do that as you have already used channel in the first part of the code , so again keep it simple use the gpio number in each gpio line.

I very much doubt the dampness of the soil will change very fast so you might be better just using a simple while try loop to check for the moisture sensor to go dry and then if its dry , turn the pump on and if wet turn the pump off.

if you specifically need to use event detect for the project then have a read of this web page that explains how to use it.

https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

danjperron
Posts: 3404
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 1:26 pm

I very much doubt the dampness of the soil will change very fast so you might be better just using a simple while try loop to check for the moisture sensor to go dry and then if its dry , turn the pump on and if wet turn the pump off.
I completely agree with pcmambob!


First you should try to figure out experimentally how much time you should turn the pump ON and what is the delay for the sensor to trigger after you wet the soil.

With those two parameters you only need to do a loop.

1 - Check if the soil is wet
2 - if is wet dry then turn the pump for n seconds
2.5 - Turn the pump off.
3- Wait n seconds to let the water sink into the soil
4- goto to step 1

** Update thanks rin67630

Code: Select all

import RPi.GPIO as GPIO
import time

# GPIO setup
GPIO.setmode(GPIO.BCM)


PumpGPIO= 21
SensorGPIO=14

GPIO.setup(PumpGPIO, GPIO.OUT)
GPIO.setup(SensorGPIO, GPIO.IN)


#time definition

#how long the pump needs to run
PumpTime = 60.0

#how long to wait  for the water to penetrate the soil
SoakTime = 300.0


def motor_on(pin=PumpGPIO):
    GPIO.output(pin, GPIO.HIGH)  # Turn motor on


def motor_off(pin=PumpGPIO):
    GPIO.output(pin, GPIO.LOW)  # Turn motor off




try:

    while True:

        #is sensor wet or dry
        if GPIO.input(SensorGPIO):
            print("sensor wet turn pump for {} seconds".format(PumpTime))
            motor_on()
            time.sleep(PumpTime)
        else:
            print("sensor wet!")
        motor_off()
        print("wait {} seconds  to test the probe".format(SoakTime))
        #wait to let the water soak the soil
        time.sleep(SoakTime)
except KeyboardInterrupt:
     pass

#be sure that the motor is off
motor_off()
This code uses time.sleep but if you want to add a display and be interactive you will need to use something else like the comparison with datetime to turn on the pump and set the cycle.
Last edited by danjperron on Thu Oct 10, 2019 10:21 pm, edited 1 time in total.

echmain
Posts: 239
Joined: Fri Mar 04, 2016 8:26 pm

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 1:44 pm

Congratulations on your new baby!

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7416
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 2:49 pm

echmain wrote:
Thu Oct 10, 2019 1:44 pm
Congratulations on your new baby!
I'm glad I'm not the only one who had incorrectly interpreted the thread title :D
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
DougieLawson
Posts: 36312
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 3:12 pm

6by9 wrote:
Thu Oct 10, 2019 2:49 pm
echmain wrote:
Thu Oct 10, 2019 1:44 pm
Congratulations on your new baby!
I'm glad I'm not the only one who had incorrectly interpreted the thread title :D
It's "due date" that triggers the thought of the Maternity Dept - as it did for me. (Although, my two grandkids have been born 14,500Km away in Western Australia - so we missed most of the excitement - until the WhatsApp message arrived.)

These "Eternal September" threads never fail to amuse.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

gordon77
Posts: 4221
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 4:25 pm

Based on your original code...

Code: Select all

import RPi.GPIO as GPIO
import time

m_channel = 21
d_channel = 14

# GPIO setup
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(m_channel, GPIO.OUT)
GPIO.setup(d_channel,GPIO.IN)

def callback(channel):
    while GPIO.input(channel):
        print ("DRY SOIL")
        motor_on(m_channel) 
        time.sleep(1)
        motor_off(m_channel)
        time.sleep(1)
    if not GPIO.input(channel):
        print ("WATERY SOIL")
 
GPIO.add_event_detect(d_channel, GPIO.BOTH, bouncetime=300)  # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(d_channel, callback)  # assign function to GPIO PIN, Run function on change

def motor_on(pin):
    GPIO.output(pin, GPIO.HIGH)  # Turn motor on

def motor_off(pin):
    GPIO.output(pin, GPIO.LOW)  # Turn motor off

if __name__ == '__main__':
    try:
        callback(d_channel) # initial test for DRY / WATERY SOIL
        while True:
            time.sleep(1)
        
    except KeyboardInterrupt:
        GPIO.cleanup()

danjperron
Posts: 3404
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 8:34 pm

Gordon I don't think it is a good idea to start and stop a pump in 1 second interval.

gordon77
Posts: 4221
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 8:46 pm

Maybe, but l simply used the OPs code and timing. They can change it as required from advice such as yours.

Maybe?

Code: Select all

print ("DRY SOIL")
motor_on(m_channel) 
time.sleep(10)
motor_off(m_channel)
time.sleep(30) 
I expect times will depend on pump size, flow rate etc.
Last edited by gordon77 on Fri Oct 11, 2019 2:07 pm, edited 2 times in total.

User avatar
rin67630
Posts: 568
Joined: Fri Mar 04, 2016 10:15 am

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 9:19 pm

danjperron wrote:
Thu Oct 10, 2019 1:26 pm

1 - Check if the soil is wet
2 - if is wet then turn the pump for n seconds
3- Wait n seconds to let the water sink into the soil
4- goto to step 1
Hmmm.
Step 2 ist wrong: if it is NOT wet...

Step 2.5 is missing: turn the pump off.

W. H. Heydt
Posts: 10995
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 9:48 pm

DougieLawson wrote:
Thu Oct 10, 2019 3:12 pm
These "Eternal September" threads never fail to amuse.
Someone else who dates himself by remembering the "Endless September".

danjperron
Posts: 3404
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 10:15 pm

Step 2 ist wrong: if it is NOT wet...

Step 2.5 is missing: turn the pump off.
Good catch.

At least the code in python was ok!

User avatar
jcyr
Posts: 427
Joined: Sun Apr 23, 2017 1:31 pm
Location: Atlanta

Re: Help me please,my due date is next week.....

Thu Oct 10, 2019 10:53 pm

danjperron wrote:
Thu Oct 10, 2019 10:15 pm
Step 2 ist wrong: if it is NOT wet...

Step 2.5 is missing: turn the pump off.
Good catch.

At least the code in python was ok!
Good way to flood your basement! :-)
It's um...uh...well it's kinda like...and it's got a bit of...

pfletch101
Posts: 527
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: Help me please,my due date is next week.....

Fri Oct 11, 2019 2:43 pm

jcyr wrote:
Thu Oct 10, 2019 10:53 pm
danjperron wrote:
Thu Oct 10, 2019 10:15 pm
Step 2 ist wrong: if it is NOT wet...

Step 2.5 is missing: turn the pump off.
Good catch.

At least the code in python was ok!
Good way to flood your basement! :-)
To me, "turn the pump on for n seconds" fairly strongly implies turning it off after n seconds - otherwise, why specify a time at all?

W. H. Heydt
Posts: 10995
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Help me please,my due date is next week.....

Fri Oct 11, 2019 3:25 pm

Note that there has been no response (or any other post) from the OP.

gordon77
Posts: 4221
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help me please,my due date is next week.....

Fri Oct 11, 2019 3:29 pm

W. H. Heydt wrote:
Fri Oct 11, 2019 3:25 pm
Note that there has been no response (or any other post) from the OP.
I'd also noticed that, happens quite regularly on here. I wonder if they can't find their post again.
Last edited by gordon77 on Fri Oct 11, 2019 3:32 pm, edited 1 time in total.

User avatar
B.Goode
Posts: 8860
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Help me please,my due date is next week.....

Fri Oct 11, 2019 3:31 pm

W. H. Heydt wrote:
Fri Oct 11, 2019 3:25 pm
Note that there has been no response (or any other post) from the OP.


...too busy writing up the assignment for their forthcoming project submission!

They have been given the conundrum of wondering which one of 2 possible solutions to copy and paste into their project. Perhaps we should leave them to find any 'deliberate mistakes' or implementation errors for themselves so that they actually do some work on the project to earn the marks or other credit?

User avatar
davidcoton
Posts: 4184
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Help me please,my due date is next week.....

Fri Oct 11, 2019 7:09 pm

Maybe the assignment setter follows raspberrypi.org/forums too :lol:
Signature retired

Andyroo

Re: Help me please,my due date is next week.....

Fri Oct 11, 2019 8:10 pm

davidcoton wrote:
Fri Oct 11, 2019 7:09 pm
Maybe the assignment setter follows raspberrypi.org/forums too :lol:
I would hope so.

Maybe give extra points if you can explain the problem clearly without using the actual question text (as no one actually seems to do)?

echmain
Posts: 239
Joined: Fri Mar 04, 2016 8:26 pm

Re: Help me please,my due date is next week.....

Fri Oct 11, 2019 9:26 pm

W. H. Heydt wrote:
Fri Oct 11, 2019 3:25 pm
Note that there has been no response (or any other post) from the OP.
She probably went into labor.

gordon77
Posts: 4221
Joined: Sun Aug 05, 2012 3:12 pm

Re: Help me please,my due date is next week.....

Thu Oct 17, 2019 9:25 am

Well the week is nearly up and nothing heard from the OP ..

pcmanbob
Posts: 7073
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help me please,my due date is next week.....

Thu Oct 17, 2019 11:23 am

gordon77 wrote:
Thu Oct 17, 2019 9:25 am
Well the week is nearly up and nothing heard from the OP ..
Seems common on this forum , make one post asking for help and then never seen again....
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Andyroo

Re: Help me please,my due date is next week.....

Thu Oct 17, 2019 2:17 pm

Well at least this one did come back a few days after - not like a lot of the five minute visitors :lol:

fruitoftheloom
Posts: 20907
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: Help me please,my due date is next week.....

Thu Oct 17, 2019 2:20 pm

echmain wrote:
Fri Oct 11, 2019 9:26 pm
W. H. Heydt wrote:
Fri Oct 11, 2019 3:25 pm
Note that there has been no response (or any other post) from the OP.
She probably went into labor.

Yes a brand new RPi 4B was born :lol: :lol:
Retired disgracefully.....

pcmanbob
Posts: 7073
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help me please,my due date is next week.....

Thu Oct 17, 2019 2:28 pm

Andyroo wrote:
Thu Oct 17, 2019 2:17 pm
Well at least this one did come back a few days after - not like a lot of the five minute visitors :lol:
No OP only ever made 1 post , guess due date came earlier than expected ;)
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “General discussion”