Anything that can be written in JavaScript will eventually be written in JavaScript.
- Atwood's Law
What can JavaScript , the most popular programming language, do? ", we listed the use of JavaScript in different fields. Today, let us explain the application of JavaScript in the Internet of Things in detail.
Fundamentals: The Three Layers of IoT
Before we start, let's briefly introduce the basics of IoT. If you have some web development experience, you know that the following figure is the CS architecture:
Compared with an IoT system, it is nothing more than an additional layer of hardware and an optional coordination layer.
This hardware layer determines that IoT applications are more complex than web applications. For most web applications, the clients are devices such as mobile phones, computers, and tablets, all of which have powerful processing capabilities and do not need to consider some additional factors.
For IoT applications, we need to consider the processing capability of the MCU on the device, and use different communication protocols according to its processing capability and usage environment. For example, we need to use the CoAP protocol on some devices. Some devices do not have network functions, and need to consider the assistance layer that can be networked, and also need to use some short-range wireless transmission protocols, such as low-power Bluetooth, infrared, Zigbee, and so on.
An IoT System: Six Languages
Two and a half years ago, in my senior year of electronic information engineering, my graduation thesis was a thesis on the Internet of Things - "The Design of the Minimal Internet of Things System Based on REST Services". This is an entry-level IoT paper, and if most people who learn CS have a little hardware foundation, they can write such a paper.
This paper is a "minimization" of the previous work papers that participated in the competition. The main thing used in it is to create a RESTful service, and it is not even a technology. In this work:
- We use the Django framework in the Python language as a web service framework, and use the Django REST Framework to create RESTful services.
- In order to use the phone as a controller, we also need to write an Android application in Java.
- We use the Raspberry Pi as the coordination layer on the hardware side to connect to the network and transmit control signals to the hardware.
- We use Arduino as the controller on the hardware side, and it's super easy to write code that allows us to focus on the business.
- Finally, we also need to make a graph on the web page to display real-time data.
For all these, we need to use five languages: Python, Java, JavaScript, C, Arduino. And if we want to write the corresponding iOS application, we also need Objective-C.
![Are you kidding me? ]( http://articles.phodal.com/js-iot/6359758744428735171956612167_are-you-serious-wtf-meme-baby-face.jpg )
The development of JavaScript in the field of IoT
Similarly, more than two years ago, just as an intern, in our project, we used Backbone as the core of the single-page application framework to build a web application in our new project. At this time, I started to pay attention to the possibility of implementing IoT applications with Node.js.
At that time, there were already libraries for the MQTT and CoAP protocols of the Internet of Things, so I wrote an Internet of Things that supports HTTP, CoAP, WebSocket and MQTT. Due to the lack of some large-scale application development classics at that time, it was not very good, but we can already see the vision of JavaScript in this regard.
More than a year ago, when Ionic had not released the official version, I found that this framework is really good - it comes with a series of UI, and also integrates a series of Cordova plugins with NgCordova. I started using Ionic to write some mobile apps and found it to be quite easy. Then, I started to use this framework to try to write IoT applications, which required some native plug-ins, such as BLE and MQTT. Later, I also wrote a simple CoAP plugin.
Later we no longer need to compile Node.js, and we can run Node.js on ARM processors. And we already have development boards like Tessel, Espruino, Kinoma Create, Ruff that can run JavaScript directly. Samsung also launched iot.js, which allows more embedded devices to use the JavaScript language as a development language.
There are many reasons why people start using JavaScript on hardware, such as the web has the most developers and JavaScript is easy to learn.
Now, this time we can use JavaScript on all three levels, just one language.
Develop IoT applications in one language: JavaScript
In the book I wrote, Design Your Own Internet of Things, I tried to demonstrate the power of JavaScript in this regard. Using Node.js + Node-CoAP + MQTT.js + MongoDB + Express to build a multi-protocol IoT:
However, the picture above is a complete version of the Internet of Things, and the code is naturally on GitHub: Lan . As a server, the capabilities of Node.js have been verified. On the hybrid application, it can still stand the test. It is easy for the hybrid application to make a chart on the mobile phone (just get the data and display it):
It is also easy to be a control side (we only need to send a POST request, the more logical thing is to get the status first):
On the hardware side, I didn't use JavaScript as an example to show the usage of JavaScript in the book, because it would limit the user's hardware device.
However, we can still use a library like Johnny-Five to do hardware programming, it's just not as fun~~.
Why should we have a cup of coffee and wait for it to compile in C when we can do it in JavaScript?
The answers you want to know are all in this book, which has been listed on Amazon, JD.com, and Dangdang:
Amazon: https://www.amazon.cn/dp/B01IBZWTWW
Jingdong: http://item.jd.com/11946585.html
after all:
Anything that can be written in JavaScript will eventually be written in JavaScript.