Has anyone successfully used Paho MQTT Python library with Digi TransPort WR21?

Here is link to the library.

I tried it first on desktop computer and got it running in Python 2.6. Running the same code in Digi TransPort WR21 didnt work. It gave some errors in this part of the code:


def _socketpair_compat():
    """TCP/IP socketpair including Windows support"""
    listensock =    socket.socket(socket.AF_INET,socket.SOCK_STREAM,socket.IPPROTO_IP)      listensock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
    listensock.bind(("127.0.0.1", 0))
    listensock.listen(1)

    iface, port = listensock.getsockname()
    sock1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM,socket.IPPROTO_IP)
    sock1.setblocking(0)
    try:
        sock1.connect(("localhost", port))
    except socket.error as err:
        if err.errno != errno.EINPROGRESS and err.errno != errno.EWOULDBLOCK and err.errno !=EAGAIN:
            raise
    sock2, address = listensock.accept()
    sock2.setblocking(0)
    listensock.close()
    return (sock1, sock2)

Errors produced by line “sock1.connect((“localhost”, port))”

gaierror: [Errno 4] getaddrinfo failed
error: [Errno 118] ERROR

I tried changing localhost to 127.0.0.1 but it didnt help. When googling about this issue, some discussions mentioned that it could also be a problem with device network settings.

Best regards,
Juha

I use the socket and digihw modules and they work fine.

I don’t see that module you want to use listed here:
http://www.digi.com/wiki/developer/index.php/Transport_Python_Programmer’s_Guide#Supported_Python_Modules

Cheers,
John

I agree with you that socket and digihw modules work.

But I am trying to use Paho MQTT library to connect my Digi TransPort to MQTT broker. Obviously this Paho MQTT library uses socket module and many other basic Python 2.6 modules to implement MQTT communication. As far as I know it should be Python 2.6 and Digi TransPort compatible with few limitations.

So I think I know what is the issue in this case. socket.getsockname() doesnt return port which was bound to the opened socket.


In ConnectPort X4:

>>> import socket
>>> listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
>>> listensock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>>> listensock.bind((“127.0.0.1”, 0))
>>> listensock.listen(1)
>>> iface, port = listensock.getsockname()
>>> port
64178


In Digi TransPort WR21:

>>> import socket
>>> listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
>>> listensock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>>> listensock.bind((“127.0.0.1”, 0))
>>> listensock.listen(1)
>>> iface, port = listensock.getsockname()
>>> port
0


I can see from Digi TransPort WR21 web management that port is bound successfully, but how can I get this port number in Python? Is there some other way to implement this same functionality?

Did you ever get this to work? I’m also trying to use paho and socket.getsockname() does seem to be the culprit.

Did you use another mqtt library?

Thanks,

Hi,

I didn´t get automatic port selection to work so I just manually assign a certain port to be used. It is not the best solution, but it is working for now.

Below is the part of Paho MQTT library that I edited and then this library works with my Digi TransPort.

def _socketpair_compat():
“”“TCP/IP socketpair including Windows support”“”
listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
listensock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listensock.bind((“127.0.0.1”, 22222))
listensock.listen(1)
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
sock1.setblocking(0)
try:
sock1.connect((“127.0.0.1”, 22222))
except:
pass
sock2, address = listensock.accept()
sock2.setblocking(0)
listensock.close()
return (sock1, sock2)