Putting Raspbian on a diet!

Are you having problems with storage space on your Raspberry Pi? Me too!

I was recently attempting to compile OpenCV on a Raspberry Pi 3 with an 8GB SD card when it crapped out complaining of a lack of disk space. Now, OpenCV does need a lot of room to compile, but come on 8GB? Err, no. As it turns out, as the Pi has become more popular and Raspbian has evolved its picked up quite a lot of (very good) default application along the way. This is great for the Pi’s primary purpose of education, but for those of us with more advanced ambitions and aspirations it can become a problem.

Fear not dear reader, there is a solution!

You can reclaim >1.1GB of your SD Card simply by removing a whole host of programs and associated packages that are installed by default and that you probably never use. For example I run the following commands from the console:

$ sudo apt-get purge wolfram-engine libreoffice libreoffice-* nodered \
  bluej greenfoot scratch sonic-pi minecraft-pi -yq
$ sudo apt-get autoremove -yq

This removes all of LibreOffice, all of the Java IDE’s (Java? When you have Python?!), Wolfram (over 600MB!), Scratch, Sonic Pi and Minecraft. And, Voila! I can now compile OpenCV and get on with taking over the world!

Setting Up Digital Signage with a Raspberry Pi

Introduction

For a while now I’ve had some Raspberry Pi‘s (Version1, Model B) running as 3 digital signage installations. They are basically mounted on the back of two 55″ and one 32″ LG TV’s and use the USB socket on said TV’s for power and are connected via HDMI. Nice, easy and simple. Recently I decided it would be good to replace the Pi’s with the new Model 3 to take advantage of the far more powerful CPU and the integrated WiFi. This post is simply a log (mostly for myself) of how to set them up.

Method

1. Install Raspbian Jessie via NOOBS

I bought three SanDisk 8GB Class 10 Micro SD Cards from a high street retailer (they were on offer!). I then downloaded the latest version (1.9) of the NOOBS offline installer from the Raspberry Pi website, unzipped the archive and copied the contents to each of the SD Cards after the had been named and formatted using the SD Formatter 4.0 tool (I did this bit on a MacBook Pro). I then connected then connected each Pi in turn to its TV along with a keyboard and mouse, fired it up and let NOOBS do its thing WRT to installing Raspbian.

2. Setup the WiFi

The first thing I would usually do when installing a Linux system for the first time is update it, however, as I didn’t yet have a network connection, I had to setup the WiFi first. Just to make things more amusing, the available WiFi network uses a security method (PEAP-MSCHAPV2) not (yet?) automatically supported by the Pi’s WiFi utility, so I had to set this up manually. If you’re connecting to a home network you can probably just connect using the GUI utility by putting your password in.

Anywho, I had to enter the following into /etc/wpa_supplicant/wpa_supplicant.conf:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
    ssid="<SSID>"
    priority=1
    proto=RSN
    key_mgmt=WPA-EAP
    pairwise=CCMP
    auth_alg=OPEN
    eap=PEAP
    identity="<USERNAME>"
    password=hash:<PASSWORD HASH>
    phase1="peaplabel=0"
    phase2="auth=MSCHAPV2"
}

Just replace <SSID> with your network name, <USERNAME> with your username and <PASSWORD HASH> with the hash string generated using the following command:

echo -n '<PASSWORD>' | iconv -t utf16le | openssl md4 > hash.txt

Replacing <PASSWORD> with your actual password. The hash string will be saved in a file called hash.txt.

Now reboot you Pi and when it comes up, it should automatically connect to your network.

3. Update and Upgrade

Now its time for the afore mentioned updating and upgrading which I achieved using the following commands:

sudo apt-get update -y
sudo apt-get dist-upgrade -y
sudo reboot

4. Install the Unclutter package

I’ll use the unclutter package later to auto-hide the mouse pointer when its not moved (i.e. almost always). We simply install the package as follows:

sudo apt-get install unclutter -y

5. Set the Hostname

The only way I’ll be able to interact with my Pi’s once they are installed is via SSH over WiFi. On my network I have a dynamic DNS which allows me to find them via nice URL rather than IP address. For this to work, I needed to give my Pi’s a unique hostname which I did using the raspi-config tool (sudo raspi-config). Select to option 9 ‘Advanced Options’, then select option ‘A2 Hostname’, OK the resulting dialog and then enter the desired hostname. The select ‘OK’, ‘Finish’ and when asked if you’d like to do so, reboot the Pi.

6. Auto-starting the Digital Signage Application

Note: I performed the rest of these steps over SSH now that I had a hostname to contact.

My Digital Signage Application is actually just a website so what I want to do is start a web browser automatically when the Pi boots and point it at the desired page. This is done by creating an autostart desktop entry for LXDE (the window manager). First we create the necessary directory as follows:

mkdir -p ~/.config/autostart/

And then we create the file ~/.config/autostart/epiphany-browser.desktop and add the following content:

[Desktop Entry]
Name=epiphany-browser
Exec=epiphany-browser -a --profile /home/pi/.config http://www.google.co.uk
Type=application

This will launch the Epiphany Web Browser in kiosk mode and point to Google (just as an example).

7. Disable the Screensaver and Hide the Mouse Pointer

We don’t want our application going to sleep on us or the screen saver kicking in after a few minutes. Also we don’t want to see the mouse pointer sitting in the middle of our beautiful web page! So we edit the /etc/xdg/lxsession/LXDE/autostart file to read as follows:

@lxpanel --profile LXDE
@pcmanfm --desktop --profile LXDE
@xscreensaver -no-splash
@unclutter -idle 0.1 -root
@xset s noblank
@xset s off
@xset -dpms

Edit: I also seem to need to modify /etc/lightdm/lightdm.conf to stop the screen blanking as follows:

...
[SeatDefaults]
xserver-command=X -s 0 -dpms
...

8. Auto-hide the Panel

the final thing we want to do is hide the task bar, or Panel as its referred to, at the top of the screen. This is simply done by right-clicking on the panel and selecting Panel Settings. In the resulting dialog, select the Advanced tab, navigate down to the Automatic hiding section and check the ‘Minimise panel when not in use’ option. Also, set the  ‘Size when minimising’ option to 0. Ok the dialog and reboot the Pi.

Conclusion

You should not have a Pi that when booted automatically connects to WiFi, opens a web browser and points it at your favourite web page, all the while decluttering the screen and stopping the screen saver from kicking in.

Hope this useful to someone, like and share 🙂

Raspberry Pi and GertDuino

Santa was very generous with his gift giving as far as I was concerned this year. Not only did I receive an Ethernet Shield for my Arduino and a pair of Nordic Semiconductor nRF24L01 boards, I was also delighted to tear the wrapping paper off of a GertDuino. For those who have not encountered one of these wonderful things yet, a GertDuino is an Arduino compatible add-on board for the Raspberry Pi which allows the developer to develop for and communicate with not one, but two Atmel Microcontrollers: an ATmega328p and an ATmega48pa. Now, I’m not going to reiterate all of the information detailed in the excellent 20+ page manual that is available for the GertDuino, if you want to know more about it and its features, read the aforementioned. What I am going to do is briefly describe the setup process and my experience of it.

I started with a vanilla Raspberry Pi Model B with a 16GB SD Card on to which I installed Raspbian via NOOBS 1.2.1. If that doesn’t mean anything to you then… this post probably isn’t for you. The next step is to fire up a terminal and install the Arduino package which contains the cross compiler and the IDE:

$ sudo apt-get install arduino

Then you need a program called avrdude to actually program the ATmega’s:

$ cd /tmp
$ wget http://project-downloads.drogon.net/gertboard/avrdude_5.10-4_armhf.deb
$ sudo dpkg -i avrdude_5.10-4_armhf.deb
$ sudo chmod 4755 /usr/bin/avrdude

The next step is to power off your Raspberry Pi and plug in the GertDuino, and this is where we deviate from the script a little. When I re-powered my Pi I got as far as, what I call the “rainbow test card”, and then nothing: the Pi wouldn’t boot. A little googling brought me to this page which explains that some GertDuino’s have been shipped with the factory test program still residing in the ATmega48 which pulls down the SCL pin and puts the Pi into safe boot. This only happens with NOOBS 1.2, if you’re using NOOBS 1.3 you apparently won’t experience this issue. To fix the issue you set the jumpers on the GertDuino so as to program the ATmega48 (see section 4.2 of the user guide). This will hold the ATmega48 in reset and stops it pulling down the SCL pin. You can now boot the Pi and erase the test program from the ATmega48 using the following command:

$ /usr/bin/avrdude -c gpio -p m48pa -e

This command unfortunately failed for me. Instead of erasing the ATmega48, avrdude complains that it doesn’t know what an “m48pa” is. Some further investigation reveals that this is due simply to a missing definition in the avrdude.conf file. I’ll save you the gory details of how to patch it and let you download it: avrdude.conf. Just place it in the /etc/ folder on your Pi and rerun the above command. Now you can power down the Pi, swap the jumpers over to program the ATmega328 and run the examples.

Thats as far as the GertDuino documentation takes you (which is a very long way!). However, if you want to use the Arduino IDE to write your sketches and program your processors then you’ll need to install one more thing. Gordon Henderson, as well as porting avrdude to the Pi, has created a script that configures the Pi and the Arduino IDE for us. All you have to do is the following:

$ cd /tmp
$ wget http://project-downloads.drogon.net/gertboard/setup.sh
$ chmod +x setup.sh
$ ./setup.sh

Now you can start the Arduino IDE:

$ arduino

You’ll need to tell the Arduino IDE to use your GertDuino and to use a programmer (the Pi’s GPIO in this case). So, from the menu:

Tools > Board > Gertboard with ATmega328

and then:

Tools > Programmer > Raspberry Pi GPIO

I then wrote the most minimalistic sketch version of the blink example documented in the GertDuino’s user guide as follows:

#define NUM_LEDS 6
// LED:              0   1   2   3   4   5
// PIN:              PB5 PB1 PB2 PD3 PD5 PD6
unsigned int LEDS[]={13, 9,  10, 3,  5,  6}, LED=0;
int DIR=-1;

void setup(){
  unsigned int i;
  for(i=0; i&lt;NUM_LEDS; i++) pinMode(LEDS[i], OUTPUT);
}

void loop(){
  digitalWrite(LEDS[LED], HIGH);
  delay(100);
  digitalWrite(LEDS[LED], LOW);

  if(LED==NUM_LEDS-1 || LED==0) DIR*=-1;
  LED+=DIR;
}

To send this to your ATmega328, you can’t use the upload button on the IDE’s toolbar, you have to use the following menu option:

File > Upload using programmer

Or use the Ctrl+Shift+U keyboard shortcut. All being well you should see a KITT like chase pattern on your GertDuino’s LED’s as can be seen in the following video:

YouTube Preview Image

My apologies for the dodgy, shaky, out of focus camera work!

That’s it, that’s as far as I’ve got so far. It took me just a couple of hours to get here and that includes waiting for downloads and googling for my couple of issues. I hope this little post helps someone else get up and running with their Pi/GertDuino combo.

Edit 06/01/14

Since writing this post I have been playing with getting serial communication between the Pi and the ATmega328 up and running. After some false starts and helpfull suggestions from the Raspberry Pi forums, I found I needed to make a change to boards.txt and adjust the CPU frequency Arduino uses to calculate baud rates. See the forum topic here for details.