From 52e58e652bad387fa8772163a298bfccde946cdc Mon Sep 17 00:00:00 2001 From: Hristo Jelev Date: Tue, 28 Apr 2020 00:12:24 +0300 Subject: [PATCH] fixed extra V in voltage value --- README.md | 13 +++++++++---- rpi-cpu2mqtt.py | 6 ++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bda62a7..96955be 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ The script if very light, it takes 4 seconds as there are 4 one second sleeps in Each value measured by the script is send via a separate message for easier craetion of home assistant sensors. Example message topic: - +``` masoko/rpi4/cpuload - +``` - first part (masoko) is the main topic configurable via the congig.py file - second part (pi4) is the host name of the raspberry which is automatically pulled by the script, so you don't have to configure it for each installation (in case you have many raspberries like me) - third part (cpuload) is the name of the value (these are all values published via MQTT - cpuload, cputemp, diskusage, voltage, sys_clock_speed) @@ -25,13 +25,18 @@ Then install this module needed for the script: ```bash $ pip install paho-mqtt ``` -Rename config.py.example to config.py and populate the needed variables + +Copy rpi-cpu2mqtt.py and config.py.example to a folder of your choise (I am using ```/home/pi/scripts/``` ). + +Rename config.py.example to config.py and populate the needed variables (MQTT host, user, password and main topic). Test the script. ```bash $ /usr/bin/python /home/pi/scripts/rpi-cpu2mqtt.py ``` -Create a cron entry like this (you might need to update the path on the cron entry below, depending on where you put the script): +Once you test the script there will be no output if it run OK but you should get 5 messages via the configured MQTT server. + +Create a cron entry like this (you might need to update the path in the cron entry below, depending on where you put the script files): ``` */2 * * * * /usr/bin/python /home/pi/scripts/rpi-cpu2mqtt.py ``` diff --git a/rpi-cpu2mqtt.py b/rpi-cpu2mqtt.py index 3cca7fb..9cb66a1 100644 --- a/rpi-cpu2mqtt.py +++ b/rpi-cpu2mqtt.py @@ -33,7 +33,9 @@ def check_cpu_load(): def check_voltage(): full_cmd = "vcgencmd measure_volts | cut -f2 -d= | sed 's/000//'" - return subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] + voltage = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] + voltage = voltage.strip()[:-1] + return voltage def check_cpu_temp(): full_cmd = "/opt/vc/bin/vcgencmd measure_temp" @@ -71,6 +73,6 @@ if __name__ == '__main__': used_space = check_used_space('/') voltage = check_voltage() sys_clock_speed = check_sys_clock_speed() - + print(voltage) #Publish messages to MQTT publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed)