Command Line experiments

Trying a few things out on my Pi from various blogs.
NB: this is a stream-of-consciousness experimentation post. I will re-post the most exciting part (text-to-speech) in another part, all cleaned up!

RPi Blog – Optimizing for a headless install
Now, I do use the desktop environment, so not doing the first bit.
But, I like the idea of speeding the terminal login up. So:
Result: Yeah, it speeds it up by a couple of seconds.


Fusion Strike – Free up RAM
free -m
sync
edit /proc/sys/vm/drop_caches
change the 0 to 3 and save
free -m
You should see a drop in memory usage.
Result: I did! 3 MB more free

What is my IP address and can you email it to me?
I just followed the instructions from the wiki
It works. Yay.

What is my IP address, and can you tell it to me?
This is to get the Pi to find it’s own IP address and tell you vocally over the 3.5mm audio jack.
First of all, you need to install Festival, which is the text-to-speech converter.

  • apt-get install festival

Takes a while, it’s about 20MB.
Plug your earphones/speakers into the 3.5mm jack
Right, apparently this should work:

  • echo “Hello” | festival –tts

But it doesn’t.
So, moving onto…
http://www.raspberrypi-spy.co.uk/2012/06/raspberry-pi-speakers-analog-sound-test/

  • apt-get install alsa-utils

Well, apparently that’s already installed.

  • modprobe snd_bcm2835
  • amixer cset numid=3 1
  • This last command should turn the analogue output (i.e. the 3.5mm jack) on.
Right, now build the test audio file:
  • cd /opt/vc/src/hello_pi
  • ./rebuild.sh
  • cd hello_audio
  • ./hello_audio.bin
  • You should hear something like a siren run through a bad 50s sci-fi tv show.
Now to get something working properly. Festival would be lovely, but right now I’d settle for anything that could just play SOMETHING without compiling it first!
  • Get mplayer:
    • apt-get install mplayer
  • mplayer doesn’t appear to be able to play the test file I downloaded.
  • Let’s try using aplay… There are some samples in a directory from a package called ‘alsa’, so let’s use one of those.
  • aplay /usr/share/sounds/alsa/Front_Center.wav
  • Yes! Works. Dull, but it works.

Let’s try a different audio package called ‘sox’.

  • sudo apt-get install sox
  • sox /usr/share/sounds/alsa/Front_Center.wav -t alsa default
Nope that didn’t work.
Okay, let’s at least make sure that that modprobe command is automatically sorted out on reboot.
  • Edit /etc/modules
  • Make sure that snd_bcm2835 is in the file.
  • Once it is (it might be already) you do not need to do the modprobe again after each reboot.
Right, let’s get back to the text-to-speech
  • Okay, let’s try mplayer again
  • mplayer /usr/share/sounds/alsa/Front_Center.wav
  • Nope, nothing. Get an error about not being able to open socket…
  • Moving on to  https://bbs.archlinux.org/viewtopic.php?id=40013
  • Edit /etc/mplayer/mplayer.conf
  • Add nolirc=yes
  • And run that mplayer command again.
  • Yes! Works!
  • Try Festival again
  • echo “Hello” | festival –tts &
  • YES! It spoke to me!!! Woo-hoo!!!
  • You can also use ax206geek’s script to use Google’s text to speech engine. Create a file called speech.sh:

#!/bin/bash

say() {
local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols “http://translate.google.com/translate_tts?tl=en&q=$*”; }
say $*
chmod u+x speech.sh
And then call it using: ./speech.sh Hello Dave
That works, too!
Now, I want to get the Pi to email me AND speak the IP address.
I can do that…
  • As root…
  • cd ~/
  • mkdir bin
  • cd bin
  • NB This script works for Google email accounts. Change the smtp server for other email hosts.
  • create/edit a file called ’email_ip_address.py’
import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
# Change to your own account information
to = ‘<where you want it sent>’
gmail_user = ‘<google email login address>’
gmail_password = ‘<google email password>’
smtpserver = smtplib.SMTP(‘smtp.gmail.com’, 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
# Very Linux Specific
arg=’ip route list’
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data = p.communicate()
split_data = data[0].split()
ipaddr = split_data[split_data.index(‘src’)+1]
my_ip = ‘Your ip is %s’ %  ipaddr
msg = MIMEText(my_ip)
msg[‘Subject’] = ‘IP For RaspberryPi on %s’ % today.strftime(‘%b %d %Y’)
msg[‘From’] = gmail_user
msg[‘To’] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()
  • Save & quit
  • chmod a+x email_ip_address.py
  • Now create/edit another file: say_my_ip_address.sh
#!/bin/sh
echo `hostname -I` > /tmp/check_ip.out
_CHECK_IP=`cat /tmp/check_ip.out`
_LEN=`expr length $_CHECK_IP`
if [ $_LEN -gt 3 ]; then
        echo “” > /tmp/shoutout.tmp
        echo “I am, a, Raspberry Pi. My I,P address is. ” >> /tmp/shoutout.tmp
        for EACH in `hostname -I | grep -o -e “[^.]*”`; do
                for BIT in `echo $EACH | grep -o -e .`; do
                        echo $BIT >> /tmp/shoutout.tmp;
                        echo “. ” >> /tmp/shoutout.tmp;
                done
                echo “dot. ” >> /tmp/shoutout.tmp;
        done
        echo “. Would you like to play a game?” >> /tmp/shoutout.tmp
        cat /tmp/shoutout.tmp | festival –tts
        rm /tmp/shoutout.tmp
        rm /tmp/check_ip.out
else
        echo “I do not yet have an I,P address” | festival –tts
        sleep 5
        /root/bin/say_my_ip_address.sh 2&>/dev/null
fi
  • Save and quit.
  • (All those fullstops and commas space out the words so it sounds like an IP address!)
  • chmod a+x say_my_ip_address.sh
  • Now we put them in to our start-up routine.
  • Edit /etc/rc.local
  • Add the following lines:
sudo -u root python /root/bin/email_ip_address.py &>/dev/null
sudo -u root /root/bin/say_my_ip_address.sh &>/dev/null
  • And reboot
  • Don’t forget to have your earphones plugged in!

Installing Raspcontrol

I subscribe to a lot of Blogs for the Raspberry Pi.
The newest post that I came across was Jacob Clark’s Raspcontrol.

He’s developed a web-based control panel for headless Pis, so you can see what Pi is doing remotely without going anywhere near a sh terminal or VNC!

Installation

  1. First of all, you will need to be running your Pi off of a decent sized SD card. I’d recommend 8GB+, but you can probably get away with 4GB. You’re going to be turning your Pi into a web server, so you need to install a few packages.
  2. I use WiFi, but as long as your Pi is connected to your network, these instructions should work.
  3. Turn your Pi on, connect a terminal to it (I use Putty) or plug a keyboard in.
  4. Login as the pi user.
  5. Go to the GitHub repository for Raspcontrol
  6. First of all (following the installation instructions at GitHub) install Apache 2 and PHP-5:
    1. sudo apt-get install apache2
    2. sudo apt-get install php5
    3. These take a while and are quite large, hence the recommendation for a decent size SD card.
  7. cd /var/www
  8. sudo mkdir raspcontrol
  9. cd raspcontrol
  10. wget https://github.com/Bioshox/Raspcontrol/zipball/master
  11. This will download the .zip file containing the Raspcontrol code. It will be called ‘master’, so you need to give it the proper extension.
  12. mv master master.zip
  13. unzip master.zip
  14. This will give you a folder starting with Bioshox…
  15. You must add the Apache user “www-data” into your sudoers file, which means they can run Superuser Commands. To do this from the shell type ‘sudo visudo’ scroll to the bottom of the file and on the last line add:
    1. www-data ALL=(ALL) NOPASSWD: ALL
  16. Okay, we now need to remove the master.zip file:
    1. rm master.zip
  17. And move everything from the Bioshox folder into the current /var/www/raspcontrol folder.
  18. (This is a pain, really, but never mind)

Let’s go a-webbing

  1. On your laptop, with a browser, go to http://<the ip address of your Pi>/raspcontrol
  2. You _should_ get the Raspcontrol login screen:
  1. Now go to http://<ip>/raspcontrol/setup.php
  2. You now get a very bare HTML form asking for a password. This is the password you will use on the login screen you saw a minute ago. So, enter a decent password and click the button. This will return you to the login screen.
  3. Login using username “admin” and your password.
  4. You should now see the dashboard!


Install complete!

New update
As I was writing this, I noticed a new update for Raspcontrol
So, first I did:

  • cd /var/www
  • mv raspcontrol raspcontrol_0.1
Then, I got the new sources from github:
  • wget https://github.com/Bioshox/Raspcontrol/zipball/master
  • mv master master.zip
  • unzip master.zip
  • mv Bioshox…. raspcontrol
This means your raspcontrol folder and URL now contains the new code and dashboard. Use the same username/password as you did the first time.
Nice GUI update, and some new bits and pieces! Good job, Jacob.

The Raspberry PiPod – version 1 – finished!

I’m calling an end to Phase 1 of the PiPod project. I have a working device and all the project aims have been completed.
There is a second Phase, but I need to get a couple of parts before I start doing that.

The original post showing the components of the PiPod can be found here.

Some photographs of my “PiPod”

So, this is it, the PiPod. As you can see, it’s a self-contained unit built around a small Really Useful Box. On top, you can see the 3.5″ car rear view monitor. This is held in place with friction from four screws and bolts. It was pretty secure in there, but I added some joined cable ties to reinforce the ‘holder’.
You can see the wires for the screen, including the power inlet (the red thing). You can also see the Belkin WiFi dongle connected using a small USB extension lead so it can be attached to the box.

This is a view of the side of the PiPod showing that I’m using the composite video output to connect to the screen. The white socket is a spare input for the screen, which I’m not using. You can see on the right a Velcro loop that I use to hold the power cable so it doesn’t drop off the table when disconnected. You can also start to see how the Pi is held in place inside the box… Cable ties again.
I hand-cut (with a craft knife) two holes for the sockets that side.

This is an end-on view showing the hole I cut out for the power cable. I know, it’s all a bit rough, but it was fun!

The other end of the PiPod showing the opening for the LAN port and the USB ports. You can just make out the nano Bluetooth adapter. One of the big problems with the Pi is that if your USB device plug is a bit on the large side (or the case of the device is) then it becomes very difficult to fit two plugs in, one on top of the other. That’s why the extension cable came in handy – it was short enough not to lose too much power and meant that the USB port didn’t get damaged.

This is the underneath of the Pi – you can see the SD card and the cable ties. Messy, but it works well.

I love my little flip-up screen! I could take it out of there and mount the screen direct to the lid of the box, but I don’t want to damage the screen by prising it out. It’s nice to have the built-in screen protection anyway.

And the top comes off… I needed to make sure that I could still get the SD card in and out, but to have it inside the box (rather than sticking out, which is how most of the ‘professional cases do it. It’s a bit fiddly, but with the cable tie going diagonally across, it’s still accessible. I cut a few of the small bits of the outer part of the box away so that the cable ties laid in the ‘grooves’ on the corners. I am covering the HDMI port here, with the USB dongle, but made it so you could remove the dongle.

View from the other side.
And it all works! I would’ve put a video up, but forgot to bring the power cable in today.
Speaking of power, the whole thing runs off two power cables – one for the screen (12V) and one for the Pi (5V). I would at some stage like to get the whole lot running off battery, but I think that might be a lot of expense just for this project, which does everything I wanted it to anyway!

In terms of power, the WiFi dongle is slightly underpowered, but my experiments with a powered USB hub were hit-and-miss and I didn’t want to buy another one. It connects to my home and work WiFi nicely anyway.

The last part of recording this project will be a video showing boot-up, X-desktop operations and using the Rii keyboard/touchpad. That’ll be tomorrow!

Raspberry Pi emulation

Taking a quick break from my PiPod project, which is essentially done, bar photos and videos of it actually working!

I’ve decided that having a “virtual Pi” would be handy so that I can mess about with the OS without worrying too much! Plus, it means I don’t have to carry my Pi around with me all the time if I just want to try some Python etc out.

Important note:
For Windows users, it seems that the recommended solution for emulating the Pi is to use qEmu on Windows. Take a look at this Sourceforge project: Raspberry Pi Emulation in Windows.

Virtual Box

Just so you know what you’re installing on your main computer… VirtualBox is a wonderful, free, virtual computer creator and viewer. There are a variety of platforms it works on. I’m actually using a version I already have on my Windows 7, 64-bit machine.

Emulating the Pi

I’m referencing this guide from the Southend Linux User Group to a certain extent. I know, random, but that’s what Google came up with!
I should say before we begin that I’ve tried both the ‘Lenny’ and ‘Sarge’ Debian installs available from virtualboxes.org and neither of them worked (Lenny stopped at “Select and install software”, Sarge gave a rather horrid “Kernel panic”) hence I now use the official Debian site download instead!
  1. Download VirtualBox for your platform.
  2. Download a net install ISO image from debian.org itself.I’ve chosen the i386 small CD in the top-left area. 191MB to download…
  3. Create a new machine on VirtualBox, take the defaults, choose to create a new disk for it.
  4. Start the machine up.
    1. You get the first-time start-up dialogue.
    2. Choose to boot from the ISO image you downloaded.
    3. Choose your language/country etc. Let it run for a bit.
    4. Now, this installer looks very similar to the one that stopped on the Select and Install Software dialog, so hopefully it’ll work better than that one!
    5. Take the defaults all the way through, no need to be picky unless you want to. Set the root password, create a new account, set that password…
    6. Use the guided partitioning mode at the top.
    7. Choose to put all the files on one partition and write the changes to the disks.
  5. Off it goes, partitioning away, installing the ‘base system’. This is where it starts extracting the downloaded packages and going off to download and install the rest of the OS.
  6. This is going to take a while, I can tell!
  7. Now we’re onto ‘Select and Install Software’… Holding my breath…Ooooh. It’s gone to 12% and still running… This could be good. Yep, “Retrieving file” counter still running… Goooood.
  8. Choose the options you like. I just left it as a ‘GUI’ and the recommended/default options.
  9. Some time later…
    Yes, I think I will install the GRUB boot loader. If only because I saw that the absence of this affected installation attempt number 2.
  10. Yay! Installation is complete. I’ll believe it when I see it 😉
  11. Now it’s running clean-up… Oh, I hope it moves on from 65% soon, otherwise… Ah. There we go.
  12. Rebooting automatically.
  13. Hmmm. Get the boot choice again… does that mean it’s going to start the install again? God, I hope not!
  14. I have a black screen… some kind of progress swirly…. Ah! Desktop!
  15. Go ahead and login. Don’t use root, use the ‘other’ account you created.
  16. Love the retro rocket-and-planet motif. Taking a while for anything to happen… Probably just a first-login thing.
  17. Hmmm. Okay… That’s a crappy GUI… no start bar or icon dock or anything… I can soon install something new of course. Oh, I see… there’s a menu bar at the top. Kind of merges into the VirtualBox menu. Some kind of message about Guest Additions came up but I missed it.
  18. Right, let’s see what we’ve got and if we can carry on with the emulation stuff…

Terminal Time

  1. From the Applications->Accessories menu, start up a terminal.
  2. You should get a shell prompt with your ‘other’ username attached to it.
  3. Right, install the qEmu stuff.
    1. su root (use the password you specified during install)I’m doing this because I can’t sudo… because I’m not in that group of users, which is unhelpful.
    2. apt-get update(i.e. update the package database).
    3. apt-get install qemu
      (Go and get and install the qemu package)
    4. Now, qemu is installed. That page I am referencing as a guide (the Southend one) makes it a lot more complicated than it needs to be!
  4. Now, you need to cd ~<other username> to go into the home folder of your ‘other’ user. We need to create a folder to mess around in and store kernels and disk images.
  5. mkdir pi_emu
  6. cd pi_emu
  7. wget xecdesign.com/downloads/linux-qemu/kernel-qemu
  8. This will get the qEmu kernel, which makes everything run. I hope.
  9. You now need to get the current Linux image for the Pi that is recommended by the RPi foundation. The one I am using is the first release of Raspbian Wheezy. But, always check for the latest one at their downloads page. Get the URL of the release and do:
  10. wget <url>
  11. This is NOT a small file – it’s about 440M, so go and get a cup of coffee.
This takes around 10 minutes to install at work, and we’re on a T1 connection. The reason it’s so slow is probably because of where the file is coming from rather than the connection itself.
And we’re back…
  1. Unzip the file your downloaded (you will have to change this filename to whatever is now in your folder.
    1. To get a list of files, the Linux command is ‘ls’.
    2. unzip yyyy-mm-dd-whatever.zip
      (to unzip it)
    3. This takes a while as well as it needs to expand the 440M file to a roughly 2GB file. Gawd bless file compression.
    4. Now, if you are logged in as root (and I was, so you should be!), you will need to make sure that the files in that folder are ‘owned’ by your other user, which will shall call ‘otheruser’. If you don’t do this, the .img file (and therefore the Virtual Raspberry Pi) will be ‘readonly’ when you run it using your otheruser account.
      1. chown otheruser *
      2. chown otheruser *.*
      3. exit
    5. You should now be back at a prompt that looks like
      otheruser@debian:~/pi_emu
  2. Now, going to use a modified version of the command from the Southend site to start it up. Here goes:
    1. qemu-system-arm -kernel kernel-qemu -cpu arm1136-r2 -M versatilepb -no-reboot -append “root=/dev/sda2 panic=1” -hda 2012-07-15-wheezy-raspbian.img
    2. The last argument there is the .img file that you decompressed just now.
    3. Apparently, you can give it more memory by adding the “-m 512” argument to the end, but I just want it started up for now.
    4. I’m getting a few ‘readonly file system errors’, so it’s possible I might have to change permissions on the .img file before it works, but let’s just see…
    5. Not bad so far. Oooh! The usual Raspberry Pi first start-up screen! Yay!
    6. Choose the ‘update raspi-config’ option at the bottom just so you are up-to-date.
    7. Now re-run that utility:
      sudo raspi-config
    8. Choose the SSH server option and enable it. This will let you open a remote terminal to the Pi if you need to. Choose OK afterwards. Then choose ‘Finish’.
    9. Now we do ‘startx’
It’s interesting… the desktop comes up (although it’s blue, rather than raspberry-coloured… Odd). It’s a problem others have come up against.

For Windows users, it seems that the recommended solution is now: Raspberry Pi Emulation in Windows.

Never mind, it was an interesting practical experiment, which sort-of works apart from the colour issue. Non-Windows users may find these instructions still useful, so I will publish this post anyway!

Bluetooth on the Pi

Here we enter the murky world known colloquially, by me, as “Oh, Bluetooth, why do you mock me?”
This has been the biggest pain in the butt to get working, but hey, no-one ever said that the RPi was going to be easy.

Nice logo, isn’t it. Took me all of 30 seconds to search for it on Google images.

Installing the Bluetooth stack

Let’s remember, folks, Bluez is the official bluetooth stack for Linux! So it should work on everything, yes? Yeah, right. Here we go with the steps…

  1. Start the Pi, login as root.
  2. apt-get install bluetooth bluez-utils blueman
  3. From left to right, that’s:
    1. The stack. (A very large thing indeed)
    2. Command line utils. (A very useful thing indeed)
    3. X-windows interface. (A very slow thing indeed).
  4. Go and make yourself a coffee. This takes ages, especially over wi-fi. In fact, you could probably go and cook dinner.

Scanning and connecting

Onto the good bit. Getting your bluetooth device connected. In my case, I have a Rii bluetooth keyboard/touchpad.

My one came with a nano USB bluetooth dongle. In other words, it’s very small. Which is good.

  1. Okay, so you have the stack installed.
  2. Turn your Rii on and put it in discoverable mode.
  3. On your Pi, type “hcitool scan” to see if you can find your device.
  4. You should get a list of devices back in your area. The Rii comes up as “Macro Keyboard”. A stunning name, to be sure.
  5. Make a careful note of the string of numbers, colons and letters.
  6. Do the following to create a connection script:
    1. cd ~
    2. mkdir bin
    3. cd bin
    4. Create a file called auto_connect_bluetooth.sh and open it in your editor. Add the following lines. Put the string of numbers, colons and letters that you noted down before instead of XX:XX etc.
      #!/bin/sh/usr/bin/bluez-test-device trusted XX:XX:XX:XX:XX:XX yes/usr/bin/bluez-test-input connect XX:XX:XX:XX:XX:XX
    5. Save the file and exit from the editor.
  7. chmod +x auto_connect_bluetooth.sh
  8. (This makes the file executable so you can do the next bit).
  9. Open /etc/rc.local in the editor. Add the following command before the exit command at the end of the file.
    1. sudo -u root /root/bin/auto_connect_bluetooth.sh
  10. This should run your connection script before the login prompt comes up.
  11. Before you do a reboot, make sure your bluetooth device is still in discoverable mode.
  12. Reboot and see if your device connects!
  13. Well… it doesn’t.
  14. You need to go into X windows (startx) and use the blueman-manager applet to pair up the device, trust it and connect to the input service.
  15. Once you’ve done that, and reboot, and it should connect this time.
If you find those last few steps unfollowable, comment on this post and I’ll try and flesh it out a bit.

Setting up VNC server on the Pi

Install VNC and Auto Start

Here’s how to install VNC and get it working with the Pi across a network

First of all, make sure you expand the SD card – you don’t want to run out of space!

  1. apt-get install tightvncserver
  2. Yes, you want it to install, so accept the space it tells you it’s going to use!
  3. It’ll go through the usual install procedure and eventually put you back at the shell prompt.
  4. Type:
    1. tightvncserver :1
    2. (This will create a few files it needs to run)
    3. Set a password when it asks. Don’t bother with a view password unless you really want one – it’s just a pain.
  5. Edit the /etc/rc.local file and add the following before the ‘exit’ command at the bottom.
    1. sudo -u root /usr/bin/vncserver :1 -geometry 1024×768 -depth 16 -pixelformat rgb565 -fp /usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/
  6. Save the file, back to the prompt you go.
  7. This will mean that your VNC server starts up before you need to login on port 1 and a screen size of 1024×768.
  8. Reboot. Watch that magic happen. Dude. S’up.

The reason you do sudo is that it needs to know who to run the server as – remember, you’re not logged in before the login prompt comes up!

Now, you should be able to reboot and connect to the VNC server from your laptop, for instance.
You’ll need to download the Tight VNC viewer to do this.