Saturday I debugged the sprinklers.
I thought I turned them on two weeks ago, and I heard someone’s sprinklers outside my window that next Monday morning at 5 a.m., but after a week of 100-degree days it was clear ours weren’t doing their job. I had skipped my usual routine of checking each line, unearthing the sunken heads, and replacing what had failed. So now I had to deal with it.
Somehow after living here for ten years I still found two new heads I had never seen before. Here is a map I’ve kept for years, maybe since our first summer:
It has every sprinkler head I’ve seen. Going by the rate I charge clients, that map is worth thousands of dollars.
In the bottom corner is the box where the water comes in from the street. There are more boxes where valves let water into each line.
One year I came across a buried water spigot in the middle of the grass. Then I lost it again.
But this was a valuable spigot. It was over by our raised beds, where there is no other convenient water. You have to drag a hose from across the yard to water there. In 2022 I borrowed a neighbor’s metal detector. I still couldn’t find it. Finally I tore up the grass with a shovel, probing what must have been a 20’ x 20’ area, until finally I heard a metal clink. I extended the pipe and topped it with a copper rabbit spigot I won as a kid at the Redlands Garden Show for a potted cactus garden. I’ve carried that rabbit with me for 35 years, waiting for a chance to use it.
That was two years ago. It’s on my map.
So why is our grass dying?
Naturally I run our sprinklers off a raspberry pi. I set it up years ago, back in 2016. The controller that came with the house was dying. Two-thirds of the time when I tried to water line 12 or 13, line 4 or 5 would turn on instead. (Yes, we have 13 sprinkler lines. It’s a big yard.) Almost always it was off by 8, or sometimes 4: pretty clearly some loose wires. Why spend fifty bucks to replace it when I could spend days building my own? Look, at least there is no Kubernetes or CI pipeline, okay?
There were raspi sprinkler products you could buy, and I think I saw an open source project, but that didn’t seem like fun. I wanted control and flexbility. I wanted power. I wanted Raspbian, Python, and cron.
Here is my script, called sprinkle
:
#!/usr/bin/env python
# sprinkle - Raspberry Pi sprinkler controller
import time
import RPi.GPIO as GPIO
import sys, signal
# Your sprinkler lines:
# Your sprinkler line 1 goes in array position 0,
# then sprinkler line 2 goes in array position 1,
# etc.
# Each value is the Raspi GPIO pin
# you will connect to that line.
# So if you say
# sprinkler_lines = [6, 19]
# then you should connect pin 6 to sprinker line 1,
# and pin 19 to sprinker line 2.
# sprinkler_lines = [21, 20, 16, 12, 25, 24, 23, 26, 19, 13, 6, 5, 22]
sprinkler_lines = [23, 24, 25, 16, 12, 20, 21, 22, 5, 6, 13, 19, 26]
def usage(err_code):
print("USAGE: sprinkle.py <sprinkler_line> <number_of_minutes>")
sys.exit(err_code)
def int_or_usage(str):
try:
return int(str)
except ValueError:
usage(1)
if len(sys.argv) != 3:
usage(1)
sprinkler_line = int_or_usage(sys.argv[1])
number_of_minutes = int_or_usage(sys.argv[2])
if sprinkler_line < 1 or sprinkler_line > len(sprinkler_lines):
print("I only know about sprinkler lines 1 to %d." % len(sprinkler_lines))
sys.exit(1)
if number_of_minutes < 1 or number_of_minutes > 30:
print("I don't want to run the sprinklers for %d minutes." % number_of_minutes)
sys.exit(1)
def exit_gracefully(signal, frame):
GPIO.cleanup()
sys.exit(0)
signal.signal(signal.SIGINT, exit_gracefully)
active_pin = sprinkler_lines[sprinkler_line - 1]
GPIO.setmode(GPIO.BCM)
for pin in sprinkler_lines:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, False)
GPIO.output(active_pin, True)
time.sleep(60 * number_of_minutes)
GPIO.output(active_pin, False)
exit_gracefully(None, None)
That is a lot of code but it turns on one GPIO pin, sleeps a while, then turns it off. Near the top you can see an array that maps sprinkler lines to GPIO pins. I kept the old sprinkler numbering, so it matches the notes the old owners left us. Array position n
means sprinker line n+1
.
Then I have a higher-level script I run each morning out of cron, which does the front on even days and the back on odd. It logs when it starts and finishes, which has helped me a lot:
#!/usr/bin/env python
# do-yard - Run sprinklers for the whole yard.
# We do the front yard on even days and the back yard on odd days.
import time
from subprocess import call
t = time.localtime()
if t.tm_yday % 2:
print("%s: Starting the back" % time.strftime("%Y-%m-%d %H:%M:%S", t))
# odd days we do the back yard:
for line in [4, 5, 6, 7, 8, 12]:
call(["/home/pi/sprinkle", str(line), "5"])
time.sleep(60)
print("%s: Finished the back" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
else:
print("%s: Starting the front" % time.strftime("%Y-%m-%d %H:%M:%S", t))
# even days we do the front yard (and a little bit of the back):
for line in [1, 2, 3, 9, 10, 11, 13]:
call(["/home/pi/sprinkle", str(line), "5"])
time.sleep(60)
print("%s: Finished the front" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
The hard part was figuring out the wiring. I’ve never gone much further than Ohm’s Law. For a long time I was stuck working out how to drive the sprinkler valves. Sprinkler valves use a solenoid to open and shut. In my garage, 13 colored wires come out of the ground, along with one neutral white wire to complete the circuit. Then plugged into the wall is an adapter to produce 24 volt AC, and two wires come out of that. In between used to be the old controller. It would send 24 VAC down whichever wire matched the spinkler line (& ~(1 << 3)
).
The pi outputs 3.3 volts DC. At first I thought there was an integrated circuit that could convert the signal for me, but eventually I resigned myself to using a bank of relays:
Oh also I never learned how to solder.
A relay is a mechanical system. The AC power goes through, but it’s blocked by an open switch. The DC power is on another circuit, and it activates an electromagnet that closes the switch. When you turn on the signal, you see a red light, and the switch closing makes a loud click.
A bank of 16 relays cost $12, almost as much as a sprinkler controller, so I really wanted my ICs to work out. Oh well.
So today I started with checking the log. Well no, because the pi wasn’t responding to ssh again.
It has always been tempermental. After a few hours the wifi dies, sometimes sooner. Pulling the plug for a moment fixes it, but then you have to wait while it boots. So I have to bring a laptop down to the garage, even just to check on things. Today I thought I would finally fix that.
Other people have the same problem. One reported culprit is power-saving mode. I checked and mine was running that way:
pi@raspberrypi:~ $ iw dev wlan0 get power_save
Power save: on
The nicest advice I found was to disable it at boot with systemd. Just run this:
sudo systemctl --full --force edit wifi_powersave@.service
and in your editor enter—ugh, nano? That had to be fixed.
Setting EDITOR
in root’s ~/.profile
should do it.
No? ~/.bashrc
then?
Still no? Back to Stack Overflow… .
No clues. I guess I’m on my own.
What is this .selected_editor
file in root’s home directory? Hmm, it already says vim.
Is sudo even launching its command through a shell? Probably not, once I think of it. If it just execs the command directly, no wonder ~/.profile
does nothing.
More Stack Overflow. Most questions are about visudo
, and I see something called sudoedit
, and people are asking how to control which editor that launches. (Why not just run the editor you want? The man page says it lets you keep your own editor configuration. Like my own ~/.vimrc
? That’s cool. Really? How does that work?) But in my case the editor is getting launched by systemd. Surely we would have all been happier if we’d just gone with runit?
Does root have $SYSTEMD_EDITOR
set? Surely not—no, too bad.
Of course I could just edit the file myself, but it’s the principle of the thing.
Okay, I give up:
sudo visudo -f /etc/sudoers.d/20_editor
I typed this:
Defaults env_keep += "editor EDITOR"
So now when I run sudo
, it will pass along my own $EDITOR
choice.
Is this a security hole? I can imagine some possible issues on a server, but for the pi in my garage it seems okay.
Now systemd launches vim! Shamelessly I copy and pasted:
[Unit]
Description=Set WiFi power save %i
After=sys-subsystem-net-devices-wlan0.device
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/iw dev wlan0 set power_save %i
[Install]
WantedBy=sys-subsystem-net-devices-wlan0.device
I’ve never seen this %i
thing before. The idea is it lets you do this:
sudo systemctl disable wifi_powersave@off.service
sudo systemctl enable wifi_powersave@on.service
or this:
sudo systemctl disable wifi_powersave@on.service
sudo systemctl enable wifi_powersave@off.service
That’s cool.
Oh, better not forget to run it now too:
sudo iw dev wlan0 set power_save off
So I turned off power saving. Maybe that will fix the wifi.
Let’s check the log file. Have the sprinklers been running?:
2024-06-30 06:00:01: Starting the front
2024-06-30 06:42:03: Finished the front
2024-07-01 06:00:01: Starting the back
2024-07-01 06:36:03: Finished the back
2024-07-02 06:00:01: Starting the front
2024-07-02 06:42:02: Finished the front
2024-07-03 06:00:01: Starting the back
2024-07-03 06:36:02: Finished the back
2024-07-04 06:00:01: Starting the front
2024-07-04 06:42:03: Finished the front
2024-07-05 06:00:01: Starting the back
2024-07-05 06:36:03: Finished the back
2024-07-06 06:00:01: Starting the front
2024-07-06 06:42:03: Finished the front
2024-07-07 06:00:01: Starting the back
2024-07-07 06:36:02: Finished the back
2024-07-08 06:00:01: Starting the front
2024-07-08 06:42:03: Finished the front
2024-07-09 06:00:01: Starting the back
2024-07-09 06:36:03: Finished the back
2024-07-10 06:00:01: Starting the front
2024-07-10 06:42:02: Finished the front
2024-07-11 06:00:02: Starting the back
2024-07-11 06:36:03: Finished the back
2024-07-12 06:00:01: Starting the front
2024-07-12 06:42:03: Finished the front
2024-07-13 06:00:01: Starting the back
2024-07-13 06:36:03: Finished the back
They’ve been running all along! 40 minutes for the front, 30 for the back.
But clearly they’re doing nothing. The pi is turning on a pin then just sitting there.
So there must be a loose connection.
I tried line 3: ./sprinkle 3 10
. No red light, no click. Line 10. No red light, no click. Line 2. No red light, no click.
I went upstairs to fetch my multimeter. Time to test connectivity and voltage.
How in the world did I wire this thing anyway?
Then I noticed a couple red wire loops, connecting GPIO pins to the breadboard power rail, but detached now from the power rail. The pins both said 5V. (That tiny text was easier to read in 2016.) So those came loose? What if I put them back in again? I think I remember . . . wasn’t this supposed to power the relay?
Trying my sprinkle
command again made the light come on! I must have missed the click though. Were the sprinklers running? No? What if I try a few lines? I’m really not hearing the click. But the light is on.
How does each relay work again? I set the multimeter to connectivity to probe each pair of posts. They were more connected than I expected. Was that bad? Okay I remember the white neutral wire running from one relay to another in series. And the colored wires go out and into the ground, one per relay.
I remember something about those two little red wire loops. They really looked disconnected on purpose. They weren’t just loose, they were completely out of the breadboard.
Is anything else loose? A bit, but when I fix it nothing changes.
I remember those two red wires. They are supposed to give 5 volts to power the relay, but it never worked did it? It was supposed to, but it didn’t. Like the pi just didn’t have enough oomph. Or was the board supposed to power the pi?
What are these other two thin black wires leaving the relay board? Where do they go? Off to the right, oh, to a power adapter! Two weeks ago I plugged in the adapter for the pi, and I plugged in the 24 VAC adapter, but the relays need power too, and they get it from the power strip over by the garage freezer.
I guess this is why phone support asks if you’ve plugged it in.
blog comments powered by Disqus Prev: Temporal Ops Next: Temporal Reverted