Friday, November 16, 2012

My Raspberry Pi has gone wireless - part 2

I found more info that answers my own questions from my previous post My Raspberry Pi has gone wireless.

Related to the first question I don't really have an answer: for some reason, it doesn't work on my home network but it does at work. Not sure why. A suggestion on github was to clean the cookies but that didn't help. I'll try more stuff when I get to it.

More important is the answer to the second question. Between this Debian and this RaspberryPi forum posts, I found out that I can have more than one network configured in the /etc/wpa_supplicant.conf file. Most of the time this works fine, I get an IP using the Wifi adapter; sometimes though I need to run "sudo ifup wlan1" (even in the Debian forum post it says I shouldn't). It may be something that I did wrong or maybe my adapter is not working quite right.

In the Debian forum it says that you can use either auto or allow-hotplug for the wlan configuration so I commented out the auto line. Maybe I'll try putting it back in to see if this fixes the times when I don't get an IP.

I plan to get a second Pi some day and at that time I will buy another Wifi adapter, either this one from Adafruit or if that one is not in stock then this one from Amazon (it seems in the reviews that lots of people were able to easily make it work with their Pi's and it is pretty fast).

Just for reference, the config files on my Pi now look like:

/etc/wpa_supplicant.conf
network={
        ssid="SSID_work"
        psk=abc123
        id_str="work"
        priority=5
}

network={
        ssid="SSID_home"
        psk=abc123
        id_str="home"
        priority=10
}

network={
        key_mgmt=NONE
}
/etc/network/interface
#auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
   wpa-driver wext
   wpa-roam /etc/wpa_supplicant.conf

#auto wlan1
allow-hotplug wlan1
iface wlan1 inet manual
   wpa-driver wext
   wpa-roam /etc/wpa_supplicant.conf

iface home inet dhcp
iface work inet dhcp
iface default inet dhcp

Wednesday, November 14, 2012

A quick fun project with my Pi

I was looking for a simple Raspberry Pi project that would be fun for Halloween, something with some spooky sounds that used pygame (I've read about pygame on Adafruit Learning System and wanted to give it a try) and I found this article on the Make blog.

I didn't use an Arduino though, instead I just read 2,3 and 4 from keyboard. At the time I didn't know pygame can help with reading keys as well so instead I used some simple code I found on the web and ended up with this program.

Since I run my Raspberry Pi headless it was very easy to set it up by my sound system; connected my laptop wirelessly with the Pi and turned the keyboard to my 3 year old son. He had a blast pushing keys and making sounds. A quick simple project that turned out to be very fun for him - thanks Make and Shawn Wallace in particular for the idea.

Next, I want to change the code so I read the arrow keys instead of number ones; this will be a step towards using my Makey Makey with the Pi. I found some resources, the most promising seems to be this one. I'll update when I have something working.

Tuesday, November 13, 2012

Import Dropbox folder into existing remote git repository and store the local repository outside Dropbox

All this info is already available on the net but since I had a few problems along the way, I decided to document these steps so I remember how to do it in the future.

These steps allow me to import existing code from a Dropbox folder into an existing git repository (BitBucket in my case) while keeping the local repository outside Dropbox, thus separating the source code from the git repository files.

First let's not worry about separating the local repository from the code source.

1. Locally, at the root directory of your source:
> git init

2. Attach your remote repo with the name 'origin' (like cloning would do):
> git remote add origin [URL]
in my case URL was the https one not the SSH one (because I want to use password)

3. Locally, add what you want in your initial repo:
> git add . (for all the files)
OR
> git add subdir_name (for the files in a subdirectory)

4. Locally, commit what you added:
> git commit -m 'initial commit comment'

5. Pull the existing code from the remote repository:
> git pull origin master

6. Push up your master branch (change master to something else for a different branch):
> git push origin master

The above works without any problems when the code is in the same directory as the git repository. However, my code is already in Dropbox and I didn't want to have the .git repository in the same directory (there are some known problems with Dropbox correctly syncing git files) so some of the steps above are a little bit different in this case.

After steps 1 and 2 above, I changed the config of the local git repository using:

> git config core.worktree "c:/<path to My Dropbox folder>/projects"

Note: I didn't use the --global modifier because I only want this git repository worktree to be different, I don't want to change all the git repos configuration (with --global, the .gitconfig file in my home directory would change; without --global, only the .git/config file changes).

Now, when I add files, I can use a subdir of the worktree directory without fully qualifying it, so basically it works as above, step 3. Also, commit and push work fine. The only issue I found is pull returns an error:

fatal: Not a git repository (or any of the parent directories): .git

After some research, I found that adding the --git-dir modifier fixes this so the pull command becomes:

> git --git-dir .git pull origin master

After all this, I now have the configuration I wanted:
- local git repository in a directory outside Dropbox
- all the code in Dropbox
- some of the projects in the remote repository.

This helps me have a copy of all the code in Dropbox so I can access it from several computers but only some projects in git (because most are just things I start and never finish and they are not worth saving in a git repository as well). On each computer, the only thing that changes is the worktree of the local repository.

I am pretty sure there are other ways of doing all this, git submodules seem interesting, but this is what worked for me.

Wednesday, November 07, 2012

My Raspberry Pi has gone wireless

A few days ago I found a Dynamode Wifi USB adapter on ebay for a very nice price and since Adafruit's wifi adapter was not in stock, I decided to get it. I was worried it may not work with my Pi but I risked it. For some reason, I thought it uses a Realtek 8188CU chip so I figured it will be easy to configure it, given all the info I found at elinux.org and other websites. I plugged it in my powered hub and the first problem showed up: a bunch of errors at boot time, like smsc95xx ... kevent 4 may have been dropped - the only way to get the Pi to boot was to unplug the dongle. This happened on both SD cards I have, one with Raspbian Wheezy and the other with Adafruit's Occidentalis.

After a bit of digging, I found that this is a common problem that is addressed by Hexxeh's rpi-update. It took a while for the firmware to be updated but it worked, next time I booted there were no more errors.

Now I had to configure it. After a lot of frustrating time trying to make it work, I had the idea of using lsusb (I know, should've done this from the beginning!) and to my surprise, lsusb reported a Ralink RT5370 chip. Again elinux.org helped a lot, this page has all the info (I will not mention the time I wasted thinking the router was setup with WPA when instead it was WEP and not being able to understand why I was not getting an IP address and all the DHCP requests were not successful). Finally, got it to work! This was an awesome moment!

After that, I gave up on the old router and used the main one we have setup with WPA. It took a little more setup but again, all the info was on the same elinux.org page and it worked from the first try. This was on Raspbian. Next I tried the same on Occidentalis and after a bit of hickup, got it to work here as well (on Occidentalis for some reason the adapter is wlan1 instead of wlan0, not sure why this is the case but after realizing this, all was well). The steps are:

1) sudo apt-get install wpasupplicant
2) sudo apt-get install firmware-ralink
3) sudo wpa_passphrase NetworkEssid password
4) sudo nano /etc/wpa_supplicant.conf - paste text from step 3, remove the passkey line:
network={
       ssid="NetworkEssid"
       psk=945609a382413e64d57daef00eb5fab3ae228716e1e440981c004bc61dccc98c
 }
5) sudo wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf - ctrl+c to stop
6) sudo nano /etc/network/interfaces - add:
auto wlan0
     iface wlan0 inet dhcp
     wpa-driver wext
     wpa-conf /etc/wpa_supplicant.conf
7) sudo ifup wlan0

After a few messages like "DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 6" the wifi adapter should get an IP. If not, something is wrong, start over with the steps above.

So now I have a wireless connection for my Pi. I love it!

There are 2 things I still need to figure it out:
  1. For some reason, the Adafruit's WebIDE doesn't want to connect when I use the wireless adapter. It works great when using the wired connection but not on wireless. SSH works fine using raspberrypi.local so probably this is not the problem - I logged an issue on github and hopefully I will hear back. If not, no problem, I can use the wired connection for WebIDE and wireless for other projects I plan.
  2. I am not sure how to connect to different wireless networks automatically - I want to do this in case I take my Pi with me at work for example. From what I gather at this time, I need to change the /etc/wpa_supplicant.conf config file and run the config command sudo wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf every time but there has to be a simpler way. I'll try and see if I can come up with something.

In any case, this was a lot of work for me being a Linux newbie but getting connected was a real breakthrough. I'm really happy to have one less wire to connect when I run my Pi headless.

A huge thanks to everyone posting information and help on the web, particularly to Hexxeh and the guys contributing to elinux.org Raspberry Pi pages!