Posts Tagged ‘firmata’

A few weeks ago I ported the Firmata library to Lua, as a first step of an experiment of a DIY Arduino Yun. For several reasons I had to postpone that experiment (reasons being writing my Ph.D. thesis, and the fact that the Carambola board has the wrong pin distance to do anything good with it), and now I have an Arduino Yun, which essentially is a Carambola done well (and I couldn’t expect less by the Arduino people).

So, the next step is to run my Erlang framework, ELIoT, on it (the fact that the Yun has double the internal flash memory of the Carambola and support for external SD cards is simply great), and to interact with the microcontroller (the usual Atmega 32u4) I needed a porting of Firmata to Erlang. The Yun team gives you a Python library for intercommunication, but since it is not C/C++ I couldn’t make a wrapper to an Erlang driver, so I decided to go (Erlang) native.

Thus, meet the library: as before, it is a simple port of the Java Processing one, meaning that it supports only analog and digital write/read operations, among the other operations of Firmata. I have tested it on the Arduino Yun: the Atmega 32u4 was running the Standard Firmata sketch, and a computer was running Erlang and the library (I still have to install Erlang on the Yun microprocessor itself), and it works, at least with the usual simple test of enabling the integrated LED and read from one of the analog sensors.

The library itself is structured as a gen_server: just one instance each time, of course.

*** UPDATE ***

Now the library works in both push and pull modes: a client can query the server to obtain the current values of sensors, or it can receive the updates directly from the server by subscribing to a specific (analog or digital) pin.

Beware that the latter will generate a new update message for each Firmata update from the Arduino (for example, the StandardFirmata sketch running on my Yun produces about 50 messages per second, one update every 20 ms). You may need to filter these messages, e.g., taking action when a certain threshold between consecutive values is exceeded.

Read Full Post »

Carambola and Arduino

Carambola and Arduino

The makers world, or at least myself, is waiting for September 10th, when Arduino will start selling a new product, the Arduino Yun: it’s an interesting event, because it’s been a few months since several other (for a lack of a better word) IoT devices producers have started developing and selling similar devices (e.g., pcDuino), combining microcontrollers and microprocessors in a single, embedded board, and I (for one) want to see what the Arduino company has to offer in this field.

The main idea behind these devices is that you can use microcontrollers for some tasks, in particular for interfacing with hardware through GPIO, PWM (which is not often found in microprocessor boards) and others, with a (soft) real time approach, while the microprocessor often includes a WiFi (and Ethernet) chip, and runs an operating system like Linux (or variants such as OpenWRT), thus is more apt to handle network communication with the Internet and online services. I have to admit that at the beginning I thought it didn’t make a lot of sense: if you needed a microcontroller, you could use a microcontroller, and if you needed a microprocessor, you could use one. But then the idea of having both on the same board, with good libraries for data exchange between the two, started to make sense (needless to say, I already have in mind a couple of projects, but no spoilers yet).

That said, while waiting for September 10th, I tought it could be interesting to make my own “Arduino Yun”, to combine a router-like device running OpenWRT with a microcontroller. What you can see in the photo above is the first step in this direction: hardware connections and software libraries.

The hardware

The OpenWRT board is a Carambola, about which I already wrote several posts in the past couple of years: it is a MIPS board with 32 MB of RAM and 8 (!) MB of Flash disk, plus several I/O pins (but no PWM), two Ethernet ports and a WiFi chip; in the photo it is shown with its dev board (the Carambola itself is the smaller piece with the two leds in the upper part of the photo). The microcontroller board is simply an Arduino Uno (rev2).

The Carambola (with the dev board) is powered by a 12V supply, and in turn it powers the Arduino through the Vin pin; the two communicate through one of the two UART ports of the Carambola, in particular the one “exported” on the pins (ttyS0; ttyS1, the other one, is connected on the dev board to a DE-9 connector). A voltage divider is needed, because the Carambola pins run at 3.3V, while Arduino ones run at 5V, so you cannot connect directly the Tx pin of the Arduino to the Rx pin of the Carambola (while the other direction works: 3.3V is enough for the “high” state of Arduino pins).

The small breadboard also contains a photoresistor, connected to the first analog input of the Arduino, and the reason why it is there will be explained in the next section.

The software

The Arduino people have developed a new library for the Arduino Yun (called “Bridge”), but in my experiment I wanted to use something already existent, so my first thought was Firmata. I used it in the last couple of experiments to interface Arduino with Processing (Java, basically), and I thought I could do the same here.

So, Arduino Uno is running the latest Firmata library (the one distributed with Arduino IDE 1.0.5); at this point, I had to find the corresponding library to run on the Carambola board. Now, on OpenWRT, with only 8 MB of Flash, it is really hard to make space to languages or other not-so-small applications, so the first choice would have been C: I’m pretty sure there is a Firmata library for C/C++ somewhere (I didn’t search for it, but nevertheless…), but I thought it would be less difficult to use other languages (especially because I would like to implement something a little more complicated that the example that I’m showing here).

Now, the language with the smallest footprint I think is Lua (there is also the choice of “Python mini”, but I didn’t try it), so I recompiled OpenWRT with Lua and a serial library that I found on the Internet, and I installed everything. At this point, I only needed a Firmata port to Lua, which didn’t exist (there are a couple of posts on the Web of people saying that they needed it, but no code whatsoever). So I decided to write it down myself (without knowing the language: I met the creator of Lua at the LASER Summer School last year, but I never used it): if non-developers are able to use that language to write scripts for various applications, I should be able to write some hundreds lines of code!

So I ported the Java library (~300 lines of code including comments) to Lua (~200 lines of code), with the help of a couple of libraries for handling bitwise operations and scheduling coroutines (to be able to read and write on a serial port at the same time). I had to go past a couple of “peculiar” characteristics of Lua (e.g., counters in arrays start from 1, or the coroutine scheduling itself was pretty new to me), but everything seems to work correctly now.

The example application simply connects to Arduino (the serial port name may have to be changed depending on your devices), then switches the test led (pin 13) on and off every two seconds, while reading from the analog port values coming from the photoresistor (or any other analog sensor) when switching the led itself (photo below, the peak in the photoresistor values is due to the flash light of the camera). The other coroutine reads periodically from the serial port, if there are bytes waiting to be read. The code may not be the best one written in Lua, but I still don’t know all the peculiarities of the language so I had to improvise from time to time (and take inspiration from the Lua wiki). There is a line that needs to be changed, whether the code is running on a computer or on OpenWRT (because of different implementations of the gettimeofday posix call).

Photoresistor values

Photoresistor values


The code works with the configuration described above; I have to admit that I had problems with older Arduino boards (e.g, with the Duemilanove running on ATMEGA 168, Firmata seems not to be working at all, also using Java on the computer side) and with the newest one (an Arduino Uno rev3 SMD, which seemed not to be working with Lua on the Carambola with serial communication through pins, while it worked if connected to the Carambola through USB, so maybe there is a catch with serial pins… I didn’t investigate the problem, I simply used a different Arduino Uno board that I had lying around).

The next steps

The next step is to remove the dev boards: this means remove the dev board from the Carambola and power it directly through its pins (and access it through WiFi with ssh), and take the ATMEGA 328P (the microcontroller from the Arduino Uno) alone (maybe with an external quartz oscillator, not sure about it because I could run it with the internal one, at 8 MHz instead of 16 MHz), program it with Arduino Firmata, and then connect it directly to the Carambola, again through its pins.

Stay tuned!

Read Full Post »