Wiot, a distributed IoT operating system based on NodeJS.
# Project Description
## The original intention of the project
This project aims to help web developers who are not familiar with firmware programming to implement their own IoT applications.
When developing traditional Internet of Things programs, we need to write, burn, and debug each development board again and again, and connect them in a certain way to provide a certain service. This method is very cumbersome, and the development cost and threshold are relatively high. In addition, the ancient C and C++ languages lack mature dependency management tools, so that it is difficult for us to write development boards as easy and convenient as developing python or nodeJS programs.
This project is trying to provide a method to automate the process of burning and debugging the development board, and move the steps of logic writing to the NodeJS interface of the remote computer. In this way, we are able to centrally manage the functions of distributed IoT nodes. In addition, with NodeJS's mature dependency manager npm, the development of IoT applications is more convenient and efficient.
So far, down to every IoT node, up to various types of databases and user terminals, all can be configured and operated on the same nodeJS interface. Developers will be able to focus on the development of IoT logic and realize more colorful IoT applications.
# First round of development
> The first round of development was completed in May 2019, please refer to [this blog](https://www.eee.dog/tech/wiot.html)
Realize the pin port mapping of D1 MINI development board to nodeJS, and support a series of functions such as ota.
By providing standardized microcontroller firmware and automated burning tools, wiot abstracts, organizes, and maps the pin port functions of each development board into a callable NodeJS interface.
As shown in the figure above, the wiot architecture mainly consists of three parts: Node node, wiot Cloud Module, and wiot cli command line program. Among them, the Node node is the NodeMCU microcontroller with the wiot firmware burned. They use IEEE 802.11 WiFi for distributed communication, and connect to a router at a node to connect to the wiot cloud module. Users use NodeJS to call Wiot on their own PCs and rely on writing IoT programs. After completion, the user uses wiot cli to check, test and deploy the developed program. The deployed IoT program will run on the wiot cloud module, and can use the nodeJS interface to interact with other cloud services or databases.
### Node node
Node node uses NodeMCU development board (expansion board based on ESP8266). After using wiot-cli to burn the wiot firmware, NodeMCU will become a wiot node.
#### **Mode**
Wiot nodes have four modes: master mode, slave mode, bridge mode and debug mode. This mode can only be configured when burning firmware. In master mode and bridge mode, nodeMCU will enable STA and AP mode at the same time. In slave mode, only STA mode is enabled. The master needs to have a stable and unlimited power supply, which has a large power and is not suitable for low power consumption scenarios. Bridge and slave can flexibly adjust power, which can be used in low power consumption scenarios.
In debug mode, the node works in STA and AP mode. Users can use mobile phones and other devices to connect to this node and open the root directory webpage. The web page will display various information about the current location of this node in real time, such as the signal strength received from other nodes, which is suitable for deployment scenarios.
#### **Low power**
Wiot nodes support Low Power state. But this state can only be opened in slave mode and bridge. This state can be turned on or off through wiot api or wiot-cli. The low power consumption state is realized by the sleep function of esp8266. In this state, it can be powered by dry batteries for several years [[1]](https://makecademy.com/esp8266-battery).
#### **Networking**
Wiot nodes use distributed networking. Where the router signal is strong, the master node will access the router WiFi network and bridge it. Where the router signal is not strong, each node will find another upstream master or bridge node with the strongest signal and bridge it (except slave).
As shown in the figure above, the two master nodes close to the router are automatically connected to the router wifi. The other downstream nodes are respectively connected to the upstream node with the strongest signal.
#### **Certification**
![Radio frequency fingerprint identification[2]](https://api.yimian.xyz/img/?path=imgbed/img_d184ff2_1442x502_8_null_normal.jpeg)
Wiot nodes support the use of **RF Fingerprint Recognition**[[2]](https://ieeexplore.ieee.org/document/9149584) to achieve downstream communication authentication. This method uses the RSSI function of esp8266 and scan return parameters. When the downstream device attempts to access the network, the upstream device will collect fingerprint information and report to the server. The server compares the fingerprint with the previous version stored in the database and makes a decision whether to allow access to the network.
The cloud module acts as the cloud part of Wiot and provides cloud functions. The cloud module can guard the wiot program running in the run-time state. In addition, the cloud module can also provide functions such as testing and deployment. The cloud module uses Docker for virtualization.
### Command Line Program
That is wiot-cli. It is a set of operation tools provided by Wiot on the developer terminal. Through wiot-cli, developers can implement functions such as testing, deploying, and restoring wiot programs. In addition, wiot-cli also supports a series of common functions such as debug. See the `wiot-cli command` below for details.
## User operation steps
1. The user downloads, installs and configures the NodeJS environment.
2. The user runs the command `npm i -g wiot-cli` through the command line to install the wiot command line tool
3. The user runs the command `wiot init my-wiot-proj` to create and configure a wiot project folder in the current directory, the project name is my-wiot-proj. At this point, the user will be prompted to fill in some information about this project, such as project description, author information, WiFi account password, wiot cloud module address, project main script, etc.
4. The user enters the project directory, connects the nodeMCU to the computer through the data cable, and runs `wiot node init my-first-node [--type=<master|slave|bridge|debug>]`. This will automatically configure and burn the Wiot firmware to the nodeMCU development board and name it my-first-node. type is an optional parameter, which can be set to `master` or `slave` or `bridge` or `debug`, and the default is `master`.
5. Follow this method to burn all the development boards and deploy them in the production environment.
6. Run the command `wiot status` in the project folder to see if all nodes are online. If not, use the command `wiot log` with the `grep` tool to find the log location problem.
7. Start writing your wiot program from the main project script (default is `index.js`).
8. Use the command `wiot check` to check the logic of your wiot program.
9. Use the command `wiot test` to suspend the running program in the cloud module and run the program in the current project folder. At this point, all console output will be displayed in your command line window. Use `Ctrl+C` to terminate the current test and restore the previous program of the cloud module.
10. Use the command `wiot push` to deploy the local wiot program to the cloud.
## Other operations
### Deploy your own wiot cloud module
Install docker and run the command `docker run -d --name=wiot-cloud -p 3594:3594 -v /var/wiot/data/:/var/wiot/data/ iotcat/wiot`.
### wiot-cli command
-`wiot help [command]` view help
-`wiot clone <host:user/project>` to clone the project from the cloud module to the local
-`wiot pull` uses remote code to overwrite local code
-`wiot push` deploys the current folder wiot program to the cloud module
-`wiot ls` shows all current nodes
-`wiot map [nodeID]` shows the connection path of a node
-`wiot log [nodeID] [--head=<number>|--tail=<number>]` view the log of a node
-`wiot stop` stop wiot service
-`wiot start` to start wiot service
-`wiot restart` restart wiot service
-`wiot reload` restarts the wiot service without resetting the flash variable
-`wiot init <name>` creates and initializes the wiot project folder
-`wiot node init <node-name> [--type=<master|slave|bridge|debug>]` configure and burn nodeMCU development board
-`wiot check` checks the current folder project logic problem.
-`wiot test` will suspend the running program in the cloud module and run the program in the current project folder. At this point, all console output will be displayed in your command line window. Use `Ctrl+C` to terminate the current test and restore the previous program of the cloud module.
-`wiot status [nodeID]` displays current project or node status information
-`wiot debug start` terminates the current cloud program and enters debug mode
-`wiot debug set <nodeID> <pinID> <0-255>`Set the PWM output value of a node pin in debug mode
- In `wiot debug reset <nodeID>`debug mode, reset the data storage of a node and reconnect
-`wiot debug get <nodeID> <pinID>` get the PWM value of a node pin in debug mode
-`wiot debug sleep <nodeID> <pinID> <HIGH|LOW>`debug mode to set a node to lightly sleep until it is awakened by a pin's high or low level
-`wiot debug dsleep <nodeID> <millisecond>` set how many milliseconds a node deeply sleeps in debug mode
- In `wiot debug reconnect [nodeID]`debug mode, reconnect to the network or disconnect and reconnect to a node
### Common API
> To be designed, please refer to [One-round development wiot document](https://wiot.yimian.xyz/#/home)