Python Noob building an automation irrigation system in Python for Raspberry Pi.
Python has two functions in the most basic use case:
- on/off the LED (which will in future be a relay->pump) and
- Notifies "This is out of water" (which will in future be a HTTP post)
- I have a GPIO switch that working successfully on:
water_level_sensor.value
- I obviously don't want the While loop to just spam that its looping, it should only print on each change of state
The below code appears to work successfully on both use cases, however, I am unsatisfied/unconfident that this is a very good/clean method to achieve the "print once" per change of state loop.
It works by having:
- X = "Wet" outside the outer while loop
- X = "Dry" at the bottom of the nest while
- X = "NULL" at the bottom of the inner loop
It just seems messy... How should someone solve this kind of problem neatly/efficiently? I'm also concerned that I will struggle to pull out of this solution any state data.
def water_level():
while True:
x = "Wet"
while water_level_sensor.value == False:
water_level_led.value = False
if x == "Wet":
print("They System has Water")
x = "Dry"
while water_level_sensor.value == True:
water_level_led.value = True
if x == "Dry":
print("The System is Dry and needs Water")
x = ""