×
Community Blog Developing and Testing IoT (MQTT) Applications Using ECS Instance

Developing and Testing IoT (MQTT) Applications Using ECS Instance

This blog walks you through the process of setting up and using ThingsBoard Community Edition on an ECS instance.

Introduction

This blog demonstrates a step by step procedure for setting up ThingsBoard Community Edition on an ECS (Elastic Compute Service) instance and sending telemetry data from MQTT client, namely MQTTX. Both ThingsBoard and MQTTX are open-source and can be used for IoT demonstrations to collect, process data as well perform device management. To check how our prototype will look like, Please watch this video:

Prerequisites

• An ECS instance with the Ubuntu Operating System. If you don’t know how to create to create one, please watch this video

• Basic knowledge of Linux command line.

Step 1: Log Into the ECS Instance

Start by logging into your ECS instance

1

We can either log into the ECS instance by clicking the “Connect” option or using SSH. To use SSH on a Windows-based PC, open-source software like putty can be used. For Mac, we can use Terminal to login by using the following command:

ssh root@your-ecs-instance-Public-ip

2

Step 2: Update the System and Install JDK

a. Update the Package List:

apt update

3

b. Install OpenJDK 11:

apt install openjdk-11-jdk –y

c. Verify the Java Installation:

java –version

Step 3: Download ThingsBoard Community Edition

Download the ThingsBoard Community Edition package using wget.

wget https://github.com/thingsboard/thingsboard/releases/download/v3.5.1/thingsboard-3.5.1.deb

4

List the files to confirm the download:

ls

5

Step 4: Install ThingsBoard

Install the downloaded ThingsBoard package:

dpkg -i thingsboard-3.5.1.deb

6

Step 5: Download and Install PostgreSQL

a. Add PostgreSQL GPG Key:

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

7

b. Add PostgreSQL Repository:

echo "deb https://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list

c. Update the Package List:

apt update

d. Install PostgreSQL:

apt -y install postgresql-15

e. Start PostgreSQL Service:

service postgresql start

Step 6: Create User and Password for the Database

The instructions below will help to set the password for main postgresql user

su - postgres

Log into PostgreSQL:

psql

Set a password for the postgres user:

\password

It will prompt you to re-enter the password. This password will be used later on in the configuration of ThingsBoard, so make sure to remember it.

Exit the PostgreSQL prompt:

\q

8

Then, press Ctrl+D to return to main user console

Now connect to the database again to create thingsboard DB:

psql -U postgres -d postgres -h 127.0.0.1 -W
CREATE DATABASE thingsboard;
\q

9

Then, press Ctrl+D to logout and return to main user console

Step 7: Configure ThingsBoard

Edit the ThingsBoard configuration file to add database settings:

nano /etc/thingsboard/conf/thingsboard.conf

Add the following lines to configure PostgreSQL. Don’t forget to use your database password here:

# DB Configuration
export DATABASE_TS_TYPE=sql
export SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/thingsboard
export SPRING_DATASOURCE_USERNAME=postgres
export SPRING_DATASOURCE_PASSWORD=your_postgres_password_here
# Specify partitioning size for timestamp key-value storage.
export SQL_POSTGRES_TS_KV_PARTITIONING=MONTHS

10

Step 8: Load Demo Data

To load demo data, run the following script:

/usr/share/thingsboard/bin/install/install.sh --loadDemo

11

Step 9: Start ThingsBoard

Start the ThingsBoard service:

service thingsboard start

Step 10: Access ThingsBoard

You can access the ThingsBoard web UI at:http://<ECS_instance_public_ip>:8080/

12

Default Credentials

System Administrator: sysadmin@thingsboard.org / sysadmin

Tenant Administrator: tenant@thingsboard.org / tenant

Customer User: customer@thingsboard.org / customer

We may use the System Administrator credentials for the login.

13

Step 11: Creating Tenant and User Account

System Administrator (sysadmin) Account has the highest level of administrative control in ThingsBoard with full access to create, manage, and delete tenant accounts, perform system-wide configurations, and monitor system health and performance.

A Tenant Account represents an organization or business entity using ThingsBoard and manages its own isolated environment, including devices, assets, dashboards, and customer accounts.

A Customer Account represents an end-user or sub-organization within a tenant with limited access to the resources assigned by the tenant, such as specific devices and dashboards.

For the security reasons, it is recommended to change the passwords of the default accounts or disable them.

Here we will create a tenant account named Alibaba Cloud Academy. To do that, click on the Tenants option on the left, press + sign, then fill in the details and click on the add button.

14

This will create a new tenant account, as shown below:

15

To create users, click on the Manage Tenants option on the right. It will take us to the Tenants Admin portal where we need to create a new user here. An email address is required for this process. The steps are shown below:

16

After clicking the add button, we will get the activation code, as shown below:

17

Copy the activation code and paste it into the browser and press enter. You will be prompted to enter a new password.
Provide the new password and press create password. It will direct us to the Tenant Administrator page as shown below:

18

This page can be used to create Customers, Add Devices and Create Dashboard etc. For this demo, we will create a device and then push the data using MQTT to display it on the Dashboard.

Clicking on Add Device will display a page asking for the device name etc.

19

Once done, press Add button and the devices will be added.

20

Every newly created device will initially have "Inactive" status. If there are customers’ accounts, we can assign a device to a specific customer as well.

To send data to the device, we need the device access token and MQTT topic. ThingsBoard community edition uses v1/devices/me/telemetry as the MQTT topic that can be used by the sensors to publish data.

To get the device credentials, click on the name of the device and copy the access token:

21

Save the device token as we will need information later on.

Step 12: Connecting a Client and Sending Messages

To connect a client to ThingsBoard and send telemetry data to the device, we can either use mosquitto client for Commnad line interface or can install open-source software like MQTTX. We will go with the later method.

1.  Install MQTTX client:

Depending on the operating system, the GUI version MQTTX can be downloaded from the website https://mqttx.app/

Once downloaded and installed, we can use it as an MQTT client to send data to the ThingsBoard device. To configure MQTTX, we need the ECS public IP address and the Device Access token from ThingsBoard Device (as shown in Step-11).

The following figure shows the connection configuration details of MQTTX. Note that the Device Access token is being used as the Username.

22

2.  Publish telemetry data using MQTTX:

To publish data, we need to configure the MQTT topic, as shown below. The data to ThingsBoard can be sent in the json format e.g. {“Temp”:22}

23

3.  Checking Data and Creating Dashboard on ThingsBoard

To see and visualize the telemetry data on the ThingsBoard, select the device that we created already, go to the Latest Telemetry data and we may be able to see the data published by the MQTTX along with the timestamp. When we click on the checkbox, the box Show on Widget will appear, as shown below:

24

Clicking on Show on Widget, will display options for the visualization. Select Analogue gauges and click Add to dashboard, as shown below:

25

ThingsBoard will ask for the option of creating Dashboard. Select Create New Dashboard, give a title and click Add button.

26
27

The Dashboard can made public and can be shared with others.

Conclusion

By following these steps, you should have a fully functional ThingsBoard Community Edition setup on your ECS instance. You can now start managing your IoT devices and visualize the telemetry data effectively.

1 2 0
Share on

JwdShah

6 posts | 2 followers

You may also like

Comments

Santhakumar Munuswamy August 12, 2024 at 6:36 am

Thank you for sharing

JwdShah

6 posts | 2 followers

Related Products