ThomDietrich / miflora-mqtt-daemon

Linux service to collect and transfer Xiaomi Mi Flora plant sensor data via MQTT to your smart home system, with cluster support 🌱🌼🥀🏡🌳
MIT License
607 stars 139 forks source link

Damon stop working #19

Open ViperRNMC opened 6 years ago

ViperRNMC commented 6 years ago

Hi, i install the daemon on a pi 0w with raspbian. Works great for a while, but couple of weeks I got almost every 1 a 2 days an error and my daemon stops working.

* miflora.service - Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
   Loaded: loaded (/etc/systemd/system/miflora.service; enabled; vendor preset: 
   Active: failed (Result: exit-code) since Thu 2018-01-11 10:47:28 UTC; 29min a
     Docs: https://github.com/ThomDietrich/miflora-mqtt-daemon
  Process: 242 ExecStart=/opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py (code=e
 Main PID: 242 (code=exited, status=1/FAILURE)
   Status: "Jan 11 10:47:28 - MQTT connection error. Please check your settings 

jan 11 10:47:20 pi-zero-w systemd[1]: Starting Xiaomi Mi Flora Plant Sensor MQTT
jan 11 10:47:28 pi-zero-w miflora-mqtt-daemon.py[242]: [2018-01-11 10:47:28] MQT
jan 11 10:47:28 pi-zero-w systemd[1]: miflora.service: Main process exited, code
jan 11 10:47:28 pi-zero-w systemd[1]: Failed to start Xiaomi Mi Flora Plant Sens
jan 11 10:47:28 pi-zero-w systemd[1]: miflora.service: Unit entered failed state
jan 11 10:47:28 pi-zero-w systemd[1]: miflora.service: Failed with result 'exit-
ThomDietrich commented 6 years ago

Please provide the non-cut version of the error messages. Besides that the error seems clear, your mqtt broker is not reachable...

ViperRNMC commented 6 years ago

this is the output of the Damon status, where can I find the log?

ThomDietrich commented 6 years ago

You just have to maximize your console window =)

ViperRNMC commented 6 years ago

strange enough when I start the service again it connects fine

* miflora.service - Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
   Loaded: loaded (/etc/systemd/system/miflora.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2018-01-17 17:19:12 UTC; 54min ago
     Docs: https://github.com/ThomDietrich/miflora-mqtt-daemon
  Process: 243 ExecStart=/opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py (code=exited, status=1/FAILURE)
 Main PID: 243 (code=exited, status=1/FAILURE)
   Status: "Jan 17 17:19:11 - MQTT connection error. Please check your settings in the configuration file "config.ini"."

jan 17 17:19:03 pi-zero-w systemd[1]: Starting Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon...
jan 17 17:19:11 pi-zero-w miflora-mqtt-daemon.py[243]: [2018-01-17 17:19:11] MQTT connection error. Please check your settings in the configuration file "config.in
jan 17 17:19:12 pi-zero-w systemd[1]: miflora.service: Main process exited, code=exited, status=1/FAILURE
jan 17 17:19:12 pi-zero-w systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
jan 17 17:19:12 pi-zero-w systemd[1]: miflora.service: Unit entered failed state.
jan 17 17:19:12 pi-zero-w systemd[1]: miflora.service: Failed with result 'exit-code'.
~
~
ThomDietrich commented 6 years ago

Hmm that means that your broker is temporarily not available I guess. Maybe you need to add reconnection attempts (a simple loop with a short waiting period) to the code here. I can't do that at the moment but when you find a configuration that works, please let us know!

chrostek commented 6 years ago

@ThomDietrich can you please add Restart=always to the [Service] part of the systemd service file? this helped in my case.

ThomDietrich commented 6 years ago

When is this needed? I never had issues with the execution.

I am not against the addition. Please create a PR. Thanks!

chrostek commented 6 years ago

On my system the services crashed every 2-3 hours because of connection issues. i created a PR

ThomDietrich commented 6 years ago

The PR is merged but this issue should definitely be solved inside the daemon by checking the connection prior to sending data.

yayitazale commented 4 years ago

Hi:

I have a related Issue. If the MQTT server is down for an update for some time, before it restarts I didn't get new messages until I reboot the RPI or restart the daemon service, so I think that the daemon looses the connection and It gets stucked.

¿Is there any way to change the reconnection loop to make it infinite?

Thanks for your work

yayitazale commented 4 years ago

I'm not sure but I think you have to add this:

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print("Unexpected disconnection. Reconnecting...")
        mqtt_client.reconnect()
    else :
        print "Disconnected successfully"

and this:

mqtt_client.on_disconnect = on_disconnect

But I'm not sure that this is happening. I think that the problem comes froms this:

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print_line('MQTT connection established', console=True, sd_notify=True)
        print()
    else:
        print_line('Connection error with result code {} - {}'.format(str(rc), mqtt.connack_string(rc)), error=True)
        #kill main thread
        os._exit(1)

Going to an os._exit because of a connection error is maybe stoping the daemon after several reconnection attempts... not sure.