2017-Aug-27, Sunday

chebe: (Wild)
I spent way too long trying to build a solution out of parts I had readily available before I discovered that Adafruit had exactly the thing I needed: the piOLED, tiny screen to attach to my Raspberry Pi Zero W. One of the examples even included what I was after; a way to display the ip address, in stats.py.

The script doesn't exit as cleanly as I'd like, so the first thing is to put the while True: loop into a try: and put the cleanup in the catch:
except KeyboardInterrupt:

The tutorial also has instructions on how to run the script on startup; by putting it in rc.local
sudo vim /etc/rc.local
And add the call to the script, before exit 0, pushing it into the background;
sudo python /home/pi/Adafruit_Python_SSD1306/examples/stats.py &
Save and exit.

Cool, now we have a way to display the ip address of the pi so we can ssh in. But, there is nothing to turn it off again.

First thing needed is a python script to blank the screen. You can take a copy of stats.py and strip out everything except the setup code and the cleanup code. Cool, this works, kinda. It clears for a moment and then starts displaying again. This is because we haven't killed the processes doing that yet.

So, we write a script to do that. (Here I ran into some dash versus bash problems. Damn you dash, damn you.)
Something like;

pids=$(ps -ef | grep 'python .*examples/stats.py' | awk '{print $2}')

echo $pids | while read -r line;
    sudo kill -9 $line

sudo python /home/pi/Adafruit_Python_SSD1306/examples/blank.py

Great, this works wonderfully, when we manually invoke it. We're going to want it to run at shutdown too though. (Because, while power is still supplied to the pi, even though it's off, and the screen isn't updating, the old data is still displaying.)

This is not as easy as it should be. I basically wanted a rc-local for shutting down. I'm not the only person who has had this idea, so have a look here for a way to add shutdown functionality to rc-local.

* Copy /etc/rc.local to /etc/rc.local.stop
* Edit /etc/init.d/rc.local, adding in extra info to # Required-Stop:, # Default-Stop:, edit the case statement to call do_stop in event of stop), and add the do_stop function (which is a copy of the do_start function changed to point at rc.local.stop).
* Delete the old rc-local daemon; sudo update-rc.d -f rc.local remove
* Pick up the new; sudo update-rc.d rc.local defaults

But, this alone isn't enough for Debian/Raspbian to pick up the changes. Enter systemd.
sudo vim /lib/systemd/system/rc-local.service
And add this line just after ExecStart;
ExecStop=/etc/rc.local.stop stop
Save and exit.

Now, stop the daemon, reload it, and then start it.
sudo systemctl stop rc-local
sudo systemctl daemon-reload
sudo systemctl start rc-local

One final thing, edit rc.local.stop to use the stop script instead of the start one. And don't push it to the background. Something like;
sudo /home/pi/Adafruit_Python_SSD1306/examples/kill_SSD1306.sh

Finally, when you reboot, startup, and all the other fun things the screen should behave nicely. Enjoy.
Page generated 2017-Sep-19, Tuesday 08:26 pm
Powered by Dreamwidth Studios