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