|
| 1 | +--- |
| 2 | +author: |
| 3 | + name: Jared Kobos |
| 4 | + |
| 5 | +description: 'This guide will show how to track and visualize data from an Internet of Things device using Thingsboard.' |
| 6 | +og_description: 'This guide shows how to install the Thingsboard open source dashboard for Internet of Things devices. A Raspberry Pi is used to demonstrate sending data to the cloud dashboard.' |
| 7 | +keywords: ["iot", "raspberry pi", "internet of things", "dashboard"] |
| 8 | +license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)' |
| 9 | +published: 2018-01-30 |
| 10 | +modified: 2018-01-30 |
| 11 | +modified_by: |
| 12 | + name: Linode |
| 13 | +title: 'View IoT Data with Thingsboard' |
| 14 | +external_resources: |
| 15 | + - '[Getting Started – Thingsboard](https://thingsboard.io/docs/getting-started-guides/helloworld)' |
| 16 | + - '[Thingsboard Github Repo](https://github.com/thingsboard/thingsboard)' |
| 17 | +--- |
| 18 | + |
| 19 | +[Thingsboard](https://thingsboard.io/) is an open source platform for collecting and visualizing data from Internet of Things devices. Data from any number of devices can be sent to a cloud server where it can be viewed or shared through a highly customizable dashboard. |
| 20 | + |
| 21 | +This guide will show how to install Thingsboard on a Linode and use a Raspberry Pi to send simple telemetry data to a cloud dashboard. |
| 22 | + |
| 23 | +{{< note >}} |
| 24 | +This guide will use a Raspberry Pi 3 with a [Sense HAT](https://www.raspberrypi.org/products/sense-hat/). You can substitute any device capable of sending telemetry data, or use `curl` to experiment with Thingsboard without using any external devices. |
| 25 | +{{< /note >}} |
| 26 | + |
| 27 | +## Install Thingsboard |
| 28 | + |
| 29 | +Thingsboard runs on Java 8, and the Oracle JDK is recommended. |
| 30 | + |
| 31 | +{{< content "install-java-jdk.md" >}} |
| 32 | + |
| 33 | +### Set Up PostgreSQL |
| 34 | + |
| 35 | +1. Install PostgreSQL: |
| 36 | + |
| 37 | + sudo apt install postgresql postgresql-contrib |
| 38 | + |
| 39 | +2. Create a database and database user for Thingsboard: |
| 40 | + |
| 41 | + sudo -u postgres createdb thingsboard |
| 42 | + sudo -u postgres createuser thingsboard |
| 43 | + |
| 44 | +3. Set a password for the `thingsboard` user and grant access to the database: |
| 45 | + |
| 46 | + sudo -u postgres psql thingsboard |
| 47 | + ALTER USER thingsboard WITH PASSWORD 'thingsboard'; |
| 48 | + GRANT ALL PRIVILEGES ON DATABASE thingsboard TO thingsboard; |
| 49 | + \q |
| 50 | + |
| 51 | +### Install Thingsboard |
| 52 | + |
| 53 | +1. Download the installation package. Check the [releases](https://github.com/thingsboard/thingsboard/releases) page and replace the version numbers in the following command with the version tagged **Latest release**: |
| 54 | + |
| 55 | + wget https://github.com/thingsboard/thingsboard/releases/download/v1.3.1/thingsboard-1.3.1.deb |
| 56 | + |
| 57 | +2. Install Thingsboard: |
| 58 | + |
| 59 | + sudo dpkg -i thingsboard-1.3.1.deb |
| 60 | + |
| 61 | + |
| 62 | +3. Open `/etc/thingsboard/conf/thingsboard.yml` in a text editor and comment out the `HSQLDB DAO Configuration` section: |
| 63 | + |
| 64 | + {{< file-excerpt "/etc/thingsboard/conf/thingsboard.yml" yaml >}} |
| 65 | +# HSQLDB DAO Configuration |
| 66 | +#spring: |
| 67 | +# data: |
| 68 | +# jpa: |
| 69 | +# repositories: |
| 70 | +# enabled: "true" |
| 71 | +# jpa: |
| 72 | +# hibernate: |
| 73 | +# ddl-auto: "validate" |
| 74 | +# database-platform: "org.hibernate.dialect.HSQLDialect" |
| 75 | +# datasource: |
| 76 | +# driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.hsqldb.jdbc.JDBCDriver}" |
| 77 | +# url: "${SPRING_DATASOURCE_URL:jdbc:hsqldb:file:${SQL_DATA_FOLDER:/tmp}/thingsboardDb;sql.enforce_size=false}" |
| 78 | +# username: "${SPRING_DATASOURCE_USERNAME:sa}" |
| 79 | +# password: "${SPRING_DATASOURCE_PASSWORD:}" |
| 80 | +{{< /file-excerpt >}} |
| 81 | + |
| 82 | +4. In the same section, uncomment the PostgreSQL configuration block. Replace `thingsboard` in the username and password fields with the username and password of your `thingsboard` user: |
| 83 | + |
| 84 | + {{< file-excerpt "/etc/thingsboard/conf/thingsboard.yml" yaml >}} |
| 85 | +# PostgreSQL DAO Configuration |
| 86 | +spring: |
| 87 | + data: |
| 88 | + jpa: |
| 89 | + repositories: |
| 90 | + enabled: "true" |
| 91 | + jpa: |
| 92 | + hibernate: |
| 93 | + ddl-auto: "validate" |
| 94 | + database-platform: "org.hibernate.dialect.PostgreSQLDialect" |
| 95 | + datasource: |
| 96 | + driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}" |
| 97 | + url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/thingsboard}" |
| 98 | + username: "${SPRING_DATASOURCE_USERNAME:thingsboard}" |
| 99 | + password: "${SPRING_DATASOURCE_PASSWORD:thingsboard}" |
| 100 | +{{< /file-excerpt >}} |
| 101 | + |
| 102 | +5. Run this installation script: |
| 103 | + |
| 104 | + sudo /usr/share/thingsboard/bin/install/install.sh --loadDemo |
| 105 | + |
| 106 | +6. Start the Thingsboard service: |
| 107 | + |
| 108 | + sudo systemctl enable thingsboard |
| 109 | + sudo systemctl start thingsboard |
| 110 | + |
| 111 | +## NGINX Reverse Proxy |
| 112 | + |
| 113 | +Thingsboard listens on `localhost:8080`, by default. For security purposes, it's better to serve the dashboard through a reverse proxy. This guide will use NGINX, but any webserver can be used. |
| 114 | + |
| 115 | +1. Install NGINX: |
| 116 | + |
| 117 | + sudo apt install nginx |
| 118 | + |
| 119 | +2. Create `/etc/nginx/conf.d/thingsboard.conf` with a text editor and edit it to match the example below. Replace `example.com` with the public IP address or FQDN of your Linode. |
| 120 | + |
| 121 | + {{< file "/etc/nginx/conf.d/thingsboard.conf" nginx >}} |
| 122 | +server { |
| 123 | + listen 80; |
| 124 | + listen [::]:80; |
| 125 | + |
| 126 | + server_name example.com; |
| 127 | + |
| 128 | + location / { |
| 129 | + # try_files $uri $uri/ =404; |
| 130 | + proxy_pass http://localhost:8080/; |
| 131 | + proxy_http_version 1.1; |
| 132 | + proxy_set_header Upgrade $http_upgrade; |
| 133 | + proxy_set_header Connection "upgrade"; |
| 134 | + proxy_set_header Host $host; |
| 135 | + } |
| 136 | +} |
| 137 | +{{< /file >}} |
| 138 | + |
| 139 | +3. Restart NGINX: |
| 140 | + |
| 141 | + sudo systemctl restart nginx |
| 142 | + |
| 143 | +## Set Up Thingsboard Device |
| 144 | + |
| 145 | +1. Navigate to your Linode's IP address with a web browser. You should see the Thingsboard login page: |
| 146 | + |
| 147 | +  |
| 148 | + |
| 149 | + The demo account login `[email protected]` and the password is `tenant`. You should change this to a more secure password after you have signed in. |
| 150 | + |
| 151 | +2. From the main menu, click on the **Devices** icon, then click the **+** icon in the lower right to add a new device. |
| 152 | + |
| 153 | +3. Choose a name for your device. Set the **Device type** to **PI**. |
| 154 | + |
| 155 | +3. After the device is added, click on its icon in the **Devices** menu. Click on **COPY ACCESS TOKEN** to copy the API key for this device (used below). |
| 156 | + |
| 157 | +## Configure Raspberry Pi |
| 158 | + |
| 159 | +{{< note >}} |
| 160 | +The following steps assume that you have terminal access to a Raspberry Pi, and that Sense HAT and its libraries are already configured. For more information on getting started with Sense HAT, see the Raspberry Pi [official documentation](https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat). If you would prefer to use `curl` to send mock data to Thingsboard, you can skip this section. |
| 161 | +{{< /note >}} |
| 162 | + |
| 163 | +### Basic Python Script |
| 164 | + |
| 165 | +1. Using a text editor, create `thingsboard.py` in a directory of your choice. Add the following content, using the API key copied to your clipboard in the previous section: |
| 166 | + |
| 167 | + {{< file "thingsboard.py" python >}} |
| 168 | +#!/usr/bin/env python |
| 169 | + |
| 170 | +import json |
| 171 | +import requests |
| 172 | +from sense_hat import SenseHat |
| 173 | +from time import sleep |
| 174 | + |
| 175 | +# Constants |
| 176 | + |
| 177 | +API_KEY = "<Thingsboard API Key>" |
| 178 | +THINGSBOARD_HOST = "<Linode Public IP Address>" |
| 179 | + |
| 180 | +thingsboard_url = "http://{0}/api/v1/{1}/telemetry".format(THINGSBOARD_HOST, API_KEY) |
| 181 | + |
| 182 | +sense = SenseHat() |
| 183 | + |
| 184 | + |
| 185 | +data = {} |
| 186 | + |
| 187 | +while True: |
| 188 | + data['temperature'] = sense.get_temperature() |
| 189 | + data['pressure'] = sense.get_pressure() |
| 190 | + data['humidity'] = sense.get_humidity() |
| 191 | + |
| 192 | + #r = requests.post(thingsboard_url, data=json.dumps(data)) |
| 193 | + print(str(data)) |
| 194 | + sleep(5) |
| 195 | +{{< /file >}} |
| 196 | + |
| 197 | +2. Test the script by running it from the command line: |
| 198 | + |
| 199 | + python thingsboard.py |
| 200 | + |
| 201 | + Basic telemetry should be printed to the console every five seconds: |
| 202 | + |
| 203 | + {{< output >}} |
| 204 | +{'pressure': 1020.10400390625, 'temperature': 31.81730842590332, 'humidity': 19.72637939453125} |
| 205 | +{'pressure': 1020.166259765625, 'temperature': 31.871795654296875, 'humidity': 20.247455596923828} |
| 206 | +{'pressure': 1020.119140625, 'temperature': 31.908119201660156, 'humidity': 19.18065643310547} |
| 207 | +{'pressure': 1020.11669921875, 'temperature': 31.908119201660156, 'humidity': 20.279142379760742} |
| 208 | +{'pressure': 1020.045166015625, 'temperature': 31.92628288269043, 'humidity': 20.177040100097656} |
| 209 | +{{< /output >}} |
| 210 | + |
| 211 | +3. If the script is working correctly, remove the `print` statement and uncomment the `r = requests.post()` line. Also increase the `sleep()` time interval: |
| 212 | + |
| 213 | + {{< file-excerpt "thingsboard.py" python >}} |
| 214 | +while True: |
| 215 | + data['temperature'] = sense.get_temperature() |
| 216 | + data['pressure'] = sense.get_pressure() |
| 217 | + data['humidity'] = sense.get_humidity() |
| 218 | + |
| 219 | + r = requests.post(thingsboard_url, data=json.dumps(data)) |
| 220 | + sleep(60) |
| 221 | +{{< /file-excerpt >}} |
| 222 | + |
| 223 | +### Create a Systemd Service |
| 224 | + |
| 225 | +You should now be able to run the script from the command line to transmit temperature, pressure, and humidity data once per minute. However, to make sure that data is sent continually, it's a good idea to enable a new service that will run the script automatically whenever the server is restarted. |
| 226 | + |
| 227 | +1. Copy the script to `/usr/bin/` and make it executable: |
| 228 | + |
| 229 | + sudo cp thingsboard.py /usr/bin/thingsboard.py |
| 230 | + sudo chmod +x /usr/bin/thingsboard.py |
| 231 | + |
| 232 | +2. Create a service file to run the Python script as a service: |
| 233 | + |
| 234 | + {{< file "/lib/systemd/system/thingsdata.service" conf >}} |
| 235 | +[Unit] |
| 236 | +Description=Push telemetry data from Sense HAT to Thingsboard. |
| 237 | + |
| 238 | +[Service] |
| 239 | +Type=simple |
| 240 | +ExecStart=/usr/bin/thingsboard.py |
| 241 | + |
| 242 | +[Install] |
| 243 | +WantedBy=multi-user.target |
| 244 | +{{< /file >}} |
| 245 | + |
| 246 | +3. Enable and start the service: |
| 247 | + |
| 248 | + sudo systemctl enable thingsdata.service |
| 249 | + sudo systemctl start thingsdata.service |
| 250 | + |
| 251 | +4. Check the status of the new service: |
| 252 | + |
| 253 | + sudo systemctl status thingsdata.service |
| 254 | + |
| 255 | +## Send Data with cURL |
| 256 | + |
| 257 | +{{< note >}} |
| 258 | +Skip this section if you are using a Raspberry Pi. |
| 259 | +{{< /note >}} |
| 260 | + |
| 261 | +1. Create a sample JSON file with dummy data: |
| 262 | + |
| 263 | + {{< file "dummy_data.json" json >}} |
| 264 | +{ |
| 265 | + "temperature": 38, |
| 266 | + "humidity": 50, |
| 267 | + "pressure": 1100 |
| 268 | +} |
| 269 | +{{< /file >}} |
| 270 | + |
| 271 | +2. Use `curl` to send a POST request to the Thingsboard server: |
| 272 | + |
| 273 | + curl -v -X POST -d @dummy_data.json http://$THINGSBOARD_HOST:$THINGSBOARD_PORT/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json" |
| 274 | + |
| 275 | +## View Data in Thingsboard |
| 276 | + |
| 277 | +If the service is running successfully, data should be transmitted to your Thingsboard server every 60 seconds. |
| 278 | + |
| 279 | +1. Log back into the Thingsboard dashboard in your browser and click on your device's card in the **Devices** menu. Choose the **Latest Telemetry** tab from the resulting details page. You should see the temperature, humidity, and pressure data from your device: |
| 280 | + |
| 281 | +  |
| 282 | + |
| 283 | +2. Click the checkbox next to one of the data types and then click **Show on Widget**. |
| 284 | + |
| 285 | +3. Use the drop-down and carousel menus to choose a one of the preset widgets to display this data type on a dashboard. Click **Add to Dashboard** when you have chosen a widget. |
| 286 | + |
| 287 | +  |
| 288 | + |
| 289 | +## Next Steps |
| 290 | + |
| 291 | +The widgets provided by Thingsboard can be easily edited, and it is possible to create new ones as well. Multiple widgets, representing multiple datastreams from multiple devices, can be combined to produce customized dashboards. These dashboards can then be made public, or shared with customers. |
| 292 | + |
| 293 | +For more information on how to customize and set up widgets and dashboards, see the Thingsboard [Widget Library](https://thingsboard.io/docs/user-guide/ui/widget-library/#time-series) and [Dashboard page](https://thingsboard.io/docs/user-guide/ui/dashboards/) The [Thingsboard Github repo](https://github.com/thingsboard/thingsboard) also has images of example dashboards. |
0 commit comments