miflora-mqtt-daemon icon indicating copy to clipboard operation
miflora-mqtt-daemon copied to clipboard

Damon stop working

Open ViperRNMC opened this issue 6 years ago β€’ 11 comments

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-

ViperRNMC avatar Jan 11 '18 11:01 ViperRNMC

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

ThomDietrich avatar Jan 11 '18 13:01 ThomDietrich

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

ViperRNMC avatar Jan 11 '18 14:01 ViperRNMC

You just have to maximize your console window =)

ThomDietrich avatar Jan 15 '18 14:01 ThomDietrich

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'.
~
~

ViperRNMC avatar Jan 17 '18 18:01 ViperRNMC

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!

ThomDietrich avatar Jan 17 '18 18:01 ThomDietrich

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

chrostek avatar Apr 11 '18 18:04 chrostek

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

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

ThomDietrich avatar Apr 18 '18 13:04 ThomDietrich

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

chrostek avatar Apr 18 '18 19:04 chrostek

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

ThomDietrich avatar Apr 18 '18 19:04 ThomDietrich

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 avatar Feb 03 '20 11:02 yayitazale

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.

yayitazale avatar Feb 03 '20 12:02 yayitazale