plant watering

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
johnv
Posts: 14
Joined: Sun Nov 10, 2019 6:01 pm

plant watering

Post by johnv » Sun Nov 10, 2019 7:34 pm

hi, i'm fairly new to programming and i wanted to build a super easy plant watering mechanism with a nodemcu, a soil sensor, and a 3v waterpump from amazon: https://www.amazon.com/WayinTop-Automat ... 607&sr=8-6

unfortunatly it only comes with arduino code C,

i was wondering if someone have some basic micropython code that i can use as a tutorial,

i find it hard to find some basic stuff what to import from machine and how to appoint the pins on nodemcu,

i found some documentation from the raspberry pi, but that is in full python, and the pin.out is also very different,

if someone could help get started with the code that would be awesome.

i would like to know how to read the soilsensor, witch pin to use and witch components to import from the machine(gpio, time, ?), i'm a bit lost on this. :(

kind regards, john.

torwag
Posts: 216
Joined: Fri Dec 13, 2013 9:25 am

Re: plant watering

Post by torwag » Mon Nov 11, 2019 7:32 am

Hi,

That should be relatively simple. The relay does the main work for the pump. All you need here is a I/O pin to be set high or low.
That is not different to any LED-example you find in the tutorials and documentation.
The soil sensor delivers an analogues signal. This can be read in by Micropython as well and is part of the standard libs. Just look into reading of an ADC.
The simplest way would be:
Building a loop which measures e.g. 20 values of the soil sensor and create the average out of it (to make sure that it is not triggered by a wrong measurement), then decide to turn or not turn on the pump for a fixed amount of time, finally wait for 1 hour before cycle again (the process itself is slow, no need to measure every second).
You just have one very mission critical feature you need to take care of. In case of a fail or whatever goes wrong, you want the relay to turn of the pump. There should be as much as possible no situation where the pump stays on during a system error. Therefore, I say, let the pump been turned on a minute or so and do not rely on the fact that you resp. your program might come back to turn it off again. If this fails for one or the other reason, flooding is the result.

For all of this you need nothing special and Micropythons REPL system should already allow you to test and get most of it.

If this works everything in addition is part of how complex you want it. Deep sleep to save power, timer, send/receive MQTT, a webinterface or an HTTP Request to trigger the pump, you can make it as complex as you could ;)

johnv
Posts: 14
Joined: Sun Nov 10, 2019 6:01 pm

Re: plant watering

Post by johnv » Mon Nov 11, 2019 8:45 am

thanks man, this should get me started, appreciate the feedback!!!

johnv
Posts: 14
Joined: Sun Nov 10, 2019 6:01 pm

Re: plant watering

Post by johnv » Mon Nov 11, 2019 4:38 pm

so far so good, got the soil sensor to spit out some readings, and the relay is functioning!

now all i have to try, is to make them communicate,

but that would be for an other day,

btw i used the thonny ide, it was easier than the repl, and visual studio code was a bit hard to figure out aswell!

tnx!

Code: Select all

import machine
from machine import Pin
from machine import ADC
import time
from time import sleep


moisture = ADC(0)

while True:
    moisture_value = moisture.read()
    print(moisture_value)
    sleep(3)
    

pump = Pin(4, Pin.OUT)
while True:
    pump.on()
    print(pump.value())
    sleep(10)
    pump.off()
    sleep(10)

torwag
Posts: 216
Joined: Fri Dec 13, 2013 9:25 am

Re: plant watering

Post by torwag » Tue Nov 12, 2019 6:53 am

Hi,

this looks like a good start. You can add code using the code tags. Just mark the code with the mouse and press the </> button. This adds the code into its own window with some better formatting and an own scroll bar, thus it does not take up much space.
Did this for you on your last post.

As for the communication. Your code has two while loops which never terminate. That is, the first run forever, the second never. You have to add both to the same loop.

Code: Select all

import machine
from machine import Pin
from machine import ADC
import time
from time import sleep

moisture = ADC(0)
pump = Pin(4, Pin.OUT)
dry_value = 0.5 # Add the value below which the relay should be triggered

while True:
    moisture_value = moisture.read()
    print(moisture_value)
    sleep(3)
    if (moisture_value < dry):
	pump.on()
    	print(pump.value())
    	sleep(10)
    	pump.off()
    	sleep(10)    
This reads the moisture, and waits for 3 seconds. Then it judges the value if it is below a critical value dry, the pump will be turned on for 10 seconds and after that turned off and we will wait another 10 seconds (e.g. because we want to make sure the water really reaches the sensor). After that the loop starts again.
In reality you would like to wait much longer periods, as moisture and water might not be that quick.

Now you could start to enhance this, e.g. read 10 values and create an average. Write functions for the pump and the sensor, thus you have encapsulated each functionality and only need to call the functions in the main loop. If the wait cycles get longer, considering sending the ESP to sleep-mode, thus you save energy, etc.

Also consider the hardware incidents. If you run out of water, the pump will be turned on after each reading. Thus, your sleep command at the end of the pump could be critical to make sure, the pump has time to cool down and does not overheat.
Same is true if the water never reaches the sensor, etc. You could add some emergency protocol, like checking if the pump was turned on 5 times in a row and if this is the case, stop the entire system as you might expect something went wrong.

Enjoy

johnv
Posts: 14
Joined: Sun Nov 10, 2019 6:01 pm

Re: plant watering

Post by johnv » Tue Nov 12, 2019 8:16 am

Cool, thanks for the tips. I ll try them out!

It will take some time. But rome wasnt build in a day 😜

johnv
Posts: 14
Joined: Sun Nov 10, 2019 6:01 pm

Re: plant watering

Post by johnv » Thu Nov 21, 2019 7:42 pm

Code: Select all

import machine
from machine import Pin
from machine import ADC
import time
from time import sleep

moisture = ADC(0)        #declare soil sensor
pump = Pin(4, Pin.OUT)    #declare pin 4 as output, naming it pump
dry_value = 600                #declare dry_value as 600

while True:                                       #create infinit loop till somthing breaks the true statement
    moisture_value = moisture.read()    #when there are readings from the soil sensor put them in 'moisture_value'
    print(moisture_value)                      #show readings from soil sensor
    sleep(3)                                          #wait for ... seconds
    
    if (moisture_value < dry_value):      #when readings from soil sensor is smaller than 600 then ...
        pump.on()                                  #pin 4 high
        print(pump.value())                      #shows pin state 1 (high)
        sleep(10)                                      #wait ... seconds
        pump.off()                                     #pin 4 (0)low
        sleep(10)                                        #wait 10 seconds
    
i filled in the dry_value, when dry it was like 250 and wet was 1024, so i picked something in between for testing,

somehownthe pump relay has not turned on but it could be a hardware thingy.

so this weekend i will test it out.

and in the line: (moisture_value < dry) i made it (moisture_value < dry_value) because i declared dry_value = 600, is this correctly or could i leave it as 'dry', i tought it would read it as a string.

i also been watching a ton of youtube videos on python, those from the uhasselt university are a great help as well,

i kinda like the python community, and printed out the entire micro python docs!!! :mrgreen:

tnx for helping!

KevinTK
Posts: 1
Joined: Mon Dec 02, 2019 7:45 am
Location: United Kingdom
Contact:

Re: plant watering

Post by KevinTK » Mon Dec 02, 2019 7:53 am

Got the same Watering System and you already solve the problem that saves time! Really appreciate you guys. :D

User avatar
MostlyHarmless
Posts: 56
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: plant watering

Post by MostlyHarmless » Mon Dec 02, 2019 1:02 pm

torwag wrote:
Mon Nov 11, 2019 7:32 am
Therefore, I say, let the pump been turned on a minute or so and do not rely on the fact that you resp. your program might come back to turn it off again. If this fails for one or the other reason, flooding is the result.
This is indeed the biggest problem with the kit.

If you intend to leave this unattended, you could add something like a 555 timer in monostable mode to it. There is a pretty good tutorial on Circuit Basics that explains in great detail how exactly the circuit works. The example is showing a 5 second on time, but can easily be recalculated for a longer duration.

That circuit still has a problem. It will trigger a cycle on power up. That means that repeated power brownouts or blackouts may still lead to flooding. This can be prevented by having an appropriate R/C config and maybe a "Stop" push-button connected to the reset pin (4) of the 555.

I know, this all is a lot of extra learning curve and I totally understand that for now you just want to get it all working. Just don't forget to come back and add some sort of flood protection later.

johnv
Posts: 14
Joined: Sun Nov 10, 2019 6:01 pm

Re: plant watering

Post by johnv » Sat Dec 07, 2019 11:09 am

i encountered another problem,

the relay is 5v in the kit and the nodemcu only puts out a 3.3v

so i ordered a pack of 3v relays,

i tested it with a led and everything worked like a charm, but the relay just clicked once in a while.

perhaps you could use a opamp circuit like: https://community.particle.io/t/analog- ... ting/16149

but i recon it would be cleaner easier and cheaper to just buy a pack of 3v relays.


i found also this line of code:

(ADC.ATTN_11DB) #range 0-4095 -> 3,3 V

the readings of the sensor is measured on 1v input, and with this line it should tell the nodemcu that the sensor measures 3.3v instead of 1v

dont know if i explain this rightly, so here is the link: https://docs.pycom.io/firmwareapi/pycom/machine/adc/

so somthing else to tamper with :D

Post Reply