Wednesday, October 29, 2014

node.js and Arduino

In my quest of learning more about node.js one of the searches returned results about the johnny-five framework that can be used to send commands from node.js to Arduino, along with Anna Gerber's awesome tutorial pages at node-ardx. Installing Firmata on my Arduino was a breeze using codebender (a while back codebender didn't have that many libraries so I had to use my own Arduino IDE for some of the code; nowadays I have to say that it is awesome, I am using it more and more and loading Firmata in this case took less than a minute). Unfortunately, this is where the easy stuff ended; I am writing this in case someone else will go on the same path and have the same issues and frustrations as I did.

My problem started with the fact that I was using an old Arduino from the great guys at Seeed Studio: nothing wrong with the board, I used is for so many projects and I will keep using it, but in this case it turns out it wasn't a good idea to try to make it work. I connected it to my Linux Mint laptop and tried the first small sample code from node-ardx: nothing - the code would start, detect the Arduino on /dev/ttyUSB0 sit there for a while and then time out. Searched a lot left and write and found a lot of ideas on the github johnny-five issues list but nothing worked (used cu to set the speed, installed an older Firmata 2.2, and many more tries). I knew node-red has an Arduino node (using arduino-firmata) so I tried it as well on my laptop without any success (getting errors like serial port not found, post closed and others). Since I knew node-red can work with Arduino on a Raspberry Pi because I was able to make it work very easily a while back during a hackathon, I connected my Arduino to my B+ RasPi and gave it a try with node-red: it worked from the first try. However, johnny-five still didn't work on the RasPi.

This was getting frustrating so following one of the issues on johnny-five repo mentioning problems with Duemilanove Arduinos I decided to use a different board: I have a SparkFun RedBoard around so installed Firmata on it, connected it to my Linux Mint laptop and guess what: johnny-five worked immediately. I was a bit surprised when I saw johnny-five identified the port as /dev/ttyUSB0 because I thought RedBoard is basically a Uno and according to the list found in this stackoverflow issue it should have been /dev/ttyACM0 (as the first Arduino I tried with node-red on RasPi at the hackathon); the list by the way says:
  • Duemilanove - Serial chip: FTDI FT232RL; Serial port: /dev/ttyUSB0
  • Uno - Serial chip: Atmel ATmega16U2 (or 8U2 on older boards); Serial port: /dev/ttyACM0
  • Leonardo - Serial chip: Atmel ATmega32U4 (built-in); Serial port: /dev/ttyACM0
Well, it doesn't really matter what port is used, the important thing is that it worked. Tried node-red after that and it worked great as well. Because in the end I would like to connect the Arduino via Bluetooth and the HC-07 module I have is set to 9600 baud, I changed the StandardFirmata.ino speed to 9600, also changed arduino-firmata.js in node-red node_modules to 9600, restarted node-red and it still works great! I still need to figure out how to use the bluetooth serial port module with node.js but that will happen later.

For now, my lesson is that Duemilanove Arduinos may have problems talking to node.js so I decided to stick with the RedBoard (or one of the Leonardos I have around) and keep the older Seeeduino board with the bluetooth module for communication with my Android phone (which works great by the way, using the Amarino library but that is subject for some future post).

No comments: