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- | sh

After the Docker installation is complete we checkout the framework code:
git clone

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

and run the build process:

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.

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 –

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 =;

  //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.

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);}); {
    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:

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
$ chmod u+x
$ ./ --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
$ export PATH=$PATH:`pwd`//depot_tools


Download the Dart 1.7 source code (if you want the latest developed version use this url:

$ gclient config
$ gclient sync


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

$ git clone

SDK compilation

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

$ cd dart
$ ./tools/ -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.