Handling polymorphism with Jackson

Sometime you need to serialize a hierarchical model to JSON. Jackson offers different ways to handle the problem. Here I will expose two of them.


First solution: Annotate the super class with @JsonTypeInfo(use=Id.CLASS)Pros: works with any subtype. Cons: breaks with classes or package renaming.

Second solution: Annotate the super class with:

		@JsonSubTypes.Type(value=Cat.class, name="Cat"),
		@JsonSubTypes.Type(value=Dog.class, name="Dog")

adding a JsonSubTypes.Type for each sub-class. Pros: no problems with renaming if custom names are provided. Cons: requires to specify all the sub-classes in the super-class.

Long story

We have animals:

public abstract class Animal {
 public String name;

between them dogs:
public class Dog extends Animal {

 public double barkVolume;

 public String toString() {
 return "Dog [name=" + name + ", barkVolume=" + barkVolume + "]";

and cats:
public class Cat extends Animal {
 public boolean likesCream;
 public int lives;
 public String toString() {
 return "Cat [name=" + name + ", likesCream=" + likesCream + ", lives=" + lives + "]";

We try to serialize a cat:
ObjectMapper mapper = new ObjectMapper();
Cat cat = new Cat();
cat.name = "Fuffy";
cat.likesCream = false;
cat.lives = 7;
String json = mapper.writeValueAsString(cat);

obtaining the following JSON:
  "name" : "Fuffy",
  "likesCream" : false,
  "lives" : 7

But when we try to deserialize it:
String json = ...

Animal expectedCat = mapper.readValue(json, Animal.class);

we get an exception:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.github.fedy2.jhe.model.Animal: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: {"name":"Fuffy","likesCream":false,"lives":7}; line: 1, column: 1]
	at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
	at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1012)
	at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:149)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
	at com.github.fedy2.jhe.SerializeDeserialize.main(SerializeDeserialize.java:28)

Not good. Looks like Jackson is not able to understand if the JSON refers to a cat or to a dog.

We can help Jackson annotating the super-class with the JsonTypeInfo annotation. The annotation will add type information to the generated JSON.

We can tell Jackson to use the fully-qualified Java class name as type information:

public abstract class Animal {

a @class property will be added with the full class name:
  "@class" : "com.github.fedy2.jhe.model.Cat",
  "name" : "Fuffy",
  "likesCream" : false,
  "lives" : 7

If you like a shorter class name you can use the Id.MINIMAL_CLASS option:
public abstract class Animal {

a @c property will be added with a shorter class name:
  "@c" : ".Cat",
  "name" : "Fuffy",
  "likesCream" : false,
  "lives" : 7

With both solutions we should be worried about refactoring: if we change the classes or package names we will not be able to deserialized previously stored JSON.

As alternative we can store custom names using the Id.NAME option:

public abstract class Animal {

Obtaining a JSON with a new property @type with the type name:
  "@type" : "Cat",
  "name" : "Fuffy",
  "likesCream" : false,
  "lives" : 7

By default Jackson uses the class name as name.

Unfortunately during the deserialization we get an exception:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'Cat' into a subtype of [simple type, class com.github.fedy2.jhe.model.Animal]: known type ids = [Animal]
 at [Source: {"@type":"Cat","name":"Fuffy","likesCream":false,"lives":7}; line: 1, column: 10]
	at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:42)
	at com.fasterxml.jackson.databind.DeserializationContext.unknownTypeIdException(DeserializationContext.java:1477)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownTypeId(DeserializationContext.java:1170)
	at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._handleUnknownTypeId(TypeDeserializerBase.java:282)
	at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:156)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:112)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97)
	at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:142)
	at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:63)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
	at com.github.fedy2.jhe.SerializeDeserialize.main(SerializeDeserialize.java:28)

Jackson is not able to map the type name to a class. To solve the problem we provide sub-class information with the JsonSubTypes annotation:
public abstract class Animal {

We can use custom names specifying them in the Type annotation:
		@JsonSubTypes.Type(value=Cat.class, name="Gatto"),
		@JsonSubTypes.Type(value=Dog.class, name="Cane")
public abstract class Animal {

Now we get a JSON with our custom names:
  "@type" : "Gatto",
  "name" : "Fuffy",
  "likesCream" : false,
  "lives" : 7

We can obtain the same result annotating the sub-class with a JsonTypeName annotation:
public class Cat extends Animal {

With custom names we can refactoring without problems but we will need to specify all the subtypes in the super class.

For more information please refer to official documentation: http://wiki.fasterxml.com/JacksonPolymorphicDeserialization

Run JavaScript on your ESP8266 using Espruino firmware


Espruino is a board that comes with a pre-installed firmware capable of running JavaScript code. You can buy one of the official board or you can flash a list of “not officially supported” boards. The list contains also the ESP8266 board, so let’s try to run our JavaScript code on it.

For this article I’m using a NodeMCU Amica board which lets us play with the ESP8266 (ESP-12) without the burden of connecting it to the computer and also powering it to the right voltage all from a micro USB port.

All the programs in the article are run from a Linux Mint 17 operative system.

Flash the firmware on ESP8266

First step is to flash the Espruino firmware into the ESP board. You can find a tutorial in the Espruino site. In this article I’m putting a short list of instructions ready to use.

Create a working folder and download there the ESP tools.

mkdir espruino
cd espruino
wget https://github.com/themadinventor/esptool/archive/master.zip
unzip master.zip

The ESP tools are a set of Python scripts necessary to flash a firmware in the ESP board.

We need now a ESP8266 dedicate version of the Espruino firmware. You can find the latest version in this forum post. Here the instructions to download the last version available today (26/06/2016):

wget http://s3.voneicken.com/espruino/espruino_1v85.tve_master_124390c_esp8266.tgz
tar -xvf espruino_1v85.tve_master_124390c_esp8266.tgz 

We are ready to flash our board. The following instruction are for a ESP-12 board connected to the /dev/ttyUSB0 port. Please refer to the original tutorial for a different version of the ESP board.
cd espruino_1v85.tve_master_124390c_esp8266
../esptool-master/esptool.py --port /dev/ttyUSB0 --baud 115200 \
  write_flash --flash_freq 80m --flash_mode qio --flash_size 32m \
  0x0000 "boot_v1.5.bin" 0x1000 espruino_esp8266_user1.bin \
  0x3FC000 esp_init_data_default.bin 0x3FE000 blank.bin

The blue led in the board will start blinking and you will see the following output in console:
esptool.py v1.2-dev
Running Cesanta flasher stub...
Flash params set to 0x004f
Writing 4096 @ 0x0... 4096 (100 %)
Wrote 4096 bytes at 0x0 in 0.4 seconds (90.2 kbit/s)...
Writing 462848 @ 0x1000... 462848 (100 %)
Wrote 462848 bytes at 0x1000 in 40.1 seconds (92.4 kbit/s)...
Writing 4096 @ 0x3fc000... 4096 (100 %)
Wrote 4096 bytes at 0x3fc000 in 0.4 seconds (90.2 kbit/s)...
Writing 4096 @ 0x3fe000... 4096 (100 %)
Wrote 4096 bytes at 0x3fe000 in 0.4 seconds (90.5 kbit/s)...

(Did you noticed the “Running Cesanta flasher stub…” print? Looks familiar?)

If you’re having problems finding the correct device path in Linux:

  • first check that you’re using an USB data cable. The first time I’ve tried I was getting crazy because I was using a simple power USB cable.
  • second try this console command to print the system events when the USB cable is plugged:
    watch -n 0.1 "dmesg | tail -n $((LINES-6))"

    When you plug the cable you will get something like this:
    [ 5597.132098] usb 6-1: new full-speed USB device number 11 using uhci_hcd
    [ 5597.298148] usb 6-1: New USB device found, idVendor=10c4, idProduct=ea60
    [ 5597.298156] usb 6-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    [ 5597.298160] usb 6-1: Product: CP2102 USB to UART Bridge Controller
    [ 5597.298163] usb 6-1: Manufacturer: Silicon Labs
    [ 5597.298165] usb 6-1: SerialNumber: 0001
    [ 5597.304121] cp210x 6-1:1.0: cp210x converter detected
    [ 5597.306173] usb 6-1: cp210x converter now attached to ttyUSB0

After the flash operation the board is ready to receive your JavaScript commands!

To send the commands you need to connect to the board using a serial connection (through the USB port). Under linux you can use the screen program:

screen /dev/ttyUSB0 115200

Now you can send JavaScript commands that will be interpreted and the output provided as response.

For example writing:


you will get:

You can call functions, for example:

it will reset the board and print a boot message:
 _____                 _ 
|   __|___ ___ ___ _ _|_|___ ___ 
|   __|_ -| . |  _| | | |   | . |
|_____|___|  _|_| |___|_|_|_|___|
          |_| http://espruino.com
 1v85.tve_master_124390c Copyright 2016 G.Williams

Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
Flash map 4MB:512/512, manuf 0xe0 chip 0x4016

Time to use the Espruino Web IDE.

Espruino Web IDE

The Espruino project provides a Web IDE for editing and uploading your JavaScript code to your board. The IDE also manages the modules that can be used in your code in order to extend the basic functionalities offered by the SDK, for example to use the Wi-Fi device or external sensors like the DHT-22.

The Web IDE can be installed as Chrome application or as NodeJS package.

Espruino Web IDE

After the IDE has been installed we have to configure the baud rate in order to have the communication with the board working fine. Click on the gear icon on the top right, select the COMMUNICATIONS tab, then set the “Baud Rate” to 115200.


Now everything is ready to play. Click the connect button on the top left, select the USB port to use and the IDE will connect to the board.

On the left side you can write commands directly to the board and get the response live.


On the right side you can write your code and then upload it to the board using the upload button on the center.


Now we ready to write our first JavaScript program to run on our ESP8266 board!

Run JavaScript in your ESP8266 device

In the previous article we saw how to build the Smart.js framework and flash the firmware in the ESP8266 device. Now is time to play a little more with it.

In this article I will show how to read the temperature and the humidity from a DHT11 sensor connected to the ESP8266 device and how to send those values to a public site using an HTTP GET request, all using JavaScript code.

How to add new files to the firmware

To add a new file to the firmware add it in the smart.js/platforms/esp8266/fs folder. All the files in the fs folder are added to the file system of the firmware during the build process. Those file will be accessible using the File API.

Lets try it creating a file myfirst.js with the following code:

//print the Hello World message
function sayHello() {
   print('Hello World');

//print my external IP
function getMyIp() {
 Http.get("http://jsonip.com", function(data, error) { 
   if (error) { 
     print("error ", error); 
   } else { 
     print("my ip is ", JSON.parse(data).ip); 

Now save the file and run the firmware build process:
sh make.sh

Connect the ESP device as explained before and flash the new firmware.

In the console we can now load and evaluate our JavaScript file using the eval function from the File API:


The code in the file is evaluated and the functions that we have defined can now be called:
smartjs 29592/2268$ sayHello();
Hello World

The sayHello method prints the “Hello World” message visible in the output area. Instead the “undefined” value is the value returned by the function and printed by default.
Let’s try the other function getMyIp:
smartjs 29408/2160$ getMyIp();
smartjs 26728/1956$ error cannot resolve

The execution terminates with an error message, this happens because the WiFi is not configured. We can configure it using the Wifi API:
Wifi.setup('yourssid', 'yourpassword');

The setup function will make the ESP device scan for the specified network and connect to it. If everything go fine the method return the true value.

The Wifi API offers also a method to check the current Wifi status:

smartjs 29384/2160$ Wifi.status()
got ip

Now that the Wifi is configured we can try the getMyIpFunction:
smartjs 28752/1860$ getMyIp();
smartjs 25656/1860$ my ip is

The printed IP is our external IP, the one of our modem. If, instead, we want the internal IP, the one assigned by our access point, simply call Wifi.ip().
Now we are able to write our program and execute it. Time to read values from a DHT11 sensor.

Wiring the DHT11

To read the sensor values we will use the only available pin in the ESP device the GPIO_02.
First connect the VCC and GND pins of the sensor to the respective VCC and GND on the USB adapter. Then connect the DATA pin to the ESP GPIO_02 pin. Note that the GPIO_0 is NOT connected to the GND.

Now connect the USB adapter to the computer and click the “connect” button on the Flash’n’Chips tool. I don’t know why but sometime I have to disconnect the sensor DATA pin before turn on the ESP device otherwise the ESP console don’t reply.

The framework comes with the support for the DHT11 sensor, a global object DHT11 is defined with a read function. The function takes an integer as input, the sensor port, in our case is the number 2.

We can use the DHT11 object simply calling his read method:

smartjs 29584/2268$ DHT11.read(2);

The returned value is an object with temperature and relative humidity. Time to publish the sensor values.

Publish the sensor values

We will publish the sensor values in a site that accepts a GET request with the values as url parameters, something like http://mysite.com/publish.php?temperature=22.0&humidity=55.0

Create a sensor.js file in the fs folder with the following code:

function readSensor() {

  //read the sensor data
  var data = DHT11.read(2);

  //compose the GET url
  var url = "http://mysite.com/publish.php?temperature="+data.temp+"&humidity="+data.rh;

  //do the GET
  Http.get(url, function(data, error) {
    if (error) {
      print("error ", error);
    } else {
      print("data published");

Build the firmware with the new file. Before flash the device re-connect the GPIO_0 to the GND and disconnect the sensor DATA pin. After the flash is complete disconnect the adapter e disconnect the GPIO_0 from the GND.

Connect the adapter to the computer, then click the “connect” button in the Flash’n’Chips tool. After the console is connected re-connect the DATA pin.

First, setup the WiFi:

Wifi.setup('yourssid', 'yourpassword');

Then load and evaluate the sensor code:

We can now run our method readSensor. It will read the values from the sensor and publish them to a site using the HTTP API. Here the console output:
smartjs 25656/1860$ data published

And a chart built using the published values:


So our little ESP device is able to read values from a sensor and publish them in a site using the Wi-Fi network.

Continuous and automatic publication

What if we want a continuous publication of sensor values?
Here the solution:

function streamSensorData() {
  setTimeout(streamSensorData, 5000);

This method will publish the sensor data every 5 seconds.

What if we want to start the publication automatically without the need to run it from a console?
Here the solution.

First append those lines to the sensor.js code:

if (Wifi.status() === "idle") Wifi.setup('yourssid', 'yourpassword');

The first line checks if the WiFi is active otherwise setups it. The second line starts the data streaming.

In order to make our sensor.js file evaluated at boot time we need to modify the user.js file under smart.js/src/js folder appending this line:


Build and flash the firmware. After rebooting the device it will start to publish the sensor values.


We saw how to run our JavaScript code in our ESP device thanks to the great work from Cesanta. The framework is still under development so the APIs or the tools can change in any moment.

I think there will be for sure a lot of news in the coming months. One thing will be the integration of the web server Mongoose in the Smart.js framework, that means connecting directly to the device using the browser!
Another thing, as I can understand from the Smart.js online dashboard, is that we will be able to upload our JavaScript code directly from our browser so we don’t need to flash the firmware every time we update the code!!!

Build the Smart.js framework

Cesanta has developed a JavaScript engine, capable of run JavaScript code in the ESP8266 device and in other embedded devices. The JavaScript engine is provided with the Smart.js framework. The framework offers a set of APIs for files management, networking and WiFi management, SPI and I2C communication, GPIOs and Cloud services. The framework is distributed ready to flash under Windows and Mac. Here you can find a short guide.

In this article I will show how to build the framework under the Ubuntu’s like OS and flash your ESP8266-01 device with the latest Smart.js framework version.

Framework build

In order to build the Smart.js framework we need first to install Docker. A complete and updated set of instructions on how to install Docker can be found here. To make it short you can simply run this command in the console:

wget -qO- https://get.docker.com/ | sh

After the Docker installation is complete we checkout the framework code:
git clone https://github.com/cesanta/Smart.js.git

To build the framework move to the esp platform directory:
cd Smart.js/platforms/esp8266

and run the build process:
sh make.sh

The first time it will require a little bit because the tool has to download the docker images. Next builds will be really fast.

The three binary files composing our ESP firmware will be generated under the firmware folder. Now we need the tool to flash the ESP device.

Flash’n’Chips build

The Smart.js framework is provided with a tool for flashing our devices: Flash’n’Chips.


To build the tool first we need to install the required dependencies: Qt 5 and libftdi.

To install Qt 5 you can use the installer distributed by Qt here. It will install all the Qt tools under the specified folder. In the folder you will find the binary required to compile Flash’n’Chips. I suggest to add the bin folder to your PATH variable:

export PATH=Qt5.5.0/5.5/gcc_64/bin:$PATH

To install the libftdi library run:
sudo apt-get install libftdi-dev

Now we are ready to compile the Flash’n’Chips tool. Move to the source folder:
cd smart.js/flashnchips

And run first:


In a bit the tool will be ready to be executed. Time to connect the ESP device to your computer.


To connect my ESP 8266-01 I’m using a FT232 adapter. If you are using it remember to set it to 3v3.


We connect the respective VCC and GND pins, same for RX and TX pins but inverting them. We need also to connect the ESP CH_PD to the VCC and, only for the flashing process, the ESP GPIO_0 to the GND.

ft232 esp8266_bb

We are now ready to flash the previously built firmware into the ESP device.

Flash the ESP

Connect the USB adapter and run the Flash’n’Chips tool using this command from the flashnchips folder (I had to use this parameters for some issues with my ESP board):

./flashnchips --overwrite-flash-fs --esp8266-flash-params 0x0000 --gui

The tool should show the “Flash firmware” button active.

fnc ready

If not press the “Detect devices” button and see if the adapter is recognized.

Press the “Flash firmware” button and the flash process will start. You can see the progress bar running and the current operation explained under it.

fnc flash

When the process is completed the communication console will be activated under the progress bar area. You will see some messages from the device.

fnc console

Now we are ready to run some JavaScript commands in our ESP device.

Play with console

The Flash’n’Chips console has two areas, the first shows the device output, the second let us write commands to send to the device. The commands are snippets of JavaScript code that are executed on the device.

We can start writing a simple expression:


The device will calculate it and reply with the result:
smartjs 29176/2256$ 2+2

We can also use the framework’s APIs, for example we can list the files present on the device:

It should output something like:
smartjs 29536/2268$ File.list('.')


These are some of the JavaScript functions that our device can run. In the next article we will see how to run a simple program that reads values from a DHT11 sensor and publish them in a site.

How to translate a search query into a JavaScript filtering function

In a project I had to translate a search query into a filtering function to apply to a stream of JavaScript objects. The query language was very simple, boolean expressions of property constraints, for example “name:john AND age:52” where the search should return all the objects with property “name” equals to “john” and property “age” equals to 52.

The first step was to parse the search query with a parser. You can write a parser by hand or your can use a parser generator like PEG.js. I prefered the second one.

With the online editor I’ve wrote the language grammar:

start = andExpression

andExpression = orExpression (and andExpression)?

orExpression = expression (or andExpression)?

expression = string ':' string

string = char*

char = [a-zA-Z0-9]

and = ws 'AND'i ws

or = ws 'OR'i ws

ws = [ \t\n\r]*

The generated parser takes as input the text to parse and returns the parsed elements:
            " "
            " "

The grammar can be changed to make the parser method return a boolean expression:
  function combine(left, right) {

    if (right === null) return left;
    return left + " " + right[0] + " " +right[1];

start = ex:andExpression 
        {return 'return '+ex+';';}

andExpression = left:orExpression right:(and andExpression)? 
                {return combine(left,right);}

orExpression = left:expression right:(or andExpression)?  
               {return combine(left,right);}

expression = name:string ':' value:string
             {return 'LangParser.hasKeyValue(obj,\''+name+'\', \''+value+'\')';}

string = chars:char* 
         { return chars.join(""); }

char = [a-zA-Z0-9]

and = ws 'AND'i ws 
      {return '&&';}

or = ws 'OR'i ws 
     {return '||';}

ws = [ \t\n\r]*

The parser for a query like “name:john AND age:52” generates the following expression:
return LangParser.hasKeyValue(obj,'name', 'john') 
        && LangParser.hasKeyValue(obj,'age', '52');

In the generated expression I’ve used an utility method that checks if the specified couple of name and value exists as property for the specified object:
 * Checks if the passed Object has the specified couple of key and value as property.
 * @param obj the object to check.
 * @param key the key.
 * @param value the value-
 * @returns {Boolean} <code>true</code> if the couple is found, <code>false</code> otherwise.
LangParser.hasKeyValue = function (obj, key, value) {
    return obj.hasOwnProperty(key) && value === obj[key];

The expression can be then passed as parameter in the function constructor:
var expression = parser.parse(query);
var filterFunction = new Function("obj", expression);

Now you can use the new function as filter, for example with stream.js streams:
var filtered = new Stream.filter(filterFunction).toArray();

The grammar can be expanded with more operators and capabilities.

Arduino digital clock

As free time project I’ve built a digital clock using Arduino Duemilanove, the DS1307 Real Time Clock, the MCP23017 port expander and a bunch of blue leds.




Seven segment display

The idea is to show the current time using four 7 segment display, the display are built using some leds and a cardboard.

Each digit segment is realized using a blue led. The led is inserted in a hole partially covered with reflective material (I’ve used the internal part of a Tetra Pack container). The front part is covered with masking tape.


Each digit has seven segments. The segments holes have been made in a cardboard using a stencil.


Controlling the seven segment display

The first two digits segments are controlled by the MCP23017 port expander, the other two are controlled directly using the Arduino ports.

The MCP23017 port expander can control 16 ports, the 16 ports are managed by two registry. The port expander is controlled by Arduino using I2C.MCP23017The single registry controls 8 ports and you can set which ports are ON or OFF using a byte. Each bit of the byte tells the port expander if the port should be ON or OFF.

For example in the following image you can see the registry turning ON leds 1 and 4 having his value set to 9.

leds1Here you can see the bits converted to the byte value 9:

port 1   2   3   4   5   6   7   8
bits 1   0   0   1   0   0   0   0
     1   2   4   8  16  32  64 128
     1           8                 = 9

I’ve used one registry for digit then I’ve left unused one port for registry.

In order to identify each segment they have been labeled as follow:


“7 segment display labeled” by user:h2g2bob – Own work using Inkscape. Licensed under CC BY-SA 3.0 via Wikimedia Commons – https://commons.wikimedia.org/wiki/File:7_segment_display_labeled.svg#/media/File:7_segment_display_labeled.svg

Assigning each port to a single digit segment you have this mapping:

   A   B   C   D   E   F   G
   1   2   3   4   5   6   7
   1   2   4   8  16  32  64

For example if you want to display the digit 2 you need to turn ON segments A, B, G, E and D.



In order to do so you have to set the registry value to 91 (0x5B in HEX).

   A   B   C   D   E   F   G
   1   2   4   8  16  32  64
   1   1   0   1   1   0   1
   1   2       8  16      64 = 91 (0x5B)

I wasn’t lucky and due to wires length and leds positions I had to connect the leds to the ports in a random order getting such mapping:

   E   D   G   C   A   F   B
   1   2   4   8  16  32  64
   1   1   1   0   1   0   1
   1   2   4      16      64 = 87

This random order required to “re-map” the segments before converting it as byte.

Here you can find a nice tutorial about the MCP23017 port expander.

The remaining two digit segments are controlled one by one using Arduino ports and standard digital write.

The time

The time is keep using the DS1307 RTC. The RTC is controlled through I2C and the RTC library.DS1307RTC


Here a schema of main components connections.


The code

#include <Wire.h>
#include "RTClib.h"

//Real time clock
RTC_DS1307 rtc;

// Digit segments labels:
//     a 
//    ---
// f |   | b
//    --- g
// e |   | c
//    ---
//     d

// mapping between segments and registry for digits managed by MCP23017 port expander
                   // a   b   c   d   e   f   g
int segmentsA[7] = { 16, 64,  8,  2,  1, 32,  4};
int segmentsB[7] = {  1,  4, 16, 32, 64,  2,  8};

// mapping between arduino ports and segments for digits managed directly by Arduino
                   // a   b   c   d   e   f   g
int segmentsC[7] = {  2,  0, 10, 13,  9,  4,  8};
int segmentsD[7] = {  5,  7,  6, 12, 11,  1,  3};

// mapping between digits and segments bits configuration
byte digits[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};

void setup() {

  //i2c setup
  //setup MCP23017 registries
  Wire.write(0x00); // IODIRA register
  Wire.write(0x00); // set all of port A to outputs
  Wire.write(0x01); // IODIRB register
  Wire.write(0x00); // set all of port B to outputs

  //setup arduino ports
  for (int i = 0; i< 14; i++) pinMode(i, OUTPUT);
  //RTC setup
  if (! rtc.isrunning()) {
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //clear clock digits

void loop() {
  //get time from RTC
  DateTime now = rtc.now();

  //prints hour and minutes
  print(now.hour(), now.minute());

//prints hour and minute
void print(int hour, int min) {

//prints hour digits
void printHour(int hour) {
  //extract the two digits from the hour value
  int h = hour%100;
  int hd1 = h%10;
  int hd2 = (h/10)%10;
  int d1 = mapSegments(segmentsA, hd1);
  int d2 = mapSegments(segmentsB, hd2);

//prints the minute digits
void printMinute(int min) {
  //extract the two digits from the minute value
  int m = min%100;
  int d1 = m%10;
  int d2 = (m/10)%10;
  printDigit(segmentsC, d2);
  printDigit(segmentsD, d1);

//clear the clock digits
void clearClock() {

/** HELPERS **/

//writes into the A registry of MCP23017
void writeARegistry(int value) {
  Wire.write(0x12); // GPIOA
  Wire.write(value); // port A

//writes into the B registry of MCP23017
void writeBRegistry(int value) {
  Wire.write(0x13); // GPIOB
  Wire.write(value); // port B

//translate the digit into a registry bits configuration
byte mapSegments(int segments[], int digit) {
  //the segments configuration
  byte bits = digits[digit];
  int pow = 1;
  //registry configuration
  int registryBits = 0;
  for (int i = 0; i< 7; i++) {
    registryBits += (pow & bits)?segments[i]:0;
    pow = pow*2;
  return registryBits;

//turn on the segments for the specified digit
void printDigit(int segments[], int digit) {
  //segments configuration
  byte bits = digits[digit];
  int pow = 1;
  for (int i = 0; i< 7; i++) {
    //turns on the segment based on digit configuration
    digitalWrite(segments[i], pow & bits?HIGH:LOW);
    pow = pow*2;

//turn off all the specified segments
void clearDigit(int segments[]) {
  for (int i = 0; i< 7; i++) digitalWrite(segments[i], LOW);

//turn on all the specified segments
void all(int segments[]) {
  for (int i = 0; i< 7; i++) digitalWrite(segments[i], HIGH);

In the setup phase the program setup the I2C library, the RTC library and the Arduino ports.

In the loop phase it reads the current time value from the RTC and “print” it.

The hour value is split into two digits, each digit is mapped into a segments configuration then translated in a byte value for the registry. Finally the byte value is sent to the correct registry.

The minute value is split also into two digits, each digit mapped into a segments configuration and then each Arduino port set consequently.

Generate a map-like navigable family tree

In this post I will show you how to generate a navigable map of your family tree starting from an image of your tree. You can see an example of what you will obtain here.


The problem

One of my hobbies during the free time is genealogy. I like doing research on my family and as a result of such activity I’m building a family tree.

When you start your genealogical research the family tree is small and easily printable or visualizable in a common size monitor. Often you like to share the results of your hard work with other family members, maybe using a web site were to show your beautiful tree.

If you’re lucky you start “collecting” more and more individuals, as consequence, the family tree grows and become an huge tree (or sometime what I call a forest). Putting that enormous image of the family tree in a web page starts to be problematic. First, the browser will have some difficulties loading such a big image, if not, in some cases, it crashes. Then the navigating would not be so easy, especially if you want to have a general idea of the tree or zoom in into an individual details.

Here the idea of make the tree navigable like a map. In this post I will try to show you the necessary steps to obtain such navigable tree map.

NOTE: All the instructions are for Ubuntu like distributions.

The solution idea

Have you never used Google Maps or other similar sites? Those sites lets you navigate big maps of continents, countries or cities in the browser simply dragging it with the mouse pointer.

How they make it possible? To make it simple, they split the huge image map into small pieces, called tiles. When you navigate an area of the huge big map the browser simply download and visualizes only the tiles for that area. This make the browser loading only a small part of the map. You can notice this mechanism when your internet connection is slow and you move fast in the map, you will see that the map is loaded by pieces.

You can also zoom in and out to different altitudes, this is done visualizing pieces from different “versions” of the map, a version with more or less details.

We will try to use the same mechanism for our tree. We will use a software to generate this pieces and to generate a demo web page where navigate our tree.

Obtaining the huge tree

Let’s start obtaining our family tree. You can produce it using a genealogical software or by hand, if you are brave enough.

I prefer to use Gramps and his Descendant tree report. The report tool produces a family tree starting from the active individual. The report can be saved in different formats, but only SVG for image types. I like to use the PDF format and then convert it to image.

For the post I’ve used the Royal92 gedcom file, that as the site says “Features the Kings and Queens of Europe, including Great Britain.”. The report has been generated from the individual Stewart Matthew (I1507) and saved as PDF. In order to obtain a tree with his natural dimension and not stretched I’ve checked the Resize Page to Fit Tree Size checkbox in Size tab. You can download the generated file here.

Finally I’ve converted the file using the convert command from ImageMagick:

convert mytree.pdf mytree.jpeg

The conversion produced an image of 5635×32466 pixels. You can download the image here.

Tools installation

In order to generate the map we need GDAL. It “is a translator library for raster and vector geospatial data formats“.

To install GDAL you need to add the GIS PPA repository to your source repositories:

sudo add-apt-repository https://launchpad.net/~ubuntugis/+archive/ppa/

Then install GDAL bin and GDAL python support:
convert mytree.pdf mytree.jpeg

Now test if everything is installed running:
gdal2tiles.py --version

It will reply with the version installed.

Navigable map generation

Now we are ready to generate our navigable family tree. To do so, we will use gdal2tiles.py. The tool takes an images as input and generates an HTML page, with all the necessary scripts, and the image tiles to make our tree navigable.

First create a destination folder:

mkdir mytree

Then run the tool:
gdal2tiles.py -p raster tree.jpeg mytree

The execution will requires a little bit showing a progress like this:
Generating Base Tiles:
0...10...20...30...40...50...60...70...80...90...100 - done.
Generating Overview Tiles:
0...10...20...30...40...50...60...70...80...90...100 - done.

When it completes the generation, you can open the openlayers.html file create inside the mytree folder with the browser. You can navigate the map produced with the example family tree here.

You can tune the result with the tool options like the numbers of zoom level to produce or the resampling method. The produce map has a little border that appears when it is zoomed out, looks like the tool requires particular sizes for input image. More information here.

You can easily integrate the obtained map on your family site.

I hope you liked the proposed solution. Feel free to comment or add suggestion as post comment.

Raspberry Pi talks to Arduino using Dart


This post will show you how a Dart program running in a Raspberry Pi board can communicate to an Arduino board.

To show it we will send a text message from a Dart program running in a Raspberry Pi and we will visualize it in an LCD controlled by an Arduino board.


The main components are:

  • A Raspberry Pi Model B revision 1.0 running Raspbian 2014-09-09
  • An Arduino Duemilanove
  • A 16×4 LCD
  • breadboard, wires and cables

The Raspberry Pi board is connected to the Arduino board through an USB cable.


The LCD is wired to the Arduino board following the schema proposed in the Liquid Crystal Library example.



For the Arduino side I wrote a simple sketch using the Liquid Crystal library.

In the loop block the sketch reads a String from the USB and writes it to the LCD. The sketch replies to the Raspberry Pi board with an “Hello Pi!” message.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  //communication setup

  //LCD setup
  lcd.begin(16, 4);

void loop() {
  String read = Serial.readString();
  if (read.length()!=0) {
    Serial.print("Hello Pi!");

For the Raspberry Pi side I wrote a Dart program that sends a message to the USB port using the serial_portal package.

The program initializes the USB port and sets a listener for the incoming bytes in order to write them in the console. After the setup it opens the USB port and sends the “Hello Dart!” message.

import 'package:serial_port/serial_port.dart';
import 'dart:async';
import 'dart:io';

  var arduino = new SerialPort("/dev/ttyUSB0");
  arduino.onRead.listen((List<int> bytes){bytes.forEach(stdout.writeCharCode);});
  arduino.open().then((_) {
    new Timer(new Duration(seconds:3), (){
        arduino.writeString("Hello Dart!");
        print('sent "Hello Dart!"');

To run the program in the Raspbian environment I had to compile the Dart SDK.

In the screenshot you can see the Dart program running in the Raspbian terminal.



Here the source code: Dart-RaspberryPi-Arduino.zip

How to build the Dart SDK for the Raspberry Pi

For a DIY project I’m working on I need to run a Dart project on my Raspberry Pi board, precisely in his Raspbian OS. The Dart team has done a good job publishing a wiki page with all the information to build the SDK. Here I’m summarizing the necessary steps in a handy list, or, if you prefer, at the end of the post you can find a zip file with the SDK already built.


I suggest you run the entire process in a virtual machine in order to have a clean environment and the best compatibility with the provided scripts.

  • Download the Ubuntu server 12.04.5 64-bit ISO.
  • Install your virtualization software, I’ve used VirtualBox.
  • Create a new Virtual Machine. In VirtualBox using the wizard, select Linux OS and Ubuntu 64 bit. Here’s my VM configuration:
    • 4 GB ram
    • 23 GB HD, the used space after the entire process is about 11 GB
    • 2 processors (the core available in my hosting machine)
    • TCP ssh port forwarding (My host OS is a Linux with sshd active)
  • Install Ubuntu server through the downloaded ISO. During the configuration, in the package selection phase, select the OpenSSH Server, we will use scp to copy the produced SDK.


Login in Ubuntu and create a working directory. All the following commands are relative to the created working folder.


Download and run the script that installs all the necessary dependencies.

$ wget http://src.chromium.org/svn/trunk/src/build/install-build-deps.sh
$ chmod u+x install-build-deps.sh
$ ./install-build-deps.sh --no-chromeos-fonts --arm
$ sudo apt-get install libc6-dev-i386 g++-multilib


Download and setup the source code management tools provided for chromium.

$ svn co http://src.chromium.org/svn/trunk/tools/depot_tools
$ export PATH=$PATH:`pwd`//depot_tools


Download the Dart 1.7 source code (if you want the latest developed version use this url: http://dart.googlecode.com/svn/branches/bleeding_edge/deps/all.deps)

$ gclient config http://dart.googlecode.com/svn/branches/1.7/deps/all.deps
$ gclient sync


We are going to cross compile the code so we need the tool chain provided by Raspberry Pi.

$ git clone https://github.com/raspberrypi/tools.git

SDK compilation

Finally we are ready to compile. In the meanwhile you can go to take a coffee.

$ cd dart
$ ./tools/build.py -m release -a arm --toolchain=../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf create_sdk


Copy the dart-sdk folder under the dart/out/ReleaseXARM folder to your Raspberry Pi machine (using scp or your folder explorer).

If you like to make the Dart commands available in the command line edit your .bashrc file in your Raspbian installation and add these lines (modify the dart-sdk path based on your installation path):

export DART_SDK=/home/pi/dart/dart-sdk
export PATH=$DART_SDK/bin:$PATH
export PATH="$PATH":"~/.pub-cache/bin"


You can check if the dart installation is working by simply running it. You should see this output:

pi@raspberrypi ~ $ dart
Usage: dart [<vm-flags>] <dart-script-file> [<dart-options>]

Executes the Dart script passed as <dart-script-file>.

Common options:
--checked or -c
 Insert runtime type checks and enable assertions (checked mode).
--help or -h
 Display this message (add -v or --verbose for information about
 all VM options).
--package-root=<path> or -p<path>
 Where to find packages, that is, "package:..." imports.
 Print the VM version.


As final step let’s run a simple Hello World project. We can create such simple project quickly using the new Stagehand tool. First we need to install it:

pi@raspberrypi ~/dart/projects $ pub global activate stagehand

We are now ready to create the project:
pi@raspberrypi ~/dart/projects $ mkdir HelloWorld
pi@raspberrypi ~/dart/projects $ cd HelloWorld/
pi@raspberrypi ~/dart/projects/HelloWorld $ stagehand consoleapp

The project is ready, let’s get hits dependencies and run it:
pi@raspberrypi ~/dart/projects/HelloWorld $ pub get
pi@raspberrypi ~/dart/projects/HelloWorld $ dart bin/main.dart
Hello world: 42!


If all these steps scares you, then you can download the zipped folder with the Dart SDK that I’ve prepared. You’re downloading it and using it at your own risk.

Plugin mechanism for GWT application

Cotrix, the RIA I’m working on, is packaged differently depending on the target environment. For each environment we want some features enabled or not, both client and server side. From the client POV I’ve to enable or disable some GWT components, I will call those components plugins. In our case each plugin is bounded in a separate Java library.

In GWT to enable a component you need to:

  • include it in the module file:
    <inherits name='com.google.gwt.user.User'/>
  • call it somewhere.

The first point can’t be automatized, or you can but you need to manipulate the module file during the packaging phase. This can be done using Maven facilities, but I will avoid it. Each packaging in the Cotrix application has his own GWT module, all of them inheriting from a common super module. The module for a specific environment adds the components (plugins) that I want to add.

The second point is a little tricky. Let’s assume that all the plugin implement a Plugin interface:

public interface Plugin {

	 * Returns the plugin name.
	 * @return the plugin name.
	public String getName();

	 * Activates the plugin.
	public void activate();


Once we have all the required plugin included in the main GWT module we need to discover them. In GWT you can’t use reflection or Service locator mechanism. With GIN I don’t have found any way to retrieve all the interface implementations (as you can do win CDI).

Therefore I’ve decided to go with a generator solution. In this solution the generator will be responsible to collect all the Plugin interface implementations and to generate the code that will instantiate them.

First I’ve declared a PluginProvider which will return all the Plugin instances:

public interface PluginProvider {

	 * Returns all the plugin instance.
	 * @return the plugin instances.
	List<Plugin> getPlugins();


Then how to retrieve the Plugin interface implementations? One way is using reflections during the compilation phase. Another one is to use the module configuration properties. A GWT module can declare a configuration property, both single value and multi-value. A third module can extend the property adding it more values.

I’ve used the property mechanism in order to let each module containing a Plugin implementation to extends a particular property with the Plugin implementation class name. The code generator will read this property values in order to know the Plugin interface implementations.

In the main GWT module we will declare the property used to declare the plugins:

<define-configuration-property name="plugins" is-multi-valued="true" />

Then each module containing a Plugin interface implementations will extend the property with the name of the implementation class:
<set-configuration-property name="plugins" value="org.gwtplugin.client.MyFirstPlugin"/>

Let see the PluginProvider generator code:
public class PluginProviderGenerator extends Generator {

	private static final String PROPERTY_NAME = "plugins";

	public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {

		try {
			JClassType classType = context.getTypeOracle().getType(typeName);
			String packageName = classType.getPackage().getName();
			String simpleName = classType.getSimpleSourceName() + "Generated";
			PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
			String name = typeName + "Generated";

			if (printWriter == null) return name;

			ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName);


			SourceWriter src = composer.createSourceWriter(context, printWriter);

			List<String> plugins = getPlugins(context.getPropertyOracle());

			src.println("public List<Plugin> getPlugins() {");
			if (plugins.isEmpty()) src.println("return Collections.emptyList();");
			else {
				src.println("List<Plugin> plugins = new ArrayList<Plugin>("+plugins.size()+");");
				for (String plugin:plugins) {
					src.println("plugins.add(new "+plugin+"());");
				src.println("return plugins;");



			return typeName + "Generated";

		} catch (NotFoundException e) {
			throw new UnableToCompleteException();

	private List<String> getPlugins(PropertyOracle propertyOracle) {

		try {
			ConfigurationProperty propertyValue = propertyOracle.getConfigurationProperty(PROPERTY_NAME);
			return propertyValue.getValues();

		} catch (BadPropertyValueException e) {
			// intentional
		return Collections.emptyList();

The generator retrieves the “plugins” property values, then use each value to instantiate the Plugin implementation, returning it in a list.

In the main GWT module we need to declare the generator :

<generate-with class="org.gwtplugin.gen.PluginProviderGenerator">
	<when-type-assignable class="org.gwtplugin.client.plugin.PluginProvider" />

Finally we will use the generated PluginProvider in this way:
PluginProvider pluginsProvider = GWT.create(PluginProvider.class);

 * This is the entry point method.
public void onModuleLoad() {
	for (Plugin plugin:pluginsProvider.getPlugins()) plugin.activate();

I’ve created a sample project in GitHub: https://github.com/fedy2/gwtplugin