IT Solutions IT Solutions

Ɯberleben in der Digitalen Revolution - IT Sicherheit, Digitalisierung und alles andere rund um das digitale Leben.


Tags IT Solutions

Thingsboard with Raspberry and Sensehat

It's hot outside, we added air conditioning to our office and now of course we all want to know if it helps in any way. Of course we can feel it in the office but we can't measure it. So let's fix that.

Cristian LivadaruCristian Livadaru


We could have gone with a thermometer from the hardware store, it would be cheap and fast but no fun at all. So let's look at the more fun version of what we are building.


First, you need a running thingsboard server, the easiest way to do that is by using docker.

docker run -it -p 9090:9090 -p 1883:1883 -p 5683:5683/udp -v /opt/docker/thingsboard/data:/data -v /opt/docker/thingsboard/logs:/var/log/thingsboard --name mytb --restart always thingsboard/tb-postgres

If you are hooking up a reverse proxy in front of thingsboard you need to add some additional configuration for the websockets to work

    location /api/ws {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header CLIENT_IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 86400;
        proxy_pass http://thingsboard/api/ws;

After setting this up, add a new device as tenant admin in thingsboard.

After adding the device go to dashboard and edit the existing "Temperature & Humidity Demo Dashboard" and remove the existing DHT11 device and add the raspberry you just created, do this for both temperature and humidity sensors.

Check out the thingsboard documentation for more details.

Raspberry setup

I had a sense hat laying around the office so I used that to gather temperature and humidity.

Raspberry Sense Hat

Install the required sense hat package.

sudo apt-get install sense-hat

Then create this python script and replace **IP** with the IP of your thingsboard installation and also replace ACCESSTOKENw with the access token from your device.

import os
import time
import sys
import paho.mqtt.client as mqtt
import json
from sense_hat import SenseHat



sense = SenseHat()
sensor_data = {'temperature': 0, 'humidity': 0}

next_reading = time.time()

client = mqtt.Client()

# Set access token

# Connect to ThingsBoard using default MQTT port and 60 seconds keepalive interval
client.connect(THINGSBOARD_HOST, 1883, 60)


    while True:
        humidity = sense.get_humidity()
        temperature = sense.get_temperature()
        humidity = round(humidity, 2)
        temperature = round(temperature, 2)
        print(u"Temperature: {:g}, Humidity: {:g}%".format(temperature, humidity))
        sensor_data['temperature'] = temperature
        sensor_data['humidity'] = humidity

        # Sending humidity and temperature data to ThingsBoard
        client.publish('v1/devices/me/telemetry', json.dumps(sensor_data), 1)

        next_reading += INTERVAL
        sleep_time = next_reading-time.time()
        if sleep_time > 0:
except KeyboardInterrupt:


Run the script and you are done! Your raspberry will be sending data to thingsboard.

Now you probably wish you went with the cheap and quick thermometer from the hardware store.

View Comments