Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
The Royal We
Posts: 8
Joined: Thu Jan 12, 2017 5:40 pm

Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by The Royal We » Thu Jan 12, 2017 6:14 pm

First, I am very new to programming, microcontollers, and networking so please be gentle.

I currently have two Adafruit Feather Huzzah's; Feather1 has a temperature sensor attached to it, Feather2 has a 7 segment display attached to it. I also have a Raspberry Pi running as an MQTT broker. The end goal is to have multiple temperature sensors transmitting data via MQTT to the display.

Currently I am able to publish from the temp sensor on Feather1 to my laptop. Additionally, I am able to subscribe from the display on Feather2 from my laptop. However, whenever I try to publish from Feather1 to Feather2 I get :

File "umqtt/simple.py", line 56, in connect
OSError: [Errno 103] ECONNABORTED

I have a suspicion that the problem has something to do with network sockets based on following that traceback.

When I run ifconfig on each of the feathers (as well as when looking on Fing) I see that they each have different IP addresses on my local network, but their gateway and DNS are the same (10.0.1.1). I cannot find information in the documentation about adjusting the DNS on the feather, but have a feeling its being determined by the router (5th gen Apple Airport Extreme).

My connection to the network is basically this code here https://docs.micropython.org/en/latest/ ... networking

Any help on getting these feathers to send data to one another would be greatly appreciated. Thanks for looking and please let me know if I can clarify with details.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by torwag » Thu Jan 12, 2017 7:29 pm

Hi,

Not sure I understood you right, however in MQTT there is nothing like publish from one device to another. Each and every message goes to the mqtt broker (your rpi). From there other clients can subscribe to certain channels and read what others published there.
In a sense MQTT is for IOT devices what a chat server like irc (or whatsapp) is for humans. You do not connect between devices but both devices connect to the server.

Might this be your problem?

User avatar
The Royal We
Posts: 8
Joined: Thu Jan 12, 2017 5:40 pm

Re: Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by The Royal We » Thu Jan 12, 2017 9:09 pm

Thank you for the response and apologies for any confusion. I am using the broker on the rpi as an intermediary and I have had success in various tests. I am able to take data from the temp sensor on one feather, pass it via MQTT to the rpi broker and read it on a laptop. Similarly, I am able to take data from my laptop, pass it through the rpi broker and display it on another feather. Its when I go from temp sensor feather via rpi to display feather that things break. Here is a visualization that I made to help me conceptualize

These two scenarios work:
Temp Sensor Feather ---[publish to topic1]---> RaspberryPi MQTT broker ---[subscribe to topic1]---> Laptop
Laptop ---[publish to topic1]---> RaspberryPi MQTT broker ---[subscribe to topic1]---> Display Feather

However, when I try the following I get the ECONNABORTED error...
Temp Sensor Feather ---[publish to topic1]---> RaspberryPi MQTT broker ---[subscribe to topic1]---> Display Feather

Hope this helps clarify.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by torwag » Thu Jan 12, 2017 9:16 pm

Hi,
yes this makes things more clear.
Which unit fails to connect? the display or the sensor unit?

ADDON:

Did you make sure you have two unique client IDs set during the initialisation of the MQTT client?

User avatar
The Royal We
Posts: 8
Joined: Thu Jan 12, 2017 5:40 pm

Re: Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by The Royal We » Thu Jan 12, 2017 9:27 pm

The display unit is the one that fails to connect. The sensor unit appears to be chugging along just fine.

EDIT:
Both feathers had different client id's in the initialization

hsmade
Posts: 4
Joined: Thu Jan 12, 2017 9:31 pm

Re: Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by hsmade » Thu Jan 12, 2017 9:33 pm

I see the same issue. As soon as I establish 2 connections to my broker, with different IDs, I will get this error message. When I close one connection, it works.

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by torwag » Thu Jan 12, 2017 11:46 pm

Which kind of broker do you use. This is indeed a rather strange behaviour as I can't see in which way umqtt should be involved here. The client does not have any idea how many other clients or which clients are connected to the broker.

hsmade
Posts: 4
Joined: Thu Jan 12, 2017 9:31 pm

Re: Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by hsmade » Fri Jan 13, 2017 7:53 am

I'm thinking that if it's a micropython related problem, it could be in socket.
I'm using mosquitto on raspberry pi. I will do a test with 2 simultaneously connected python clients on intel, to see if I can reproduce it on another platform as well.

-- Wim

hsmade
Posts: 4
Joined: Thu Jan 12, 2017 9:31 pm

Re: Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by hsmade » Fri Jan 13, 2017 8:07 am

Ok, so this is indeed a problem with the socket implementation:

>>> import socket
>>> sock = socket.socket()
>>> addr = socket.getaddrinfo('192.168.178.35', 80)[0][-1]
>>> sock.connect(addr)
>>> sock.write('GET / HTTP/1.1\nHost: localhost\n\n')
32
>>> print(sock.read(10))
b'HTTP/1.1 2'


>>> sock2 = socket.socket()
>>> addr = socket.getaddrinfo('192.168.178.35', 80)[0][-1]
>>> sock2.connect(addr)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 103] ECONNABORTED

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problem with Multiple ESP8266 and MQTT. Blocked Sockets?

Post by pythoncoder » Sat Jan 14, 2017 6:53 am

I don't understand why your display unit is opening two sockets. I'd expect to see a single instance of the MQTTClient class, with all publications and subscriptions being routed through that. A single instance opens just one socket.

On the other hand it does look as if you've found a bug: if confirmed it should be reported on GitHub.
Peter Hinch
Index to my micropython libraries.

Post Reply