diff --git a/README.md b/README.md index 595822a..e86cdee 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ The easiest way to track your Raspberry Pi or Ubuntu computer system health and ## What is new +* 2024-01-28: Improved error handling for the MQTT connection * 2024-01-28: Script version is displayed in home assistant device information * 2024-01-28: Update the script by calling it with command line argument --update * 2024-01-27: Now you can run the script as a service (systemd) or as a cron job diff --git a/src/rpi-cpu2mqtt.py b/src/rpi-cpu2mqtt.py index a22b1df..a3fdb2f 100644 --- a/src/rpi-cpu2mqtt.py +++ b/src/rpi-cpu2mqtt.py @@ -150,7 +150,7 @@ def check_git_update(script_dir): except subprocess.CalledProcessError as e: print("Error updating git repository:", e.output) - if any(s in git_update for s in ('Your branch is up to date', 'Your branch is up-to-date')): + if any(s in git_update for s in ('Your branch is up to date', 'Your branch is up-to-date', 'Votre branche est à jour')): git_update = 'off' else: git_update = 'on' @@ -300,14 +300,30 @@ def config_json(what_config): return json.dumps(data) +def on_log(client, userdata, level, buf): + if level == paho.MQTT_LOG_ERR: + print("MQTT error: ", buf) + +def on_connect(client, userdata, flags, rc): + if rc != 0: + print("Error: Unable to connect to MQTT broker, return code:", rc) + + 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, wifi_signal_dbm=0, rpi5_fan_speed=0, git_update=False): # connect to mqtt server client = paho.Client(client_id="rpi-mqtt-monitor-" + hostname) client.username_pw_set(config.mqtt_user, config.mqtt_password) - client.connect(config.mqtt_host, int(config.mqtt_port)) - client.loop_start() + client.on_log = on_log + client.on_connect = on_connect + try: + client.connect(config.mqtt_host, int(config.mqtt_port)) + except Exception as e: + print("Error connecting to MQTT broker:", e) + return + + client.loop_start() # publish monitored values to MQTT if config.cpu_load: if config.discovery_messages: @@ -382,10 +398,16 @@ def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_cl values = cpu_load, cpu_temp, used_space, voltage, int(sys_clock_speed), swap, memory, uptime_days, wifi_signal, wifi_signal_dbm, rpi5_fan_speed, git_update values = str(values)[1:-1] - # connect to mqtt server client = paho.Client(client_id="rpi-mqtt-monitor-" + hostname) client.username_pw_set(config.mqtt_user, config.mqtt_password) - client.connect(config.mqtt_host, int(config.mqtt_port)) + client.on_log = on_log + client.on_connect = on_connect + + try: + client.connect(config.mqtt_host, int(config.mqtt_port)) + except Exception as e: + print("Error connecting to MQTT broker:", e) + return # publish monitored values to MQTT client.publish(config.mqtt_topic_prefix + "/" + hostname, values, qos=config.qos, retain=config.retain) diff --git a/src/update.py b/src/update.py index 1991750..e5296df 100644 --- a/src/update.py +++ b/src/update.py @@ -39,11 +39,11 @@ def display_config_differences(current_config, example_config, display=True): else: return False -def update_config_version(version): - with open('config.py', 'r') as f: +def update_config_version(version, script_dir): + with open(script_dir + '/config.py', 'r') as f: lines = f.readlines() - with open('config.py', 'w') as f: + with open(script_dir + '/config.py', 'w') as f: print(":: Updating config version to {}".format(version)) for line in lines: if 'version = ' in line: @@ -62,10 +62,10 @@ def do_update(version=config.version, git_update=True, config_update=True): if display_config_differences(script_dir + '/config.py', script_dir + '/config.py.example') and config_update: print(":: Updating config.py") - update_config(script_dir + 'config.py',script_dir + 'config.py.example') + update_config(script_dir + '/config.py',script_dir + '/config.py.example') if version != config.version: - update_config_version(version) + update_config_version(version, script_dir) if __name__ == '__main__':