Transfer Data from one RaspberryPi to another RaspberryPi (Python Ultrasonic Sensor)

  • Platform information: Raspberry Pi 3 Model B Rev 1.2
    • OS: openHABian
    • openHAB version: openHAB 2.5.1-2

I am running my openHAB on a RaspberryPi 3 and everything is fine so far. Now I’ve got a second RaspberryPi in my network. This RaspberryPi is on another floor of our house and it’s connected with a data cable to our water-tank in the garden. In the water-tank I’ve installed the ultrasonig modul HC-SR04. I am able to get the distance to the water-surface of the water-tank with the connected RaspberryPi via a Pyhton script.

My question is now, how can I get the measure result from the one RaspberryPi with the ultrasonic sensor to the other RaspberryPi with the openHAB?

Thank you very much for your help!

You can write a script on the pi3 that used an ssh connection to the second py. In this script:

  • Connect to the second pi via ssh. You need to have ssh keys exchanged from the user openhab and the second pi (sudo -u openhab ssh-keygen)
  • Run the script on the second pi and echo the output in the script
  • You can extract the output via the exec binding.

I am doing the exact same thing to monitor system information for my second pi. Let me show you an example:
All following scripts/files are on the OpenHAB Server

cpu_1min.sh

ssh helper ‘uptime’ > .${0##/}
if [[ "$(cat .${0##
/})" == “Connection” ]] 2>/dev/null || [[ “$(cat .${0##/})" == “host” ]] 2>/dev/null || [[ "$(cat .${0##/})” == “not known” ]] 2>/dev/null; then
exit
fi
cpu_load_all=$(cat .${0##*/} | sed -n -e 's/.average: //p’ )
rm .${0##
/}
cpu_load_1m=$(echo $cpu_load_all | sed ‘s/ /\n/g’ | sed -n 1p | sed ‘s/.$//’ | sed ‘s/,/./’ )
echo $cpu_load_1m
exit

exec.things

Thing exec:command:Command_RPI2Helper_Cpuload_1min [command=“/etc/openhab2/scripts/facts_about_rpihelper/cpu_1min.sh”, interval=10]

exec.whitelist

/etc/openhab2/scripts/facts_about_rpihelper/cpu_1min.sh

*.items

String RPI2Helper_CPUAuslastung_1min “CPU Auslastung in der letzten Minute” (Gruppe_RPi2Helper_CPU) {channel=“exec:command:Command_RPI2Helper_Cpuload_1min:output”}

Or you could update the python script on the second Pi to either send the output via MQTT or to post the update via the REST API to your openHAB Pi.

I am curious: how can you do that?

You would just need to execute a curl command from within your python script. Something like

curl -X PUT --header "Content-Type: text/plain" --header "Accept: application/json" -d "<NEW-STATE>" "http://<SERVER NAME OR IP>:8080/rest/items/<ITEM NAME>/state"

replacing the items between < and > with relevant details for your environment.

Thanks! I think this will become useful in the future.
@formicula1979 I would use the method of @Mark_Webster. I think it’s a lot more simple and straight to the point. I might switch to this solution too. :thinking:

The full documentation for the REST API can be installed from the UIs tab in PaperUI. It’s also interactive so you can issue commands and updates and modify Things a stuff straight from the docs (useful for testing) and it shows you the equivalent curl command.

1 Like

Thank you very much guys!
I didn’t know anything about the REST-API! :woman_shrugging:
With the REST-API it works pefect and is super simple.

That was exactly what I was looking have!

Thank you all for your support and have a nice day!

Pls mark the post from @rlkoshak as solution for future problem solvers