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!