Working with the Raspberry Pi

Someone I know was looking into controlling some electronics via cell phone app, and I thought one way might be to use a Raspberry Pi or Aduino with a WiFi connection, accessible from a cell phone application.  I have no prior experience with any of these items, so let’s start with the Raspberry Pi, or Raspi as I will call it.

I ordered a model B from newegg at about $45.  Apparently they are available for less money at other places.  This hardware can become a complete Linux server on a board the size of a credit card.  The root file system is provided by an SD card, and there are two USB ports, an Ethernet port, HDMI output, and a mini USB for power.  There is also an RCA plug and a mini phone plug – I assume these are for video and/or audio.

And probably more important, there are a few dozen wire wrap pins sticking up that could be used for a plug or wired directly.  I assume these are digital outputs that can be used to control external devices such as relays, lights, etc.

I bought the model B even though there is a B+ available that is supposedly better.  The B takes a larger style SD card which I happened to have available (32G).  The B+ takes a mini card and I don’t have one.  The B has 512M of memory and a processor they say is as good as the original Xbox, so this should be way more storage and power than I need.  Probably enough power to run a small php web site with mysql.  Maybe something to try later.

The Raspi board came with no documentation other than a note on the box indicating I should go to http://www.raspberrypi.org/downloads/ which I did.  There are “noob” instructions but I decided I was not a noob so I took the longer road and ran through the Debian Wheezy installation process, which basically any noob could do anyway.  It was easy, and here’s what I did:

OS Download and Installation

Someone has already prepared the Debian OS for the Raspi, and created a disk image.  So all I need to do is download the image and copy it to my 32G SD card.  I simply followed the instructions for Windows on this page with no problems.

Booting the First Time

I connected the following to the Raspberry:

HDMI cable to my TV set
32G SD Card
USB Keyboard
Power supply (1W USB)

Soon after connecting power (red light on), the HDMI2 option appeared on my TV. Note: on my TV HDMI options do not actually appear until a cable is connected and the driving device powered up. Then all sorts of nice Linux console messages appeared, resulting in a “Software Configuration Tool” (windowed text). Let’s see if we can navigate with the keyboard because I didn’t connect a mouse.

Let’s use option 1 “Expand Filesystem” which I’ve read will expand the FS to the size of the SD. Seems ok, but needs a reboot. Then let’s use option 2 to “Change the Default Password” (not sure what id this is for yet). Option 4 sets local settings. You have to wait a bit when setting Locale because a big list is generated. Let’s try en_US ISO-8859-1, then select en_US on the secondary screen. A message “this might take a while” appears. Set timezone, US, Pacific Ocean. I guess that’s where I am at.

There are other settings such as keyboard and overclocking, but we’ll ignore all of those except for hostname. Let’s pick a new name other than the default raspberrypi. How about raspi? And then we finally click “Finish”.

The app asks if we want to reboot, so let’s say Yes and let it do its thing. More console messages and we reboot.

Booting the Second Time

This time we come up with “raspi login:” so let’s try root and the password we set: Oops. No go. Ah… the userid is “pi” and the password I selected previously works.  From there we can sudo to root so we’re in good shape.

Now what? The board is getting a bit warm, I’d guess around 500 milliamps just sitting there. Heat seems mostly coming from the two large chips. Wish I had the HDMI to VGA adapter I ordered because my TV is being used as a monitor and it’s really quiet in here right now.

I don’t know where the system is getting the time from, but the “date” command shows it is now set correctly.

Connecting Ethernet

Let’s connect an ethernet cable just for fun and see what happens. I connected the cable to a free port on my 8 port switch, and the switch lights up. Also, one yellow and two green lights are now active on the card. One of the greens is blinking randomly. Hmm… ifconfig says my IP address is 192.168.1.132 which must have been assigned by DHCP in my router. Cool! And pinging the outside world seems to be fine. That was easy.

SSH Connection

Hey (duh), I wonder if I can dump my keyboard and TV and just SSH to the thing now.  I may not have needed all this HDMI stuff after all (well, except to get the thing running and to fix problems). Turns out SSH works fine. And we’re behind the router so we should be pretty-well protected no matter how the firewall is setup on this thing. Let’s watch TV again and put my temporary keyboard back on the shelf. And let’s try a reboot via SSH without HDMI and make sure SSH comes up ok without the TV and keyboard.

Futurama on Netflix is back on the TV, we rebooted with no monitor and no keyboard connected, and SSH came up ok and I could logon through ethernet. No wires are connected to the device except USB power and ethernet. Don’t know why I didn’t think of this earlier. I was even thinking about buying a separate display for this board, and my backup plan was the HDMI to VGA converter which I will hardly use now once it arrives.

Quite Amazing

We should also set 192.168.1.132 as reserved for this MAC in the router, and also setup some names like raspi and www.raspi.com in my hosts file on my Windows PC.

The router reservation is a bit hard to find. Click Setup, Basic Setup, then find the DHCP Reservation button hidden somewhere on the screen. I think it moves around. Now we don’t have to hassle with setting up a static IP address on the raspi.

Software Updates

Since we’re nice and connected, it’s probably time for some OS updates. Uh oh… we need to use apt-get instead of yum. I’m so confused, but I can copy commands such as:

sudo su -
apt-get update
apt-get upgrade
apt-get dist-upgrade
shutdown -r now

Other stuff

Looks like we have plenty of disk space. I don’t know what some of these mounts are for though:

Filesystem 1K-blocks Used Available Use% Mounted on
 rootfs 30658496 2418928 26943532 9% /
 /dev/root 30658496 2418928 26943532 9% /
 devtmpfs 219764 0 219764 0% /dev
 tmpfs 44788 208 44580 1% /run
 tmpfs 5120 0 5120 0% /run/lock
 tmpfs 89560 0 89560 0% /run/shm
 /dev/mmcblk0p1 57288 9864 47424 18% /boot

I’m wondering what happens when we stuff a USB drive in one of the slots. Let’s try. Fdisk shows the following single disk as /dev/mmcblk0 (what happened to sda? – maybe that’s centos?)

Disk /dev/mmcblk0: 32.0 GB, 32026656768 bytes
 4 heads, 16 sectors/track, 977376 cylinders, total 62552064 sectors
 Units = sectors of 1 * 512 = 512 bytes
 Sector size (logical/physical): 512 bytes / 512 bytes
 I/O size (minimum/optimal): 512 bytes / 512 bytes
 Disk identifier: 0x00090806
Device Boot Start End Blocks Id System
 /dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA)
 /dev/mmcblk0p2 122880 62552063 31214592 83 Linux

Ahh… we plug in a USB and we get sda:

Disk /dev/sda: 8103 MB, 8103395328 bytes
 256 heads, 21 sectors/track, 2944 cylinders, total 15826944 sectors
 Units = sectors of 1 * 512 = 512 bytes
 Sector size (logical/physical): 512 bytes / 512 bytes
 I/O size (minimum/optimal): 512 bytes / 512 bytes
 Disk identifier: 0xc3072e18
Device Boot Start End Blocks Id System
 /dev/sda1 * 63 15826943 7913440+ c W95 FAT32 (LBA)

And the system seems to know it is FAT32 from my Windows machine, so that’s good. Can we get data off it?

mount /dev/sda1 /mnt -t vfat

And it works. This little OS is all setup with everything I need so far. Hopefully setting up the WiFi link (yet to arrive) will be as easy. Then I need to learn how to access the control ports, which I assume is easy and I would bet can be done by running a supplied command. Just a guess so far since I’ve heard Raspberry normally uses PERL as a standard scripting language. We’ll see.

Debian Services

Here is a list of the running services:

service --status-all 2>&1 | grep +

 [ + ] cron as usual
 [ + ] dbus program to program communication (I think)
 [ + ] ifplugd handles i/o device plug-in and removal, such as ethernet (I think)
 [ + ] ntp as usual
 [ + ] rsyslog replaces syslogd (I think)
 [ + ] ssh as usual
 [ + ] triggerhappy watches keyboard and other manual device input such as joystick input (I think)
 [ + ] udev identifies devices such as disks (I think)

Debian version is 7. Looks like they use names such as “wheezy” for each release. Ok.

WiFi Connection

I bought a Edimax EW-7811Un adapter which is the item that seems most recommended by people for the Raspi.  Following the directions on one web page didn’t seem to work, but when I added the process from another web page, it started working.  However, it seems to disable the ethernet connection, which would be ok except that I really would like both up at once, talking to different devices.  For now though, here’s how to get Wifi to work in place of Ethernet:

Plug in the WiFi USB while the system is up.  The dmesg command should show things like this:

[ 3.256123] usb 1-1.2: new high-speed USB device number 4 using dwc_otg
[ 3.377993] usb 1-1.2: New USB device found, idVendor=7392, idProduct=7811
[ 3.387294] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 3.396423] usb 1-1.2: Product: 802.11n WLAN Adapter
[ 3.403903] usb 1-1.2: Manufacturer: Realtek
[ 3.409947] usb 1-1.2: SerialNumber: 00e04c000001

The web page says to reboot, but I don’t think that is necessary.  Now we want to edit the network interface parameters and add our router’s SSID and WPA pas

vi /etc/network/interfaces
(add the following lines to the end)
wpa-ssid "yyyyyyyyyyyy"
wpa-psk "xxxxxxxxxxxx"

Then we need to reload something:

service networking reload

That showed the wlan0 hardware in ifconfig, but without an inet addr.  I suspect that’s the same as when you get “limited” access to wifi or ethernet in Windows.  In other words, it doesn’t work.  So I continued with the struggle, installing this software:

apt-get install wicd-curses

That’s actually a pretty cool program because it shows the various Wifi connections available to the unit.  I only saw my own so there must be some limits.  But that’s ok.  With the wicd-curses command I was able to configure each and add the WPA password.  Then I issued the C command on one of them and my SSH session (ethernet) disconnected.  However, my router shows a new Wifi connection with the Wifi MAC, on a different IP address than the ethernet port (192.168.1.111).  I guess that’s how IP works.

And if I connect to that new IP address I can disconnect the ethernet and stay connected.  So it is working, at least for internet access.  But what I probably need is to actually remove the WiFi item from the networking parameters so it does not connect, and then somehow access it directly from my own program or script, in order to communicate with my phone as a server.

Hmm… I wonder if Wifi follows the standards of client and server.  If so, I probably just setup the device as a client and connected it to as server (my router).  I need to google to see if it is possible to connect Raspi to a Samsung.

Raspi Control via Android App

While googling a bit, it appears some people control their Raspi via an HTTP GET or something like that.  This gives me an idea… why not write some C code on the Raspi as an IP server listening on a specific port, and then write an Android or iPhone app that communicates via IP to that port?  That actually should work fine, except for one thing – you would need to have the router (with DHCP) involved for IP connectivity.

What I’m looking for is a way to talk directly from Android to Raspi, and that may be the wrong idea, assuming the Wifi in my Samsung phone can only talk to one device at a time.  We certainly wouldn’t want to remove it from the router in order to talk to the Raspi.

I wonder how the Roku app works?  My Samsung phone can talk to Roku.  I’m wondering if that also goes through the router.  Looks like my Roku is setup right now as IP address 192.168.1.106 (although that might change depending on DHCP).  Maybe it’s time I learn nmap and see if there are any ports open?

No, we don’t need to bother with that.  Roku has a nice web page showing how the thing works at http://sdkdocs.roku.com/display/sdkdoc/External+Control+Guide  – port 8060, so it is as I expected.  The Android is talking over the already-connected internet.  Some fun from Linux:

curl -d '' http://192.168.1.106:8060/keypress/Home
curl -d '' http://192.168.1.106:8060/keypress/Select
curl -d '' http://192.168.1.106:8060/keypress/Select
curl -d '' http://192.168.1.106:8060/keypress/Select
curl -d '' http://192.168.1.106:8060/keypress/Select
curl -d '' http://192.168.1.106:8060/keypress/Down

Let’s kill Android port 8060 in my router to prove this theory.  Hmm… I don’t know how to do that. I’m not even sure there is a way to do that on my Linksys router.

BlueTooth ?

I think I might be going at this the wrong way.  What I really want is Bluetooth, which is a point-to-point (bind) connection that can be used for multiple devices (I think).  I just want to connect the phone to the Raspi like you would bind to a headset or something.  Is hardware like that available to plug into the USB port?

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>