我在用ESP32-C3 MQTT通信时遇到发送数据出错而重启的情况,谁能帮忙解决一下问题。这是报错导致重启的日志:
I (134496) MQTT: MQTT_EVENT_ERROR
I (134496) MQTT: Last errno string (Success)
E (134496) MQTT_CLIENT: Error write data or timeout, written len = 0, errno=0
Guru Meditation Error: Core 0 panic'ed (Store access fault). Exception was unhandled.
Stack dump detected
Core 0 register dump:
MEPC : 0x40388ac2 RA : 0x40388456 SP : 0x3fcad710 GP : 0x3fc8e200
0x40388ac2: uxListRemove at C:/ESP32Tool/ESP32-IDF/esp-idf/components/freertos/list.c:177
0x40388456: vTaskDelete at C:/ESP32Tool/ESP32-IDF/esp-idf/components/freertos/tasks.c:1311 (discriminator 4)
TP : 0x3fc8d0e8 T0 : 0x3fca9090 T1 : 0x00000000 T2 : 0x00000002
S0/FP : 0x42009c18 S1 : 0x3fc97e9c A0 : 0x42009c1c A1 : 0x3c0c69e8
0x42009c18: mqtt_test_task at F:\ESP32C3project\smart_config\build/../main/mymqtt.c:399
0x42009c1c: mqtt_test_task at F:\ESP32C3project\smart_config\build/../main/mymqtt.c:399
A2 : 0x00000002 A3 : 0xcf290027 A4 : 0x3fc927b7 A5 : 0x10100793
A6 : 0x42008eb2 A7 : 0x3fca9efc S2 : 0x42009c1c S3 : 0x00003463
0x42008eb2: mqtt_event_handler at F:\ESP32C3project\smart_config\build/../main/mymqtt.c:194
0x42009c1c: mqtt_test_task at F:\ESP32C3project\smart_config\build/../main/mymqtt.c:399
S4 : 0x00000000 S5 : 0x00000000 S6 : 0x00000000 S7 : 0x00000000
S8 : 0x00000000 S9 : 0x00000000 S10 : 0x00000000 S11 : 0x00000000
T3 : 0x0000004b T4 : 0x0000004b T5 : 0x3fca9f19 T6 : 0x00000000
MSTATUS : 0x00001881 MTVEC : 0x40380001 MCAUSE : 0x00000007 MTVAL : 0xcf29002f
0x40380001: _vector_table at ??:?
MHARTID : 0x00000000
Failed to run gdb_panic_server.py script: Command '['riscv32-esp-elf-gdb', '--batch', '-n', 'f:\\esp32c3project\\smart_config\\build\\smart_config.elf', '-ex', 'target remote | "C:\\ESP32Tool\\.espressif\\python_env\\idf4.3_py3.8_env\\Scripts\\python.exe" "c:\\ESP32Tool\\ESP32-IDF\\esp-idf\\tools\\..\\tools\\gdb_panic_server.py" --target esp32c3 "C:\\Users\\??????\\AppData\\Local\\Temp\\tmpul294ipa"', '-ex', 'bt']' returned non-zero exit status 3221225781.
b''
Core 0 register dump:
MEPC : 0x40388ac2 RA : 0x40388456 SP : 0x3fcad710 GP : 0x3fc8e200
TP : 0x3fc8d0e8 T0 : 0x3fca9090 T1 : 0x00000000 T2 : 0x00000002
S0/FP : 0x42009c18 S1 : 0x3fc97e9c A0 : 0x42009c1c A1 : 0x3c0c69e8
A2 : 0x00000002 A3 : 0xcf290027 A4 : 0x3fc927b7 A5 : 0x10100793
A6 : 0x42008eb2 A7 : 0x3fca9efc S2 : 0x42009c1c S3 : 0x00003463
S4 : 0x00000000 S5 : 0x00000000 S6 : 0x00000000 S7 : 0x00000000
S8 : 0x00000000 S9 : 0x00000000 S10 : 0x00000000 S11 : 0x00000000
T3 : 0x0000004b T4 : 0x0000004b T5 : 0x3fca9f19 T6 : 0x00000000
MSTATUS : 0x00001881 MTVEC : 0x40380001 MCAUSE : 0x00000007 MTVAL : 0xcf29002f
MHARTID : 0x00000000
Stack memory:
3fcad710: 0x00000000 0x3fca9d6c 0x3fca9054 0x42008a40 0x00000001 0x00000000 0x3fca9e48 0x4038619a
3fcad730: 0x00000000 0x00000000 0x3fca9e48 0x403887d4 0x00000000 0x00000000 0x3fca9df0 0x00386dfc
3fcad750: 0x00000000 0x00000000 0x3fca9df0 0x00003463 0x00000000 0x3fca9d6c 0x3fcaaf48 0x42008ebc
3fcad770: 0xffffffff 0x00000000 0x3fca9df0 0x420bb700 0x3fc98a08 0x3fca9d6c 0x3fca9090 0x420bbf48
3fcad790: 0x00000101 0x3c0c69e8 0x00000002 0x3fca9054 0x00000101 0x3c0c69e8 0x00000002 0x3fca9054
3fcad7b0: 0x00000101 0x3c0c69e8 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000001
3fcad7d0: 0x3fc98a08 0x00000000 0x3fca9090 0x4200bde8 0x3fc98a08 0x00000000 0x3fca9090 0x4200be04
3fcad7f0: 0x3fc98a08 0x00000000 0x3fca9090 0x4200c4a8 0x3fc98a08 0x00000000 0x3fca9090 0x4200e51c
3fcad810: 0x3c0ca2d8 0x3fc9884c 0x3fc98a08 0x00009bbe 0x00000000 0x00000000 0x00000000 0x00000000
3fcad830: 0x3fc98d58 0x3fc9884c 0x3fc98a08 0x42009bd4 0x00000000 0x00000002 0x69666977 0x7665642d
3fcad850: 0x2f656369 0x74737075 0x6d616572 0x6634382f 0x35333037 0x32623034 0x00000034 0x00000000
3fcad870: 0x00000000 0x00000000 0x00000000 0x42009c74 0x00000000 0x00000000 0x00000000 0x40389564
3fcad890: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcad8b0: 0x27146414 0x01010107 0x00000001 0x000000ff 0x00000000 0x00000000 0x7f7fffff 0x00000000
3fcad8d0: 0x00000000 0x3ff00000 0x00000000 0x40000000 0x94a03595 0x3fdfffff 0x35afe535 0x3fe00000
3fcad8f0: 0x94a03595 0x3fcfffff 0x00000000 0x3fc00000 0x00000000 0x40300000 0x00000000 0x3ff80000
3fcad910: 0x636f4361 0x3fd287a7 0x8b60c8b3 0x3fc68a28 0x509f79fb 0x3fd34413 0x00000000 0x40140000
3fcad930: 0x00000000 0x43500000 0x00000000 0x7ff80000 0x7fc00000 0x3fc8da00 0x000034b0 0x3c0de610
3fcad950: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0x00000514 0x00000000 0x3fcad9b4 0x04c404c4
3fcad970: 0x00010080 0x00000000 0x00000000 0x00070000 0x00000000 0x00000000 0x3fca8af0 0x00000000
3fcad990: 0x3fcad9a0 0x000a8640 0x3fcad9c4 0x00000000 0x00000100 0x06ff6453 0x00000101 0x00000000
3fcad9b0: 0x00000000 0x5b0703d8 0x4f4772ca 0x00000000 0x00000850 0x000025bf 0x0002f530 0x66697721
3fcad9d0: 0x65642d69 0x65636976 0x7370752f 0x61657274 0x34382f6d 0x33303766 0x62303435 0x0a7b3432
3fcad9f0: 0x6c662209 0x3a226761 0x52442209 0x2c224e55 0x6d22090a 0x6c65646f 0x22093a22 0x30364342
3fcada10: 0x0a2c2231 0x64692209 0x32093a22 0x31333731 0x33363639 0x22090a2c 0x2263616d 0x3822093a
3fcada30: 0x30376634 0x30343533 0x22343262 0x22090a2c 0x61746164 0x7b093a22 0x2209090a 0x67617473
3fcada50: 0x093a2265 0x090a2c33 0x74732209 0x22317065 0x2c30093a 0x2209090a 0x70657473 0x093a2232
3fcada70: 0x090a2c30 0x74732209 0x22337065 0x3239093a 0x09090a2c 0x65747322 0x3a223470 0x0a2c3009
3fcada90: 0x73220909 0x35706574 0x30093a22 0x09090a2c 0x65747322 0x3a223670 0x0a2c3009 0x73220909
3fcadab0: 0x37706574 0x30093a22 0x09090a2c 0x65747322 0x3a223870 0x0a2c3009 0x76220909 0x3a226c6f
3fcadad0: 0x32333109 0x0a2c3333 0x73220909 0x3a22636f 0x2c353909 0x2209090a 0x22727563 0x3234093a
3fcadaf0: 0x090a2c33 0x616c2209 0x3a226576 0x32393209 0x0a2c3535 0x74220909 0x6c61746f 0x39093a22
ELF file SHA256: cf3f5f1934102e8f
ELF file SHA256: cf3f5f1934102e8f
Rebooting...
���0ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0xc (RTC_SW_CPU_RST),boot:0xe (SPI_FAST_FLASH_BOOT)
Saved PC:0x40382faa
0x40382faa: esp_restart_noos at C:/ESP32Tool/ESP32-IDF/esp-idf/components/esp32c3/system_api_esp32c3.c:137 (discriminator 1)
[已解决] ESP32 MQTT发送数据失败导致重启
Moderator: ESP_Bob
Re: ESP32 MQTT发送数据失败导致重启
看起来是程序挂在mymqtt.c文件里,能否提供简单的示例代码和提供你使用的 IDF 版本号。
-
- Posts: 6
- Joined: Thu Nov 04, 2021 1:44 am
Re: ESP32 MQTT发送数据失败导致重启
idf4.3版本
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
{
static bool create_task_flag = false;
int msg_id;
uint8_t led_flag = 0;
uint8_t send_flag = 0;
//esp_mqtt_client_handle_t client1 = event->client;
// your_context_t *context = event->context;
switch (event->event_id) {
case MQTT_EVENT_CONNECTED: //连接MQTT成功
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
Mybattery.mqttstate = 1;
if(create_task_flag == false)
{
xTaskCreate(mqtt_test_task, "mqtt_test_task", 4096, NULL, 4, NULL);
printf("Create Task success!!!\n");
create_task_flag = true;
char topic_buff[40];
int len;
len = strlen("env-device/cmd/");
memset(topic_buff,0,sizeof(topic_buff));
memcpy(topic_buff,"env-device/cmd/",len);
memcpy(topic_buff+len,MACbuff,strlen(MACbuff));
msg_id = esp_mqtt_client_subscribe(client, topic_buff, 1);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
}
break;
case MQTT_EVENT_DISCONNECTED://断开MQTT
Mybattery.mqttstate = 0;
if(create_task_flag == true)
{
vTaskDelete(mqtt_test_task);
printf("Delete Task success!!!\n");
create_task_flag = false;
}
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_SUBSCRIBED://订阅成功
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED://取消订阅
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED://发布成功
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA://数据接收
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);
cJSON *pJsonRoot = cJSON_Parse(event->data);
char TempBuff[20] = {0};
//数据处理内容
cJSON_Delete(pJsonRoot);
break;
case MQTT_EVENT_ERROR://MQTT错误
Mybattery.mqttstate = 0;
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
Mybattery.mqttstate = 0;
if(create_task_flag == true)
{
vTaskDelete(mqtt_test_task);
printf("Delete Task success!!!\n");
create_task_flag = false;
}
/*if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
}
*/
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
return ESP_OK;
}
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
mqtt_event_handler_cb(event_data);
}
void mqtt_app_start(void)
{
esp_mqtt_client_config_t my_mqtt_cfg = {
.host = "xxxxxxx",
.port = 1883,
.username = "xxxxxxx",
.password = "xxxxxxxx",
.client_id = MACbuff,
.disable_auto_reconnect = false,
.keepalive = 10,
.transport = MQTT_TRANSPORT_OVER_TCP,
.out_buffer_size = 2048,//设置发送缓冲区大小
.buffer_size = 2048, //设置接收缓冲区大小
};
client = esp_mqtt_client_init(&my_mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
esp_mqtt_client_start(client);
}
void mqtt_up_heart_data(void)
{
int msg_id;
char topic_buff[40];
int len;
len = strlen("wifi-device/upstream/");
memset(topic_buff,0,sizeof(topic_buff));
memcpy(topic_buff,"wifi-device/upstream/",len);
memcpy(topic_buff+len,MACbuff,12);
cJSON *pRoot = cJSON_CreateObject();
cJSON_AddStringToObject(pRoot,"flag","HEAD");
cJSON_AddStringToObject(pRoot,"mac",MACbuff);
char *heartdata = cJSON_Print(pRoot);
printf("JSON = %s\n",heartdata);
//if(client != NULL)
msg_id = esp_mqtt_client_publish(client, topic_buff, heartdata, 0, 0, 0);//发布消息
cJSON_free((void*)heartdata);
cJSON_Delete(pRoot);
ESP_LOGI(TAG, "sent heart_data, msg_id=%d", msg_id);
}
//其他发送的函数,类似上边
我觉得问题可能是网络不好,数据发送失败导致哪个API用了非法的指针导致重启的
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
{
static bool create_task_flag = false;
int msg_id;
uint8_t led_flag = 0;
uint8_t send_flag = 0;
//esp_mqtt_client_handle_t client1 = event->client;
// your_context_t *context = event->context;
switch (event->event_id) {
case MQTT_EVENT_CONNECTED: //连接MQTT成功
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
Mybattery.mqttstate = 1;
if(create_task_flag == false)
{
xTaskCreate(mqtt_test_task, "mqtt_test_task", 4096, NULL, 4, NULL);
printf("Create Task success!!!\n");
create_task_flag = true;
char topic_buff[40];
int len;
len = strlen("env-device/cmd/");
memset(topic_buff,0,sizeof(topic_buff));
memcpy(topic_buff,"env-device/cmd/",len);
memcpy(topic_buff+len,MACbuff,strlen(MACbuff));
msg_id = esp_mqtt_client_subscribe(client, topic_buff, 1);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
}
break;
case MQTT_EVENT_DISCONNECTED://断开MQTT
Mybattery.mqttstate = 0;
if(create_task_flag == true)
{
vTaskDelete(mqtt_test_task);
printf("Delete Task success!!!\n");
create_task_flag = false;
}
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_SUBSCRIBED://订阅成功
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED://取消订阅
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED://发布成功
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA://数据接收
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);
cJSON *pJsonRoot = cJSON_Parse(event->data);
char TempBuff[20] = {0};
//数据处理内容
cJSON_Delete(pJsonRoot);
break;
case MQTT_EVENT_ERROR://MQTT错误
Mybattery.mqttstate = 0;
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
Mybattery.mqttstate = 0;
if(create_task_flag == true)
{
vTaskDelete(mqtt_test_task);
printf("Delete Task success!!!\n");
create_task_flag = false;
}
/*if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
}
*/
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
return ESP_OK;
}
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
mqtt_event_handler_cb(event_data);
}
void mqtt_app_start(void)
{
esp_mqtt_client_config_t my_mqtt_cfg = {
.host = "xxxxxxx",
.port = 1883,
.username = "xxxxxxx",
.password = "xxxxxxxx",
.client_id = MACbuff,
.disable_auto_reconnect = false,
.keepalive = 10,
.transport = MQTT_TRANSPORT_OVER_TCP,
.out_buffer_size = 2048,//设置发送缓冲区大小
.buffer_size = 2048, //设置接收缓冲区大小
};
client = esp_mqtt_client_init(&my_mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
esp_mqtt_client_start(client);
}
void mqtt_up_heart_data(void)
{
int msg_id;
char topic_buff[40];
int len;
len = strlen("wifi-device/upstream/");
memset(topic_buff,0,sizeof(topic_buff));
memcpy(topic_buff,"wifi-device/upstream/",len);
memcpy(topic_buff+len,MACbuff,12);
cJSON *pRoot = cJSON_CreateObject();
cJSON_AddStringToObject(pRoot,"flag","HEAD");
cJSON_AddStringToObject(pRoot,"mac",MACbuff);
char *heartdata = cJSON_Print(pRoot);
printf("JSON = %s\n",heartdata);
//if(client != NULL)
msg_id = esp_mqtt_client_publish(client, topic_buff, heartdata, 0, 0, 0);//发布消息
cJSON_free((void*)heartdata);
cJSON_Delete(pRoot);
ESP_LOGI(TAG, "sent heart_data, msg_id=%d", msg_id);
}
//其他发送的函数,类似上边
我觉得问题可能是网络不好,数据发送失败导致哪个API用了非法的指针导致重启的
-
- Posts: 6
- Joined: Thu Nov 04, 2021 1:44 am
Re: ESP32 MQTT发送数据失败导致重启
问题也不是每次都必定出现的,有时候测试几十分钟才会有这个问题
-
- Posts: 6
- Joined: Thu Nov 04, 2021 1:44 am
Re: ESP32 MQTT发送数据失败导致重启
我找到问题了,在MQTT断开或者出错的情况下把删除任务函数去掉就不会报错了,重连功能正常
Who is online
Users browsing this forum: No registered users and 8 guests