README.md 10 KB
Newer Older
shastry's avatar
shastry committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# MEC-LORA

## Read me

Project Title: **Leveraging RED-Node to support LORA-based IOT applications**

Supervisor: 
- Adlen Ksentini

Students:
 - SHASTRY sanjeev prasad

This project is a continuation of an old project started by other students at EURECOM.
An appendix file is also given to define the use of tech we worked on.

### Project Definition
>Node-RED (https://nodered.org/) is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click. Node-RED is highly employed to design and deploy IOT applications using a web interface, with 0 line of code to write.
Meanwhile, most of the recent IOT deployment is using the LORA family protocols, as the latter enables to running over a low energy system that maximizes the IOT device lifetime. EURECOM has developed its own LORA network using a local Edge Computing platforms, which allows to deploy IOT applications at the edge and connected to LORA network. The expected work during the project is to leverage the RED-Node framework to enable the development and deployment of IOT applications compliant to EURECOM’s LORA Edge platform.

>Key words: IOT, LORA, REST API, Python, RED-Node

### Tech
This project uses a number of open source projects to work properly:
* [Mosquitto] - message broker
* [Flask Server] - lightweight WSGI web application framework
* [Chirpstack] - open-source LoRaWAN Network Server stack
* [Redis] - in-memory data structure 
* [Node-RED] - low-based development tool for visual programming developed

### Installation

##### Mosquitto
Mosquitto installation steps is expanded in [mosquitto_installation].
###### Prerequisites
- An Ubuntu 16.04 server with root access
- Open port TCP:1883 on firewall
###### Commands
#
```sh
$ sudo apt-get update
$ sudo apt-get install mosquitto
$ sudo apt-get install mosquitto-clients
```

##### Redis Server
Redis installation steps is expanded in [redis_installation].
###### Commands
#
```sh
$ wget http://download.redis.io/redis-stable.tar.gz
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make
```
##### Chirpstack
Chirpstack installation steps is expanded in the following youtube video [chirpstack_installation].
The project providers updated their project from lora to chirpstack so the installation files (name modifications) could be found in their official website found in the upper section but the project could also work with the previous version of Lora so far.
This helpful youtube video could be used: https://www.youtube.com/watch?v=Q0-7zG5uff8&feature=youtu.be
##### Node-RED
Node-RED installation steps is expanded in [nod_installation].


**What is Node-red?**
Node-Red is a flow-based visual programming language which is better suited for ease-of-use applications. Node-red works on a simple principle of designing a '.JS' file which includes the logic for the application and an 'HTML' file that defines how this logic is represented on the portal and later combining the two into a single '.json' package file to add to the Node-Red database.

**Why Node-RED?**
Let us look at our problem statement. We need to primarily design an interface which is easily understandable to the end-user to register devices into the LORA server. The programming in Node-Red is simple yet detailed. The interface is tailor-made even for a layman with no coding knowledge. Node-red is best suited for our project because we intend to reach a diverse audience through LORA setup and with Node-Red in place, the end-user just needs to drag and drop the node we created and rest will be done automatically by the backend system we have built. Through the below explanations, we look in-depth on to how our design is implemented.

###### Prerequisites
- Node.js
###### Commands
# 
With NPM:
```sh
$ sudo npm install -g --unsafe-perm node-red
```
With docker:
```sh
$ docker run -it -p 1880:1880 --name mynodered nodered/node-red
```
With snap:
```sh
$ sudo snap install node-red
```

For this project, the files wihtin the node-red folder should be downloaded to the specific path on the machine in a new folder (ex: test_node): 
```sh
~/.node-red/node_modules/
```
After each modifitcation of these files, you have to install the modifications again to the node-red by running the following command:
```sh
$ npm install
```
**How does the system work**

 The Node-Red is mainly two components - Javascript file and HTML file. 
1.    ***Javascript File***: Fundamentally defines what the node does. The node overall is wrapped as a Node.js module. The module exports a function that gets called when the runtime loads the node on start-up. In the file, a component is added to provide access to Node-Red API at runtime. 
In our case, we have defined a function - 'create_yaml' which contains our logic. The function is called with a single argument, 'RED' that provides the module access to the Node-RED runtime API. We then call on the createNode characteristic of Node-Red which brings in key features shared by all nodes. node.on 'input' in the code indicates that the program will run at each input. We have two different types of selection available for authentication which we leave it to the end-users choice - OTAA(Over The Air Authentication) and ABP(Activation by personalization). The major difference is that when OTAA is invoked, we take in both Application Identifier (AppEUI) and Application Key (AppKey) but the case of ABP asks only for Application Session Key (AppSKey) and the Network Session Key. OTTA performs an additional step of handshake which provides better security. We use javascript knowledge to hide ABP options when OTAA is selected and hide OTAA options when ABP is selected, for user convenience. 

2.    ***HTML File***:  Defines node properties and other HTML related components which are desired by the application. We use the basic HTML knowledge to design the desired properties.  We have to design a simple and yet efficient display which supports layman use. The HTML code attached is self-explanatory and accomplishes this task. 

**The set up in NODE-RED is as displayed in the diagram below. The design logic is implemented in three different stages:**
1. The Application flow
2. The HTTP Flow
3. The Flask server- Yaml interface
#### THE APPLICATION FLOW
shastry's avatar
shastry committed
107
![Flow1](Flow1.png)
shastry's avatar
shastry committed
108
109
110
111

The Flow starts with an input node which is connected to our custom Test Node. As the code is written to act upon each input, once an input enters the system, we send it to the Node we created. In the custom node, we take in the application data (details of fields found in the javascript explanation) and these pass this data as parameters in message payload to a create URL which now has localhost as the address with msg.payload attached at the end. Then we use the inbuilt Node-Red **Change** Node to set URL according to the payload received. Then this whole detailed setup is represented as an HTTP request and the message payload is available to the user for verification.

### The MQTT Flow
shastry's avatar
shastry committed
112
![Flow2](Flow2.png)
shastry's avatar
shastry committed
113

shastry's avatar
shastry committed
114
The MQTT peaks into two integration offerred by chirpstack - Uplink and Status to obtain the corresponding device data. The devie EUI and application id, for now, has to be manually added by the user to the node
shastry's avatar
shastry committed
115

shastry's avatar
shastry committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
### The Flask server Yaml Interface


The final flow of our application is better explained in the form of steps:
    1. In the YAML file, change the "GET" address to our implementation.
    2. We define a function(In this case "nodered_test") which has a query code to extract the device EUI from the localhost. This is stored as "idd".
    3. The "idd" obtained is the key to be passed on to the modification of the YAML process.
    4. Once a connection is established between the two through device EUI, we have an acknowledgement message "Hi You" to verify this to us.
    5. On message reception, MQTT will publish this back to the user so that end-user is fully aware of what data is being stored. This information should also be visible on the LORA server platform.


### Running the project

Start Lora components:

```sh
$ redis-server
```
```sh
$ sudo systemctl start chirpstack-network-server
$ sudo journalctl -f -n 100 -u chirpstack-network-server
```
```sh
$ sudo systemctl start chirpstack-application-server
$ sudo journalctl -f -n 100 -u chirpstack-application-server
```	
```sh
$ sudo systemctl start chirpstack-gateway-bridge
$ sudo journalctl -f -n 100 -u chirpstack-gateway-bridge
```	
Open the following link in the browser:
```sh
http://127.0.0.1:8080/
```
Login with the following default admin credentials:
username: admin
password: admin
Do all the configuration steps presented in the LORA report in order to configure the gateway, devices...

**Start Node-Red:**

```sh
$ node-red
```
Open the following link on a browser:
```sh
http://127.0.0.1:1880/
```

**Start Mosquitto:**
```sh
$ mosquitto -p 2550
$ mosquitto -p 2551
```

**Start Flask server:**
```sh
$ python3 run.py
```
The above file can be found in mec-lora-api-master folder

### Running the project

Having done all the previous steps, node-red and lora server are up.
In Node red:
- Double click on test_node_lora
shastry's avatar
shastry committed
182
- Configure the parameters (Be careful to read placeholders)
shastry's avatar
shastry committed
183
184
185
- Click on deploy button
- Click on deploy node
- You will receive an id in the debugger window.
shastry's avatar
shastry committed
186
- Put the application id from chirpstack server and device eui is available from the debugger window. Replace the values in MQTT in nodes
shastry's avatar
shastry committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

Test:
- Go to Chirpstack server, devices you will find the new deployed devices.
- Run code on the device to send test data and you will see the result on MQTT node red

License
----

EURECOM

[mosquitto_installation]: <https://www.vultr.com/docs/how-to-install-mosquitto-mqtt-broker-server-on-ubuntu-16-04/>
[redis_installation]: <https://redis.io/topics/quickstart>
[chirpstack_installation]: <https://www.youtube.com/watch?v=FnTP7t47DlI>
[Mosquitto]: <https://mosquitto.org/>
[Flask Server]: <https://flask.palletsprojects.com/en/1.1.x/quickstart/>
[Chirpstack]: <https://www.chirpstack.io/>
[Redis]: <https://redis.io/>
[Node-Red]: <https://nodered.org/>
shastry's avatar
shastry committed
205