New Raspberry Pi Project

It has been a while since I have updated my site with things. So where exactly has the time gone? A lot of it has gone to my day job where I sit in front of a computer for a good portion of the day, and the last thing I want to do is continue to sit in front of a computer when I am not in the office. Also, a lot of my time has been going into projects that have either been thrown at me or ones that my wild mind has come up with.

The latest of them involves a modifying a Raspberry Pi Model B. Not the new Raspberry Pi 2 Model B with 1GB of RAM, but the original Model B with 512MB of RAM. One thing that has not really bothered me, but I have felt that there really was not point to it was the video and audio connections next to the GPIO of the Raspberry Pi. I felt like trying my hand at de-soldering these connections and really making this a slimline model of the tiny computer.

So far everything still works. I have removed both the RCA video and 3.5mm audio connections as well as the Ethernet connection. I will post an update on the reason for removing the Ethernet connection in due time. But here is a picture of it for now.

Raspberry Pi Mod

Because this project is going to be a headless device, here is a list of packages that I have removed from Raspbian.

xserver* x11-common x11-utils x11-xkb-utils x11-xserver-utils xarchiver xauth xkb-data console-setup xinit lightdm libx{composite,cb,cursor,damage,dmcp,ext,font,ft,i,inerama,kbfile,klavier,mu,pm,randr,render,res,t,xf86}* lxde* lx{input,menu-data,panel,polkit,randr,session,session-edit,shortcut,task,terminal} obconf openbox gtk* libgtk* alsa* nano python-pygame python-tk python3-tk scratch tsconf xdg-tools desktop-file-utils python3-numpy python3 python omxplayer

That’s all for now. Stay tuned for updates on the project, feel free to comment below, and thanks for reading.

Mounting USB drive in Raspbian

A project that I am currently working on requires a USB stick plugged into the Raspberry Pi Model A USB port. All of the writes go to the USB stick and not the SD card with the operating system. In theory when the SD card is write protected the card will last longer. Since (most) USB drives come with some software installers, I wanted to have a clean drive to work with. The one I  got was a 16GB PNY  miniature USB from Best Buy for $8.

It came formatted as FAT32 and this one did not have any additional files on it, but just to be on the safe side I re-formatted the drive. Keeping it as FAT32, because this file system has read and write capabilities with Linux, Mac, and Windows. At this point, power on the Raspberry Pi but keep the USB drive unplugged. Since the Model A does not have a network port, I used the USB Console Cable available from Adafruit. This worked perfectly because it kept the single USB port open.

To start lets make a folder under the pi users home for where we can mount the USB drive later and can be written to and read from.

mkdir /home/pi/usb

 

After connecting the console cable and connecting via the serial interface, I was able to log in and run the commands to check what was mounted.

sudo blkid

The output should show the SD card mounted, then after plugging in the USB drive you should see another line added that looks similar to this

/dev/sda1: LABEL="Untitled" UUID="7E4F-6DA1" TYPE="vfat"

Next we need to edit the file systems table. To do this you can use your favorite terminal text editor, for me I like nano.

sudo nano /etc/fstab

Add this line to the end of the file. This tells the OS to mount the device sda1 to the folder /home/pi/usb. Giving permissions for reads and writes to the default pi user. (note that the line is broken into two, it should be all on one on Pi):

/dev/sda1    /home/pi/usb    vfat    uid=pi,gid=pi,umask=0022,sync,auto,nosuid,rw,nouser 0   0

Then save the document (crtl+x / y) and exit the text editor. After a reboot of the pi, you can test it by creating a simple text document. If everything went smoothly, when the Pi is shutdown and the USB drive is removed the document should be able to be opened on another computer.

Some changes

I am making the change from a self hosted WordPress site to one at WordPress.com; this is because I have decided to ditch my hosting company. I did not have any problems with them; they had a 100% uptime and were really reliable. The decision came from wanting to place my domains in one place, and reduce my costs. For now, since this is mostly a place for me to write about things I do and not to monetize it. This is a free blog at WordPress, and I figure I barely used my self hosted one to the full potential I decided to take the plunge.

As part of this change, I am going to attempt to update this blog more often and focus on projects that have been pending and placed on hold because of the amount of work that has been piled on me during my day job. Since the busy season is now winding down and coming to a close, I figure I will put my free time to good use.

Thanks for sticking with me, and hopefully these changes are for the better and not for the worse. So stay tuned.

Mounting Disk Image in Ubuntu

The default Raspbian image for the Raspberry Pi sometimes needs some modification before and after writing to an SD card. This post is mostly so I have something to refer back to, because there are times when you just can’t remember where you find that post or page that was so useful. I wanted to create an image with files added to the default user, and the user I created after writing it to an SD card, so I mounted the it to my Linux laptop running Ubuntu.

To do this, open a terminal session and navigate to the location where the image is located on the computer.

sudo fdisk filename.img

Then press p to display the partitions within the image file

Device         Boot     Start       End  Blocks  Id System
whatever.img1            8192    122879   57344   c W95 FAT32 (LBA)
whatever.img2          122880   5785599 2831360  83 Linux

The first is the boot partition, with a FAT32 file system; and the second is the Linux ext4 file system. In order to mount the file systems, you’ll need to find the offset of the partitions. This can be done by multiplying the start block (8192, and 122880) by one unit; where one unit is equal to 512 bytes.

1st partition (512 * 8192) = 4194304
2nd partition (512 * 122880) = 62914560

These numbers can be used with the offset option of the mount command and you’ll want to create a directory on where to mount the images to. In this case, I created two folders under the /mnt directory, one for the boot partition and one of the system partition. All that is really needed is the second one. Both partitions can now be edited and anything added or deleted will be committed back to the image upon unmounting. If all you would like to do is read only, you can add the option -r.

sudo mount -v -o offset=4194304 -t vfat filename.img /mnt/img/boot
sudo mount -v -o offset=62914560 -t ext4 filename.img /mnt/img/system

To unmout the partions:

sudo umount /mnt

Raspbian in a trademark of the Raspberry Pi Foundation
Raspberry Pi is a trademark of the Raspberry Pi Foundation
Image: Wikipedia

Compile hostapd for Raspberry Pi

After following the tutorial from Adafruit on setting up the Raspberry Pi as WiFi access point, the INVALID_ARGUMENT error would display on screen. The SSID was still available, but nothing could connect to it. This is mentioned in the tutorial, however the link that it points to is no longer valid. Doing some searching from my favorite search engine, I came across the Adafruit forums where someone had posted the steps they took to compile hostapd.

Preform this on another computer; because you need the URL for the driver package which was included in the instructions from the forum, but the Pi could not download them with wget because of the ftp link needed a username and password (there was no anonymous login). Go to Realtek’s driver download site (which is here), and select the RTL8188CUS model. Download the Linux drivers by selecting any mirror (it does not matter which one, but recommended is the one closest to your location). Once the download starts, it can be cancelled because all that is needed is the link and it is time to log into the Raspberry Pi’s console and download the driver with wget.

cd /tmp
wget ftp://WebUser:YbwD5pB2@209.222.7.36/cn/wlan/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip
sudo unzip RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip
cd RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911
cd wpa_supplicant_hostapd
sudo tar -xvf wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz
cd wpa_supplicant_hostapd-0.8_rtw_r7475.20130812
cd hostapd
sudo make (grab a cup of coffee)
sudo make install

These steps will create a hostapd binary. Before you move it, backup the original hostapd (or the Adafruit replacement from the tutorial).

sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.orig

or

sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.adafruit

Move the new replacement file

sudo mv hostapd /usr/sbin/hostapd

Make the replacement file executable and change the ownership to the root user and group

sudo chown root.root /usr/sbin/hostapd
sudo chmod 755 /usr/sbin/hostapd

Once this is all said and done, you can now run the command to test the access point:

sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

and the Raspberry Pi should not throw any errors, with the wireless SSID broadcasting and clients are now able to authenticate to it. For more information on the Adafruit tutorial; you can find it here, and the Adafruit forum topic is located here.

 

Store ownCloud Files on an External Drive

The ownCloud setup on a Raspberry Pi is great because it can be always on and consume very little power. Performance from the web client is a little slow because the fact the Raspberry Pi does not have very much processing power, but that can be alleviated with the use of server side PHP cahcing. The main feature I wanted it for was to backup photos from my Samsung Galaxy S5. With the use of the Android App it works much like Dropbox, or Google’s Auto Upload. Except for one major difference, no storage limits. You can attach as much space as you can find to the Raspberry Pi. At the moment, all the ownCloud media is redirected to a 32GB USB thumb drive. Soon to be upgraded to a 240GB SSD drive.

To setup ownCloud to save to an external device is not only a smart thing to do, because the life of some SD cards vary based on the amount of writes to the card; it is also easy to setup the external device. The first thing you need to do is format the drive in either ext3 or ext4. This can be done in the command line of the Raspberry Pi, or if you are more comfortable, there are tools such as GParted to present a graphical user interface. From the terminal type this command to list all detected hard disks:

sudo fdisk -l | grep '^Disk'

This will output:

Disk/dev/mmcblk0: 15.9 GB, 15931539456 bytes
Disk identifier: 0x000b5098
Disk /dev/sda: 32.0 GB, 32015679488 bytes
Disk identifier: 0x00000000

My USB thumb drive is /dev/sda. It may be different in your case. To partition the disk /dev/sda, use this command:

sudo fdisk /dev/sda

Here are the basic commands for the fdsik utility:

  • m – display help
  • p – display partition table
  • n – create partition
  • d – delete partition
  • q – quit without saving changes
  • w – write changes and exit

The 32GB thumb drive out of the box already had a FAT32 partition which I deleted, and then after creating a new one and exiting it was formatted with:

sudo mkfs.ext4 /dev/sda1

After it completed formatting, then a new mount point was created:

sudo mkdir /media/owncloud

Next, you need to find the device you want to mount. In the terminal type:

sudo blkid

The output should look similar to this:

/dev/mmcblk0p1: SEC_TYPE="msdos" LABEL="boot" UUID="7D5C-A285" TYPE="vfat" 
/dev/mmcblk0p2: UUID="5d18be51-3217-4679-9c72-a54e0fc53d6b" TYPE="ext4" 
/dev/sda1: LABEL="data" UUID="712d2cc3-50c3-420d-a4e6-ca5c826a7767" TYPE="ext4"

Now edit the file /etc/fstab, this controls which drives are mounted and where at bootup. So if you do loose power to the Raspberry Pi, the drive will remount.

sudo nano /etc/fstab

Add the UUID of your device to the bottom of the file just above the comment line with the (#). You will add something similar to this, but remember to replace the UUID with the UUID of your device.

UUID="712d2cc3-50c3-420d-a4e6-ca5c826a7767"    /media/owncloud    ext4    defaults    0    2

It should look something similar to this:

proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
UUID="712d2cc3-50c3-420d-a4e6-ca5c826a7767" /media/owncloud ext4 defaults 0 2

Now reboot your Raspberry Pi and check that the drive is mounted properly:

cd /media/owncloud
ls -al

If all went well, you need to move the ownCloud data directory and create some symbolic links so that the rest of ownCloud knows where to look for the data. Change directory to where you installed ownCloud (in my case it is in the root of the Apache server /var/www/)

cd /var/www/
sudo service apache2 stop

Move the data folder and create a symbolic link:

sudo mv /var/www/data /media/owncloud/data
sudo ln -s /media/owncloud/data /var/www/data

Move the config folder and create a symbolic link:

sudo mv /var/www/config /media/owncloud/config
sudo ln -s /media/owncloud/config /var/www/config

Make sure the ownership of /media/owncloud is correct:

sudo chown -R www-data:www-data /media/owncloud

And start the Apache service:

sudo service apache2 start

The data should now upload to the external USB device reducing the number of writes to the SD card, prolonging the life of the card, and keeping your data safe.

Setup ownCloud to Backup Android Photos

With Android and Google services such as Google Drive or Google+, you can automatically upload your photos so you can always have a backup. Google offers unlimited uploads for photos compressed to 2048px, and you are allowed 15GB of free storage, with the opportunity to purchase more for a nominal monthly fee. Many other services offer this, such as Dropbox or Microsoft’s SkyDrive. But if you are someone who doesn’t trust their information in the cloud and want to maintain control, then this is where ownCloud comes in. ownCloud is a software system for file-hosting, very similar to Dropbox. The best part about it is it’s free and can run on virtually any system. If you don’t want to go through the steps of setting up a server on your own, you can download an ISO or Virtual Machine image from TurnKey Linux that has everything setup for an ownCloud server and when you go through the install it asks for app and MySQL passwords. In my case, I had a spare Raspberry Pi and a 32GB USB stick and followed a tutorial from Sam Hobbs (but ended up tweaking it a little). After installing Raspbian to an SDcard, and running the initial configuration of the Raspberry Pi, it’s time to install some things to run ownCloud. The first things you will need is Apache 2 with PHP 5

sudo apt-get install apache2 php5 php5-gd php-xml-parser php5-intl php-apc php5-curl

There are two ways to setup ownCloud. One is with a MySQL database, the other is with SQLite. Since this setup was on a Raspberry Pi, I chose to go with SQLite.

sudo apt-get install php5-sqlite

If you would like a little more organization, it is best to go with MySQL. You can install that with the following command. (Just a side note, it is not nessecary to install both MySQL and SQLite)

sudo apt-get install php5-mysql mysql-server mysql-client

Now it is time to download ownCloud and run the installation. Go to ownCloud’s install page and choose “Archive File”

download_ownCloud

Then copy “Download Unix” link address in your web browser.

archive_ownCloud

The link will look something like this https://download.owncloud.org/community/owncloud-x.x.x.tar.bz2 (replace the x.x.x with the version number). At the time it was 6.0.4. Save the link, and SSH into your Raspberry Pi and download the file with:

wget https://download.owncloud.org/community/owncloud-6.0.4.tar.bz2

This will save it into your home directory, unless you cd after logging into the SSH session. Next, extract the .tar archive file

tar -xzvf owncloud-6.0.4.tar.bz2

A new sub directory called owncloud is in your home directory, it is time to copy it into Apache’s data directory. This will copy everything from the owncloud directory into a directory at /var/www/owncloud. So to access it you would use <IPaddress>/owncloud/ or if you are placing it on a hosting server such as bluehost it would be domain.com/owncloud/.

cp -R owncloud /var/www/

If you do not want ownCloud to remain in a sub-directory of the site and you wan to move in into Apache’s root directory, you can move it by using the following commands:

cd /var/www/
sudo find owncloud -maxdepth 1 -mindepth 1 -exec mv {} . ;
sudo rmdir owncloud

Next thing to make sure of, is that Apache is the owner of all the files just added.

sudo chown -R www-data:www-data /var/www/

As well as enable .htaccess so ownCloud can override settings such as the global maximum file size for uploads.

sudo nano /etc/apache2/sites-enabled/000-default

Here, change the AllowOverride setting from None to All in the Directory /var/www/ section then enable a few apache modules and restart the apache service:

sudo a2enmod rewrite
sudo a2enmod headers
sudo service apache2 restart

Now you should be able to complete the ownCloud installation with the setup wizard using SQLite. Choose a username and password for your ownCloud user / admin account. If you would like to use MySQL as the database for your ownCloud installation, you will need to enter the satabase root username and password and then choose a name for the ownCloud database (this can be any name, “owncloud” works well so you know exactly what it is) with the location of the database being localhost. Something to note, because the Raspberry Pi does not have as much resources as a traditional server, you can speed up load times by using a server side caching agent with PHP. Since ownCloud is written in PHP, when you request a page on the server, the server has to compile it from the PHP code. You can install the PHP cache and drastically decrease loading time with:

sudo apt-get install php-apc

Then restart the Apache web server:

sudo service apache2 restart

It may be slow to begin with, but as you navigate around the site and return to familiar pages you should notice the difference. And once you have the server all setup, you can download the Android App from the Google Play Store and set the “Instant pictures upload” to the server setting so you have a backup of the photos from your Android device. Or if you’re savvy enough, you could build it from the source code which can be found here. There is an app for iOS devices as well, but it does not include instant upload. Something to note is that you may be able to save the photos to the SDcard if you used an appropriate sized card. Have fun! and let me know if you have any questions or problems in the comments.