Skip to content

About

An IoT sensor daemon for the Tinkerforge system

Topics

Resources

Stars

0 stars

Watchers

2 watching

Forks

Latest commit

 

History

829 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub release CI workflow Coverage code style License: GPL v3 Docker

LabKraken Sensor Daemon

LabKraken is a logging daemon for lab applications. It can extract data from a number of sources commonly found in laboratories like:

The data is extracted from the devices and published via MQTT for further processing. Used with a datalogger like the Kraken Logger and a data visualisation frontend like Grafana it allows precise monitoring of labs and experiments in real time.

LabKraken is written in Python using the asyncio framework and runs on both large scale servers and small Raspberry Pis.

Setup

LabKraken is best installed via the Docker image provided with this repository.

Basic example docker-compose.yml for the LabKraken:

services:
  kraken:
    image: ghcr.io/patrickbaus/sensordaemon:latest
    container_name: kraken
    restart: always
    environment:
      - SENSORS_DATABASE_HOST=mongodb://foo:bar@database-server:27017
      - MQTT_HOST=mqtt-broker
      - NODE_ID=a4777632-3de5-4682-a0a7-3f86d879c74d
      - NODE_NAME=Lab 1

Node status

Each node with a NODE_ID publishes its status to two retained MQTT topics. Clients, like a web interface, can use them to show a human-readable name instead of the node id and to see, which nodes are online.

nodes/<NODE_ID>/info describes the node:

{"name": "Lab 1", "hostname": "lab1-pi", "address": "192.168.1.23", "version": "5.1.0", "started": 1700000000.0}

started is the Unix timestamp of the start of the node.

nodes/<NODE_ID>/online is true or false. The node publishes true when it connects to the broker and false when it shuts down. If the node loses its connection to the broker, the broker publishes false as the last will of the node.

A client asks all nodes to publish their info, their online state, their discovered devices and their logs again by publishing any message to nodes/announce, e.g. mosquitto_pub -t nodes/announce -n. A client subscribed to nodes/# should do this after subscribing, because some brokers, e.g. RabbitMQ, do not deliver retained messages to subscriptions with wildcards. It would not learn about the nodes, that are already running, otherwise. A node answers the first request at once. Further requests within 5 seconds are answered once, at the end of these 5 seconds.

Variable Default Description
NODE_NAME the hostname A human-readable name of the node
NODE_HOSTNAME the hostname of the container The hostname of the machine running the node
NODE_ADDRESS detected The IP address of the node
NODE_STATUS_INTERVAL 10 The time in seconds between two updates of the details of the node

If NODE_ADDRESS is not set, the address of NODE_HOSTNAME is published, if that is set. Otherwise, the local address used to reach the broker is published. Loopback addresses are skipped.

Inside a container, the hostname is the id of the container and the local address is the address of the container, which is of no use to clients. Set NODE_HOSTNAME to the name of the machine. Its address is then looked up and published. If the name cannot be resolved inside the container, set NODE_ADDRESS as well. Without either, the address is published as null, because the address of the container is never published:

services:
  kraken:
    environment:
      - NODE_HOSTNAME=lab1-pi

Details

Details, that change while the node is running, are published periodically to their own topics nodes/<NODE_ID>/<name>. Statistics, like the load, are published below nodes/<NODE_ID>/statistics/. They are updated every NODE_STATUS_INTERVAL seconds (default 10). They are not retained, because they are of no interest while the node is down. A client receives them within one interval after subscribing.

Load

The load of the node is published to nodes/<NODE_ID>/statistics/load:

{
  "average": [0.52, 0.31, 0.2],
  "cpus": 4,
  "process_cpu": 3.5,
  "process_memory": 52428800,
  "memory_total": 3977527296,
  "memory_available": 3111387136
}

average is the system load average over the last 1, 5 and 15 minutes and cpus the number of CPUs. process_cpu is the CPU time used by Kraken since the last status update in percent of one CPU. process_memory is the resident memory of Kraken, memory_total and memory_available are the memory of the machine, all in bytes. Values that are not available on the platform are null. Inside a container, the load average and the memory are those of the host.

Sensors

The number of sensors the node is connected to is published to nodes/<NODE_ID>/sensors:

{"connected": 12, "disabled": 1, "unconfigured": 2}

connected counts the sensors, that stream data, disabled the sensors, whose configuration is disabled, and unconfigured the sensors found without a valid configuration, e.g. a Tinkerforge bricklet that is not in the database.

Datagrams

Statistics of the data sent to the broker are published to nodes/<NODE_ID>/statistics/datagrams:

{"rate": 12.5, "received": 104523, "published": 104520, "dropped": 0, "queued": 3}

rate is the number of datagrams published per second since the last status update. received counts the datagrams received from the sensors, published those sent to the broker and dropped those that could not be sent, e.g. because they could not be serialized, all since the start of the node. queued is the number of datagrams waiting to be sent. It grows while the broker is unreachable.

Logs

The last 5 errors and the last 5 warnings of a node are published as retained JSON lists to nodes/<NODE_ID>/logs/errors and nodes/<NODE_ID>/logs/warnings, the oldest entry first:

[
  {
    "timestamp": 1700000000.0,
    "level": "ERROR",
    "logger": "managers",
    "message": "Unexpected error while streaming data from Lab DMM. Restarting the transport.",
    "exception": "Traceback (most recent call last): ..."
  }
]

exception is null if there is none. Long messages and tracebacks are shortened. The lists survive a restart of the node, because the node reads them back from the broker when it starts. To clear a list, stop the node, delete the retained message, e.g. using mosquitto_pub -t nodes/<NODE_ID>/logs/errors -r -n, then start the node again. Like the status, the lists are published again, when a client asks for it via nodes/announce.

Discovered devices

Each node with a NODE_ID publishes, which of its hosts found which device. A node without a NODE_ID does not publish them. Each device found is published as a retained message to nodes/<NODE_ID>/hosts/<host_uuid>/devices/<device_uuid>:

{"started": 1700000000.0, "driver": "tinkerforge2", "uid": 125633, "device_type": "Humidity Bricklet 2.0", "host_label": "Lab", "seen_since": 1700000005.2}
  • <host_uuid> is the id of the host config in the database and <device_uuid> the id of the sensor config of the device. The uid is not used to identify a device, because it can be changed in the device.
    • Generic sensors (Ethernet and GPIB) are published while their data is streamed, i.e. while their host is connected. device_type is the driver of the sensor and uid is null.
    • Tinkerforge bricklets and APQ LabNodes are not linked to a host in the database. A node finds them on every host of their driver. A LabNode uses its own uuid as the id of its sensor config. A Tinkerforge bricklet is published once its sensor config is found by its uid. Bricklets without a sensor config are not published.
  • driver is the driver of the host. uid is informational only: the uid of a Tinkerforge bricklet in decimal or the serial number of a LabNode.
  • seen_since is the Unix timestamp, when the device was found. It does not change, when the device is enumerated again.
  • When the device is disconnected, its sensor config is removed, or the connection to its host is closed for any reason, the message is deleted by publishing an empty retained message.
  • started is the start of the node, see nodes/<NODE_ID>/info. A node that crashed cannot delete its devices. A device is only current, while the node is online and the started of the device equals the started of the info.

Versioning

I use SemVer for versioning. For the versions available, see the tags available for this repository.

The version is not stored in the source code. It is taken from the git tag by setuptools-scm. To release a new version, push a new tag, e.g. 4.7.0. The Docker image built from it reports version 4.7.0. Commits after a tag get a development version like 4.7.1.dev3.

When running Kraken from a git checkout, write _version.py first:

python3 -m pip install --group version
python3 -m setuptools_scm --force-write-version-files

Documentation

I use the Numpydoc style for documentation.

Authors

License

This project is licensed under the GPL v3 license - see the LICENSE file for details.

About

An IoT sensor daemon for the Tinkerforge system

Topics

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages