Cannot allocate pbuf to receive packet

HI, stucked once again.

I’m trying to establish an mqtt connection, but I’m getting “cannot allocate pbuf packet” from lwip. Any clue?

This is my code

/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

/* FreeRTOS includes. */
#include “FreeRTOS.h”
#include “task.h”
#include “queue.h”

#include “lwip/api.h”
#define usTaskStackSize 1024

#include “MQTTClient/MQTTClient.h”

#include “wifi_conf.h”

#define MQTT_SELECT_TIMEOUT 1

static void messageArrived(MessageData* data)
{
DiagPrintf(“Message arrived on topic %s: %s\n”, data->topicName->lenstring.data, (char *)data->message->payload);
}

void MQTT_Msg_Received()
{
/* To avoid gcc warnings /
//( void ) pvParameters;
DiagPrintf(“MQTT Recv\n”);
/
connect to gpssensor.ddns.net, subscribe to a topic, send and receive messages regularly every 5 sec /
MQTTClient client;
Network network;
unsigned char sendbuf[512], readbuf[80];
int rc = 0, count = 0;
MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer;
char
address = “192.168.1.239”;
char* sub_topic = “Ameba/test/#”;
char* pub_topic = “Ameba/Publish”;

memset(readbuf, 0x00, sizeof(readbuf));

DiagPrintf(“Network Init\n”);
NetworkInit(&network);
DiagPrintf(“Network Up\n”);
MQTTClientInit(&client, &network, 30000, sendbuf, sizeof(sendbuf), readbuf, sizeof(readbuf));
DiagPrintf(“Client MQTT started\n”);

while(wifi_is_ready_to_transceive(RTW_STA_INTERFACE) != RTW_SUCCESS) {
DiagPrintf( “Wiating for Wi-Fi.\n”);
vTaskDelay(5000 / portTICK_PERIOD_MS);

}

DiagPrintf( “Wi-Fi connected.\n”);

DiagPrintf( “Connect Network "%s"\n”, address);
while ((rc = NetworkConnect(&network, address, 1883)) != 0){
DiagPrintf( “Return code from network connect is %d\n”, rc);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
DiagPrintf( “"%s" Connected\n”, address);

connectData.MQTTVersion = 3;
connectData.clientID.cstring = “FT1_018”;

DiagPrintf( “Start MQTT connection\n”);
while ((rc = MQTTConnect(&client, &connectData)) != 0){
DiagPrintf( “Return code from MQTT connect is %d\n”, rc);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
DiagPrintf( “MQTT Connected\n”);

DiagPrintf( “Subscribe to Topic: %s\n”, sub_topic);
if ((rc = MQTTSubscribe(&client, sub_topic, QOS2, messageArrived)) != 0)
DiagPrintf( “Return code from MQTT subscribe is %d\n”, rc);

DiagPrintf( “Publish Topics: %s\n”, pub_topic);
while (1)
{
MQTTMessage message;
char payload[300];

  if (++count == 0)
  	count = 1;
  
  message.qos = QOS1;
  message.retained = 0;
  message.payload = payload;
  sprintf(payload, "hello from AMEBA %d", count);
  message.payloadlen = strlen(payload);

  if ((rc = MQTTPublish(&client, pub_topic, &message)) != 0)
  	DiagPrintf("Return code from MQTT publish is %d\n", rc);
  if ((rc = MQTTYield(&client, 1000)) != 0)
  	DiagPrintf("Return code from yield is %d\n", rc);
  vTaskDelay(5000);

}
/* do not return */
}

void init()
{
int rc;

DiagPrintf(“Task started\n”);
wifi_on(RTW_MODE_STA);

rc=wifi_connect(“Chano2”,RTW_SECURITY_WPA_TKIP_PSK,“*********”,6,8,-1,NULL);
if (rc == RTW_SUCCESS)
DiagPrintf(“Connected to Wifi \n”);
else
DiagPrintf(“Not connected %d\n”,rc);

while(wifi_is_ready_to_transceive(RTW_STA_INTERFACE) != RTW_SUCCESS) {
DiagPrintf( “Waiting for Wi-Fi.\n”);
vTaskDelay(5000 / portTICK_PERIOD_MS);

}

rc=xTaskCreate(MQTT_Msg_Received, /* The function that implements the task. /
((const char
)“MQTT-Task”), /* Just a text name for the task to aid debugging. /
usTaskStackSize + 128 , /
The stack size is defined in FreeRTOSIPConfig.h. /
NULL, /
The task parameter, not used in this case. /
tskIDLE_PRIORITY + 3 + PRIORITIE_OFFSET, /
The priority assigned to the task is defined in FreeRTOSConfig.h. */
NULL);

while(1);
return;
}

int main(void)
{
int rc;
DiagPrintf(“Main started\n”);
rc=xTaskCreate(init, ((const char*)“init”), usTaskStackSize + 128, NULL, tskIDLE_PRIORITY + 3 + PRIORITIE_OFFSET, NULL);
vTaskStartScheduler();
DiagPrintf(“Scheduler Ended\n”);
}

and this is the console output

#calibration_ok:[2:19:11]
Main started
Task started

Initializing WIFI …
WIFI initialized

RTL8721D[Driver]: set ssid [Chano2]

RTL8721D[Driver]: rtw_set_wpa_ie[1160]: AuthKeyMgmt = 0x2

RTL8721D[Driver]: rtw_restruct_sec_ie[4275]: no pmksa cached

RTL8721D[Driver]: start auth to 02:ec:da:36:02:48

RTL8721D[Driver]: auth alg = 2

RTL8721D[Driver]:
OnAuthClient:algthm = 0, seq = 2, status = 0, sae_msg_len = 0

RTL8721D[Driver]: auth success, start assoc

RTL8721D[Driver]: association success(res=2)

RTL8721D[Driver]: ClientSendEAPOL[1650]: no use cache pmksa

RTL8721D[Driver]: set pairwise key to hw: alg:4(WEP40-1 WEP104-5 TKIP-2 AES-4)

RTL8721D[Driver]: set group key to hw: alg:4(WEP40-1 WEP104-5 TKIP-2 AES-4) keyid:1
Connected to Wifi
Waiting for Wi-Fi.

Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packetWaiting for Wi-Fi.

Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet
Cannot allocate pbuf to receive packet

Thx!

Hi @Chano ,

You may refer to the link below to increase the PBUF_POOL_SIZE, then compile the source code again:

Larger PBUF_POOL_SIZE won’t trigger the Cannot allocate pbuf to receive packet warning.

Thank you

No, this didn’t work.

I’ve tried with #define PBUF_POOL_SIZE 60 and I still get the same error, and reading the code you mentioned I’ve also tried #define WIFI_LOGO_CERTIFICATION_CONFIG 1 to no avail… I still get the “Cannot allocate …:” message.

It’s weird because it seems like it’s out of buffers in the very first call to wifi_is_ready_to_transceive().

I’am assuming that wifi_connect() call is setting up the whole wifi connection, but I’m starting to think that this is not correcte, that this call only enable the radio part and it does not invoke DHCP to get an IP from the network nor it starts the IP stack. is this correct?

If I look to thw wifi connection from my AP point of view, I can see the device and it’s MAC address, but no IP. I’ll keep investigating

It’s really disappointing that there is no sample or doc that specifies the way this work. Only test&error and dive into the source code… :sweat_smile: :sweat_smile:

Does anyone know another SOC that supports 5Ghz networks??

@Chano
Did you try to increase free RTOS heap size?

FreeRTOSConfig.h
#define configTOTAL_HEAP_SIZE

Finally I solved (part) of the issue.

As I said in the last post, the problem was that wifi_connect ONLY connects to wifi, it does not sertup a LWIP stack nor calls DHCP to get an IP.

I’ve added this two lines after wifi connect.:

DiagPrintf( “Start LwIP\n”);
LwIP_Init();
DiagPrintf( “Start DHCP\n”);
LwIP_DHCP(0, DHCP_START);

with this, done, the Cannot allocate … message is gone. If I look from the AP point of view, now I can see that my board has a proper IP!! and I can ping my device from other machines in the same lan and even in remote lans (so DHCP is working properly)

But now, next stopper, no way to establish a socket with anything in the net. I always get error: -1