Following on from my previous blog post, I will be exploring some expansions to the basic hardware. Previous post can be found here and example code can be found here. I will be using the same setup as before, so use the previous post for reference.
What is Firmata?
As we saw with the previous post we could receive data from the Arduino board via serial. What this did was open up a connection between the board and the computer, operating on baud 9600, and allow for a print out in the Arduino IDE console.
This was good enough for logging things from the board to the computer but the connect is only one way. When a script is uploaded to the board, it loops continuously. We can log stuff to the console as it is within the script, but to send information from the computer to the board is not possible via serial.
Firmata is a protocol for communicating with microcontrollers from software on a computer. This means that all programming logic will be on your computer, rather than uploaded to the board. Instructions are sent at run-time, via USB port or some other protocol/medium(bluetooth, wifi). Whenever communication is required the Firmata protocol tells the Arduino what to do.
The main difference, aside from being able to have a two way connection, is that the script is running on the computers side. The Arduino no long executes a script like before be waits for instructions on what to do. We now have the ability to run things in a more controlled manner rather than powering up the board and watching it go.
This enables use to do a lot more things with the board than we were previously able to. Firmata allows for us to theoretically write commands in any language, but here is a list of available libraries. It also allows for communication between multiple boards as the computer is the host for commands and can communicate to more than one board.
Firstly the Firmata script needs to uploaded to the board. I will be using Johnny-five and this works with the StandardFirmataPlus library. Go to File>Example>Firmata>StandardFirmataPlus in the Arduino IDE to open up the file. Now upload it to the board. This file sets up the board for communication, and is all that needs to be done for the hardware.
First step is to create a folder where you code will go. Then install Node.js if you don’t have it already. Run npm install johnny-five, and then head on over to the johnny-five docs, and have a look at some of the example code. To run the code all you need to do is create a new file in the folder you just created, copy the code in, give it a name and save. Navigate to the folder via the command line and run node your_file_name.js. This will execute the code via node.js.
If you are still using the same setup as the previous post head over to here, for some edited code to work with LED we had wired up last time. You will still need to run npm install johnny-five to get the code to work.
The foundations of the script are completely different. We no longer are working through setup and a loop. Instead, the library is required and a board object is created. The boards .on method is called, passed the string ‘ready’ and a function is called containing the code we want to run. The .on method works and looks very much like an event listener. A led object is created, the blink method is then called and passed a delay value as an argument.
The code it self is a lot cleaner than with the Arduino standard syntax. The led is created in a very declarative way. Simply calling a constructor for the led on the library and passing in the pin number that it’s connected to, is enough to setup the led. This gives us access to all the predefined methods for the led. These include on, off, intensity, fade, toggle and strobe. This means that we can achieve the same as the Arduino code in a much easier way.
In the rainbow.js file, I use the Led.RGB constructor. This is interesting as an object is passed in as an argument.
The pins are set with the key pins and value of an object containing the pins. Also as i’m using an anode led, I set the isAnode to true.
A loop is then created using the keyword this, this refers to the board and using the .loop method we can create a loop similar to how we would create a time interval. The led color is set by calling the method on the led and passing it a hexcode for the color, which is contained in the rainbow array.
Firmata and Johnny-five open up a load more possibilities with Arduino that aren’t possible whilst just using the native code. It also allows writing code outside of the Arduino IDE in a very easy manner. Have a look at more examples here.