Feeds:
Posts
Comments

Posts Tagged ‘carambola’

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

Limitations

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 »

This slideshow requires JavaScript.

Farò un post rapido e senza estratti di codice, dato che al momento tutta l’applicazione è un po’ messy: questo weekend ho installato una piccola Wireless Sensor Network a casa, così da avere (finalmente!) una misurazione delle temperature presenti sul soppalco e nel soggiorno del posto dove abito, ed allo stesso tempo per testare il software che stiamo per usare in un’installazione per un progetto che sto seguendo in università.

Il giro software ammetto che è lievemente complicato, e sarebbe stato più semplice usare un servizio tipo Cosm per risolverlo in parte, ma dove sarebbe il divertimento? I due nodi della WSN che fungono da sensori rilevano una volta al minuto la temperatura e l’umidità dell’ambiente dove si trovano, utilizzando un piccolo programma TinyOS che utilizza un protocollo di disseminazione sviluppato da alcuni colleghi; un terzo nodo funge da sink e riceve i dati dai primi due, ed è collegato alla porta USB di una Carambola; un secondo programma viene eseguito in quest’ultima, appoggiandosi ad una libreria C già presente in TinyOS, e da un lato riceve i messaggi e li inoltra al server che raccoglie i dati, dall’altro lato permette di modificare alcuni parametri dell’applicazione situata nei sensori.

Il server è un free tier su Amazon, ed un terzo programma, scritto in Erlang, riceve i messaggi tramite la rete, li deserializza (operazione estremamente semplice data la bit syntax del linguaggio) e li salva in un database Mnesia; un Web server poi pubblica tali dati tramite JSON verso una pagina Web estremamente semplice (presente in una delle figure), scritta utilizzando Highcharts, la quale mostra i grafici (con tanto di possibilità di zoom e cose simili).

Tutto questo ha permesso di sperimentare tutta una serie di tecnologie, dall’applicazione TinyOS (che riutilizzeremo altrove) alla libreria C, dalla bit syntax in un contesto reale all’utilizzo di Mnesia come database; ammetto inizialmente di aver pensato ad un NoSQL serio, mentre Mnesia è più una sorta di via di mezzo tra SQL e NoSQL, nel senso che non supporta questo linguaggio ma allo stesso tempo richiede uno schema dei dati (a differenza di altri tipo CouchDB).

La parte messy sta in realtà nell’applicazione Erlang, che è mescolata con una versione precedente che avevo usato accoppiata con un Arduino per raccogliere dati da un accelerometro; tra l’altro, ho anche sperimentato la generazione (semi-)automatica di releases di un programma con Erlang, e l’installazione a caldo degli aggiornamenti: spettacolare!

Read Full Post »

News flash: le USB parrebbero funzionare correttamente con l’ultimo update del firmware di Carambola.

Come avevo anticipato due giorni fa, ho scaricato l’update del firmware di Carambola ed ho ricompilato il tutto ieri (operazione non brevissima, dato che sono cambiate sia le versioni di gcc che di uclibc in uso, e questo ha implicato una ricompilazione a due passi di praticamente tutta la toolchain), ed ho fatto immediatamente un test con una chiavetta USB: viene riconosciuta tranquillamente, montata correttamente ed ho anche provato a creare qualche file o eseguire codice Erlang senza alcun problema o errore (e senza usare alcun hub).

Ora, è necessario qualche altro test intensivo, ma se tanto mi dà tanto, forse ci siamo…

P.S.: giusto per essere fair, l’unica altra spiegazione che posso dare è che la chiavetta testata ieri era diversa da quella che avevo provato in precedenza, e “di marca” (mentre l’altra era regalata ad una conferenza); non so se questo possa influire in maniera così decisa, ulteriori test vaglieranno tutte le ipotesi sul tavolo.

Read Full Post »

Avevo promesso un breve update su Carambola, e quindi eccolo qui.

Dico “breve” per il semplice motivo che non ho fatto molti esperimenti fino ad ora: la maggior parte del tempo l’ho infatti spesa per cercare di far funzionare la versione 15B di Erlang sul sistema OpenWRT installato sulla scheda, operazione che posso assicurarvi non è stata completamente banale causa limiti di spazio sulla flash (8 MB sono veramente pochi!) e necessità di patchare la nuova release (sorvolando sui problemi di cross-compilazione con il nuovo Mac che sto usando per lavoro), e per provare i pin GPIO presenti sempre sulla scheda stessa.

Il giudizio complessivo che posso dare ora come ora è il seguente: voto 6.5 in crescendo. Questo perchè:

  • lato connettività, direi che non ci sono particolari problemi: non ho provato le porte wired, se non per fare aggiornamento del sistema, ma con la connessione WiFi ho creato addirittura reti ad-hoc senza nessun problema (se non capire bene che parametri inserire nei file di configurazione). Certo, i chip scaldano abbastanza… sarebbe interessante capire se spegnendo la wireless scalda di meno, dato che magari in un contesto in cui si tiene la scheda collegata ad un router per far girare qualcosa tipo torrent o un mini-serverino (VPN et al.), potrebbe essere una possibilità concreta (in fondo sono gli usi tipici che si fanno con router riprogrammabili con OpenWRT, e ricordo che la Carambola ha di fatto un set hardware di quel tipo)
  • lato connettività hardware (GPIO, SPI, I2C etc), scarseggiamo discretamente: far funzionare i GPIO è in realtà piuttosto semplice (la modalità più facile è leggendo e scrivendo il device /dev/gpio), ma abilitarli non è facile e/o chiarissimo, dato che il chip RT3050 ha i pin che possono funzionare secondo diverse modalità; esistono alcune guide sulla Wiki o sul forum, ed un tool che è necessario scaricare per scrivere alcuni valori nei registri del microprocessore, ma la cosa resta non banale. Con SPI e I2C sembrano esserci un po’ di ritardi lato software, non è chiarissimo se le cose funzionano o meno: in particolare, con SPI pare che le velocità siano discretamente lente. Le cose non vanno meglio con le USB, dove pare che l’hub integrato nella devboard non riesca a reggere benissimo ad alcuni picchi di potenza; pare in realtà che il problema si risolva usando uno hub esterno, il che se risolvesse il problema sarebbe decisamente interessante, vedi punto successivo; non ho ancora testato questa ultima soluzione, ma mi ripropongo di farlo al più presto
  • lo spazio su disco è veramente poco (almeno, per i miei usi): chiaramente, la memoria flash interna viene usata solo per installare i programmi, ma ce ne stanno veramente pochi; per questo, sarebbe molto importante far funzionare la presa USB, anche come repository per applicazioni tipo server Web/FTP/p2p, magari collegando addirittura hard disk esterni (meglio se anche alimentati esternamente, penso)
  • recentemente gli sviluppatori hanno rilasciato una 2.0 del software: l’ho scoperto oggi, ho ricompilato mezzo mondo ed ora ho un’immagine pronta da testare, vedremo se ci sono stati dei benefici…

Ora, ho scritto “in crescendo” perchè alcuni dei problemi riportati qui sopra si stanno risolvendo (anche se mooolto lentamente) soprattutto sui forum, dove se non altro gli sviluppatori hw/sw intervengono spesso e volentieri per aiutare gli utenti, ed il forum stesso inizia ad essere un po’ più frequentato rispetto a qualche mese fa; resta comunque un device utile all’interno di un progetto che seguo nell’ambito del mio dottorato, quindi continuerò ad usarlo e non appena ho qualche statistica ed opinione più ampia non esiterò a pubblicarla (ad un certo punto spero infatti di raggiungere una release stabile del software di cui ho bisogno, così da potermi concentrare nell’uso della scheda invece che nell’uso della sua toolchain di compilazione!).

Peraltro, sto giusto pensando di acquistarne una seconda “ad uso privato”, con tanto di scatola, da usare a casa come piccolo database per salvare dati da sensori…

Read Full Post »

Carambola

Carambola

Habemus Carambola! Non ho ancora avuto tempo di metterci le mani dentro (salvo un boot di test), ma intanto è lì. Ricordo le specifiche:

  • processore MIPS @ 320 MHz (Ralink RT3050)
  • 32 MB RAM
  • 8 MB flash
  • 2 porte Ethernet
  • Wifi
  • 20 GPIO
  • USB host

Read Full Post »