JavaScript and Embedded Systems: Are They a Good Fit?

Arno Slatius

Embedded systems are everywhere. They run your dishwasher, control the temperature in your home, make sure the engine of your car runs correctly, and help you brake in case of emergencies. An embedded system is a combination of hardware and software that perform a very specific, predefined task. The electronics themselves are usually encapsulated within the device it controls. They’re usually equipped with sensors and actuators to perform their task. Some have a user interface, but most don’t, or perhaps only a few LEDs to tell what it’s doing.

Embedded System Capabilities

The processing power of the embedded processor is usually dependent on the tasks they have to perform. The processor can vary from simple 8- or 16-bit processors (families like PIC, AVR, MSP and H8) to more powerful 32-bit systems (like ARM Cortex or SuperH). ARM cores are getting more and more popular because of the lowering prices, and the simple processors are mostly interesting for high volume products. Their clock frequencies are usually not very high – somewhere between 8 to 25 MHz is quite common, although the 32-bit systems quite often run at higher speeds. Of course there are some applying even higher power processors like the ARM7/9 or MIPS at +500MHz.

The list of embedded operating systems is long, but quite often embedded systems don’t use an OS. A simple ‘task scheduler’ is usually enough because their tasks are relatively simple and they don’t need a real OS with all it’s overhead.

The question today is: what can we do with JavaScript on these embedded systems?

A Few JavaScript Enabled Options

There are some projects which aim to program embedded systems using JavaScript. We’re all JavaScript lovers here, and we can see some of the benefits of course. These systems all implement a JavaScript interpreter and supply an API for the hardware specific functions.

The first system I want to mention is Tessel. It consists of a base board that can be extended with hardware modules that you can buy through the website. The base board is well equipped; it has a WiFi module and plenty of memory available. The extension modules are very interesting as well – camera, bluetooth LE, GPRS, micro SD, and quite a few sensors. The only unfortunate thing is that most are out of stock or pre-order. Don’t let that spoil the fun for you though. Their documentation looks very good and there are plenty of code examples to choose from. Another plus is that package management is done with npm.

Then there is the Espruino Board. Like Tessel, it is powered by a CortexM3 processor which will give you excellent performance. The main difference with this project is that there are no extension boards available. However, you can connect things to the system by wiring up electronics. This may sound like a negative point, but it isn’t necessarily. There are quite a few libraries available for specific ICs. They are mostly for sensors and displays though. Memory might be an issue on this system because you’ll have to use the processor’s embedded RAM.

Is JavaScript the Right Choice?

I feel the need to ask this question – is it a good choice to run JavaScript on an embedded system? If you’re a hobbyist, or want to experiment with technology, and JavaScript is your thing, I’d say go for it. Both projects have potential and possibilities for creating interesting applications. However, if you’re going for a professional solution, I’d say: NO (in capital letters, indeed). An embedded system should be dependable and predictable. There is no way to guarantee correct timing and response times with code that runs on an interpreter. Memory management is another thing which may be quite critical in a system with little memory.

JavaScript and a Web Interface

JavaScript is a perfect way to give an embedded device a web interface. JavaScript can use the power of the web browser to create an experience that the device itself could never produce. To be able to serve anything, at least two conditions must be met:

  1. The system will need to have some sort of network connection.
  2. The system must be able to accept and respond to HTTP requests over that network connection. The HTTP request can, of course, be proxied by some other system.

Most embedded processors have a family member that embeds an Ethernet controller, or an interface to an external one. Most embedded operating systems have TCP/IP available as a library. In other cases the lightweight TCP/IP stack is a good option which can be integrated quite easily. The contributions include a HTTP server example. It might also be interesting to see if WebSockets are available, or can be implemented on the platform.

The interaction between the system and the browser is best done using Ajax calls. They are relatively simple, and the response from the server can be simple as well. This reduces the amount of work that the embedded device has to do. All it has to do is parse the requested URL with parameters, set a value in its configuration, or return requested values.

We can implement this using one of the many popular JavaScript libraries like jQuery, AngularJS, etc. Just build basic pages and let the library do the request handling to fill in the value and handle the forms. If there is more to show, then a more structured approach would probably be to create an application that uses a MVC approach. Angular would be my pick for that.

Other Considerations

Security is a concern. Most embedded systems don’t have a lot of libraries and power to run SSL/TLS, so an important question should be, is that acceptable. Next, you’ll probably want to do authentication. Ravi Kiran wrote a a very nice article here on SitePoint about implementing authentication in AngularJS, which I recommend should you need it.

If possible you’ll also want to serve the application from the embedded system. If you don’t you’ll run into problems with the Same-origin policy. If the system only has limited space available, or the web application is large, you can use some sort of content delivery network for it. Host only the initial HTML file on the system and fetch the JavaScript, CSS and images from the CDN. This also allows you to update your web application more easily later on.

Conclusion

You can definitely run your embedded system on JavaScript. The hardware of the projects that I mentioned looks very promising. There are a lot of possibilities with that. What are your experiences and thoughts on embedded systems and JavaScript?

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.