diff --git a/README.md b/README.md index 3ab1cb0..a78e907 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Raspberry Pi MQTT monitor -Python script to check the cpu load, cpu temperature, free space, used memory, swap usage, voltage and system clock speed +Python script to check the cpu load, cpu temperature, free space, used memory, swap usage, uptime, wifi signal quality voltage and system clock speed on a Raspberry Pi or any computer running Ubuntu and publish this data to a MQTT broker. I wrote this to monitor my raspberries at home with [home assistant](https://www.home-assistant.io/). The script works fine both on Python 2 and 3 @@ -23,7 +23,7 @@ masoko/rpi4 The csv message looks like this: ```csv -9.0, 43.0, 25, 25, 0.85, 1500, False, False +9.0, 43.0, 25, 25, 0.85, 1500, False, False, False ``` Disabled sensors are represented with False in the message. @@ -73,6 +73,7 @@ sys_clock_speed = True swap = False memory = False uptime = True +wifi_signal = True ``` 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. @@ -80,7 +81,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 +1.3, 47.1, 12, 1.2, 600, nan, 14.1, 12, 50.0 ``` Test the script. @@ -144,12 +145,19 @@ This is the sensors configuration if ```group_messages = True``` assuming your s value_template: '{{ value.split(",")[6] }}' name: rpi4 memory unit_of_measurement: "%" + - platform: mqtt state_topic: 'masoko/rpi4' value_template: '{{ value.split(",")[7] }}' name: rpi4 uptime unit_of_measurement: "days" + - platform: mqtt + state_topic: 'masoko/rpi4' + value_template: '{{ value.split(",")[8] }}' + name: rpi4 wifi signal + unit_of_measurement: "%" + ``` This is the sensors configuration if ```group_messages = False``` assuming your sensors are separated in ```sensors.yaml``` file. @@ -188,10 +196,17 @@ This is the sensors configuration if ```group_messages = False``` assuming your state_topic: "masoko/rpi4/memory" name: rpi4 memory unit_of_measurement: "%" + - platform: mqtt state_topic: "masoko/rpi4/uptime_days" name: rpi4 uptime unit_of_measurement: "days" + + - platform: mqtt + state_topic: "masoko/rpi4/wifi_signal" + name: rpi4 wifi signal + unit_of_measurement: "%" + ``` Add this to your ```customize.yaml``` file to change the icons of the sensors. @@ -231,6 +246,7 @@ entities: - entity: sensor.rpi4_swap - entity: sensor.rpi4_memory - entity: sensor.rpi4_uptime + - entity: sensor.rpi4_wifi_signal ``` # 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) diff --git a/install.sh b/install.sh index cc668e9..c8b658f 100755 --- a/install.sh +++ b/install.sh @@ -60,6 +60,7 @@ check_and_install_pip(){ install_requirements(){ printm "Installing requirements" pip install -r requirements.txt + sudo pip3 install -r requirements.txt } update_config(){ diff --git a/remote_install.sh b/remote_install.sh index 6faeea1..cad44ee 100755 --- a/remote_install.sh +++ b/remote_install.sh @@ -9,6 +9,12 @@ printm(){ } main(){ + if [[ $(git --version) ]]; then + git=$(which git) + else + sudo apt-get install git + fi + printm "Cloning rpi-mqtt-monitor git repository" git clone https://github.com/hjelev/rpi-mqtt-monitor.git cd rpi-mqtt-monitor diff --git a/src/config.py.example b/src/config.py.example index c666375..75ef50c 100644 --- a/src/config.py.example +++ b/src/config.py.example @@ -31,3 +31,4 @@ sys_clock_speed = True swap = True memory = True uptime = True +wifi_signal = True \ No newline at end of file diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index 50e41b4..60b8677 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -16,6 +16,16 @@ 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" + 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) + except Exception: + wifi_signal_calc = 'NA' + return wifi_signal_calc + def check_used_space(path): st = os.statvfs(path) @@ -114,7 +124,7 @@ def config_json(what_config): data["name"] = hostname + " Disk Usage" data["unit_of_measurement"] = "%" elif what_config == "voltage": - data["icon"] = "mdi:speedometer" + data["icon"] = "mdi:current-dc" data["name"] = hostname + " CPU Voltage" data["unit_of_measurement"] = "V" elif what_config == "swap": @@ -133,6 +143,10 @@ def config_json(what_config): data["icon"] = "mdi:timer" data["name"] = hostname + " Uptime" data["unit_of_measurement"] = "days" + elif what_config == "wifi_signal": + data["icon"] = "mdi:wifi" + data["name"] = hostname + " Wifi Signal" + data["unit_of_measurement"] = "%" else: return "" # Return our built discovery config @@ -140,7 +154,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): + uptime_days=0, wifi_signal=0): # connect to mqtt server client = paho.Client() client.username_pw_set(config.mqtt_user, config.mqtt_password) @@ -204,15 +218,22 @@ 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 + "/uptime_days", uptime_days, qos=1) time.sleep(config.sleep_time) + if config.wifi_signal: + if config.discovery_messages: + client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_wifi_signal/config", + config_json('wifi_signal'), qos=0) + time.sleep(config.sleep_time) + client.publish(config.mqtt_topic_prefix + "/" + hostname + "/wifi_signal", wifi_signal, 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): + uptime_days=0, wifi_signal=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 + values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days, wifi_signal values = str(values)[1:-1] # connect to mqtt server @@ -229,7 +250,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 = False + cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = False # delay the execution of the script time.sleep(config.random_delay) @@ -251,8 +272,10 @@ if __name__ == '__main__': memory = check_memory() if config.uptime: uptime_days = check_uptime() + if config.wifi_signal: + wifi_signal = check_wifi_signal() # 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) + bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal) else: - publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days) + publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal)