MQTT connection fails intermittently

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
greenorange
 
Posts: 7
Joined: Tue Jan 03, 2017 8:11 am

MQTT connection fails intermittently

Post by greenorange »

It is a project based on Arduino + Fona, using the Adafruit MQTT and Fona-related library. Most of the time the system works very well and is able to publish data to feeds... but except that the connection could also fail intermittently. When i trace down to the function:

Adafruit_MQTT::connect()

It is seen that, after sendPacket() is called to send out the MQTT packet (of 57 bytes as seen on serial port), and then readFullPacket() is called to do a read back, it should normally return 4 bytes of data when things go well. On the contrary, if 0 bytes is read, the connection will eventually fail. I have observed this many times and am wondering whether it is a local software issue, or whether it is the server side refusing to connect at that particular period of time.

Is there anything that can be done at the device side to make the connection more intact? below is part of the traffic:

Writing packet
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
AT+CIPSEND=57
0x10 0x37 0x0 0x4 0x4D 0x51 0x54 0x54 0x4 0xFFFFFFC2 0x1 0x2C 0x0 0x0 0x0 0x7 0x53 0x30 0x30 0x5F 0x57 0x57 0x46 0x0 0x20 0x62 0x36 0x33 0x39 0x64 0x38 0x37 0x38 0x38 0x36 0x36 0x64 0x34 0x34 0x31 0x65 0x38 0x37 0x62 0x37 0x35 0x33 0x63 0x31 0x61 0x38 0x38 0x62 0x37 0x37 0x34 0x39
<--- >
GO!01
<--- SEND OK
Reading data..
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPRXGET=4
<--- +CIPRXGET: 4,0
0 bytes available
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPRXGET=4
<--- +CIPRXGET: 4,0
0 bytes available
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPRXGET=4
<--- +CIPRXGET: 4,0
0 bytes available
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPRXGET=4
<--- +CIPRXGET: 4,0
0 bytes available
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPRXGET=4
<--- +CIPRXGET: 4,4
4 bytes available
1 bytes read
0x20
Read: [0x20],
Packet Type: [0x20],
Reading data..
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPRXGET=4
<--- +CIPRXGET: 4,3
3 bytes available
1 bytes read
0x2
Read: [0x02],
Packet Length: 2
Reading data..
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPSTATUS
<--- OK
<--- STATE: CONNECT OK
---> AT+CIPRXGET=4
<--- +CIPRXGET: 4,2
2 bytes available
2 bytes read
0x0 0x6
Read: [0x00], [0x06],
Failed to subscribe
Retrying MQTT connection

User avatar
ds18s20
 
Posts: 38
Joined: Tue Jan 24, 2012 7:45 pm

Re: MQTT connection fails intermittently

Post by ds18s20 »

same here; upon loss of connection only a reboot makes it work again; very interested if you find a remedy to the issue with AIO. Thanks

User avatar
rtapp1
 
Posts: 3
Joined: Sat Aug 13, 2016 7:15 am

Re: MQTT connection fails intermittently

Post by rtapp1 »

I have some huzzah feather esp8266's that collect sensor data... temps, ac unit running, door locked, water heater heating, stuff like that. They are sending the data to an mqtt server being managed with home assistant. They were rock solid for 6 or 8 months or so until I changed the code and now I have intermittent mqtt connection fails that take a reset to get it going again.

I haven't figured it out just yet, but I didn't have trouble with it until I put lastwill into the code. I have the module send out its ip address, mac address and whether the thing is online. I have a bunch of devices on the network and this way I can see what ip's are assigned to the esp8266's and whether they are online or not. Adding the lastwill gives mea way to show that it is offline if the connection is dropped and has not been reconnected, so I like the option. But since I added the code, it will drop the connection after a day or so and not reconnect. It works, when the module powers up, it connects to mqtt broker and sends a message that it is "ONLINE" and gives a lastwill of "OFFLINE". Unplug the module and it fails to OFFLINE after the set time. But something is happening and it is dying and not reconnecting. The broker does give the lastwill message of OFFLINE, so it is partly working. I probably have a simple error in a few lines of code or something. I have several modules and I have mostly used the same basic code for all of the modules, so I think it is a line of code or something that has it jacked. I will report my findings if I figure it out and I will keep an eye on this thread to see if someone has any info.

Rob

User avatar
ds18s20
 
Posts: 38
Joined: Tue Jan 24, 2012 7:45 pm

Re: MQTT connection fails intermittently

Post by ds18s20 »

Thanks for the tip Rob - to clarify; your broker is in fact AIO, yes?

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”