物联网项目的最佳 6 种编程语言
The top 6 programming languages for IoT projects
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.
It’s no surprise that what he calls an “actual computer” uses many of the same languages. In a survey of developers who write code for the Internet of Things (IoT) conducted in April 2016, the Eclipse Foundation discovered that Java, C, JavaScript, and Python were the top four choices for developers who are “building IoT solutions.”
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.
What we mean when we say 'IoT devices'
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.
Choosing a language for IoT
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.”
The Eclipse Project’s IoT open-source effort has been expanding, and there are now more than 20 different projects spanning millions of lines of code. Much is in Java, but there’s plenty in Python, JavaScript, C, and other important frameworks.
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.
Java
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.
There are multiple open-source projects, such as Pi4J and BlueJ, that show how the embedded version of Java runs well, even on chips that seem limited.
C
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).
Python
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.
JavaScript
While many still think of JavaScript as a language for popping up alert boxes on web pages, the language’s relatively newfound popularity on the server makes it a surprisingly popular choice for IoT applications. A full 41.8 percent of the developers in the Eclipse survey chose JavaScript, and 31.5 percent indicated that they were using Node.js in their projects.
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.
But even if most of the Node.js code runs on larger machines, there are some efforts designed to bring it to smaller ones. Espruino and Tessel are two examples of microcontrollers that run JavaScript from the beginning. Tessel, for instance, is built around Node.js, making it easy for web developers to move into the IoT without learning a new language.
Swift
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.
PHP
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?
So what languages are you using for IoT projects? How many tools on our list of 67 IoT resources are you using?
网友评论