Tuesday, April 30, 2013

Voice Control on the Raspberry Pi

Note: Updated version here:
http://stevenhickson.blogspot.com/2013/05/voice-command-v20-for-raspberry-pi.html

I'm finally releasing my Raspberry Pi voice control software.
The beauty of this is that you can use it and customize it without programming anything.
This will work on any linux machine but I find it uniquely suited for the Raspberry Pi and that's what I developed it for.
You just type voicecommand -e to edit the configuration file and add any speech and the coinciding command. You can see all of the little nifty commands it can recognize just with my config file in the video. I use all of these pretty often. It is very mutable and works with lots of other programs.

Watch this video to really get a feel for the program:




The commands should be in the format speech=command. Ex:
music=xterm -e pianobar
Doctor Who=playvideo -r -f Doctor Who

Unfortunately, it is caps sensitive voice control right now, but you can test the input by running
speech-recog.sh
on you machine to see what the speech recognition will give you. It uses Google's api and is really good at recognizing what you say. I used to use espeak instead of festival for the text to speech because of the speed difference. However, after some very helpful comments from people, I've switched to using Google's TTS api, which is much more pleasant to listen to. The video above is still using espeak but the newest code does not.

I hope you will try this out and make some cool things with it. Instructions to install are below.
I'm in the process of setting up a repository so until then, here are the instructions (do this in the terminal):


sudo apt-get install git-core
git clone git://github.com/StevenHickson/PiAUISuite.git
cd PiAUISuite/Install/
./InstallAUISuite.sh

Update Instructions 

cd PiAUISuite
git pull
cd Install
sudo ./UpdateAUISuite.sh


Here are the dependencies required to run and build:
sudo apt-get install libboost1.50-dev libboost-regex1.50-dev youtube-dl axel curl xterm libcurl4-gnutls-dev mpg123 flac sox

**Note: If the file .commands.conf doesn't exist in your home directory and you have an old version of code, the program will exit. You should either grab the newest code from github or create the file yourself.

**To make it listen for longer, edit the file /usr/bin/speech-recog.sh and change -f cd -t wav -d 3 to -f cd -t wav -d # where number is how many seconds it should listen.

Consider donating to further my tinkering


Places you can find me

Thursday, April 25, 2013

Raspberry Pi Settlers of Catan Server

So a couple of days ago I decided I wanted my Pi to be a Settlers of Catan server.
I decided on using a custom compiled JSettlers version instead of the C++ pioneer version because of the extra perks it had.

It runs surprisingly well considering it is in java but it makes sense since it is making the client's computer do most of the work. I was also decently surprised with the bots. The server allows you to create practice games against bots or real games where other people can sign up to play with you.

It also has multiple scenarios and lots of documentation. Sometimes it is a little buggy but overall I'm pretty happy with it as of right now.

I've uploaded the version I have compiled and an install script. To run it, first you need to have java and apache (or some other web server) installed. If you don't, run:
sudo apt-get install apache2 openjdk-7-*

**Note, you don't need apache to run, you could use others such as NGinx or no web server at all and just play the game by yourself.

Then download the file, unzip it, and install:
wget "https://stevenhickson-code.googlecode.com/files/PiSettlers.tar.gz"
tar -xvf PiSettlers.tar.gz
cd JSettlers2
sudo ./install-web.sh

and now you should be done, you can go to your IP address in any computer on your network and play it.
Mine is at http://192.168.1.100/settlers/
**Note, if you want to play it outside of your network, i.e. you have a domain name, then you need to open port 8880 on your router to go to the pi.

Here is what it looks like playing a game hosted from the pi on my fedora laptop.

Consider donating to further my tinkering


Places you can find me

Friday, April 19, 2013

Using Youtube on the Raspberry Pi without XBMC or gnash

There is an update to this here.

You can install this by pulling the source from git and running the InstallAUISuite.sh file.
Commands to install are below (your user should have sudo priveledges but you don't need to sudo or be root to run the commands unless shown otherwise):

sudo apt-get install git-core
git clone git://github.com/StevenHickson/PiAUISuite.git
cd PiAUISuite/Install/
./InstallAUISuite.sh

Update Instructions 

cd PiAUISuite
git pull
cd Install
sudo ./UpdateAUISuite.sh


Demonstration:


So this is a quick video to demonstrate youtube working fast and easily on the Raspberry Pi without the use of XBMC or gnash.
I tried out gnash first which didn't work at all.
So then I tried out XBMC and although it is pretty, it is awfully slow so I decided to get youtube to work on my own.
The basis of this is really simple. There is a nifty program out there called youtube-dl that can get the media file from the website and download it. It's great because it does it fast and works for many other websites like the daily show and the colbert report.
So what I did was basically write a little script that analyzes your input and then gets the proper information from the youtube-dl program without downloading the video, then it sends this http stream to omxplayer to play.
It can handle playlists or individual files from multiple websites as well as a bunch of other things.

After that, I started implementing a nice little search feature using C++ and Curl.
I'm starting to implement a GUI which will essentially look like youtube kind of. Until then I have this nifty search tool, which was essentially my start to the GUI I am currently creating.

If you've ever used XBMC's youtube, you will notice that this is a lot faster, and in my opinion, easier to use and more natural. Plus it only requires a tiny file on your system as opposed to XBMC's massive footprint.

The nice part of having an easy command line program like this is that it can be incorporated into a lot of other things easily. For instance, I have incorporated it into my voicecommand script as shown in the video.

The source code can be found here. To install see the top.

Sources:
This post for using axel with the youtube-dl.
This page for the youtube-dl source and documentation.

 Consider donating to further my tinkering


Places you can find me

Tuesday, April 16, 2013

How to fix Raspberry Pi boot problems

If you have any problems with your SD card messing up or your Pi not booting, this is an attempt at documenting my issues with booting or SD card corruption and how I fixed them.

This can range from problems with rpi-update to changing the /boot/config.txt file to restoring the SD card superblocks from the superblock backup to Oh No my SD card is hosed and I need to recover files. We've all been there so need to be embarrassed about messing something up. Things mess up a lot.

Boot Problems

First of all, this is a great reference that I use a lot.
Start by figuring out what changed.
  • Did you just change your /boot/config.txt?
  • Did you delete any important files?
  • Did you run rpi-update?
  • Did you add a new device?
  • Did you overclock too much?
  • Do you get a kbd prompt?
  • Did it run some weird commands on the shutdown about "device still open for writing"?
If you just changed your config.txt, you should change it back and then change line by line while rebooting until you see what option is causing the error.

If you deleted any files, restore them.

If you ran rpi-update and it won't reboot, try making your /boot/config.txt the default one. The cma_lwm and cma_hwm options sometimes cause the new firmware to break.
If that doesn't work or you have flashing light errors, restore the old boot files by copying the /boot.bak/ files into /boot/
sudo cp -ap /boot.bak/* /boot/

If you recently overclocked, try reducing it back down. I run 1100 MHz easily but 1200 MHz tends to be unstable for me.

If you get a kbd prompt or error, check your power supply to make sure its supplying enough voltage, then reduce any overclocking or overvolting in your config.txt, then (if the first two don't solve it) replace the boot files like described above.

If you have a kbd prompt that you couldn't fix, you get read/write or I/O errors on shutdown or startup, or 
it can't find the ext4 partition, then you have been a victim of SD card corruption.
Don't worry though. There are still ways to recover your files.

The first thing you should do is make a copy of your SD card image.
Plug the card into another computer with linux and use dd to copy the image. Then you can try to fix the image file without further corrupting the data.
The first thing to try to do is replace your superblock with a backup. To do that click here.
If that doesn't work and all else fails, it is possible to use scalpel and custom recovery configuration to get your files. See here for how to do that.

Consider donating to further my tinkering


Places you can find me

How to recover any file from corrupted or broken HDs, SSDs, or SD cards


A couple weeks ago, I lost a Raspberry Pi SD card to corruption that didn't even have backup superblocks to restore. There seemed to be nothing to do about it since I couldn't mount it. Restoring the system wasn't a big deal. However, I really wanted some of the cpp, h, js, php, sh, and other files that I had changed a good bit but hadn't yet backed up or committed. Sure we all back up and/or use git/svn but we have to remember Murphy's Law.

This is a guide on how to easily recover those files that you really want but hadn't backed up.

Needed packages

To do this, we are going to use scalpel, xxd, diff, and grep (So we are most definitely using Linux)

You will already most likely have xxd, diff, and grep on your system. To install scalpel, run:
sudo apt-get install scalpel
or yum or pacman or whatever package manager you use.

**Note: It is probably a good idea to make an image of the drive and use scalpel on that if you are able.

Editing Scalpel configuration

The default scalpel configuration file is located at /etc/scalpel.conf
All you need to do is comment out the file types you don't care about and add the filetypes you do care about that aren't in there.
To create custom scalpel configurations, we will use a little bit of common sense and xxd as a hex dumper.
Find a different file with the same file type or an older copy of the file you want and run the following command:
xxd -l 0x04 filename.type; xxd -s -0x04 filename.type

This will get the first 4 and last 4 bytes of the file. Which should be enough to make the filetype unique, though you can change this as needed. You should see a pattern with a lot of files.
For instance, my sh files (the ones I wrote) often start with #!/b and end with sac. or fi..
and my cpp files often start with #inc or // and end with }.
Now we will take these hex patterns, reformat them as \x?? and put them into the scalpel.conf.
You should use REVERSE whenever the file may continue multiple instances of the end statement. You may also need to increase the file size from 50000 to something much bigger for larger files.

Here are some of my custom scalpel configurations:

php         y        50000    \x3c\x3f\x70\x68\x70        \x3f\x3e REVERSE
js y 50000 \x3c\x73\x63\x72\x69\x70\x74\x20\x74\x79\x70\x65\x3d\x22\x74\x65\x78\x74\x2f\x6a\x61\x76\x61\x73\x63\x72\x69\x70\x74\x22\x3e \x3c\x2f\x73\x63\x72\x69\x70\x74\x3e 
cpp        y        50000   \x23\x69\x6e\x63              \x7d\x2e  REVERSE      
cpp        y        50000   \x2f\x2f\x20                      \x7d\x2e             REVERSE
h            y        50000   \x23\x69\x66\x6e              \x64\x69\x66 REVERSE
sh           y        50000   \x23\x21\x2f\x62              \x73\x61\x63 REVERSE


Running Scalpel

Once your scalpel configuration is finished, you need to start extracting the files. This will probably take a while and will get a lot of files (multiple copies of each that match on your system). It won't know the filename or newest version but we will look at that later. You can run scalpel with the following command:
scalpel -c /etc/scalpel.conf yourcopy.img -o output
or the following if you didn't make a copy of your HDD (you will have to specify the appropriate drive and partition number).
scalpel -c /etc/scalpel.conf /dev/sdx# -o output

Once everything is finished it will be in the output folder and we can look for the newest version of your file.

Finding your files

cd into the output directory and you can see all the different filetypes organized.
cd output; ls
For all your graphical file types, i.e. videos and pictures, they should all be listed there and you will have to rename them. 
For your other files, we can use grep recursively to find our newest version. This unfortunately requires you to remember things. It is a good idea to use a function or variable name that you used in order to find your files. Ex:
grep -R "playing()" ./
Searches for the function playing in all your files, this could give you matches in js, php, cpp, c, or h files.
grep -R "::CurlWriter" ./cpp*
Searches for the definition of CurlWriter in all of the cpp files
grep -R "VoiceCommand" ./h*
Searches for the variable or function VoiceCommand in all of your header (h files)

These will probably give you a couple of different results. In my case, these were all different versions of my file. This is a really good chance to use diff. All you have to do is select two of the files and diff to see the differences. Ex:
diff ./sh-5-1/00135386.sh ./sh-5-1/00135393.sh

Once you start getting used to this, you will be better about searching for the newest function or variable name you remember. Once you narrow it down to the newest copy of your file, you can move it elsewhere.
Ex:
mv ./sh-5-1/00135393.sh ~/ImportantFile.sh


There it is, this should allow you to recover almost any file you have created from a corrupted or bad HD, SSD, or SD card.

Places you can find me

Monday, April 8, 2013

Mounting the home directory on a different drive on the Raspberry Pi


I found myself struggling with SD card corruption this week.
I think it was due to a combination of overclocking and an old SD card. Although I use svn or git with most of my code, occasionally I do a lot of work in a day and forget to add a file. After this last time, I decided to move my home directory to a partition on an external HDD just in case.

I will be posting some file recovery methods later in case you have also lost data.

Start by making sure your external drive is connected and open a terminal.
First of all you need to have root privileges to do all this (or use sudo privileges)
Make sure your drive is not mounted. To unmount it:
umount /mntpoint
If you already have your external drive partitioned, skip to Step 4.

Step 1: Partition External Drive

The fdisk command with the -l flag can list all of your drives and partitions. So start by running that:
fdisk -l
Pick the HDD you want to use (mine was /dev/sdb but I'll put sdx and you can fill in appropriately) and run fdisk again to see partition information
fdisk /dev/sdx
You should be in an interactive prompt. Type p to see the partitions. There should be none. If there aren't any, skip to Step 3.

Step 2: Deleting or resizing

If you need the old partitions and want to shrink them, type q to exit the fdisk prompt, otherwise skip to Step  3.
If you are using the partitions and just want to resize them, then there are various commands to do that (also its a good idea to have a back up).
For ext3/ext4, just use: resize2fs /dev/sdx #size
Or use parted
parted /dev/sdx (opens interactive prompt)
resize #size
For ntfs: 
ntfsresize --size #sizeM /dev/sdx
Then open back up the fdisk prompt, you need to make new partitions that match.

Step 3: Writing partition changes

(Open fdisk back open if you closed it)
Now just use d #partition to delete any old partitions.
Then type n to make a new partition followed by p to make it a primary partition. If this is the only partition you need, you can make it the whole disk size. If you shrank a partition, you need to make two new partitions, with the first one having a size that matches your resize options.
Now type w to write changes to disk then q to quit. It will probably give you some warnings, it almost always does. Pay attention to them but don't freak out.
Now format the new partition (If you only made one partition #=1):
mkfs.ext4 /dev/sdx#

Step 4: Copying over your home directory files

Mount your new partition:
sudo mkdir /media/tmp
sudo mount -t ext4 /media/tmp
Navigate to your root folder
cd /home
Copy all your data recursively (this option seems to have worked the best for me to get all of the files including .bashrc and .vimrc files)
sudo cp -rp ./ /media/tmp

Step 5: Mounting your new home directory

Once that is finished, you can move the home directory and mount the new one (make sure no program is currently using the home directory or you will get errors).
sudo umount /media/tmp
sudo rm -rf /media/tmp (get rid of the tmp folder)
sudo mv /home /old_home
sudo mkdir /home
sudo echo "/dev/sdx# /home ext4 defaults,noatime,nodiratime 0 0" >> /etc/fstab
Again replacing x# with your drive number (mine was b2).
Now we can test it by mounting home. If this doesn't work, somewhere you messed up
sudo mount /home
After you have confirmed everything is working and copied over and you don't need your old home directory, you can delete it.
sudo rm -fr /old_home


Now you don't need to worry about SD Corruption.
References for help:



Consider donating to further my tinkering.


Places you can find me

Tuesday, April 2, 2013

Cloud storage on the Raspberry Pi



I recently started playing around with cloud storage on the Raspberry Pi. I'm already using it as a web server and a media server, why not this as well.

I started hacking up something to make Dropbox work and then decided that was stupid and found some open source solutions instead.

I found two options that were easy to install and seemed to work decently.

The first one is Owncloud


If you already have apache installed and running, this one is easy (If not see here).

All you have to do is install the packages required, download the files and do some simple setup.

sudo apt-get update && sudo apt-get install curl libcurl3 php5-curl sqlite3 php5 php5-sqlite php5-gd php-xml-parser php5-intl

cd /var/www/
sudo wget http://download.owncloud.org/community/owncloud-5.0.2.tar.bz2
sudo tar -xvf owncloud-5.0.2.tar.bz2
sudo chown -R www-data:www-data owncloud
sudo nano /etc/apache2/sites-enabled/000-default
Then change the line AllowOverride None to AllowOveride All in the /var/www/ section.
sudo a2enmod rewrite
sudo a2enmod headers
sudo service apache2 restart

Now just go to your http://ip-address/owncloud/ with a separate computer or go to http://localhost/owncloud/ on the Raspberry Pi and pick a username and password.

Pros:

  • Can access local data from your external drives easily.
  • Can access your dropbox data.
  • Doesn't require its own service.
  • Password protected (storing the salted hash of the password!).
Cons:
  • Unencrypted SQL database that anyone can download.
  • Slow!
  • Doesn't automatically sort through your data like its features imply. You have to manually upload everything.


The second one is Seafile


This one is also pretty easy to install and has a Raspberry Pi specific version.
I put my stuff in a directory in my home folder but you could put it wherever. For more security, consider creating a special user for seafile.

sudo apt-get update && sudo apt-get install python2.7 python-setuptools python-simplejson python-imaging sqlite3
cd
mkdir cloud
cd cloud
wget http://seafile.googlecode.com/files/seafile-server_1.5.1_pi.tar.gz
tar -xzf seafile-server_*
mkdir installed
mv seafile-server_* installed
cd seafile-server-*
./setup-seafile.sh

The last script will install it and ask some easy questions for you to answer to set up things.
To run the seafile server, you have to do the following:

sudo ./seafile.sh start
sudo ./seahub.sh start

This has to be done every time your Raspberry Pi restarts, which is fairly easy using a cron script
crontab -e

then add:
@reboot sudo /home/pi/cloud/seafile-server-1.5.1/seafile.sh start
@reboot sudo /home/pi/cloud/seafile-server-1.5.1/seahub.sh start

Then save and exit.
For more help, see here.
Now just go to your http://ip-address:8000 with a separate computer or go to http://localhost:8000

Pros:

  • Fast!
  • Doesn't run if you don't want it to.
  • Doesn't have all of it's files accessible over the normal webserver.
  • Requires username and password.
Cons:
  • Takes time to start up and sometimes seahub.sh doesn't start properly
  • Unencrypted SQL database (Though I haven't found a way to easily grab this db).
  • Can't access your dropbox data..

After a quick trial with the two of them, I'm going with seafile. I haven't removed OwnCloud but I'm seriously considering it do to the speed and security issues.

Please let me know your experiences with the two of them and any more options to try out!


Consider donating to further my tinkering.


Places you can find me

Monday, April 1, 2013

Attaching heat sinks to the Raspberry Pi for overclocking

My Raspberry Pi was getting a little hot when I overclocked it to above 1100 MHz.
So I decided to attach some heat sinks to it.
All you need to do this are the following:

  • Some heat sinks (copper or aluminum depending on your preference)
  • Thermal adhesive, I used Arctic Silver
  • Isopropyl alcohol
  • Lint-free cloth
  • Some clamps
  • A clean surface
Unplug your Raspberry Pi and put your it on a clean surface like so:

I got this heatmap from modmypi.com and put heat sinks over the  three hot spots.
Start by cleaning the black surfaces of the hot spots with the isopropyl alcohol and a lint free cloth.
Once that is dried, it's important you don't touch it or get anything on it.

Next get your heat sinks and thermal adhesive ready. Instructions should come with your thermal adhesive but it is commonly to mix a 1:1 ratio on a clean surface. Wait 1-2 minutes for it to thicken just a little bit then spread a thin layer on the processors and attach the heat sinks. Make sure to hold them down with rubber bands or clamps while they cure.

I was able to get a 6 degrees Celsius difference from the commands:
/opt/vc/bin/vcgencmd measure_temp
cat /sys/class/thermal/thermal_zone0/temp

With this done, I have the following stats in /boot/config.txt and they seem to be stable

force_turbo=1
over_voltage=8
arm_freq=1180
sdram_freq=500
core_freq=500
gpu_freq=350


Consider donating to further my tinkering.


Places you can find me