Howto transfer data from one rPi to a remote rPi running openHAB using MQTT

I am using dialEye for reading the water usage. This is a python script and I run it from an exec command. I am not really pleased with this solution as it takes quite a lot of CPU and Openhab chaches rather frequently and I think this is related to dialEye (although this is an unfonfirmed hypotesis).
To improve the situation I want to run dialEye in a seperate Raspberry pie using cron. How can I transfere the output (4 digits) to Openhab running in another raspberry pi?

Can I transfere the outcome of a python script to MQTT?
I guess that I can transfere the outcome to a text file (have not tested), but can I import the data (4 digits) in the text file from the dialEye raspberry pi to openhab running in another raspberry pi?
Are there other solutions?

You should be able to do this directly from your python script. Check: https://pypi.python.org/pypi/paho-mqtt/1.3.0
Use the dedicated rPi to run the dialEye solution with the python script that regularly publishes the 4-digits output to a topic on the Broker.

Then, in your main/production rPi3? running OH2, define a MQTT bound item that subscribes to the same topic and gets the state updates.

The MQTT broker would run on the production rPi (together with the OH2 process).

Unfortunately I have no kowledge at all of Phyton, I just downloaded the dialEye script https://olammi.iki.fi/sw/dialEye/download.php
and figured out how to access the result through the exec binding: python /var/lib/openhab2/watermeter/dialEye.py -f /var/lib/openhab2/watermeter/dialEye.conf -c /var/lib/openhab2/watermeter/docs/watercal.jpg

Is it very difficult to update the Phyton script to include MQTT?

I already have the MQTT broker and connection to openahab!

I am also not that skilled with python to be able to help you with some code to be added to the dialEye.py :frowning:

A dirty workaround would be:
i) Install a minimal OH2 on your dialEye rPi
ii) Define a dual bound item that does the "xlation"
xlation=Means runs the exec bound python script and publishes the resulting state to an MQTT topic

Not very nice but it should work… (but you may experience crashes again)

Maybe someone can recommend a better way (or post a solution for the python script)

Ps: Another workaround could be to create (or google find) another script that reads the text file based output of the python script and publishes it to a MQTT topic (lighter implementation)

You should be able to publish the output of dialEye using mosquitto_pub

From the mosquitto_pub man page:

Send parsed electricity usage data from a Current Cost meter, reading from stdin with one line/reading as one message:

◦read_cc128.pl | mosquitto_pub -t sensors/cc128 -l

Try something like:

python dialEye.py <options> | mosquitto_pub -t <your topic> --stdin-line

Of course you’ll also need an MQTT broker (e.g. mosquitto) running and the MQTT binding installed in openHAB, but since you already mention MQTT I reckon you are aware of that.

edit: changed -l option to --stdin-line to avoid confusion between the letter l and the number 1

1 Like

It worked perfectly with mosquitto_pub!!! Thank you very much!!!

1 Like