乐鑫技术交流
直播中

陈勇

7年用户 1254经验值
私信 关注
[问答]

ESP8266 Smartconfig配网成功之后无法连接MQTT客户端,为什么?


/* main.c -- MQTT client example
*
* Copyright (c) 2014-2015, Tuan PM
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "ets_sys.h"
#include "driver/uart.h"
#include "osapi.h"
#include "mqtt.h"
#include "wifi.h"
#include "config.h"
#include "debug.h"
#include "gpio.h"
#include "user_interface.h"
#include "mem.h"
#include "driver/ir_tx_rx.h"
#include"driver/ringbuf.h"

#include "ip_addr.h"
#include "espconn.h"
#include "smartconfig.h"
#include "airkiss.h"

#define DEVICE_TYPE       "gh_e2728760afcf"//wechat public number
#define DEVICE_ID           "gh_e2728760afcf_bf51293fd4cd31a8"//,"qrticket" //model ID

#define DEFAULT_LAN_PORT    12476

os_timer_t ir_timer;
os_timer_t Send_temp_timer_t;
MQTT_Client mqttClient;

LOCAL esp_udp ssdp_udp;
LOCAL struct espconn pssdpudpconn;
LOCAL os_timer_t ssdp_time_serv;

uint8_t  lan_buf[200];
uint16_t lan_buf_len;
uint8     udp_sent_cnt = 0;

int mark=0;

const airkiss_config_t akconf =
{
   (airkiss_memset_fn)&memset,
   (airkiss_memcpy_fn)&memcpy,
   (airkiss_memcmp_fn)&memcmp,
   0,
};
void wifiConnectCb(uint8_t status)
{
   if(status == STATION_GOT_IP){
      MQTT_Connect(&mqttClient);
   } else {
      MQTT_Disconnect(&mqttClient);
   }
}
void mqttConnectedCb(uint32_t *args)
{
   MQTT_Client* client = (MQTT_Client*)args;
   INFO("MQTT: Connectedrn");
   MQTT_Subscribe(client, "remote", 0);
}

void mqttDisconnectedCb(uint32_t *args)
{
   MQTT_Client* client = (MQTT_Client*)args;
   INFO("MQTT: Disconnectedrn");
}

void mqttPublishedCb(uint32_t *args)
{
   MQTT_Client* client = (MQTT_Client*)args;
   INFO("MQTT: Publishedrn");
}



void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
{
   char *topicBuf = (char*)os_zalloc(topic_len+1),
       *dataBuf = (char*)os_zalloc(data_len+1);

   MQTT_Client* client = (MQTT_Client*)args;

   os_memcpy(topicBuf, topic, topic_len);
   topicBuf[topic_len] = 0;

   os_memcpy(dataBuf, data, data_len);
   dataBuf[data_len] = 0;
   INFO("Receive topic: %s, data: %s rn", topicBuf, dataBuf);
   os_free(topicBuf);
   os_free(dataBuf);
}



LOCAL void ICACHE_FLASH_ATTR
airkiss_wifilan_time_callback(void)
{
   uint16 i;
   airkiss_lan_ret_t ret;

   if ((udp_sent_cnt++) >30) {
      udp_sent_cnt = 0;
      os_timer_disarm(&ssdp_time_serv);//s
      //return;
   }

   ssdp_udp.remote_port = DEFAULT_LAN_PORT;
   ssdp_udp.remote_ip[0] = 255;
   ssdp_udp.remote_ip[1] = 255;
   ssdp_udp.remote_ip[2] = 255;
   ssdp_udp.remote_ip[3] = 255;
   lan_buf_len = sizeof(lan_buf);
   ret = airkiss_lan_pack(AIRKISS_LAN_SSDP_NOTIFY_CMD,
      DEVICE_TYPE, DEVICE_ID, 0, 0, lan_buf, &lan_buf_len, &akconf);
   if (ret != AIRKISS_LAN_PAKE_READY) {
      os_printf("Pack lan packet error!");
      return;
   }

   ret = espconn_sendto(&pssdpudpconn, lan_buf, lan_buf_len);
   if (ret != 0) {
      os_printf("UDP send error!");
   }
   os_printf("Finish send notify!n");
}

LOCAL void ICACHE_FLASH_ATTR
airkiss_wifilan_recv_callbk(void *arg, char *pdata, unsigned short len)
{
   uint16 i;
   remot_info* pcon_info = NULL;

   airkiss_lan_ret_t ret = airkiss_lan_recv(pdata, len, &akconf);
   airkiss_lan_ret_t packret;

   switch (ret){
   case AIRKISS_LAN_SSDP_REQ:
      espconn_get_connection_info(&pssdpudpconn, &pcon_info, 0);
      os_printf("remote ip: %d.%d.%d.%d rn",pcon_info->remote_ip[0],pcon_info->remote_ip[1],
                                             pcon_info->remote_ip[2],pcon_info->remote_ip[3]);
      os_printf("remote port: %d rn",pcon_info->remote_port);

        pssdpudpconn.proto.udp->remote_port = pcon_info->remote_port;
      os_memcpy(pssdpudpconn.proto.udp->remote_ip,pcon_info->remote_ip,4);
      ssdp_udp.remote_port = DEFAULT_LAN_PORT;

      lan_buf_len = sizeof(lan_buf);
      packret = airkiss_lan_pack(AIRKISS_LAN_SSDP_RESP_CMD,
         DEVICE_TYPE, DEVICE_ID, 0, 0, lan_buf, &lan_buf_len, &akconf);

      if (packret != AIRKISS_LAN_PAKE_READY) {
         os_printf("Pack lan packet error!");
         return;
      }

      os_printf("rnrn");
      for (i=0; i
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "ets_sys.h"
#include "driver/uart.h"
#include "osapi.h"
#include "mqtt.h"
#include "wifi.h"
#include "config.h"
#include "debug.h"
#include "gpio.h"
#include "user_interface.h"
#include "mem.h"
#include "driver/ir_tx_rx.h"
#include"driver/ringbuf.h"

#include "ip_addr.h"
#include "espconn.h"
#include "smartconfig.h"
#include "airkiss.h"

#define DEVICE_TYPE       "gh_e2728760afcf"//wechat public number
#define DEVICE_ID           "gh_e2728760afcf_bf51293fd4cd31a8"//,"qrticket" //model ID

#define DEFAULT_LAN_PORT    12476

os_timer_t ir_timer;
os_timer_t Send_temp_timer_t;
MQTT_Client mqttClient;

LOCAL esp_udp ssdp_udp;
LOCAL struct espconn pssdpudpconn;
LOCAL os_timer_t ssdp_time_serv;

uint8_t  lan_buf[200];
uint16_t lan_buf_len;
uint8     udp_sent_cnt = 0;

int mark=0;

const airkiss_config_t akconf =
{
   (airkiss_memset_fn)&memset,
   (airkiss_memcpy_fn)&memcpy,
   (airkiss_memcmp_fn)&memcmp,
   0,
};
void wifiConnectCb(uint8_t status)
{
   if(status == STATION_GOT_IP){
      MQTT_Connect(&mqttClient);
   } else {
      MQTT_Disconnect(&mqttClient);
   }
}
void mqttConnectedCb(uint32_t *args)
{
   MQTT_Client* client = (MQTT_Client*)args;
   INFO("MQTT: Connectedrn");
   MQTT_Subscribe(client, "remote", 0);
}

void mqttDisconnectedCb(uint32_t *args)
{
   MQTT_Client* client = (MQTT_Client*)args;
   INFO("MQTT: Disconnectedrn");
}

void mqttPublishedCb(uint32_t *args)
{
   MQTT_Client* client = (MQTT_Client*)args;
   INFO("MQTT: Publishedrn");
}



void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
{
   char *topicBuf = (char*)os_zalloc(topic_len+1),
       *dataBuf = (char*)os_zalloc(data_len+1);

   MQTT_Client* client = (MQTT_Client*)args;

   os_memcpy(topicBuf, topic, topic_len);
   topicBuf[topic_len] = 0;

   os_memcpy(dataBuf, data, data_len);
   dataBuf[data_len] = 0;
   INFO("Receive topic: %s, data: %s rn", topicBuf, dataBuf);
   os_free(topicBuf);
   os_free(dataBuf);
}



LOCAL void ICACHE_FLASH_ATTR
airkiss_wifilan_time_callback(void)
{
   uint16 i;
   airkiss_lan_ret_t ret;

   if ((udp_sent_cnt++) >30) {
      udp_sent_cnt = 0;
      os_timer_disarm(&ssdp_time_serv);//s
      //return;
   }

   ssdp_udp.remote_port = DEFAULT_LAN_PORT;
   ssdp_udp.remote_ip[0] = 255;
   ssdp_udp.remote_ip[1] = 255;
   ssdp_udp.remote_ip[2] = 255;
   ssdp_udp.remote_ip[3] = 255;
   lan_buf_len = sizeof(lan_buf);
   ret = airkiss_lan_pack(AIRKISS_LAN_SSDP_NOTIFY_CMD,
      DEVICE_TYPE, DEVICE_ID, 0, 0, lan_buf, &lan_buf_len, &akconf);
   if (ret != AIRKISS_LAN_PAKE_READY) {
      os_printf("Pack lan packet error!");
      return;
   }

   ret = espconn_sendto(&pssdpudpconn, lan_buf, lan_buf_len);
   if (ret != 0) {
      os_printf("UDP send error!");
   }
   os_printf("Finish send notify!n");
}

LOCAL void ICACHE_FLASH_ATTR
airkiss_wifilan_recv_callbk(void *arg, char *pdata, unsigned short len)
{
   uint16 i;
   remot_info* pcon_info = NULL;

   airkiss_lan_ret_t ret = airkiss_lan_recv(pdata, len, &akconf);
   airkiss_lan_ret_t packret;

   switch (ret){
   case AIRKISS_LAN_SSDP_REQ:
      espconn_get_connection_info(&pssdpudpconn, &pcon_info, 0);
      os_printf("remote ip: %d.%d.%d.%d rn",pcon_info->remote_ip[0],pcon_info->remote_ip[1],
                                             pcon_info->remote_ip[2],pcon_info->remote_ip[3]);
      os_printf("remote port: %d rn",pcon_info->remote_port);

        pssdpudpconn.proto.udp->remote_port = pcon_info->remote_port;
      os_memcpy(pssdpudpconn.proto.udp->remote_ip,pcon_info->remote_ip,4);
      ssdp_udp.remote_port = DEFAULT_LAN_PORT;

      lan_buf_len = sizeof(lan_buf);
      packret = airkiss_lan_pack(AIRKISS_LAN_SSDP_RESP_CMD,
         DEVICE_TYPE, DEVICE_ID, 0, 0, lan_buf, &lan_buf_len, &akconf);

      if (packret != AIRKISS_LAN_PAKE_READY) {
         os_printf("Pack lan packet error!");
         return;
      }

      os_printf("rnrn");
      for (i=0; i
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "ets_sys.h"
#include "driver/uart.h"
#include "osapi.h"
#include "mqtt.h"
#include "wifi.h"
#include "config.h"
#include "debug.h"
#include "gpio.h"
#include "user_interface.h"
#include "mem.h"
#include "driver/ir_tx_rx.h"
#include"driver/ringbuf.h"

#include "ip_addr.h"
#include "espconn.h"
#include "smartconfig.h"
#include "airkiss.h"

#define DEVICE_TYPE       "gh_e2728760afcf"//wechat public number
#define DEVICE_ID           "gh_e2728760afcf_bf51293fd4cd31a8"//,"qrticket" //model ID

#define DEFAULT_LAN_PORT    12476

os_timer_t ir_timer;
os_timer_t Send_temp_timer_t;
MQTT_Client mqttClient;

LOCAL esp_udp ssdp_udp;
LOCAL struct espconn pssdpudpconn;
LOCAL os_timer_t ssdp_time_serv;

uint8_t  lan_buf[200];
uint16_t lan_buf_len;
uint8     udp_sent_cnt = 0;

int mark=0;

const airkiss_config_t akconf =
{
   (airkiss_memset_fn)&memset,
   (airkiss_memcpy_fn)&memcpy,
   (airkiss_memcmp_fn)&memcmp,
   0,
};
void wifiConnectCb(uint8_t status)
{
   if(status == STATION_GOT_IP){
      MQTT_Connect(&mqttClient);
   } else {
      MQTT_Disconnect(&mqttClient);
   }
}
void mqttConnectedCb(uint32_t *args)
{
   MQTT_Client* client = (MQTT_Client*)args;
   INFO("MQTT: Connectedrn");
   MQTT_Subscribe(client, "remote", 0);
}

void mqttDisconnectedCb(uint32_t *args)
{
   MQTT_Client* client = (MQTT_Client*)args;
   INFO("MQTT: Disconnectedrn");
}

void mqttPublishedCb(uint32_t *args)
{
   MQTT_Client* client = (MQTT_Client*)args;
   INFO("MQTT: Publishedrn");
}



void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
{
   char *topicBuf = (char*)os_zalloc(topic_len+1),
       *dataBuf = (char*)os_zalloc(data_len+1);

   MQTT_Client* client = (MQTT_Client*)args;

   os_memcpy(topicBuf, topic, topic_len);
   topicBuf[topic_len] = 0;

   os_memcpy(dataBuf, data, data_len);
   dataBuf[data_len] = 0;
   INFO("Receive topic: %s, data: %s rn", topicBuf, dataBuf);
   os_free(topicBuf);
   os_free(dataBuf);
}



LOCAL void ICACHE_FLASH_ATTR
airkiss_wifilan_time_callback(void)
{
   uint16 i;
   airkiss_lan_ret_t ret;

   if ((udp_sent_cnt++) >30) {
      udp_sent_cnt = 0;
      os_timer_disarm(&ssdp_time_serv);//s
      //return;
   }

   ssdp_udp.remote_port = DEFAULT_LAN_PORT;
   ssdp_udp.remote_ip[0] = 255;
   ssdp_udp.remote_ip[1] = 255;
   ssdp_udp.remote_ip[2] = 255;
   ssdp_udp.remote_ip[3] = 255;
   lan_buf_len = sizeof(lan_buf);
   ret = airkiss_lan_pack(AIRKISS_LAN_SSDP_NOTIFY_CMD,
      DEVICE_TYPE, DEVICE_ID, 0, 0, lan_buf, &lan_buf_len, &akconf);
   if (ret != AIRKISS_LAN_PAKE_READY) {
      os_printf("Pack lan packet error!");
      return;
   }

   ret = espconn_sendto(&pssdpudpconn, lan_buf, lan_buf_len);
   if (ret != 0) {
      os_printf("UDP send error!");
   }
   os_printf("Finish send notify!n");
}

LOCAL void ICACHE_FLASH_ATTR
airkiss_wifilan_recv_callbk(void *arg, char *pdata, unsigned short len)
{
   uint16 i;
   remot_info* pcon_info = NULL;

   airkiss_lan_ret_t ret = airkiss_lan_recv(pdata, len, &akconf);
   airkiss_lan_ret_t packret;

   switch (ret){
   case AIRKISS_LAN_SSDP_REQ:
      espconn_get_connection_info(&pssdpudpconn, &pcon_info, 0);
      os_printf("remote ip: %d.%d.%d.%d rn",pcon_info->remote_ip[0],pcon_info->remote_ip[1],
                                             pcon_info->remote_ip[2],pcon_info->remote_ip[3]);
      os_printf("remote port: %d rn",pcon_info->remote_port);

        pssdpudpconn.proto.udp->remote_port = pcon_info->remote_port;
      os_memcpy(pssdpudpconn.proto.udp->remote_ip,pcon_info->remote_ip,4);
      ssdp_udp.remote_port = DEFAULT_LAN_PORT;

      lan_buf_len = sizeof(lan_buf);
      packret = airkiss_lan_pack(AIRKISS_LAN_SSDP_RESP_CMD,
         DEVICE_TYPE, DEVICE_ID, 0, 0, lan_buf, &lan_buf_len, &akconf);

      if (packret != AIRKISS_LAN_PAKE_READY) {
         os_printf("Pack lan packet error!");
         return;
      }

      os_printf("rnrn");
      for (i=0; i

回帖(1)

王强

2024-7-10 17:49:41

1. **检查MQTT服务器地址和端口**:确保您在代码中设置的MQTT服务器地址和端口是正确的。如果服务器地址或端口错误,您的设备将无法连接到MQTT服务器。

2. **检查网络连接**:虽然您的设备已经通过Smartconfig成功配网,但是网络连接可能仍然存在问题。您可以使用ESP8266的`WiFi.status()`函数来检查当前的网络状态。

3. **检查MQTT客户端ID**:确保您的MQTT客户端ID是唯一的,因为MQTT服务器可能会拒绝重复的客户端ID。

4. **检查MQTT用户名和密码**:如果您的MQTT服务器需要身份验证,请确保您在代码中设置了正确的用户名和密码。

5. **检查MQTT连接超时设置**:如果您的设备在连接MQTT服务器时超时,请尝试增加连接超时时间。

6. **检查MQTT回调函数**:确保您正确实现了MQTT连接成功、断开连接和消息接收的回调函数。

7. **检查MQTT消息发布和订阅**:确保您正确地发布了消息到MQTT服务器,并订阅了正确的主题。

8. **检查日志输出**:查看您的设备在尝试连接MQTT服务器时的日志输出,这可能会提供一些关于问题的线索。

9. **检查MQTT库的版本**:确保您使用的MQTT库是最新版本,或者至少是与您的ESP8266固件兼容的版本。

10. **硬件问题**:虽然不太可能,但是硬件问题(如Wi-Fi模块故障)也可能导致连接问题。


举报

更多回帖

发帖
×
20
完善资料,
赚取积分