Firmata Remote Device Control in the Moddable SDK

Firmata is a popular protocol for controlling remote hardware resources. It is often used to allow a more powerful device running a non-real time operating system, such as Linux, to control the hardware ports of a less powerful microcontroller device with a real-time operating system (or no operating system at all). The most common use of Firmata is with the Johnny-Five JavaScript robotics platform. A goal of the Moddable SDK is to do as much work as possible on a microcontroller using a JavaScript framework to eliminate the need for more expensive and more energy hungry hardware. Therefore, the common motivation for using Firmata -- to control a less powerful realtime device from a more powerful non-realtime device -- doesn't apply to the Moddable SDK. Instead, we were motivated to implement the Firmata protocol as a testbed for our work on a new JavaScript IO framework as part of our involvement with Ecma TC53, a standards committee focused on defining vendor neutral, multi-platform ECMAScript Modules for Embedded Systems.

This article is an introduction of the Firmata implementation work in the Moddable SDK. There is comprehensive documentation in the repository for those interested in learning more. The source code of the client and server is also available.

Firmata Context

Firmata itself is a communication protocol. The protocol is derived from the MIDI protocol, reusing the basic message framing mechanisms. However, the meaning of the messages is entirely different between Firmata and MIDI. Firmata, like MIDI, is designed to run over a relatively low-speed serial connection (57600 baud). However, it is not restricted to this. The Moddable SDK implementation supports both serial and TCP connections, and can operate at much higher speeds.

The Firmata protocol is a client / server protocol. The microcontroller is the server, responding to requests from the client running on the more powerful device. There are many more Firmata client implementations than server implementations. Most of the server implementations are written in C or C++ using the Arduino APIs. The client implementations are much more diverse, written in a variety of languages for different operating runtimes. The Moddable SDK implementation work has relied heavily on the Firmata JS client written for the Node.js environment and the Standard Firmata Arduino server.

Moddable's Firmata Implementation

The Moddable SDK now contains implementations of both a Firmata client and server. They are implemented entirely in modern JavaScript. The client even uses JavaScript private fields. The implementations use the proposed JavaScript IO design from Ecma TC53 to work with the IO ports and to communicate between the client and server. They support both serial and TCP connections.

Use of TC53 IO Class Pattern

The TC53 IO class pattern implementation in the Moddable SDK is available only for the ESP8266 at this time. Consequently, the Firmata implementation may only be used on ESP8266 boards at this time. For development work, the Moddable One board was used. It is expected that the TC53 IO APIs will be available for additional microcontrollers in the future, with the ESP32 likely next. As additional ports become available, these Firmata implementations should work more-or-less as-is.

The Firmata client API is simply the TC53 IO Provider API. This is the most complex IO provider yet implemented that provides this API to its clients. This work provided an opportunity to experience both what it is like to implement and to use the API with a non-trivial set of IO resources.

IO Kinds Supported

The Firmata protocol supports a wide variety of IO resources. No implementation supports all of the possibilities allowed by the protocol. The Moddable SDK implementation supports digital input, digital output, analog input, I2C, and serial for IO. Using the basic structure now in place, it should be straightforward to add support for additional kinds of IO.

Graphics Support Added

A key part of Moddable's mission to improve the IoT experience for users is to integrate screens into more products. The implementations of the Firmata client and server extend Firmata to support a small subset of the Poco graphics framework. The graphics capabilities themselves are simple, but give a sense of the potential power of including a remote rendering capability in Firmata. The Poco graphics framework has a JavaScript API that is used to implement the graphics rendering in the server. The client implements the same protocol, allowing a single API to be used for both client and server rendering.

The following video shows the Moddable implementations of the Firmata client running on a Moddable Three and server running on a Moddable One (with touch screen) communicating over Wi-Fi to perform basic interactive operations with remote rendering.

IO Efficiency

The Firmata implementations are designed to make efficient use of resources. They achieve that by applying certain features of the IO Class Pattern and the XS JavaScript engine. All classes are frozen which ensures they use no RAM beyond their instance variables and data buffers. Where allowed by Firmata, IO uses callbacks rather than polling. The overall runtime impact on an ESP8266 is small enough that Firmata could be one of several services provided by the microcontroller.


Whether or not this work evolves into a widely-used implementation of Firmata, the exercise of implementing a Firmata client and server using the proposed TC53 IO APIs has been valuable. It has led to several refinements in the API design and bug fixes in the Moddable SDK's implementation of TC53 IO for the ESP8266. It also provides a starting point for others to begin to explore. Contributions to improve and enhance the Firmata client and server are welcome. There are many additional IO kinds that could be supported, and likely some lingering bugs. Please share your experiences working with the code by opening an issue report in our GitHub repository.