I will be a Milesight temperature and humidity sensor, for collecting my data, this will connect to my local TTN Community gateways. In TTN Community we create an application and publish it to MQTT.
Node-RED will be deployed in AWS, where MQTT will receive the payload from TTN application. Data will be stored in an InfluxDB and display that in Grafana graph embedded in Node-Red dashboard.
-
AWS Account and Services
1.1. Head over to AWS and create a free account
1.2. Follow the tutorials and create an EC2 Ubuntu Server 18.04 LTS (HVM), SSD Volume t2.micro (free tier eligible), please check if you qualify for free tier.
1.3. Availability zone needs to be picked with care, TTN are deployed in 3 different clusters. In my instance eu1.cloud.thethings.network is the close to me. So I have a trade off with latency between where I am in the world, where my EC2 resides and TTN clusters.
1.4. Make sure you enable under your security the following ports: 3306, 3000, 1880. Care should be taken when you set your security and opening ports.
2. Node-RED setup on AWS
2.1. Head over to Node-Red here they have also so information on AWS and Node-RED. Please note that we are working with Ubuntu so follow those instructions.
2.2. Please remember to set that Node-RED to auto start up if and when youâre EC2 reboots (Please test this).
2.3. Once your Node-RED is up and running you can access the editor via http://:1880/
3. Grafana setup in AWS
3.1. Head over to Grafana and follow there tutorial on the installation.
3.2. Please remember to set that Grafana auto start up if and when you EC2 reboots (Please test this)
3.3. Once your Grafana is up and running you can access via http://:3000/
3.4. Chang the following setting in your /etc/grafana/grafana.ini,
[security] allow_embedding = true and [auth.anonymous]# enable anonymous access enabled = true, you need to reboot Grafana for the settings to take effect
4. InfluxDB setup in AWS
4.1. Head over to InfluxDB and follow the installation steps to install it.
4.2. Log into your EC2 instance via Putty (or Tera Term).
4.3. Run âinfluxdâ to start InfluxDB and then âinfluxâ to enter the CLI of Influx DB
4.4. Then run âCREATE DATABASE em300_thâ to create your database.
4.5. Run âSHOW DATABASEâ this will display all running databases.
4.6. Access the database âUSE em300_thâ
4.7. Add a user âCREATE USER admin WITH PASSWORD âpasswordâ WITH ALL PRIVILEGES
5. TTN application
5.1. Head over to the The Things Network, you should have an account by now, if not create one.
5.2. Open the Consol.
5.3. Here is a trade-off (or at least for me), the latency between me and any of the clusters are 200ms plus. So you can either latency between your AWS instance and the cluster, or latency between you and your AWS instance.
Select the cluster where you want to deploy you application.
5.4. Select âGo to Applicationâ.
5.5. Select âAdd Applicationâ.
Fill in the âApplication IDâ and âApplication nameâ, they need to be unique, I just called mine âtemperature-collectionâ, then select âCreate applicationâ.
5.7 Select âEnd deviceâ and â+ Add End Deviceâ
5.8 Select the âBrandâ, âModelâ and âProfileâ, I am using a Milesight EM300-TH and EU863_870 (But this depends on you region.
Also complete the fields âAppEuiâ, âDevEUIâ, âAppKeyâ and âEnd device IDâ, the first 3 fields com with your device the last one âEnd device IDâ is so that you can identify you device easily.
5.9 Select âIntegrationâ and the âMQTTâ. Copy the Username and then select the âGenerate new API keyâ and copy this âPasswordâ, store both in a safe place, we are going to require them in Node-RED âMQTT Inâ node.
6. Node-RED in AWS setting up MQTT and connection to InfluxDB
6.1 Now open as in Step 2.3 http://:1880/
6.2 Drag and drop an âmqtt inâ node on the pallet
6.3 Double click on the âmqtt nodeâ, it will pop open. Set Topic to âv3/+/devices/+/upâ, âOutputâ to âa parsed JSON objectâ and âNameâ to âmqtt from TTN - temperature-collectionâ. Then select the âpencilâ icon by âServerâ.
6.4 Set âServerâ to âeu1.cloud.thethings.networkâ from the TTN integration mqtt page and the âPortâ to 8883. Select âEnable secure (SSL/TTL) connection and click on the pencil icon.
6.5 Just select âUpdateâ and leave the rest default.
6.6 Enter your âUsernameâ and âPasswordâ from step 5.9 and select âAddâ
6.7 Select âDoneâ
6.8 Add a âdebugâ node and wire it to the âmqtt inâ
6.9 Double click on the âdebugâ node and set âOutputâ to âcomplete msg objectâ and âNameâ it âdebug mqtt â TTNâ. Select âDoneâ
6.10 Add a âfunctionâ node and wire it to the âmqtt inâ node and a âdebugâ node wired to the âfunctionâ node rename the âFunction node as âPrep for InfluxDBâ. And the following code to the node and select âDoneâ
let eui = msg.payload.end_device_ids.device_id;
let temperature = msg.payload.uplink_message.decoded_payload.temperature;
let humidity = msg.payload.uplink_message.decoded_payload.humidity;
msg = {}
msg.payload = {
eui: eui,
temperature: temperature,
humidity: humidity
}
return msg
6.10 Add the additional nodes to you Node-Red, select the âHanugerâ button in the top right a dselect âManage Palletâ
6.11 Add the following two node collection ânode-red-contrib-influxdbâ and ânode-red-dashboardâ by searching for each one and press the âinstallâ button, as you can see bot mine are installed
6.12 Add an âinfluxdb outâ node and wire the output of âPrep for InfluxDBâ.
6.13 Select the âPenâ button and âAdd new influxdbâŚâ
6.14 Add now the properties of the database created in step 4.4 and 4.7 and select âUpdateâ
6.15 Add âmeasurementâ âem300_thâ and select âDoneâ
6.16 Select deploy and the next Uplink will be written to you InfluxDB.
6.17 In you Putty connected to you EC2 and running influxdb, âSELECT * FROM em300_thâ
7. Grafana
7.1 Access Grafana via http://:3000/
7.2 Select âData sourceâ
7.3 Select âAdd data sourceâ
7.4 Select âInfluxDBâ
7.5 Set you InfluxDB credentials up as created in step 4.4 and 4.7, and select âDefaultâ
7.6 Select âSave & testâ any you should get back âData source is workingâ
7.7 Select âDashboardâ
7.8 Select âAdd an empty panelâ
7.9 Edit the query panel to select the data for your node.
7.10 Add a second query panel selecting âDuplicate queryâ
7.11 You now only need to edit the âSELECTâ
7.12 Select âConnect null valuesâ and âApplyâ
7.13 Select âSaveâ and name you dashboard
7.14 You can just use Grafana to monitor your devices, but I am going to embed this panel in Node-Red dashboard as there are other functionality I want to use in Node-Red
You can also play around with the time range and Refresh Dashboard settings
7.15 Go to âHome and select âShareâ
7.15 On the âShare Panelâ select âEmbedâ and âCopy to clipboardâ , we will use this in Node-Red
8. Emending Grafana in Node-Red
8.1 Go back to your Node-Red http://:1880/ and select the Dashboard
8.2 Select â+groupâ
8.3 Select âeditâ of the Group
8.4 Edit the Name and the width of the group and select âUpdateâ
8.5 Drag and drop a âtemplateâ node onto you flow.
8.6 Open up the âtemplateâ node and edit âGroupâ , âSizeâ and past the Embedded HLMT form step 7.15.
8.7 You will need to adjust the âSizeâ and âwidth=â1500â height=â750ââ setting to suit your screen size, select âDoneâ
8.8 Deploy your flow and select the arrow button, this will launch your Node-red Dashboard.
8.9 With luck you will now have Grafana embedded in your Node-Red dashboard.