Every day, there are more devices everywhere connected to the Internet, waiting patiently for our commands. How can we order them around? Which language do they speak?
The simple answer is that "smart" things use many of the same languages as desktops, in part because their similarities with desktop computers continue to grow. “A Raspberry Pi is an actual computer now,” said Ian Skerrett, the vice president of marketing for the Eclipse Foundation.
That’s not much different from the top languages used for writing old desktop apps and servers. But while the simple answer is that smart things aren’t much different from desktops and servers, the complex answer is that there are important differences between all of the things that make up the IoT.
Skerrett divides the IoT architectural environment into three major sections: the sensors that create the data, the hubs or local gateways that organize it, and the geographically distant, centralized servers that collect the data.
“If you’re writing for a sensor on bare metal, you’re probably using C,” said Skerrett. The ability to work directly with the RAM makes C one of the first choices of hardware developers.
The rest of the machines in the chain, though, are as familiar as desktop computers, from the developer's perspective. Thus, developers are able to use the language that’s familiar to them.
The hubs may be a smartphone or a small console, but they are just general-purpose machines inside. They usually run a standard operating system and often feel no different from a big machine, at least when communicating via the command line.
There’s no reason why they can’t choose the language in the same way they do for a desktop project. If a Raspberry Pi is running Linux, it's behavior is not that different from a desktop.
By the time the debate on languages makes its way to the servers, there’s no difference there, either. They speak with the hubs and sensors—usually with some kind of microservice architecture—then the data is pushed into standard databases.
While the usual suspects of popular languages dominate the IoT space already, the Eclipse survey found at least 14 different languages that were mentioned by 5 percent or more of the developers.
“Our Internet of Things effort at Eclipse is polyglot,” said Skerrett. “It’s not one language.”
Here are some of the top choices that are being used to build the foundations of the next generation of things connected to the Internet.
The top choice of the Eclipse survey and another survey by embedded-computing.com was Java, a result that’s not surprising for a language still known for being “write once, run anywhere.” The original project was aimed at set-top boxes, one of the first domains for non-desktop computing.
Java’s advantages are well known. Developers can create and debug code on their desktop and then move it to any chip with a Java Virtual Machine. That means the code can run not just on places where JVMs are common (servers and smartphones), but also on the smallest machines.
Java ME, or micro edition, has been available on small phone handsets and other embedded devices since the specification was approved in 2000. It saved space with a very limited collection of class libraries and other tools. Today, most of the focus is on Java SE Embedded, which is much closer in capability to the Standard Edition. Developers can use the latest features of the Java 8 platform and then move their code to a smaller, embedded device.
Most of the computing resource savings with Java SE Embedded comes from stripping out the classes needed to display information when the machines can be configured to run headlessly, without a monitor or keyboard. All of the communication goes through the network.
The syntax is cluttered with punctuation, and there are a million different little mistakes you can make, but the language is still the first choice for many programmers who write for the lowest layer of software, the one closest to the hardware. The language hides nothing from you, and that means you can fiddle with every part of the code to squeeze out the best performance from an underpowered device. Every bit can be flipped. Every value on the stack is available. Just don’t make a mistake, because there are few safety nets.
Victor Berrios is the chief technical officer at the ZigBee Alliance, a group that supports the ZigBee standard, which links small devices. “From what we see in the market, C remains the language of choice for constrained devices,” he said. “These typically do not include a 'commercial'-level OS but rather a basic task-scheduler type of resource management also coded in C.”
More advanced or bigger devices with full operating systems still use plenty of C code, he added, but he said that other languages such as Java are starting to be used just as frequently. When a smartphone comes from Apple, much of the programming is still done in Objective C, but this will probably be gradually replaced by Swift (see below).
It started as a scripting language to glue together real code, but it’s increasingly used as the main language for many developers. When small devices have enough memory and computational power, the developers are free to choose the language that makes their life easier and that is more and more often turning out to be Python.
Kinman Covey, a microcontroller developer, says that Python is both easy to learn and supported by a large, helpful community. The syntax is clean and simple, attracting a greater range of programmers. The language is often the first choice for social scientists and biologists, for instance. When they need a smart device in the lab, they’re happy to use a language they know, Python.
“Python is the language of choice for one of the most popular microcontrollers on the market, the Raspberry Pi,” said Covey. Much of the training literature is written in Python, and many schools use the platform to teach computer programming. If the project is relatively simple and there are no great computational demands, it’s possible to build effective tools from the same boards and libraries that are used in elementary schools.
There are also versions designed to be even smaller. The MicroPython board and software package is a small microcontroller optimized to run Python on a small board that’s only a few square inches.
Much of this work is focused on the servers and gateways or hubs that gather the information and then store it. The smaller smart hubs and sensors that run Linux can usually run Node.js.
While Swift is still mainly used to build applications for Apple’s iOS and macOS devices, the preponderance of these machines means that it’s often part of the IoT stack. If you want your things to interact with an iPhone or an iPad, you’re probably going to want to build the app in Swift (or perhaps its predecessor, Objective C).
There are other good reasons to work in this space. Apple wants to make its iOS devices the center of the home network of sensors, so it’s been creating libraries and infrastructure that handle much of the work. These libraries are the foundation of its HomeKit platform, which provides support for integrating the data feeds from a network of compatible devices. This means you can concentrate on the details of your task and leave much of the integration overhead to HomeKit.
This language may be the first choice of bloggers and website prototypers, but it is also surprisingly popular in the IoT. After the big languages and their cousins such as C#, PHP is the one language that is mentioned the most often by developers in the Eclipse survey; 11.2 percent say they are including PHP code in their stack.
While the code’s role on the server to juggle microservices is an obvious application, it is also finding some traction at the lowest level. A number of Raspberry Pi developers are talking about starting up a full LAMP stack with Apache, MySQL, and PHP running on top of Linux. They are effectively inverting the paradigm and turning the lowliest thing on the Internet into a full web server.
If a Raspberry Pi has enough spare cycles—and it often does—then putting a LAMP stack on the chip makes development simpler. All of the server-side code developed over the last 20 years can find a home in a small sensor. It may seem like a toy next to the full-strength, pointer juggling machismo of C, but if it gets the job done quickly and makes the boss happy, why not use it?