Sunday, December 29, 2013

Avahi works great with wifi - finally

One of the first things I do when I setup a new SD card for my Raspberry Pi is to make sure I can connect to it headlessly; this means setting up the SSH daemon to start automatically, configure it to boot to a command prompt (both of these can be done using raspi-config), change the computer name (as described here) and install and configure avahi (as noted on this page). Lately, I also install wifi - in the past, I had some problems doing this but I found that Adafruit's directions work very well, both with my new WiPi and the old wifi dongle I had. Also, on newer Raspbian installs, the gui tool works great - at some point I was even able to launch wpa_gui and configure the wifi in putty using Xming and X11 forwarding.

One thing that bothered me though was that after configuring wifi I was able to connect to my RasPi using raspberrypi.local but only at home; every time I brought my RasPi to work, I had to use the wired connection. I thought the main problem is the avahi configuration that somehow works on 192.168.x.x (at home) but not on 10.x.x.x (at work). I tried so many things without success that I gave up at some point. Until a few days ago when I realized what was happening: at work, the wired and wireless networks are on different IP subnets (if this is the right word - I am no network guru); so, I connected both my laptop and my RasPi to the same wireless network and this time I was able to connect to my Pi using the raspberrypi.local shortcut using only wifi. This was so awesome!

I am pretty sure everyone using a Raspberry Pi knows this by now but for me it was a huge revelation and success so I decided to publish this post, maybe it will help others as well.

Sunday, December 22, 2013

My CheerLight(s) small project

Last Friday I found out about CheerLights powered by ThingSpeak and thought it would be really cool to put my Arduino + Ethernet shield to some use. Looked around for a little bit and found some code here connecting an Arduino to ThingSpeak/Cheerlights channel and driving a GE Color Effect Lights. Modified the code a little bit to drive an RGB led, decorated a tiny tree I had around and here it is my Christmas project alive.

The latest code is on codebender and below are some photos. I know the code can be much nicer but it works pretty well so I will leave it as it is. While working on it I ran into some weird behavior: a lot of failures when connecting to ThingSpeak and other strange stuff - it turns out the problem was with the response variable getting too big so I am resetting it now after getting the status (at least I think this was the reason because it works now after making the changes when reading the response).


Friday, September 20, 2013

Gotta love the electric imp

I love the electric imp! I won't talk about how great the device is, or about the processor, or how cool it is that the pins (as few as they are) are configurable to do so many things - smarter people have done it very well already. The only things I'd like to mention are how easy is to upload new code (after the imp is configured and connected to your wireless network), no wires needed, not even an upload code menu option, you just click a button in the IDE and the code is ready on your imp. The other thing that I love is the new Agents API (until recently in closed beta, now available to anyone) which basically allows your code to have 2 parts, one running on the imp and the other running in the cloud and you always see both of them side by side in the new IDE; this is great because it eliminates the need for yet another piece of code running on your own server or somewhere in the cloud, like it was the case of my previous project where the processing piece runs on Heroku.

This being said, soon I'll post my first imp project, what else than yet another version of my distance from home project; this time though there are no wires (no Ethernet or power connection) which is in itself amazing. I know there are other wifi devices out there, like the new Arduino Yún or the great Raspberry Pi with a wifi dongle but the imp is my choice for now; the community is great and the help from the electric imp guys is awesome, so I'll stick with it. Stay posted for my project notes soon.

Saturday, August 10, 2013

My Arduino distance-from-home project, before and after Latitude

In my previous post, "Heroku notes" I mentioned an app I had deployed on Heroku that got my Google Latitude coordinates, calculated the distance to my home and posted this distance to ThingSpeak and to my Arduino (using Teleduino). This app is a node.js app derived from a NinjaBlocks device, using code originally written by ChrisN (thanks a lot to ChrisN for code and all his help). My app uses all of the original ChrisN's code to get the Latitude position and calculate the distance but instead of pushing these values to a NinjaBlocks device, it sends them to a ThingSpeak channel. In addition, it calculates PWM values to be sent to an RGB LED connected to my Arduino (in fact, there are 2 LEDs on both sides of a board, to make it light better), that will show red when I am far from home, blue when closer, green when home.

As a side note, if you are interested in more info about deploying NinjaBlocks app to Heroku, see this How-to.

On August 9th Google discontinued Latitude, which was very unfortunate and upset a lot of people that used it for real stuff; so I had to find another way to do all this. The easiest thing was to find or write some simple Android app that gets the coordinates and post them to some custom URL. After quite a lot of searches, I was able to find such an app in the Play store: EasyGPSTracker written by Christian Brosch. This app allows setting a custom URL to which it will post some data containing latitude, longitude and a few other optional fields (docs are here). Big thanks to Christian for writing this app, it saved me a lot of time since I am only a beginner with Android coding.

And since my first Heroku app was a node.js one, I decided this time to try a Java app; I was worried it will take me a very long time to do this since I am not familiar with maven (which is what Heroku uses for Java apps deployment) but Heroku awesome as it is (I know, I am repeating myself) has quite a few templates to start with which made writing the app a breeze. This app does basically the same thing as the old one except this time it gets the POST-ed data from Christian's app, calculates the distance which is sent to ThingSpeak and then the PWM values to be sent to my Arduino.

I kept the original app's name as generated by Heroku, pretty long but I liked it: hidden-hollows-5561 and named my servlet geo so this is the link. Anyone can try the link however it won't do anything unless you POST the data in the right format and even then it will only send back an arbitrary distance. I started working on a GET method that can take 2 latitude/longitude pairs and return the distance between them but never finished: if I do and anyone will be interested, I will post all the details of the GET call.

The next step is to find a way to get rid of the wires; right now, the Arduino uses an Ethernet shield so I need a wire going to it; I'm using a wall wart to power it so... another wire. If I find a way to make it more portable without spending a lot of money on a WiFi shield, I'll update this post.

There is nothing special in the Arduino code since I'm using Teleduino so there is nothing to post here. The Java code for the Heroku app is also pretty simple so again, nothing special to post. To calculate the distance I am using a library I found in google code: simplelatlng, very easy to use and well documented - thanks to the author!

Monday, June 24, 2013

Heroku notes

Last week I found out about Heroku and from all I've read it looks awesome so I decided to upload and run an app on their cloud. After writing a quick app and testing it locally using foreman (Heroku toolbelt is great!), I tried to push it to Heroku but got stuck with something like Heroku error: "Permission denied (public key)". After digging on the web for a while I pieced some information together, most helpful was Giordano Scalzo's post here. In a nutshell the steps are:
  1. Create a new key just for Heroku:

    ssh-keygen -t rsa -C "email_address" -f ~/.ssh/id_rsa_heroku

  2. Add the new key to Heroku:

    heroku keys:add ~/.ssh/

  3. Set the HOME variable to the /user_home folder (I kept getting errors without doing this, I guess git was getting confused as to where the key was saved).

  4. Create a config file that indicates which key should be used, this worked for me:

    IdentityFile "%HOME%/.ssh/id_rsa_heroku"

The nice thing is that now I can switch the machine used for development and just follow steps 3 and 4 above for everything to work without issues. Using more than one machine with more than one Heroku account needs a little bit more work but since I am not doing this, I won't go into details.

My app doesn't respond to any http requests - more details about it later for now let's just say that it runs as a process that posts my Google Latitude location to ThingSpeak and to my Arduino (using Teleduino) - so there is no point in posting the app URL here but in any case, now that I was able to deploy and run my app, I can say that Heroku is indeed awesome!

Monday, March 11, 2013

Stepper motor is alive!

A few weeks ago I got from ebay a couple stepper motors with driver boards, like this one. I tried to make them work with my Arduino using the stepper library that comes with the IDE, setting the pins directly, using a couple methods I found on the web but no matter what I did, the motors worked very jerkily, noisy and didn't really rotate the way I expected to. I gave up thinking the motors I have are somehow defective - I know, the probability of both being defective was really small but I could think of nothing else to try.

Again, Adafruit came to the rescue with this Raspberry Pi Lesson 10: Stepper Motors. At first, I was a bit thrown off by the fact that Simon Monk, the author of this lesson (and countless others, as great as this one) uses a ULN2803 to drive the motor while my boards from ebay use ULN2003. But a quick search on the web reveals that they are very similar, ULN2803 having one more input/output compared to the ULN2003. Encouraged by the fact that the chips on my boards use sockets and they are not soldered directly (this was a very nice thing the chinese manufacturer did), I decided to give it a try. Wired everything as in the lesson I just mentioned and it worked from the first try. Now that it worked on my RasPi, I decided to give it a try on Arduino as well, and same wiring and very similar code worked from the first try as well. Which shows that my problems were all along related to the way the driver boards are wired. It took me a while to figure this out but I'm happy I did.

Again, as I said before, I am in debt to Adafruit for all the help they offer to beginners like me! Thank you so much!

As a side note, I haven't done any awesome project with the stepper but I was able to use it on my RasPi as a NinjaBlock module, the code and some notes are here. I know I said this before but it bears repeating: the Ninja Blocks guys are simply awesome for letting people without a real block to still use the dashboard to play, experiment and learn this way. Thank you!

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):
tar -zxf node-v0.8.11.tar.gz
cd node-v0.8.11
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 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:// ninjaClient
cd ninjaClient
git checkout develop
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: OR
- this page:

In a nutshell both these boil down to:
sudo aptitude install git-core
sudo wget -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 ( 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.