Last spring I tried to answer a boring question: how late are the intercity buses actually leaving Esenler? Not the posted schedule, the real thing. I had a Pi Zero 2 W, a USB-serial adapter I’d used for a decade of other projects, and a CH340 cable bought in Karaköy for about the price of two simits. The plan was to sniff the RS-485-ish line a friend had tapped on a bus company’s departure board in the terminal, but the board was behind a locked panel and the friend lost interest. So I fell back to the dumbest possible setup: a small IR reflective sensor pointed at the departure board’s LED matrix, logging edge timings to a text file. Over three weeks I collected about 41,000 transition timestamps, roughly 2,100 departures, and a lot of noise.
The logging side runs on the Pi with a single Python script and a systemd unit so it survives reboots:
[Unit]
Description=board logger
After=multi-user.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/log_board.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
The script reads GPIO 17 with libgpiod, timestamps each edge with time.monotonic_ns(), and appends a line only when the state is stable for 50 ms, which cut the bounce by roughly half:
import gpiod, time
chip = gpiod.Chip('gpiochip0')
line = chip.get_line(17)
line.request(consumer='boardlog', type=gpiod.LINE_REQ_EV_BOTH_EDGES)
last = 0
while True:
if line.event_wait(sec=1):
ev = line.event_read()
t = time.monotonic_ns()
if t - last > 50_000_000:
with open('/home/pi/edges.log', 'a') as f:
f.write(f'{t} {ev.type}\n')
last = t
Why this shape: the sensor is analog and the Pi’s GPIO is not, so I traded resolution for reliability. Fifty milliseconds is slow enough to miss fast flicker but fast enough that a departure line update is still one clean edge. The cost is that I cannot distinguish a flicker from a real update, so downstream I cluster edges into 200 ms windows and treat each cluster as one event. That works for a board that repaints a line every few seconds and fails badly when the board scrolls text, which it does on the second row every 40 seconds. I ended up ignoring row two entirely by masking the sensor with a bit of tape. This is a hack, not instrumentation.
Where it breaks: sunlight through the terminal’s west windows at 17:00 pushed the sensor into saturation for about 40 minutes a day, so the evening departures are underrepresented. The Pi’s clock drifted without NTP after a power cut and I only noticed three days in, so I re-synced and discarded the gap. And the whole thing tells you when the board changed, not when the bus left; I confirmed that by standing at the gate for two hours one Saturday and comparing notes. If you need real departure times, talk to the dispatcher, not the LED.
Question for anyone who has done this: how do you keep a cheap optical logger honest across a season of changing daylight? I am considering a second sensor aimed at a fixed reference LED and subtracting the difference, but I have not tried it yet.
0 条评论
还没有评论,写下第一条。