From 221093e4cd36e40374ee69b2f42b3c393558b358 Mon Sep 17 00:00:00 2001 From: Hristo Date: Sat, 19 Nov 2022 01:29:00 +0200 Subject: [PATCH] add wifi signal dBm --- README.md | 19 +++++++++++++++++-- install.sh | 5 +++-- src/config.py.example | 3 ++- src/rpi-cpu2mqtt.py | 40 +++++++++++++++++++++++++++++++++------- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a78e907..4b7fc60 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ masoko/rpi4 The csv message looks like this: ```csv -9.0, 43.0, 25, 25, 0.85, 1500, False, False, False +9.0, 43.0, 25, 25, 0.85, 1500, False, False, 73, -60 ``` Disabled sensors are represented with False in the message. @@ -74,6 +74,7 @@ swap = False memory = False uptime = True wifi_signal = True +wifi_signal_dbm = False ``` If the ```discovery_messages``` is set to true, the script will send MQTT Discovery config messages which allows Home Assistant to automatically add the sensors without having to define them in configuration. Note, this setting is only available when ```group_messages``` is set to False. @@ -81,7 +82,7 @@ If the ```discovery_messages``` is set to true, the script will send MQTT Discov If the ```group_messages``` is set to true the script will send just one message containing all values in CSV format. The group message looks like this: ``` -1.3, 47.1, 12, 1.2, 600, nan, 14.1, 12, 50.0 +1.3, 47.1, 12, 1.2, 600, nan, 14.1, 12, 50, -60 ``` Test the script. @@ -158,6 +159,11 @@ This is the sensors configuration if ```group_messages = True``` assuming your s name: rpi4 wifi signal unit_of_measurement: "%" + - platform: mqtt + state_topic: 'masoko/rpi4' + value_template: '{{ value.split(",")[9] }}' + name: rpi4 wifi signal + unit_of_measurement: "dBm" ``` This is the sensors configuration if ```group_messages = False``` assuming your sensors are separated in ```sensors.yaml``` file. @@ -207,6 +213,11 @@ This is the sensors configuration if ```group_messages = False``` assuming your name: rpi4 wifi signal unit_of_measurement: "%" + - platform: mqtt + state_topic: "masoko/rpi4/wifi_signal_dbm" + name: rpi4 wifi signal + unit_of_measurement: "dBm" + ``` Add this to your ```customize.yaml``` file to change the icons of the sensors. @@ -247,6 +258,10 @@ entities: - entity: sensor.rpi4_memory - entity: sensor.rpi4_uptime - entity: sensor.rpi4_wifi_signal + - entity: sensor.rpi4_wifi_signal_dbm ``` # To Do - maybe add network traffic monitoring via some third party software (for now I can't find a way to do it without additional software) + +# Feature request: +If you want to suggest a new feature or improvement don't hesitate to open an issue or pull request. \ No newline at end of file diff --git a/install.sh b/install.sh index c8b658f..36cefad 100755 --- a/install.sh +++ b/install.sh @@ -13,9 +13,11 @@ find_python(){ if [[ $(python --version) ]]; then python=$(which python) pip="python-pip" + pip_run='pip' else python=$(which python3) pip="python3-pip" + pip_run='pip3' fi if [[ "$python" == *"python"* ]]; then @@ -59,8 +61,7 @@ check_and_install_pip(){ install_requirements(){ printm "Installing requirements" - pip install -r requirements.txt - sudo pip3 install -r requirements.txt + $pip_run install -r requirements.txt } update_config(){ diff --git a/src/config.py.example b/src/config.py.example index 75ef50c..d37de50 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -31,4 +31,5 @@ sys_clock_speed = True swap = True memory = True uptime = True -wifi_signal = True \ No newline at end of file +wifi_signal = True +wifi_signal_dbm = True \ No newline at end of file diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 60b8677..61a500e 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -16,9 +16,10 @@ import os # get device host name - used in mqtt topic hostname = socket.gethostname() + def check_wifi_signal(): try: - full_cmd = "iwconfig wlan0 | grep -i --color quality" + full_cmd = "/sbin/iwconfig wlan0 | grep -i quality" wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[1].split('=')[1].split('/')[0] wifi_signal_calc = round((int(wifi_signal) / 70)* 100) @@ -27,6 +28,16 @@ def check_wifi_signal(): return wifi_signal_calc +def check_wifi_signal_dbm(): + try: + full_cmd = "/sbin/iwconfig wlan0 | grep -i quality" + wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] + wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[4].split('=')[1] + except Exception: + wifi_signal = 'NA' + return wifi_signal + + def check_used_space(path): st = os.statvfs(path) free_space = st.f_bavail * st.f_frsize @@ -147,6 +158,10 @@ def config_json(what_config): data["icon"] = "mdi:wifi" data["name"] = hostname + " Wifi Signal" data["unit_of_measurement"] = "%" + elif what_config == "wifi_signal_dbm": + data["icon"] = "mdi:wifi" + data["name"] = hostname + " Wifi Signal" + data["unit_of_measurement"] = "dBm" else: return "" # Return our built discovery config @@ -154,7 +169,7 @@ def config_json(what_config): def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0, - uptime_days=0, wifi_signal=0): + uptime_days=0, wifi_signal=0, wifi_signal_dbm=0): # connect to mqtt server client = paho.Client() client.username_pw_set(config.mqtt_user, config.mqtt_password) @@ -225,15 +240,24 @@ def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_s time.sleep(config.sleep_time) client.publish(config.mqtt_topic_prefix + "/" + hostname + "/wifi_signal", wifi_signal, qos=1) time.sleep(config.sleep_time) + if config.wifi_signal_dbm: + if config.discovery_messages: + client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_wifi_signal_dbm/config", + config_json('wifi_signal_dbm'), qos=0) + time.sleep(config.sleep_time) + client.publish(config.mqtt_topic_prefix + "/" + hostname + "/wifi_signal_dbm", wifi_signal_dbm, qos=1) + time.sleep(config.sleep_time) + + # disconnect from mqtt server client.disconnect() def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0, - uptime_days=0, wifi_signal=0): + uptime_days=0, wifi_signal=0, wifi_signal_dbm=0): # compose the CSV message containing the measured values - values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days, wifi_signal + values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days, wifi_signal, wifi_signal_dbm values = str(values)[1:-1] # connect to mqtt server @@ -250,7 +274,7 @@ def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_cl if __name__ == '__main__': # set all monitored values to False in case they are turned off in the config - cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = False + cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = wifi_signal_dbm = False # delay the execution of the script time.sleep(config.random_delay) @@ -274,8 +298,10 @@ if __name__ == '__main__': uptime_days = check_uptime() if config.wifi_signal: wifi_signal = check_wifi_signal() + if config.wifi_signal_dbm: + wifi_signal_dbm = check_wifi_signal_dbm() # Publish messages to MQTT if config.group_messages: - bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal) + bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm) else: - publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal) + publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm)