Monday, February 25, 2013

My first Ninja Block module on dashboard

After being able to setup my RasPi as a Ninja Block (as I mentioned in my previous post) it was time to get an actual module to run. I didn't want to connect anything to my Pi while learning so I thought I could try to measure the temperature using wither vcgencmd or /sys/class/thermal/thermal_zone0/temp (there are a couple of posts in forum related, this being one of them). That proved to not be a good idea since the Occidentalis image I started with doesn't have them installed and no matter what I tried (rpi-update, dist-upgrade) I couldn't get them to work. I spent a day trying things with no success, I think I updated the Pi firmware probably 4 times and it is a very long process. Oh well, I should have listened to the comments in the forum and go a different direction and that would have saved me so much time. When I finally gave up on the firmware update, things started to get better.

This other idea was to follow this articol "Creating Modules for Fun and Profit" which in turn is based on this one: "Hack like a Ninja (Blocks developer)" which details step by step how to get the environment ready. The only thing that is missing is details about compiling nodejs from source (because the nodejs version on the image is 0.6.x while ninja blocks needs 0.8.x or greater): I found some here. To make it easier for future or for whoever find this article, these are all the steps in order.

- download, compile and install nodejs (make takes a couple of hours, make install also takes quite a bit):
wget http://nodejs.org/dist/v0.8.11/node-v0.8.11.tar.gz
tar -zxf node-v0.8.11.tar.gz
cd node-v0.8.11
./configure
make
sudo make install
- symbolically link the nodejs binary to node for future use (this errors for me with a "file exists" messsage but it seemed that it works even without it):
sudo ln -s /usr/bin/nodejs /usr/bin/node
- install node-gyp globally via the node package manager:
sudo npm install -g node-gyp
- in order to build on your platform you might need to download some extra tools:
sudo apt-get install build-essential
Ninja client setup.

- check-out the source code from https://github.com/ninjablocks/client into a directory ninjaclient, switch to the 'develop' branch, and download dependencies (last step grabs the dependencies from package.json and builds them if necessary - it may take a while):
git clone git://github.com/ninjablocks/client.git ninjaClient
cd ninjaClient
git checkout develop
./bin/install.sh
Running the Client.

- set the environment variable "NODE_ENV" to "hacking", eg. linux/mac (can also be put in something like ~/.bash_profile) - better for development:
export NODE_ENV=hacking
- from inside the client directory, run client.js with node:
node client.js
- copy the serial number from this line:
...(info) This Ninja's Serial: XXXXXXXXXXXXXX
Protip: Install the handy nodemon tool (sudo npm -g install nodemon) and use nodemon in place of node. Nodemon will restart the app whenever file changes are detected, very handy for development. This didn't work for me, but according to this article all I need to do is start index.js with nodemon instead of node:
nodemon index.js
After this, just follow the steps in the first article I mentioned "Creating Modules for Fun and Profit" and hopefully you will get a new module on the dashboard as I did - I was so happy to see it working. Also, as a bonus I got the limitlessLED module working (which was included in the ninjaClient download) which was an awesome thing in itself.

I couldn't have done this without the help of the guys on the Ninja Blocks forum, especially: marcel_d, andynix and nick. Thousand thanks to all of them!



For future reference, the way I tried to update my Pi firmware is below: In each case I ended up with errors related to a library used by vcgencmd that according to some posts should have been resolved by a reboot but that didn't happen in my case.

- use the info on this page: http://raspisimon.no-ip.org/firmware.php OR
- this page: https://github.com/Hexxeh/rpi-update

In a nutshell both these boil down to:
sudo aptitude install git-core
sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update
sudo chmod +x /usr/bin/rpi-update
sudo rpi-update
Instead of the rpi-update I also tried sudo apt-get update && sudo apt-get dist-upgrade which could be better because rpi-update gets the very latest version (with potential bugs) while apt-get upgrade gets the latest stable version, but this didn't work either. Apparently if I'd started with the official raspbian image I would have had these utilities from the start but I like more the Occidentalis one since it sets up a lot of useful stuff from the start.

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.