Thursday, February 21, 2013

Using a Raspberry PI as a Ninja Block

I wanted so much to learn more about Ninja Blocks but not been able to get one. While I am still saving for the real deal, I found this awesome post in the Ninja Blocks blog: Using a Raspberry PI as a Ninja Block. Backed up my SD card, ran the script mentioned in the post and indeed, I got a serial number and was able to pair my new "block".

The only trouble I had was that after running the script I was unable to ssh to my RasPi using RaspberryPi.local as I always do. I thought I messed up something badly but looking again at the post, I noticed in the premade image section this paragraph: you can log in to your Pi by typing “ssh ninjablock.local”. Tried and all is well, even if RaspberryPi.local doesn't work anymore, ninjablock.local works without problems.

Unfortunately, Adafruit WebIDE stopped working after setting up the RasPi as a Ninja Block but luckily I have the SD card image so I can burn another card.

Lot of thanks to the Ninja Blocks guys for allowing people without a real block to still use the dashboard and learn this way. You guys are awesome!

Backup Raspberry Pi SD card

I don't have much yet on my RasPi SD card but there are things I want to make sure I don't lose, like wireless configuration, some code and others. So I wanted to backup my SD card. Found a lot of resources on the web but most deal with Linux. This stackexchange answer has great directions for Windows - here are the steps I used and it worked great:
  1. Download Win32 Disk Imager.
  2. Create the file path you wish to use to save your image. (like C:\RasPi_Bkp)
  3. Run Win32 Disk Imager
  4. Browse to your backup file path spot, and type a file name.
  5. Click save
  6. Make sure the device shown in the drop down to the right of your file path is the one you want to back up.
  7. Click "Read"
  8. Wait.

Wednesday, February 20, 2013

Raspberry Leaf

What a simple and awesome idea: Dr Simon Monk (who has written a book on the Pi and Python: Programming the Raspberry Pi: Getting Started with Python) has come up with a really great idea: a bit of paper you can install over the GPIO pins of your prototyping Pi (and leave there) to remind you which pin is which. Visit Simon’s site to download and print your own version for free. (info found on the Raspberry Pi official site)

Thursday, February 07, 2013

Stopping Timer A on MSP430

Coding a microcontroller like MSP430 is a lot more difficult (at least for me) than Arduino but since I have a few around, I really want to learn. An important aspect of working with these are interrupts. I looked over the examples and they are all great but I wanted to do something a little bit more yet still simple. The idea: start a timer when the interrupt on a button fires, then stop it after a few seconds, if the button is not pushed again; if it is, restart the timer so it always stops the same time after the last button push. Pretty simple in theory; well, in practice as well after I figured it out. The most difficult task was to restart the timer with each button push. I tried to find info on the web and by reading the MSP430x2xx Family User's Guide but couldn't get anywhere: no matter what I tried the timer was stopping after what seemed like random times (after a lot of debugging I realized it wasn't random, the timer was always stopping after the right time measured since the first button push).

I tried several things but mainly:
  • set TACCR0=0 - I hoped this will reset the counter to 0 which is not true, and this was spelled out in the doc I mentioned

  • set TACTL = MC_0 - I hoped that stopping the timer means resetting the counter but it seems that the timer just stopped and when I re-enabled it it continued counting from where it left off.

  • In the end, re-reading the user's guide for the nth time, I noticed a paragraph in which it says that the register holding the current count (TAR) is writable (I was sure until then that is read only) and this was the key: when I want to reset the counter, I just write 0 in this register; just to make sure all is well, I also reset TACTL just to make sure the timer restarts - this may not be needed but it works so I left the code in there. Another thing is stopping the timer in the timer ISR by doing TACTL = MC_0 because I want the timer to shut down after the given time, and not keep counting - this makes the mcu go back to sleep mode.

    The final version I ended up with is here: I am sure it can be improved but again, it works now and exactly the way I wanted it so I'll keep it this way.

    Next thing: play with external interrupts.

    [Edit] Now that I figured out how to reset the timer, I changed the code to use a variable to handle the restart and stop of the timer: reset it to 0 when the button interrupt fires, increment it in the timer ISR and stop the timer after a few iterations. This is much more flexible because a) I can stop the timer at the exact time I want (not being dependent on the timer counter and the clock frequency and dividers) and b) I can reuse the timer for other things, changing the behavior based on the TAIV vector. Next step in developing my little app will use this approach.

    Friday, February 01, 2013

    Simple SimpliciTI

    TI's SimpliciTI protocol is not really simple to use for a beginner like me but with help it can be. A few years ago when I got my eZ430-RF2500 dev board I tried to look into SimpliciTI but the code was only available for IAR and not CCE which was what I used. I tried "porting" it - porting in quotes because I really didn't know what I was doing - but after hours of frustration I gave up. Fast forward a few years (a couple days ago) I found Alvaro's excellent blog with an entire section dedicated to the CC2500 radio. Well, I don't have the radio module he is using (which seems pretty cool and cheap in quantity) but I still have the eZ430-RF2500 boards. Since I am still a beginner with micros and TI is even more difficult than say Arduino, I was unable to use Alvaro's great library. So I decided to try and find more info about SimpliciTI. And this time I had more luck.

    On the SimpliciTI page, there is a download of a SimpliciTI package for CC2500 radio using CCS for MSP430 (swrc132a.zip). After downloading and installing it, you get a treasure of information: sample apps, documentation and the entire library usable in CCS (I tried 4.2 and 5.3 and they both worked, following different steps though). One of the documents "Sample Application User's Guide.pdf" found in the .\Documents directory is of particular importance since it details step by step how to make it work in either CCS v4 or CCS v5. If only I would have found this a few years ago...

    I now have a Talker and a Listener running and talking to each other, as described in the document I just mentioned, section 3.1. Hopefully I will be able to go from here and make something, that won't be as cool as Alvaro's stuff but at least it may be interesting.

    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!

    Tuesday, October 16, 2012

    I2C LCD

    I am done with buying cheap LCDs and trying to make them work. Last week I found an I2C LCD on ebay (named something like YwRobot LCM1602 IIC Arduino compatible) and I figured since it only needs a couple of wires to be used, I can give it a try. And it worked great!

    Well not quite from the start, since I thought it needs 3.3V but after I switched it to 5V, it worked like a breeze. The library I used is LiquidCrystal_I2C (I found it here) - it is not compatible with Arduino 1.0 but I still have 0022 installed so no problem. Apparently, there is another library named fmalpartida/new-liquidcrystal that works with 1.0 but I haven't tried it yet - it looks like I would need to remove the library that comes with Arduino 1.0 and replace it with this one, I may try it someday.

    Connecting to the Arduino is very easy:

  • GND to GND
  • VCC to +5V
  • SDA to analog pin 4
  • SCL to analog pin 5

    The next step is to use this LCD with my Raspberry Pi but if I understand this correctly, the LCD working at 5V and the Pi I2C pins working at 3.3V I need to be careful, I think I will wait until this board is back in stock at Adafruit.