From 2ef3fea6487a934624e295bc3776dcc555cf79d7 Mon Sep 17 00:00:00 2001 From: Chris Vik Date: Fri, 17 Jul 2020 09:53:53 +1000 Subject: [PATCH 1/3] Replaced vcgencmd temperature with generic #45 This should allow Pi hardware running non-Pi OS distros to use this project. --- src/system_sensors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/system_sensors.py b/src/system_sensors.py index 09099c6..ba9d3b9 100644 --- a/src/system_sensors.py +++ b/src/system_sensors.py @@ -131,8 +131,8 @@ def get_updates(): def get_temp(): - temp = check_output(["vcgencmd", "measure_temp"]).decode("UTF-8") - return str(findall("\d+\.\d+", temp)[0]) + temp = check_output(["cat", "/sys/class/thermal/thermal_zone0/temp"]).decode("UTF-8") + return str(temp[0] + temp[1] + "." + temp[2]) def get_disk_usage(path): From e605cd105920f5d71f76b8c8074be54f1a964b20 Mon Sep 17 00:00:00 2001 From: Chris Vik Date: Fri, 17 Jul 2020 10:52:36 +1000 Subject: [PATCH 2/3] Output 0 for WiFi strength if no WiFi detected This formats the payload correctly when the hardware doesn't have WiFi running/connected --- src/system_sensors.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/system_sensors.py b/src/system_sensors.py index ba9d3b9..26c015a 100644 --- a/src/system_sensors.py +++ b/src/system_sensors.py @@ -152,17 +152,16 @@ def get_swap_usage(): def get_wifi_strength(): # check_output(["/proc/net/wireless", "grep wlan0"]) - return ( - check_output( - [ - "bash", - "-c", - "cat /proc/net/wireless | grep wlan0: | awk '{print int($4)}'", - ] - ) - .decode("utf-8") - .rstrip() - ) + wifi_strength_value = check_output( + [ + "bash", + "-c", + "cat /proc/net/wireless | grep wlan0: | awk '{print int($4)}'", + ] + ).decode("utf-8").rstrip() + if not wifi_strength_value: + wifi_strength_value = "0" + return (wifi_strength_value) def get_rpi_power_status(): From 35c1ad6ec3722b24c1ed6b9e0e79a63d4666b565 Mon Sep 17 00:00:00 2001 From: Chris Vik Date: Sat, 18 Jul 2020 12:11:02 +1000 Subject: [PATCH 3/3] Selects temp method based on OS detect #45 Tested on both Raspberry Pi OS Lite and Ubuntu Server deployments by attaching an extra character ("R") at the end of the Pi OS temp reading and checking its existence in Node-RED payload readings (which I've obviously removed after testing). So it should be using the correct method depending on the OS. --- src/system_sensors.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/system_sensors.py b/src/system_sensors.py index 26c015a..044b0ff 100644 --- a/src/system_sensors.py +++ b/src/system_sensors.py @@ -13,6 +13,7 @@ import psutil import pytz import yaml +import csv from pytz import timezone try: @@ -24,6 +25,14 @@ UTC = pytz.utc DEFAULT_TIME_ZONE = None +# Get OS information +OS_DATA = {} +with open("/etc/os-release") as f: + reader = csv.reader(f, delimiter="=") + for row in reader: + if row: + OS_DATA[row[0]] = row[1] + mqttClient = None SYSFILE = "/sys/devices/platform/soc/soc:firmware/get_throttled" WAIT_TIME_SECONDS = 60 @@ -130,10 +139,16 @@ def get_updates(): return str(cache.get_changes().__len__()) +# Temperature method depending on system distro def get_temp(): - temp = check_output(["cat", "/sys/class/thermal/thermal_zone0/temp"]).decode("UTF-8") - return str(temp[0] + temp[1] + "." + temp[2]) - + temp = ""; + if "rasp" in OS_DATA["ID"]: + reading = check_output(["vcgencmd", "measure_temp"]).decode("UTF-8") + temp = str(findall("\d+\.\d+", reading)[0]) + else: + reading = check_output(["cat", "/sys/class/thermal/thermal_zone0/temp"]).decode("UTF-8") + temp = str(reading[0] + reading[1] + "." + reading[2]) + return temp def get_disk_usage(path): return str(psutil.disk_usage(path).percent)