Firmata Remote Device Control in the Moddable SDK
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 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
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
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.
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.