espressif / esp-aliyun

Aliyun Iotkit-embedded, support esp32 & esp8266.
339 stars 151 forks source link

连接阿里云物联网超过一定时间(24小时)掉线 #192

Open ZorkHuang opened 4 years ago

ZorkHuang commented 4 years ago

1. 开发环境

2. 问题描述

连接阿里云超过一定时间(大约24小时以上)设备掉线,无法连接阿里云;此时设备在路由器上能够正常获取到IP

2.1 复现步骤

2.2 复现问题的代码

static int linkkit_thread(void *paras) { int res = 0; iotx_linkkit_dev_meta_info_t master_meta_info; int domain_type = 0, dynamic_register = 0, post_reply_need = 0;

ifdef ATM_ENABLED

if (IOT_ATM_Init() < 0)
{
    EXAMPLE_TRACE("IOT ATM init failed!\n");
    return -1;
}

endif

memset(&g_user_example_ctx, 0, sizeof(user_example_ctx_t));
HAL_GetProductKey(PRODUCT_KEY);
HAL_GetProductSecret(PRODUCT_SECRET);
HAL_GetDeviceName(DEVICE_NAME);
HAL_GetDeviceSecret(DEVICE_SECRET);
memset(&master_meta_info, 0, sizeof(iotx_linkkit_dev_meta_info_t));
memcpy(master_meta_info.product_key, PRODUCT_KEY, strlen(PRODUCT_KEY));
memcpy(master_meta_info.product_secret, PRODUCT_SECRET, strlen(PRODUCT_SECRET));
memcpy(master_meta_info.device_name, DEVICE_NAME, strlen(DEVICE_NAME));
memcpy(master_meta_info.device_secret, DEVICE_SECRET, strlen(DEVICE_SECRET));
print_ali_config("load ali config");

/* Register Callback */
IOT_RegisterCallback(ITE_AWSS_STATUS, user_awss_status_event_handler);
IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
IOT_RegisterCallback(ITE_CONNECT_FAIL, user_connect_fail_event_handler);
IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
IOT_RegisterCallback(ITE_RAWDATA_ARRIVED, user_rawdata_arrived_event_handler);
IOT_RegisterCallback(ITE_SERVICE_REQUEST, user_service_request_event_handler);
IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
IOT_RegisterCallback(ITE_PROPERTY_GET, user_property_get_event_handler);
IOT_RegisterCallback(ITE_PROPERTY_DESIRED_GET_REPLY, user_property_desired_get_reply_event_handler);
IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
IOT_RegisterCallback(ITE_TOPOLIST_REPLY, user_toplist_reply_event_handler);
IOT_RegisterCallback(ITE_PERMIT_JOIN, user_permit_join_event_handler);
IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
IOT_RegisterCallback(ITE_MQTT_CONNECT_SUCC, user_mqtt_connect_succ_event_handler);

domain_type = IOTX_CLOUD_REGION_SHANGHAI;
IOT_Ioctl(IOTX_IOCTL_SET_DOMAIN, (void *)&domain_type);

/* Choose Login Method */
dynamic_register = 0;
IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)&dynamic_register);

/* post reply doesn't need */
/* value(int*): 0 - Disable property post reply by cloud; 1 - Enable property post reply by cloud */
post_reply_need = 0;
IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_reply_need);

/* Create Master Device Resources */
g_user_example_ctx.master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, &master_meta_info);
if (g_user_example_ctx.master_devid < 0)
{
    EXAMPLE_TRACE("IOT_Linkkit_Open Failed\n");
    return -1;
}

/* Start Connect Aliyun Server */
res = IOT_Linkkit_Connect(g_user_example_ctx.master_devid);
if (res < 0)
{
    EXAMPLE_TRACE("IOT_Linkkit_Connect Failed\n");
    IOT_Linkkit_Close(g_user_example_ctx.master_devid);
    return -1;
}

uint64_t loop_cnt = 1;
// uint16_t interval = CONFIG_MQTT_CHECK_INTERVAL;
uint64_t report_cnt = CONFIG_MQTT_REPORT_INTERVAL / 20;
xCMDMessage *cmd_msg;
xCardMessage *card_msg;
BaseType_t cmd_rst;
BaseType_t card_rst;
bool is_connected = false;
while (1)
{
    is_connected = (sta_is_connected || eth_is_connected);
    card_rst = xQueueReceive(card_queue, &card_msg, pdMS_TO_TICKS(2));
    if (card_rst == pdPASS)
    {
        EXAMPLE_TRACE("channel:%d, card:%lu => %lu\r\n", card_msg->channel, card_msg->card_id, card_msg->parity_id);
        if (is_connected && (millis() - card_msg->ticks) < 1000)
        {
            led_com_off();
            linkkit_card_event(card_msg);
            led_com_on();
        }
        HAL_Free(card_msg);
    }
    cmd_rst = xQueueReceive(dtu_queue, &cmd_msg, pdMS_TO_TICKS(2));
    if (cmd_rst == pdPASS)
    {
        if (is_connected)
        {
            led_com_off();
            if (!cmd_msg->channel)
            {
                linkkit_dtu_event(cmd_msg);
            }
            else
            {
                //com2,解析命令
            }
            led_com_on();
        }

        HAL_Free(cmd_msg);
    }

    //1. 查看是否需要上报 200毫秒
    int bits = xEventGroupWaitBits(report_event_group, REPORT_BIT, pdFALSE, pdTRUE, pdMS_TO_TICKS(2)); //interval / portTICK_PERIOD_MS
    bool report_by_event = (bits & REPORT_BIT) != 0;
    if (report_by_event)
    {
        ESP_LOGI(TAG, "xEventGroupClearBits, time: %llu, cnt: %llu", millis(), report_cnt);
        xEventGroupClearBits(report_event_group, REPORT_BIT);
    }
    //主动上报或者4秒超时
    if ((report_by_event || (loop_cnt % report_cnt == 0)) && is_connected)
    {
        led_com_off();
        ESP_LOGI(TAG, "begin property post, time: %llu, cnt: %llu", millis(), report_cnt);
        linkkit_property_post();
        led_com_on();
    }

    IOT_Linkkit_Yield(EXAMPLE_YIELD_TIMEOUT_MS);
    loop_cnt += 1;
}

IOT_Linkkit_Close(g_user_example_ctx.master_devid);

//IOT_DumpMemoryStats(IOT_LOG_INFO);
//IOT_SetLogLevel(IOT_LOG_NONE);
return 0;

}

3. 调试 Logs

[10:18:23.920]收←◆[err] iotx_mc_keepalive(1841): reconnect network fail, rc = -1 [err] _mqtt_cycle(1597): error occur rc=-27 [err] _mqtt_cycle(1597): error occur rc=-27 [inf] iotx_mc_handle_reconnect(1742): Waiting to reconnect... [inf] iotx_mc_handle_reconnect(1749): start to reconnect [inf] iotx_mc_attempt_reconnect(1722): reconnect params: MQTTVersion=4, clientID=a1c60FnaYQk.104084286F24|timestamp=2524608000000,securemode=2,signmethod=hmacsha256,gw=0,ext=0,_v=sdk-c-3.0.1|, keepAliveInterval=30, username=104084286F24&a1c60FnaYQk  [10:18:24.722]收←◆E (297058384) esp-tls: mbedtls_ssl_handshake returned -0x2700 I (297058384) esp-tls: Failed to verify peer certificate! I (297058394) esp-tls: verification info: ! The certificate is not correctly signed by the trusted CA  E (297058404) esp-tls: Failed to open new connection [err] wrapper_mqtt_connect(2702): TCP or TLS Connection failed  [10:18:26.228]收←◆E (297059894) esp-tls: mbedtls_ssl_handshake returned -0x2700 I (297059894) esp-tls: Failed to verify peer certificate! I (297059904) esp-tls: verification info: ! The certificate is not correctly signed by the trusted CA  E (297059904) esp-tls: Failed to open new connection [err] wrapper_mqtt_connect(2702): TCP or TLS Connection failed  [10:18:27.778]收←◆E (297061444) esp-tls: mbedtls_ssl_handshake returned -0x2700 I (297061444) esp-tls: Failed to verify peer certificate! I (297061454) esp-tls: verification info: ! The certificate is not correctly signed by the trusted CA  E (297061454) esp-tls: Failed to open new connection [err] wrapper_mqtt_connect(2702): TCP or TLS Connection failed  [10:18:28.800]收←◆[inf] _mqtt_connect(778): connect params: MQTTVersion=4, clientID=a1c60FnaYQk.104084286F24|timestamp=2524608000000,securemode=2,signmethod=hmacsha256,gw=0,ext=0,_v=sdk-c-3.0.1|, keepAliveInterval=30, username=104084286F24&a1c60FnaYQk E (297062474) iot_import_tls: HAL_SSL_Write, handle == NULL [err] MQTTConnect(460): send connect packet failed [err] _mqtt_connect(786): send connect packet failed, rc = -14 [err] iotx_mc_attempt_reconnect(1728): run iotx_mqtt_connect() error! [err] iotx_mc_handle_reconnect(1784): mqtt reconnect failed rc = -14 [err] iotx_mc_keepalive(1841): reconnect network fail, rc = -14  [10:18:28.931]收←◆[err] _mqtt_cycle(1597): error occur rc=-27 [inf] iotx_mc_handle_reconnect(1742): Waiting to reconnect... [err] iotx_mc_keepalive(1841): reconnect network fail, rc = -1 [err] _mqtt_cycle(1597): error occur rc=-27 [err] _mqtt_cycle(1597): error occur rc=-27 [inf] iotx_mc_handle_reconnect(1742): Waiting to reconnect...  [10:18:29.100]收←◆[err] iotx_mc_keepalive(1841): reconnect network fail, rc = -1 [err] _mqtt_cycle(1597): error occur rc=-27 [err] _mqtt_cycle(1597): error occur rc=-27 [inf] iotx_mc_handle_reconnect(1742): Waiting to reconnect...

ljy770 commented 4 years ago

看 log 是 CA 校验失败。

建议使用 esp-idf 加 tag 的版本重新测试,https://github.com/espressif/esp-idf/tags

ZorkHuang commented 4 years ago

好的,谢谢! 这个问题我们跟阿里云的技术交流过,应该是网络不稳定导致阿里云认为设备掉线(30秒无心跳包)把连接主动关闭了,客户端再重用以前的连接重试,无论如何都不行;我们是手动释放这个链接然后重新连接阿里云解决的。至于用另外的版本:esp-idf/tags来测试,晚点可以尝试下

ZorkHuang commented 4 years ago

设备运行几天后会出现如下问题,不清楚该如何处理,谢谢 I (414956973) LOCK485: IOTX_CONN_CLOUD [wrn] IOT_MQTT_Construct(467): Using default hostname: 'a1c60FnaYQk.iot-as-mqtt.cn-shanghai.aliyuncs.com' [wrn] IOT_MQTT_Construct(474): Using default port: [443] [wrn] IOT_MQTT_Construct(481): Using default client_id: a1c60FnaYQk.104084286F24|timestamp=2524608000000,securemode=2,signmethod=hmacsha256,gw=0,ext=0,_v=sdk-c-3.0.1| [wrn] IOT_MQTT_Construct(488): Using default username: 104084286F24&a1c60FnaYQk [wrn] IOT_MQTT_Construct(496): Using default password: ** [inf] iotx_mc_init(230): MQTT init success!  [08:59:05.102]收←◆E (414957863) esp-tls: mbedtls_ssl_handshake returned -0x10 I (414957873) esp-tls: Certificate verified. E (414957873) esp-tls: Failed to open new connection [err] wrapper_mqtt_connect(2702): TCP or TLS Connection failed  [08:59:06.943]收←◆E (414959713) esp-tls: mbedtls_ssl_handshake returned -0x10 I (414959713) esp-tls: Certificate verified. E (414959713) esp-tls: Failed to open new connection [err] wrapper_mqtt_connect(2702): TCP or TLS Connection failed  [08:59:08.787]收←◆E (414961553) esp-tls: mbedtls_ssl_handshake returned -0x10 I (414961553) esp-tls: Certificate verified. E (414961553) esp-tls: Failed to open new connection [err] wrapper_mqtt_connect(2702): TCP or TLS Connection failed  [08:59:09.806]收←◆[inf] _mqtt_connect(778): connect params: MQTTVersion=4, clientID=a1c60FnaYQk.104084286F24|timestamp=2524608000000,securemode=2,signmethod=hmacsha256,gw=0,ext=0,_v=sdk-c-3.0.1|, keepAliveInterval=30, username=104084286F24&a1c60FnaYQk E (414962583) iot_import_tls: HAL_SSL_Write, handle == NULL [err] MQTTConnect(460): send connect packet failed [err] _mqtt_connect(786): send connect packet failed, rc = -14 [err] IOT_MQTT_Construct(578): wrapper_mqtt_connect failed [inf] iotx_mc_disconnect(2637): mqtt disconnect! [inf] wrapper_mqtt_release(2782): mqtt release!  [08:59:10.549]收←◆[wrn] IOT_MQTT_Construct(467): Using default hostname: 'a1c60FnaYQk.iot-as-mqtt.cn-shanghai.aliyuncs.com' [wrn] IOT_MQTT_Construct(474): Using default port: [443] [wrn] IOT_MQTT_Construct(481): Using default client_id: a1c60FnaYQk.104084286F24|timestamp=2524608000000,securemode=2,signmethod=hmacsha256,gw=0,ext=0,_v=sdk-c-3.0.1| [wrn] IOT_MQTT_Construct(488): Using default username: 104084286F24&a1c60FnaYQk [wrn] IOT_MQTT_Construct(496): Using default password: ** [inf] iotx_mc_init(230): MQTT init success!  [08:59:11.445]收←◆E (414964213) esp-tls: mbedtls_ssl_handshake returned -0x10 I (414964213) esp-tls: Certificate verified. E (414964213) esp-tls: Failed to open new connection [err] wrapper_mqtt_connect(2702): TCP or TLS Connection failed

ljy770 commented 4 years ago

谢谢反馈。 对刚提到的问题,请打印下失败时候的的 heap size 或者周期性打印 heap size, 看是否内存不够导致。如果是内存不够,可能是由于内存泄露导致,排查一下您前面提到的手动释放连接过程。

include "esp_system.h"

uint32_t esp_get_free_heap_size( void );