Connecting the first sensor

Let's start as simple as possible. In the following, we will learn how to run Tenta locally. We are then going to write a short Python script that simulates our first sensor. After we connect this script with Tenta, we will be able to see the sensor's measurements in the dashboard.

💡

You don't need any special hardware to follow this tutorial.

Start Tenta locally

Tenta consists of a server and a dashboard. Sensors communicate with the server via an intermediate MQTT broker. Data is stored in a PostgreSQL+TimescaleDB database.

For this tutorial, we'll use a shell script that starts all these components for us.

If you haven't already, clone the repository from GitHub with git clone https://github.com/iterize/tenta.git. Then you can follow the instructions in the contributing guide to start the server and the dashboard locally.

Our ./scripts/develop script already initialized Tenta with some example data. You can log into the dashboard with the username happy-un1c0rn and the very secure password 12345678. Don't hesitate to have a look around!

Our first sensor

Now that Tenta is running, let's connect our first sensor. Usually, our sensors and Tenta would run on different devices, but for simplicity, we use our local machine for both in this tutorial.

We use Python together with the paho-mqtt (opens in a new tab) MQTT library in this example. Your sensors can use any other programming language that has a MQTT client library (which is most).

For now, we only want our sensor to transmit some example measurements to Tenta at regular intervals. We are going to look at some more of what Tenta can do in the next chapter.

Let's see the code!

import json
import math
import paho.mqtt.client as mqtt
import time
 
 
# Connect to the MQTT broker (here: our development instance)
client = mqtt.Client()
client.connect("localhost", port=1883)
 
while True:
    # The current time as Unix timestamp
    timestamp = time.time()
    # Create a test measurement with temperature and humidity values
    measurement = {
        "value": {
            "temperature": math.sin(timestamp / (60*60*24)),
            "humidity": timestamp % (60*60*24*7),
        },
        "timestamp": timestamp,
    }
    # Publish our measurement
    client.publish(
        topic="measurements/81bf7042-e20f-4a97-ac44-c15853e3618f",
        payload=json.dumps([measurement]).encode(),
        qos=1,
    )
    # Wait a little while
    time.sleep(5)
💡

All of the examples in this documentation are self-contained and can be run as-is.

Our sensor sends the measurements to the measurements/81bf7042-e20f-4a97-ac44-c15853e3618f topic. The 81bf7042-e20f-4a97-ac44-c15853e3618f part is the sensor's identifier. You can find this identifier in the dashboard when you create a new sensor.

Measurements are simple JSON documents. You can try to add or remove attributes, Tenta supports changing their format on the fly! Note that Tenta only supports numeric values.

The results

In the dashboard, you can see the measurements arriving in real-time now. Additionally, the dashboard shows charts of the measurement values over the last 4 weeks.

That's it for this chapter. Well done! 🎉