Skip to main content
AGaru
Associate II
July 29, 2019
Question

Network Connection Troubles

  • July 29, 2019
  • 2 replies
  • 760 views

Hi all,

I am using a Nucleo-144 board and enabled the FreeRTOS. Afterwards i've integrated FreeRTOS+TCP sources and headers and Paho MQTT application.

I created a thread to run a MQTT Echo Server but i am having troubles connecting. This is my code:

static void prvMQTTEchoTask(void *pvParameters)

{

  printf("prvMQTTEchoTask\n\r");

/* connect to m2m.eclipse.org, subscribe to a topic, send and receive messages regularly every 1 sec */

MQTTClient client;

Network network;

unsigned char sendbuf[80], readbuf[80];

int rc = 0,

count = 0;

MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer;

pvParameters = 0;

NetworkInit(&network);

printf("Network Initialized\n\r");

MQTTClientInit(&client, &network, 30000, sendbuf, sizeof(sendbuf), readbuf, sizeof(readbuf));

printf("MQTT Client Initialized\n\r");

//char* address = "iot.eclipse.org";

char* address = "0.0.0.0";

if ((rc = NetworkConnect(&network, address, 1883)) != 0)

printf("Return code from network connect is %d\n\r", rc);

#if defined(MQTT_TASK)

printf("MQTTStartTask\n\r");

if ((rc = MQTTStartTask(&client)) != pdPASS)

printf("Return code from start tasks is %d\n\r", rc);

#endif

connectData.MQTTVersion = 3;

connectData.clientID.cstring = "FreeRTOS_sample";

printf("Connection Attempt...\n\r");

if ((rc = MQTTConnect(&client, &connectData)) != 0)

printf("Return code from MQTT connect is %d\n\r", rc);

else

printf("MQTT Connected\n");

if ((rc = MQTTSubscribe(&client, "FreeRTOS/sample/#", 2, messageArrived)) != 0)

printf("Return code from MQTT subscribe is %d\n\r", rc);

while (++count)

{

MQTTMessage message;

char payload[30];

message.qos = 1;

message.retained = 0;

message.payload = payload;

sprintf(payload, "message number %d", count);

message.payloadlen = strlen(payload);

if ((rc = MQTTPublish(&client, "FreeRTOS/sample/a", &message)) != 0)

printf("Return code from MQTT publish is %d\n\r", rc);

#if !defined(MQTT_TASK)

if ((rc = MQTTYield(&client, 1000)) != 0)

printf("Return code from yield is %d\n\r", rc);

#endif

HAL_Delay(5000);

}

/* do not return */

}

I've tried passing an ip address or a name like "localhost" but it keeps giving me an error code at this point:

if ((rc = NetworkConnect(&network, address, 1883)) != 0)

printf("Return code from network connect is %d\n\r", rc);

Any suggestion? Thank you.

    This topic has been closed for replies.

    2 replies

    AGaru
    AGaruAuthor
    Associate II
    July 30, 2019

    I realized that i don't have any address set up, by running:

    FreeRTOS_GetAddressConfiguration( &ulIPAddress,

                      &ulNetMask,

                      &ulGatewayAddress,

                      &ulDNSServerAddress );

     /* Convert the address to a string then print it out. */

     FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );

     printf("IP Address: %s\r\n", cBuffer);

     FreeRTOS_inet_ntoa( ulNetMask, cBuffer );

     printf( "Subnet Mask: %s\r\n", cBuffer );

     FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer );

     printf( "Gateway Address: %s\r\n", cBuffer );

     FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer );

     printf( "DNS Server Address: %s\r\n", cBuffer );

    I print :

    IP Address: 0.0.0.0

    Subnet Mask: 0.0.0.0

    Gateway Address: 0.0.0.0

    DNS Server Address: 0.0.0.0

    AGaru
    AGaruAuthor
    Associate II
    July 30, 2019

    I modified the code and now i print different stuff:

    int main(void)

    {

    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */

    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals /

    MX_GPIO_Init();

    MX_ETH_Init();

    MX_USART3_UART_Init();

    MX_USB_OTG_FS_PCD_Init();

    / Initialise the RTOS’s TCP/IP stack. The tasks that use the network

    are created in the vApplicationIPNetworkEventHook() hook function

    below. The hook function is called when the network connects. */

    FreeRTOS_IPInit( ucIPAddress,

    ucNetMask,

    ucGatewayAddress,

    ucDNSServerAddress,

    ucMACAddress );

    uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress;

    int8_t cBuffer[ 16 ];

    /* Create the thread(s) /

    / definition and creation of defaultTask /

    //osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);

    //defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

    vStartMQTTTasks(ipconfigIP_TASK_STACK_SIZE_WORDS, 0);

    / USER CODE BEGIN RTOS_THREADS /

    / add threads, ... /

    / USER CODE END RTOS_THREADS /

    / The network is up and configured. Print out the configuration

    obtained from the DHCP server. */

    FreeRTOS_GetAddressConfiguration( &ulIPAddress,

    &ulNetMask,

    &ulGatewayAddress,

    &ulDNSServerAddress );

    /* Convert the address to a string then print it out. */

    FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );

    printf("IP Address: %s\r\n", cBuffer);

    FreeRTOS_inet_ntoa( ulNetMask, cBuffer );

    printf( "Subnet Mask: %s\r\n", cBuffer );

    FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer );

    printf( "Gateway Address: %s\r\n", cBuffer );

    FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer );

    printf( "DNS Server Address: %s\r\n", cBuffer );

    /* Start scheduler /

    //osKernelStart();

    vTaskStartScheduler();

    / We should never get here as control is now taken by the scheduler */

    /* Infinite loop /

    / USER CODE BEGIN WHILE */

    while (1)

    {

    }

    /* USER CODE END 3 */

    }

    Those printfs give me the following prints:

    IP Address: 0.0.0.0

    Subnet Mask: 255.255.255.0

    Gateway Address: 10.9.23.1

    DNS Server Address: 10.9.3.43

    Since i initialized the arguments given to FreeRTOS_IPInit as :

    static const uint8_t ucIPAddress[ 4 ] = { 10, 9, 23, 44 };

    static const uint8_t ucNetMask[ 4 ] = { 255, 255, 255, 0 };

    static const uint8_t ucGatewayAddress[ 4 ] = { 10, 9, 23, 1 };

    static const uint8_t ucDNSServerAddress[ 4 ] = { 10, 9, 3, 43 };