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!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.