Page 1 of 1
MQTT and Timers exceptions
Posted: Fri Mar 31, 2017 1:16 pm
by Lornioiz
Hello,
I was toying with mqtt and I got stuck when I introduced a timer to call the mqtt connection.
This is what happens: if I call the mqtt object connect() method while the broker server is offline, I get an OSError exception. So far so good.
However, I need my mcu to send a data report every fixed amount of time and therefore I set up a timer. If the broker is offline, everything get stuck after the connect() call giving no exception message back.
I then tried to import the micropython module and create a buffer before init the timer as follows:
Code: Select all
import micropython
micropython.alloc_emergency_exception_buf(100)
However, I'm still stuck after the connect() waiting for the exception to surface...
Can anyone help? thank you!
Re: MQTT and Timers exceptions
Posted: Sat Apr 01, 2017 1:59 pm
by jcea
Would be nice if MQTT library included a timeout value. I do monkey patching:
Code: Select all
class timeout_socket(umqtt.simple.socket.socket):
def __init__(self):
super().__init__()
self.settimeout(5)
def setblocking(self, flag):
# "setblocking()" está documentado como
# variantes de "settimeout()".
# https://docs.micropython.org/en/latest/esp8266/library/usocket.html#usocket.socket.setblocking
timeout = 5 if flag else 0
self.settimeout(timeout)
class timeout_socket_module:
def __init__(self, socket):
self._socket = socket
def getaddrinfo(self, server, port):
return self._socket.getaddrinfo(server, port)
socket = staticmethod(timeout_socket)
#def socket(self):
# return timeout_socket()
umqtt.simple.socket = timeout_socket_module(umqtt.simple.socket)
MQTTClient = umqtt.simple.MQTTClient
This code implements a 5 seconds timeout. Somebody should do a pull request for micropython-lib...
Re: MQTT and Timers exceptions
Posted: Tue Apr 04, 2017 10:38 am
by Lornioiz
jcea wrote:Would be nice if MQTT library included a timeout value. I do monkey patching:
This code implements a 5 seconds timeout. Somebody should do a pull request for micropython-lib...
Thank you for your answer.
However, it may seems dumb to you as I'm just a beginner, but I thought that the problem was the use of the timer, because if I don't, it I get an exception as expected after few seconds.
Re: MQTT and Timers exceptions
Posted: Tue Apr 04, 2017 11:36 am
by jcea
Lornioiz wrote:jcea wrote:Would be nice if MQTT library included a timeout value. I do monkey patching:
This code implements a 5 seconds timeout. Somebody should do a pull request for micropython-lib...
Thank you for your answer.
However, it may seems dumb to you as I'm just a beginner, but I thought that the problem was the use of the timer, because if I don't, it I get an exception as expected after few seconds.
Hummm... With current micropython release, timers and Interrupts have quite a few restrictions:
http://docs.micropython.org/en/latest/e ... -callbacks
If you do things "not allowed", result is undefined. It can crash, malfunction, wrong results, etc.