JavaScript and Embedded Systems: Are They a Good Fit?

Share this article

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?

Frequently Asked Questions about JavaScript for Embedded Systems

Why is JavaScript considered a good fit for embedded systems?

JavaScript is a high-level, interpreted programming language that is primarily used for web development. However, it has gained popularity in embedded systems due to its simplicity, flexibility, and wide range of libraries. JavaScript is event-driven, which makes it ideal for handling tasks in embedded systems that require real-time responses. Additionally, JavaScript’s ability to run on any platform that has a JavaScript engine makes it a versatile choice for embedded systems.

What are the advantages of using JavaScript for embedded systems development?

JavaScript offers several advantages for embedded systems development. Firstly, it is a high-level language, which means it is easier to learn and use than low-level languages. Secondly, JavaScript is platform-independent, allowing developers to write code that can run on any device with a JavaScript engine. Thirdly, JavaScript has a large community of developers, which means there are plenty of resources and libraries available to help with development.

Are there any limitations to using JavaScript for embedded systems?

While JavaScript offers many advantages, it also has some limitations when used for embedded systems. For instance, JavaScript is not as efficient as low-level languages for tasks that require direct hardware access or real-time performance. Additionally, JavaScript’s garbage collection can cause unpredictable pauses, which may not be suitable for systems that require consistent real-time responses.

How does JavaScript compare to other languages for embedded systems development?

Compared to other languages like C or C++, JavaScript is easier to learn and use, making it a good choice for beginners. However, it may not be as efficient or performant as these languages for certain tasks. JavaScript’s strength lies in its flexibility and versatility, allowing it to be used for a wide range of applications in embedded systems.

Can JavaScript be used for real-time embedded systems?

While JavaScript can handle real-time tasks due to its event-driven nature, it may not be the best choice for systems that require consistent real-time responses. This is because JavaScript’s garbage collection can cause unpredictable pauses. However, for applications that do not require strict real-time performance, JavaScript can be a viable option.

What tools are available for JavaScript embedded systems development?

There are several tools available for JavaScript embedded systems development. These include development environments like Node.js, libraries like Johnny-Five for robotics and IoT applications, and platforms like Tessel and Espruino that are specifically designed for JavaScript embedded development.

How does JavaScript handle concurrency in embedded systems?

JavaScript handles concurrency through its event-driven, non-blocking I/O model. This means that JavaScript can handle multiple tasks simultaneously without blocking the execution of other tasks. This makes JavaScript well-suited for handling tasks in embedded systems that require real-time responses.

What is the future of JavaScript in embedded systems?

The future of JavaScript in embedded systems looks promising. With the rise of IoT devices and the need for cross-platform development, JavaScript’s flexibility and platform-independence make it a strong contender for embedded systems development. Additionally, improvements in JavaScript engines and the development of JavaScript-based platforms for embedded systems are likely to further increase its use in this field.

Can JavaScript be used for low-level programming in embedded systems?

While JavaScript is a high-level language, it can be used for low-level programming in embedded systems to some extent. However, tasks that require direct hardware access or real-time performance may be better suited to low-level languages like C or C++.

Is JavaScript suitable for large-scale embedded systems projects?

JavaScript can be used for large-scale embedded systems projects, but it may not always be the best choice. For large projects that require complex computations, low-level languages may be more efficient. However, for projects that require cross-platform compatibility and flexibility, JavaScript can be a viable option.

Arno SlatiusArno Slatius
View Author

Arno Slatius is a web developer from the Netherlands. He has a background in electronics and embedded systems and started web development over a decade ago, He works with Yii professionally and loves digging into new things. Arno loves clean, commented and simple code. When he's not coding, he spends most of his time with his family or flying his model helies, planes and quads.

ColinIembedded systemshardware
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week