Raspberry Pi talks to Arduino using Dart

RaspberryPi-Dart-Arduino

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.

HARDWARE SETUP

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.

RaspberryPi-Arduino-LCD-schema

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

LCD_bb

 SOFTWARE

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.

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.

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.

RaspberryPi-Dart

 

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.

MACHINE setup

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.

READY TO START

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

DEPENDENCIES INSTALLATION

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

DEPOT TOOLS INSTALLATION

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

SOURCE CODE DOWNLOAD

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)

TOOL CHAIN DOWNLOAD

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

SDK compilation

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

SDK INSTALLATION

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):

READY TO RUN

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

LET’S RUN HELLO WORLD

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:

We are now ready to create the project:

The project is ready, let’s get hits dependencies and run it:

THE FAST WAY

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.
dart-sdk-raspberry-pi-1.7.2