cJson Library error in ESP-IDF
-
I'm trying to parse Json using cJson Library
But getting errorCore 0 register dump:
and the microcontroller is resetting.
I'm using ESP8266 and its on SDK for writing firmware. (ESP-IDF)Below I'm adding complete error log and code.
In below code this part is causing for this error and reset
json = cJSON_Parse(version); out = cJSON_Print(json); cJSON_Delete(json); printf("%s\n", out); free(out);
I (10824) example: ... socket send success I (10828) example: ... set socket receiving timeout success I (11234) example: JSon Parser calling Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled. Core 0 register dump: PC : 0x4000bf80 PS : 0x00000030 A0 : 0x4022f684 A1 : 0x3fff70e0 0x4022f684: _puts_r at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/puts.c:90 A2 : 0x00000000 A3 : 0xfffffffc A4 : 0x000000ff A5 : 0x0000ff00 A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x401078a0 A9 : 0x0000010c A10 : 0x402559f0 A11 : 0x50545448 A12 : 0x00000000 A13 : 0x40106458 A14 : 0x00000000 A15 : 0x3ffeaaf8 SAR : 0x0000001d EXCCAUSE: 0x0000001c Backtrace: 0x4000bf80:0x3fff70e0 0x4022f684:0x3fff70e0 0x4022f724:0x3fff7110 0x4022dc31:0x3fff7120 0x4022f684: _puts_r at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/puts.c:90 0x4022f724: puts at /home/dongheng/workspace/espressif/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-lx106-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/puts.c:139 0x4022dc31: http_get_task at /Users/rafi/Desktop/hawk/ESP8266_RTOS_SDK/examples/protocols/http_request/main/http_request_example_main.c:208 @?Z-nP?M 'γ? %[ )Q ?O??T5??)?))??e5?? ?L?C? I?TZ ??TR?enB?[ ??Q ?ؚ?P9?)?N9?Ɇ )dг 5?P NRA?)ڻ^?)?N9?B??JA? %Y.??+????D W? ??ZQH???~?N??))GD?@ %Y.?^Z1O????Eؓ? PF???J[ !d?TP?HP'i 3mʉOC??JC?%YB7օ+k?))GD?@5 %Y J+?1mG?A??JA? %? %^u@? 5%ʃ?E???J M % ƣ??[?P?TQ+E???J % ??C?T?nV??@ -??\)DV?Q?NX???? %[ -Z?IP?L ?E?)??JCB??q! 1iGq?P#?? %??MC??JA@!%Y%.?M%,?i !XG?)!Eؑ? h? %^u??? 1?Ls?Y?P??Q?A??J ? #??C?5dԵ1?LT?P?????????m \?]eFL?))E???J ?D????5l?V?1NT? PE??M????@P?\9'?? +??+)GD?@?F@J??I?7?n 9)5NT?,R?e7??E@@)IJ?+ YЛ? ?EBH??A?Z5dֵ?LT??G?1?1??mP#?EFB?CH?)!Eؑ? `n #???7nʋ?1DT? ? XU?MTՋ9E??5????@ )R}?OC??JC%YY?V?P%?1?LL[ !?ֳ\ZQ?N?I (306) system_api: Base MAC address is not set, read default base MAC address from EFUSE I (312) system_api: Base MAC address is not
/* HTTP GET Example using plain POSIX sockets This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include <string.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_event_loop.h" #include "esp_log.h" #include "nvs_flash.h" #include <netdb.h> #include <sys/socket.h> #include "cJSON.h" /* The examples use simple WiFi configuration that you can set via 'make menuconfig'. If you'd rather not, just change the below entries to strings with the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" */ #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID #define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD /* FreeRTOS event group to signal when we are connected & ready to make a request */ static EventGroupHandle_t wifi_event_group; /* The event group allows multiple bits for each event, but we only care about one event - are we connected to the AP with an IP? */ const int CONNECTED_BIT = BIT0; /* Constants that aren't configurable in menuconfig */ #define WEB_SERVER "hawk-ota.herokuapp.com" #define WEB_PORT 80 #define WEB_URL "http://hawk-ota.herokuapp.com/api/get-json" static const char *TAG = "example"; static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n" "Host: "WEB_SERVER"\r\n" "User-Agent: esp-idf/1.0 esp32\r\n" "\r\n"; char version[50]; char *out; cJSON *json; /* void Parse_Json(char *text) { if (!json) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } else { out = cJSON_Print(json); cJSON_Delete(json); ESP_LOGI(TAG,"\n here"); puts(out); free(out); } }*/ static esp_err_t event_handler(void *ctx, system_event_t *event) { /* For accessing reason codes in case of disconnection */ system_event_info_t *info = &event->event_info; switch(event->event_id) { case SYSTEM_EVENT_STA_START: esp_wifi_connect(); break; case SYSTEM_EVENT_STA_GOT_IP: xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); break; case SYSTEM_EVENT_STA_DISCONNECTED: ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason); if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { /*Switch to 802.11 bgn mode */ esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); } esp_wifi_connect(); xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); break; default: break; } return ESP_OK; } static void initialise_wifi(void) { tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_WIFI_SSID, .password = EXAMPLE_WIFI_PASS, }, }; ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); ESP_ERROR_CHECK( esp_wifi_start() ); } static void http_get_task(void *pvParameters) { const struct addrinfo hints = { .ai_family = AF_INET, .ai_socktype = SOCK_STREAM, }; struct addrinfo *res; struct in_addr *addr; int s, r, j = 0; char recv_buf[64]; while(1) { /* Wait for the callback to set the CONNECTED_BIT in the event group. */ xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); ESP_LOGI(TAG, "Connected to AP"); int err = getaddrinfo(WEB_SERVER, "80", &hints, &res); if(err != 0 || res == NULL) { ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res); vTaskDelay(1000 / portTICK_PERIOD_MS); continue; } /* Code to print the resolved IP. Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */ addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*addr)); s = socket(res->ai_family, res->ai_socktype, 0); if(s < 0) { ESP_LOGE(TAG, "... Failed to allocate socket."); freeaddrinfo(res); vTaskDelay(1000 / portTICK_PERIOD_MS); continue; } ESP_LOGI(TAG, "... allocated socket"); if(connect(s, res->ai_addr, res->ai_addrlen) != 0) { ESP_LOGE(TAG, "... socket connect failed errno=%d", errno); close(s); freeaddrinfo(res); vTaskDelay(4000 / portTICK_PERIOD_MS); continue; } ESP_LOGI(TAG, "... connected"); freeaddrinfo(res); if (write(s, REQUEST, strlen(REQUEST)) < 0) { ESP_LOGE(TAG, "... socket send failed"); close(s); vTaskDelay(4000 / portTICK_PERIOD_MS); continue; } ESP_LOGI(TAG, "... socket send success"); struct timeval receiving_timeout; receiving_timeout.tv_sec = 5; receiving_timeout.tv_usec = 0; if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout, sizeof(receiving_timeout)) < 0) { ESP_LOGE(TAG, "... failed to set socket receiving timeout"); close(s); vTaskDelay(4000 / portTICK_PERIOD_MS); continue; } ESP_LOGI(TAG, "... set socket receiving timeout success"); /* Read HTTP response */ do { bzero(recv_buf, sizeof(recv_buf)); r = read(s, recv_buf, sizeof(recv_buf)-1); for(int i = 0; i < r; i++) { //putchar(recv_buf[i]); version[j++] = recv_buf[i]; } } while(r > 0); version[j++] = '\0'; ESP_LOGI(TAG, "JSon Parser calling"); json = cJSON_Parse(version); out = cJSON_Print(json); cJSON_Delete(json); printf("%s\n", out); free(out); j=0; ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d\r\n", r, errno); close(s); for(int countdown = 10; countdown >= 0; countdown--) { ESP_LOGI(TAG, "%d... ", countdown); vTaskDelay(1000 / portTICK_PERIOD_MS); } ESP_LOGI(TAG, "Starting again!"); } } void app_main() { ESP_ERROR_CHECK( nvs_flash_init() ); initialise_wifi(); xTaskCreate(&http_get_task, "http_get_task", 16384, NULL, 5, NULL); } ///Users/rafi/desktop/hawk/ESP8266_RTOS_SDK/examples/protocols/http_request
-
@rafitc99 Are you able to fix this issue!
-
@salmanfaris Yes. I fixed it. But no idea about the reason for this error.
-
@rafitc99 How did you solved?
-
@salmanfaris Actually I'm not sure what was that
Core 0 register dump:
But I parsed the JSON. Refer Here for cJSON example. But while working this one, faced another problem. HTTP response includes the whole headers, status code, and body. JSON parser only work if the whole response in the form of JSON. So, Initially stored all responses into a char buffer and cleaned all headers. By usingif(response[i] == '{' )
and some flags.