This is my first complex Raspberry Pi project. The guts are working great: plug the device in and it automatically boots up and starts checking arrival times at my favorite stop. Planned improvements include a Flask-based web interface for selecting bus routes and a nicer case.
Though a Raspberry Pi can act like a conventional desktop computer, my RPi projects are generally “headless”, in that no monitor, keyboard or mouse are present. This means that we must “harden” the system settings and code to be self-starting and fault-tolerant. Here are the steps I took to ensure that my CTA bus tracker could run indefinitely without user input.
- Set the default user “pi” to automatically log in on power up.
- Follow these instructions to daemonize your script. This allows it to run as a service, which is easier to monitor and restart.
- Install and configure Monit to resurrect the daemon if it dies. A script can die/exit unexpectedly if it encounters an unhandled error. It’s good practice to take care of these errors within the script itself, but I kept getting occasional script exits despite my best efforts. Here’s a nice little python tutorial about exceptions.
- sudo nano /etc/kbd/config and search for BLANK_TIME=30 and POWERDOWN_TIME=30. Change both values to zero (meaning never). This prevents the Pi from falling asleep.
Update: I’ve been chasing bugs for a month, and I’ve finally got 24 hours of up time according to Monit. I think the problem had to do with the python requests module. I added a timeout to each URL request, allowing the loop to continue even if the CTA server is acting up.
Update 2: It also looks like the API will start bouncing requests once you hit a certain number in 24 hours from the same IP.
Let’s make a custom “clock” that constantly checks and updates the arrival times for the bus outside my house. You’ll need to apply for a CTA API key to make this work. I’ve run my clock for about 12 hours total, will update if I find reliability issues.
- Raspberry Pi
- Pi power supply
- Wifi dongle
- 7-segment LED “backpack”
Wiring (Backpack to Pi Rev2). If you look at the Pi with the SD card facing up, you’ll see GPIO pin #1 marked at upper right as “P1”.
- D >> SDA
- C >> SCL
- + >> 5v
- – >> GND
- IO >> 3.3v
Now follow this tutorial to get all your libraries in place. Once everything is wired/updated, try entering the following 3 commands into the terminal:
sudo python ex_7segment_clock.py
If that works then you’re ready for the last step. Download this python script on Github. Follow these instructions to get your Pi to automatically run the script on startup. Enjoy! Will update soon with pictures/CAD files of the finished housing.