Usage

Installation

The installation of the latest version can be done with:

pip install git+https://gitlab.com/uracoli-project/uracoli-rsensor.git

Getting Started

After successfull installation run:

mqtt_to_db -h

to see the available command line options of the script.

Create now the initial configuration file myrsensor.cfg:

mqtt:
 - host: test.mosquitto.org
   port: 1883
   prefix: rsensor-test

database:
    dbtype: sqlite
    dbname:  /tmp/rsensor/rsensor.sqlitedb

This configuration connects to the public available MQTT broker test.mosquitto.org and stores the received data in a SQlite database file /tmp/rsensor/rsensor.sqlitedb.

Run the script and see the messages on the console:

mqtt_to_db -C myrsensor.cfg

The file /tmp/rsensor/rsensor.sqlitedb is created and can be accessed with the SQLite tool:

sqlite3 /tmp/rsensor/rsensor.sqlitedb

The next section describes how to receive test data from MQTT, that are stored in the database

Generating Test Data

The package contains also a script rs_testgen that generates appropriate formatted test data.

The script uses per default the MQTT broker on localhost:1883.

After start you will see an output:

$ rs_testgen -H test.mosquitto.org
INFO:MqttRandomGenerator:create generator class
using default sensor config: /opt/esp/ve_esp/local/lib/python2.7/site-packages/rsensor/data/test_node.yml
INFO:MqttRandomGenerator:connected to test.mosquitto.org:1883, prefix=rsensor-test
INFO:MqttRandomGenerator:info message on: rsensor-test/s_1111_0001/info
INFO:MqttRandomGenerator:info message on: rsensor-test/s_1111_0002/info
INFO:MqttRandomGenerator:info message on: rsensor-test/s_1111_0003b/info
INFO:MqttRandomGenerator:data message on: rsensor-test/s_1111_0001/data
INFO:MqttRandomGenerator:data message on: rsensor-test/s_1111_0002/data
INFO:MqttRandomGenerator:data message on: rsensor-test/s_1111_0002/data
INFO:MqttRandomGenerator:data message on: rsensor-test/s_1111_0003b/data
INFO:MqttRandomGenerator:data message on: rsensor-test/s_1111_0001/data
INFO:MqttRandomGenerator:data message on: rsensor-test/s_1111_0002/data

With the command mosquitto_sub -h localhost -t "rsensor-test/#" -v you can watch the generated messages.

See rs_testgen -h for available configuration options.

Writing a SystemD-Daemon Script

To collect sensor data over a long time, the script mqtt_to_db can run as Daemon-service on a Linux server, e.g.on a Raspberry-Pi.

Create a file /etc/systemd/system/rsensor.service with the following contents:

[Unit]
Description=MqttToDb Service
After=network-online.target

[Service]
Type=simple
User=pi
Group=pi
WorkingDirectory=/home/pi/rsensor
ExecStart=/home/pi/ve_py2_rsensor/bin/mqtt_to_db -C /home/pi/rsensor/rsensor.cfg -L ERROR
SyslogIdentifier=rsensor
StandardOutput=syslog
StandardError=syslog
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

In the file above adapt the path to the script /home/pi/ve_py2_rsensor/bin/ and the location of the configuration file /home/pi/rsensor/rsensor.cfg to your system (see also section Python Virtual Environment) . Also note the option -L ERROR, which reduces the amount of messages written to the system-log.

The script rsensor.service is activated with the following commands:

systemctl daemon-reload
systemctl enable rsensor.service
systemctl start rsensor.service

The status of the script can be verified with these commands:

# see if the script is running or it is crashed.
systemctl status rsensor.service

# see all log messages from the beginning of the log.
journalctl -u rsensor.service

# see actual incoming messages
journalctl -u rsensor.service -f

The script can be stopped with:

systemctl stop rsensor.service