MQTT and Timers exceptions

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

MQTT and Timers exceptions

Post by Lornioiz » Fri Mar 31, 2017 1:16 pm

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!

jcea
Posts: 27
Joined: Fri Mar 18, 2016 5:28 pm

Re: MQTT and Timers exceptions

Post by jcea » Sat Apr 01, 2017 1:59 pm

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

Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

Re: MQTT and Timers exceptions

Post by Lornioiz » Tue Apr 04, 2017 10:38 am

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.

jcea
Posts: 27
Joined: Fri Mar 18, 2016 5:28 pm

Re: MQTT and Timers exceptions

Post by jcea » Tue Apr 04, 2017 11:36 am

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.

Post Reply