簡體   English   中英

stm32f4 發現上的 lwIP mqtt 連接錯誤

[英]lwIP mqtt connection error on stm32f4 discovery

我正在嘗試將 lwIP 用於客戶端,該客戶端通過 stm32f407 發現將數據發送到 mosquitto 代理。

Mqtt 應用程序是在 lwIP 上實現的。 我只是在初始化后在 main 中像這樣使用它們。 mqtt_client_t static_client;

之后,通過 USART 中斷,我調用
example_do_connect(&static_client); example_publish(&static_client,0);

其中調用這些函數:

{
  struct mqtt_connect_client_info_t ci;
  err_t err;

  /* Setup an empty client info structure */
  memset(&ci, 0, sizeof(ci));

  /* Minimal amount of information required is client identifier, so set it here */
  ci.client_id = "lwip_test";
    ci.client_user = NULL;
    ci.client_pass = NULL;

  /* Initiate client and connect to server, if this fails immediately an error code is returned
     otherwise mqtt_connection_cb will be called with connection result after attempting
     to establish a connection with the server.
     For now MQTT version 3.1.1 is always used */

  err = mqtt_client_connect(client, &serverIp, MQTT_PORT, mqtt_connection_cb, 0, &ci);

  /* For now just print the result code if something goes wrong*/
  if(err != ERR_OK) {

  }
}


void example_publish(mqtt_client_t *client, void *arg)
{
  const char *pub_payload= "stm32_test";
  err_t err;
  u8_t qos = 2; /* 0 1 or 2, see MQTT specification */
  u8_t retain = 0; /* No don't retain such crappy payload... */
  err = mqtt_publish(client, "pub_topic", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg);
  if(err != ERR_OK) {
  //  printf("Publish err: %d\n", err);
    err = ERR_OK;
  }
}

/* Called when publish is complete either with sucess or failure */
static void mqtt_pub_request_cb(void *arg, err_t result)
{
  if(result != ERR_OK) {
  //  printf("Publish result: %d\n", result);
  }
}

我可以 ping 板,我的 IP 地址已經通過使用IP_ADDR4(&serverIp, 192,168,2,97);

我已經使用了所有需要的函數,比如 MX_LWIP_Init()、MX_LWIP_Process(),實際上我什至能夠實現一個運行良好的 TCP 客戶端。 所以互聯網連接很好,但我想,我在 mqttclient 中遺漏了一點。 Erik Anderssen 的指南也完成了回調。

當我嘗試使用 mosquitto 訂閱板的 IP 時,錯誤:無法建立連接,因為目標主動拒絕它。 如果您注意到我遺漏的某些點或有任何想法,請告訴我。

任何幫助將不勝感激,提前致謝。

我有一個類似的問題,當 QoS(服務質量)設置為 2 時,服務器拒絕連接,但服務器需要它為 0。嘗試將連接回調行中的參數 qos 更改為 0 或 1:

err = mqtt_subscribe(mqtt.client, "topic", qos, MqttApp_SubscribeRequestCallback, arg);

同樣適用於發布函數中的參數 qos:

改變u8_t qos = 2; u8_t qos = 0; (或 1 - 無論您的服務器需要什么)

希望能幫助到你。 干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM