mqttfreertosatmellwip

MQTT Client application with lwip (MQTT_CONNECT_DISCONNECTED)


I try implementation of MQTT client on ATSAME53N20A. I achieved wih succes tcp-ip server-client application with lwip bsd socket api before. But this time some things going wrong despite of I follow the lwip MQTT documentation. I use mosquitto mqtt broker on same network with pc.

Firstly I did initialization like tcpip, ethernet_link etc. Later, I have a "mqtt_new" task.

void mqtt_new(void *p){
  vTaskDelay(1000);
  mqtt_client_t* client;
  client = mqtt_client_new();
  if (client != NULL){
    leds_off();
    mqtt_connect(&client);} 

  while(1){
    gpio_toggle_pin_level(LED_OUT);
    vTaskDelay(200);
  }
}

And, this task call the function of mqtt_connect.

void mqtt_connect(mqtt_client_t *client){
  ip_addr_t my_mqtt;
  struct mqtt_connect_client_info_t ci;
  err_t err;
  memset(&ci, 0, sizeof(ci)); 
  ci.client_id = "lwip_test";   
  my_mqtt.addr = 3232236597ul;
  err = mqtt_client_connect(client, &my_mqtt, MQTT_PORT, mqtt_connection_cb, 0, &ci);
  while (err != ERR_OK)
  {
    err = mqtt_client_connect(client, &my_mqtt, MQTT_PORT, mqtt_connection_cb, 0, &ci);
    gpio_toggle_pin_level(LED_B);
    vTaskDelay(300);
  }
  while (!mqtt_client_is_connected(&client)){
    gpio_set_pin_level(LED_R,false);
  }
}

While this function is running, function of mqtt_client_connect can return ERR_OK. After a while (maybe when mqtt_client_is_connected(&client) return 1), mqtt_connection_cb was starting.

static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status){
err_t err;

if (status == MQTT_CONNECT_ACCEPTED) {
    //printf("mqtt_connection_cb: Successfully connected\n");
    /* Setup callback for incoming publish requests */
    mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg);
    
    /* Subscribe to a topic named "subtopic" with QoS level 1, call mqtt_sub_request_cb with result */
    err = mqtt_subscribe(client, "subtopic", 1, mqtt_sub_request_cb, arg);

    if(err != ERR_OK) {
        //printf("mqtt_subscribe return: %d\n", err);
    } // !err_ok
}   // mqtt_accepted
else {
    //printf("mqtt_connection_cb: Disconnected, reason: %d\n", status);
    leds_off();
    gpio_set_pin_level(LED_G,false);
    /* Its more nice to be connected, so try to reconnect */
    mqtt_connect(client);
} // else
} // task

But, here the status is equal MQTT_CONNECT_DISCONNECTED every time. I can not find that how to solve this problem. If you help me, I will be glad.


Solution

  • I solved this problem. When I changed the following definition that was included in mqtt_connect task, the problem was solved.

    my_mqtt.addr = 3232236597ul;
    

    I wrote the following definition instead of this definition.

    ipaddr_aton("192.168.4.53",&my_mqtt);