The Pragmatic Bookshelf | Raspberry Pi

>The Pragmatic Bookshelf | Raspberry Pi:

Raspberry Pi: A Quick-Start Guide gives you everything you need to get the Raspberry Pi up and running and doing cool stuff. You’ll get started by learning what additional hardware you need and how to connect it, install Debian Linux and configure it to your needs, and customize the Pi’s firmware to get the most out of your hardware.

‘via Blog this’

Dynamic DNS – open up your Pi’s webserver to the World

I’ve installed Raspcontrol by Jacob Clark along with Apache and PHP. I did a guide for that in a previous post.

What I want to do now is to get the webserver running it open to the World.
I’m on a dynamic IP with my ISP (I think!) so I want a way of getting the Pi to use some kind Dynamic DNS  so I can refer to it by hostname.

I looked around for Dynamic DNS providers and found DNSDynamic.org
Looking at http://www.dnsdynamic.org/api.php, I can see they have an API that can be configured with ddclient, whatever that is. It’s got an obscure name, so it’s probably linux!

So first of all:

  • Go to www.dnsdynamic.org and register.
  • Then, once you’ve validated using their registration email, add a new domain.
  • For the purposes of this tutorial, let’s go with
    wibble.dnsdynamic.com

Then:

  • Start the Pi, login as root and then:
  • apt-get install ddclient

That installs and I automatically get an interface pop up on my screen:

So I choose other…

  • Asking me for the name of the provider server, so let’s put in www.dnsdynamic.org
  • DNS Dynamic uses the dyndns2 protocol, so choose that.
  • Enter your username and password.
  • It now asks for the network interface to use. Now, I use Wifi, so I put in wlan0, but if you are using a cabled LAN connection, you’ll use eth0.
  • Now enter your domain – wibble.dnsdynamic.com
  • At this point, ddclient will configure itself. At this point, I rather hope that it tells me what it does next!
  • Okay, so it installs more stuff…
  • Interesting… the installation completes and… tells me nothing…
  • Right, looking at the ddclient documentation it tells me where the configuration file is. Not quite right in the documentation – the config file is at /etc/ddclient.conf
  • Apparently, the way to run it automatically is to add the following to your /etc/rc.local:
    /usr/sbin/ddclient -daemon 300 -syslog
  • The ‘300’ is the number of seconds it should wait before checking to see if the IP address has changed. I think this is fine, even for my wifi connection with it’s customary 2 minute connection delay.
  • So, let’s try it!
You check your IP address by going to dnsdynamic.com, logging in, going to “manage” and editing your existing domains.
If you are behind a router (and let’s face it, who isn’t!?) then this will (incorrectly) show your INTERNAL IP address. (Mine says 192.168.1.80 for instance). We don’t want that, so I found a help page: http://superuser.com/questions/389125/ddclient-updating-to-local-ip-instead-of-public-ip
To fix:
  • Edit /etc/ddclient.conf (for example > nano /etc/ddclient.conf)
  • Change the line
    use=if, if=wlan0
    and add a # in front of it.
  • Add the line
    use=web, web=checkip.dyndns.org
  • Save and quit.

You  need to kill the current ddclient and restart it:

  • ps -ef | grep ddclient
  • Make a note of the process id (which is the first number on the left)
  • kill -9 <process id>
  • Then re-run ddclient by typing:
  • ddclient

This will run it in the background and update the dynamicdns.org IP address to your external ip.

Go back to dnsdynamic.org and check your existing domains again. You should have your external IP address showing now. (I did).
You should now be able to: ping wibble.dnsdynamic.org and get that same IP address back.
The next thing to do is to do a port forward on your router. There are various guides to do this. You will need to point port 80 (HTTP) of your router at your INTERNAL IP address, port 80, or however your router identifies the destination. (Assuming that’s the port your webserver is running on).
I managed to do this on my BT Home Hub without much trouble
Now, to test it. First of all you cannot just put your dynamic DNS hostname into your browser and expect it to work. I think it’s got something to do with looping back to yourself.
So, go to www.proxify.com and type your wibble.dnsdynamic.com into there. You should get your Pi’s homepage! Wahey!

Headless Raspberry Pi – Tell me my IP address!

What is my IP address and can you email it to me?
Instructions for getting your IP address emailed to you can be found on the RPi 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.

Installation

  • Install the Festival package
    • apt-get install festival
  • Install the alsa utilities/drivers if you do not already have them
    • apt-get install alsa-utils
  • 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.
  • Push audio to the 3.5mm jack
    • amixer cset numid=3 1
  • (If you want to put it back to automatic, or HDMI, change it to 0 (auto) or 2 (HDMI))

Test out your audio

  • Do the following:
    • su root
    • 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.
  • Let’s try testing ‘aplay’ to see if that works. Use one of the ‘alsa’ sample files.
    • aplay /usr/share/sounds/alsa/Front_Center.wav
  • Yes! It works. Dull, but it works.
  • Install mplayer:
    • apt-get install mplayer
  • Edit /etc/mplayer/mplayer.conf. Add the following line:
    • nolirc=yes
  • mplayer /usr/share/sounds/alsa/Front_Center.wav
  • Yes! Works!

Try out Festival

  • To make Festival say hello, try this:
    • echo “Hello” | festival –tts &
  • YES! It spoke to me!!! Woo-hoo!!!

The Script and running it

  • Return to your root folder:
    • cd /root
    • mkdir bin
    • cd bin
  • Create and edit a file called: say_my_ip_address.sh and use this:
#!/bin/sh
# First of all, see if you have an IP address yet
echo `hostname -I` > /tmp/check_ip.out
_CHECK_IP=`cat /tmp/check_ip.out`

# Find the length of the IP address
_LEN=`expr length $_CHECK_IP`

# Check the length of the IP address and make sure it’s sensible
if [ $_LEN -gt 3 ]; then
        # Construct your string to push to Festival
        echo “” > /tmp/shoutout.tmp
        echo “I am, a, Raspberry Pi. My I,P address is. ” >> /tmp/shoutout.tmp

        # This is so the IP address is said ‘nicely’
        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

        # And a bit of optional retro War Games!
        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
        # If the IP address is not valid, report vocally and delayed re-run
        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

(All those fullstops and commas space out the words so it sounds like an IP address!)
  • Save and quit.
  • Make the file executable:
    • chmod a+x say_my_ip_address.sh
  • Edit /etc/rc.local
  • Add the following lines:
sudo -u root /root/bin/say_my_ip_address.sh &>/dev/null
  • And reboot
  • Don’t forget to have your earphones plugged in!

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!