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:
    disp.clear()
    disp.display()


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;
#/bin/sh

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

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

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.

Summary;
* 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.
chebe: (AsciiC)
This is a fun one. I was playing with my Explorer pHAT and noticed that every time I Ctrl-C'd out of a script I'd get a message;

Broadcast message from root@raspberrypi (somewhere):

BCM 4 held low, system shutdown in 5 minutes


Broadcast message from root@raspberrypi:

The system is going down for power-off!


And yes, the pi would turn off. Every single time.

I don't know why the Explorer pHAT seems to pull BCM 4 low upon exit, when none of my other pHATs do. But I do know why that was triggering a shutdown.

I had also installed (though not in use) the Zero LiPo SHIM and the OnOff SHIM. They both install the clean-shutdown library that does just what it says, shuts down your pi when BCM 4 is held low.

It installs the cleanshutd service, but it didn't respond to my attempts to stop it, so I had to disable it.

In /boot/config.txt add;
disable_cleanshutd=1
Save, exit, reboot. Then you can play around with your Explorer pHAT in peace.
chebe: (VintageMoon)
You know what's super annoying? Not knowing the ip-address of the server you're trying to access. I could scan the network and find the right machine. But, I'm lazy. And, the server knows its own ip, it should simply tell me.

This is something I believed my Pirate Radio could manage. When the pHAT BEAT bonnet gets installed it sets itself up with a daemon in /usr/bin. Technically I installed it twice (once for spotipy as well). I don't know if the code differs, but as I installed vlc-radio last, that's the code I will play with.

Think it through. I've just booted up the server. The volume and power buttons do very necessary jobs, so we'll leave them alone. The fast-forward and rewind buttons start playing a station/cycle through stations. Very useful if you can't be bothered finding the ip-address. That leaves Play/Pause, which doesn't do anything until a station has started streaming. And if I have access to the web interface, well I can do that very job there. That's it, I'm sacrificing Play/Pause!

In the end I simply replaced line#204 with a call out to my script;
myip.read_ip()
(making sure to import it at the top of the file. Let's hear it for the wonders of Open Source!)

What script you ask? A simple one, to parse the wlan0 ip from ifconfig, and then a call to text-to-speech to actually read it out loud to me.

You'll probably need to install the text-to-speech stuff;
sudo pip install pyttsx
sudo apt-get install espeak


Then save this as a file named myip.py (or whatever) into /usr/bin

#!/usr/bin/env python

import os, pyttsx, time

def read_ip():
f = os.popen('ifconfig wlan0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1')
your_ip=f.read()

engine=pyttsx.init()
engine.say(your_ip)
engine.runAndWait()

time.sleep(3)
print your_ip

if __name__ == '__main__':
read_ip()


And sure, give it a reboot to make sure the changes are picked up.

Now, when I press play the Pirate Radio reads out its ip-address to me! So handy.
(Note; if you press play with monitor and OTG cable attached, it'll probably crash and reboot. So don't do that.)

Pimoroni Pirate Radio

2017-Mar-26, Sunday 10:44 pm
chebe: (Purple - DanceLikeNooneisWatching)
Hardware
Order the Raspberry Pi Zero W Pirate Radio (get it?) kit.
Assemble. Making sure you put the headers on the correct way around *cough*.

Software
Download the NOOBS installer. Extract archive. Copy contents to microSD card of 8GB+.
Put microSD card in Pi, supply power to boot. (You will need a monitor (micro HDMI), OTG micro-USB adapter to plug in keyboard and mouse.)
When boots into installer connect to wifi. (Have the details handy, you'll need them again later.)
From the network install list select Raspbian Jessie Lite.
Lite boots into console only (you can do away with the mouse now). Default login deatils are; pi/raspberry. You'll need to edit a config file to connect back to wifi;
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
And add;
network={
ssid="YOURSSID"
psk="YOURPASSWORD"
}

Once online, install all your favourites (*cough* vim *cough*). Now is also a good time to set up the Pi as you want it. For instance, change the hostname;
sudo hostnamectl set-hostname new_host_name
You'll also need to update the hosts file;
sudo vim /etc/hosts
And update the line that says;
127.0.0.1 raspberrypi

You can test the speaker with;
aplay /usr/share/sounds/alsa/Front_Center.wav

Spotipy
Okay, so far so good. But, it's supposed to be a radio yeah? Do you have Spotify Premium? Then you can hook it up, with a convenient one-line installer;
curl https://get.pimoroni.com/spotipy | bash
It will install a whole load of things (details here), asking for your input (including Spotify login details), and then reboot.

From another device you can access the web interface at http://192.168.0.2:6680/iris/ (run ifconfig on the Pi and take the ip address from the wlan0 interface). You'll need to go to the Settings tab and Authorize your Spotify account. Ta-da, that's it, rock out.

(But, it seems the installer doesn't actually remember your Spotify details properly, so open /etc/mopidy/mopidy.conf, down the bottom check that the [spotify] section contains your username and password. It had forgotten my username and mangled my password, so definitely check.)

(Also, your session will timeout regularly, just log back in, no worries.)

VLC
But, I was not content with just Spotify. I wanted actual internet radio. So I installed the vlc-radio as well.
Again, just one line installer;
curl https://get.pimoroni.com/vlcradio | bash
You can access the web interface at http://192.168.0.2:8080 (default login details; blank username/raspberry).
If you want to queue up your own internet radio stations save them as playlist.m3u, and scp over to /home/pi/.config/vlc/. Then reboot.

(SSH server is off by default, you can turn it on through sudo raspi-config, more details here.)

Other than warning you that the default volume is very high, that's it. Enjoy!
Page generated 2017-Oct-21, Saturday 01:10 am
Powered by Dreamwidth Studios