0

Client sendPacket returned: 0 Failed to send packet.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Client sendPacket returned: 0 Failed to send packet.

by candrian on Sat Jun 30, 2018 2:09 pm

Hello,

I have build a project and i have a really strange behavior. When my ESP8266 on NodeMCU 0.9 tries to connect to MQTT in debug mode it returns

Code: Select all | TOGGLE FULL SIZE
Connecting to MQTT...
Server:io.adafruit.com
Server Port:1883
Username:****
Key:****
Connecting to: io.adafruit.com
pm open,type:2 0
Connect result: 1
SERVER GENERATING CLIENT ID
MQTT connect packet:

---The packet here---

Client sendPacket returned: 0
Failed to send packet.


After a number of retries it manages to connect to MQTT then it gets disconnected and starts again trying for a number of retries until its again connected and etc etc.

I tried and flashed the same code on another ESP, connected on the same WIFI and it runs without any issues. How can this be explained?

Why does it fail to send the connect packet?

candrian
 
Posts: 3
Joined: Thu Jun 28, 2018 11:23 am

Re: Client sendPacket returned: 0 Failed to send packet.

by candrian on Mon Jul 02, 2018 3:05 am

Code: Select all | TOGGLE FULL SIZE
bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len) {
  uint16_t ret = 0;

  while (len > 0) {
    if (client->connected()) {
      // send 250 bytes at most at a time, can adjust this later based on Client

      uint16_t sendlen = min(len, (uint16_t)250);
      //Serial.print("Sending: "); Serial.println(sendlen);
      ret = client->write(buffer, sendlen);
      DEBUG_PRINT(F("Client sendPacket returned: ")); DEBUG_PRINTLN(ret);


Looking the code it seems that the client->write returns 0 so it means that it doesn't write any bytes to the server. Does anybody knows anything more?

Could this be a hardware failure? The strangest thing is that after a number of retries it manages to connect and then it takes again a lot of retries to reconnect.

candrian
 
Posts: 3
Joined: Thu Jun 28, 2018 11:23 am

Re: Client sendPacket returned: 0 Failed to send packet.

by brubell on Mon Jul 02, 2018 9:45 am

Heya candrian. I'm going to attempt to replicate this to look into it further.

Could you provide which library (and version number of the library) you are using? Also please paste the code of the sketch you are working on.

Thanks!

brubell
 
Posts: 68
Joined: Fri Jul 17, 2015 10:33 pm

Re: Client sendPacket returned: 0 Failed to send packet.

by candrian on Thu Jul 05, 2018 4:35 am

Hi again,

Thanks for your will to replicate my issue. Finally it seems i found the reason why it didn't work.

While i was working with the ESP plugged in to my laptop i thought to try and move closer to the internet wifi router and while i was moving closer i saw that the MQTT connected immediately! I moved back again to the spot where i had first installed the ESP and i faced the same issue. So i was closer to the solution and everything looked to be a wifi or RF issue. So i scanned for other neighbor wifi networks that may interfere my connection and no other wifi was on the same channel (number 2) but i switched to channel 1 which was also empty with no changes and then i thought that because channel 1 and 2 are close to the start of the range maybe the hardware for some reason can't perform well so i changed to channel 12 and suddenly everything worked!!!

The wifi power was kept in the same amount in all tests and only the wifi channel was switched.

Isn't it strange? Most of the surrounding/neighbor wifi where between channel 4 to 10 with most of them to channel 6.

Conclusion: the MQTT couldn't send via wifi the packet due to noise or loss of power on that channel and returned that error. So keep it in mind!

The lesson learned is that each time we face an issue we have to check everything from the beginning. As it seemed the log message wasn't that close to the actual issue. Maybe somebody with more knowledge on the struct of the libraries would have thought that it might be a wifi RF issue.

EDIT:
Also note that the wifi was always connected when i was facing the above issues.

candrian
 
Posts: 3
Joined: Thu Jun 28, 2018 11:23 am

Please be positive and constructive with your questions and comments.