QUERY · ISSUE
"OSError: -1" if use keepalive>0
Traceback (most recent call last):
File "main.py", line 55, in <module>
File "umqtt/simple.py", line 202, in check_msg
File "umqtt/simple.py", line 171, in wait_msg
OSError: -1
CANDIDATE · ISSUE
[esp8266] OSError: [Errno 113] EHOSTUNREACH only when executing connect() in main.py
Hi
For the following code,
import machine
import network
from umqtt.simple import MQTTClient
led = machine.Pin(D1, machine.Pin.OUT)
button = machine.Pin(D0, machine.Pin.IN)
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)
sta_if.active(True)
sta_if.connect('my-Lan', 'My-passwoed')
client = MQTTClient("esp-button", "192.168.1.199")
client.connect()
while True:
if button.value() == 1:
led.value(1)
else:
led.value(0)
I'm experiencing the error below:
Traceback (most recent call last):
File "main.py", line 17, in <module>
File "umqtt/simple.py", line 56, in connect
OSError: [Errno 113] EHOSTUNREACH
Line 17 is client.connect() and line 56 is self.sock.connect(self.addr).
The peculiarity is that the exact same code can be run without errors when imputed step by step into the interactive shell. Actually, as soon as the exception occurs and the control returns to the shell I can run client.connect() without errors (returns 0) which is shown in the MQTT broker log.
The version used is: MicroPython v1.8.7-7-gb5a1a20a3 on 2017-01-09 on ESP8266
Please try using the latest umqtt library and then provide a minimal reproducible test case that shows the error.
guess i'll just take over this issue thread ;)
when using keepalive>0, server disconnects from client and client raises an exception in given line 171 in wait_msg. So the socket disconnection is being correctly detected.
disregard my error description below! (only if you have the same problem and need a solution)
the solution was to connect to the mqtt server with mqtt.connect(False) as only then a persistent session is started and the subscriptions are stored between connection problems. see #186
I forgot that in the connect method at the beginning and did it only in the reconnect function..
the only problem i have left is that with a restart of mosquitto, mosquitto loses all subscriptions although persistence is on. but this is not an issue of this library.
now to the original issue:
i did set a last will message that is correctly send by the server.
After this exception my client tries to reconnect and does it correctly (mqtt.connect(False) or True, does not make a difference here).
I confirm the connection by sending out an online status from the client.
Sending a message to the client now does nothing. the client just does not appear to get the message.
my thoughts on this so far:
Steps to reproduce:
maybe i have a misconfigured server, won't exclude that possibility although it would be rather strange if that is the problem.
I'm using a recent version of the mosquitto server.
I had same problem with continue disconnecting from the mqtt server with keepalive > 0.
But I figure it out that disconnection happens because there was no any activity from my board with mqtt server during the period of time like keepalive value of seconds.
So if the board will publish any mqtt message ("i'm alive") - the connection will not drop.
I program to send the current board time to the debug topic during the half of the keepalive value of seconds. And it is working like a charm!
If I correct this issue is not an issue it is a feature.
If you set keepalive value - you have to send something to mqtt server to inform that you are alive in less time than keepalive value of seconds.
I'm not sure is it task for mqtt client to keep connection alive in time.
The umqtt/simple.py lib is only sending information to the server about keepalive time.
Nothing more. It looks that this task to user who use this lib.
MQTT v3.1.1 specification
that's why I call mqtt.ping() regularly. Together with last will this shows the device state in home-assistant.
But I do not check if the server actually answers the request. If the server is down, the socket breaks anyways, throwing an exception, so the only problem left would be a mqtt server that is stuck, but this should be really rare.
I'm using about a minute as keepalive.
The problem remains unsolved.
I also have this problem.
Hi! look this project https://github.com/leech001/Security-fire-alarm-ESP8266
I think I solved the problem there
keepLiveTime = 120
c = MQTTClient(CLIENT_ID, server, 1883, username, password, keepLiveTime)
tim = Timer(-1)
tim.init(period=keepLiveTime*1000, mode=Timer.PERIODIC, callback=lambda t: keepLiveTimeCb(c))
def keepLiveTimeCb(c):
c.ping()
shell use ping() func
I've explored this issue and its comments and believe it can be closed:
ping()the broker, or otherwise send the broker some traffic to communicate that it's still alive.connect()called withoutclean_session=False, etc. If it is reproducible, some code, and a new issue would be great. However, it looks like this was resolved using a periodicping()您好,您的邮件已收到。