diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index b4e1667..a64c0f6 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "main.c" "font.c" "display.c" "text.c" "wlan.c" "https.c" "cJSON/cJSON.c" +idf_component_register(SRCS "main.c" "font.c" "display.c" "text.c" "wlan.c" "https.c" "lwjson/lwjson/src/lwjson/lwjson.c" REQUIRES esp_driver_gpio esp_wifi nvs_flash esp_timer esp-tls - INCLUDE_DIRS "") + INCLUDE_DIRS "." "lwjson/lwjson/src/include") diff --git a/main/https.c b/main/https.c index c3eba6a..2e666a7 100644 --- a/main/https.c +++ b/main/https.c @@ -10,12 +10,12 @@ static const char *TAG = "https"; -//#define WEB_SERVER "bsvg.efa.de" -//#define WEB_PORT "443" -//#define WEB_URL "/bsvagstd/XML_DM_REQUEST?outputFormat=JSON&stateless=1&locationServerActive=1&type_dm=stop&name_dm=\"Freiastr,Braunschweig\"&mode=direct&ptOptionsActive=1&useRealtime=0" -#define WEB_SERVER "mustbehax.de" +#define WEB_SERVER "bsvg.efa.de" #define WEB_PORT "443" -#define WEB_URL "/web/reduced.json" +#define WEB_URL "/bsvagstd/XML_DM_REQUEST?outputFormat=JSON&stateless=1&locationServerActive=1&type_dm=stop&name_dm=\"Freiastr,Braunschweig\"&mode=direct&ptOptionsActive=1&useRealtime=1" +//#define WEB_SERVER "mustbehax.de" +//#define WEB_PORT "443" +//#define WEB_URL "/web/reduced.json" static const char REQUEST[] = "GET " WEB_URL " HTTP/1.1\r\n" "Host: "WEB_SERVER"\r\n" @@ -101,13 +101,14 @@ esp_err_t https_get_request_using_crt_bundle(char* buf, size_t bufsize) len = ret; ESP_LOGI(TAG, "%d bytes read", len); - if(!strstr(buf, "departureList")){ - ESP_LOGI(TAG,"tossing ... strstr: %ld", (uint32_t)strstr(buf, "departureList")); - write_offset=0; - }else - ESP_LOGI(TAG,"keeping ... strstr: %ld", (uint32_t)strstr(buf, "departureList")); + //if(!strstr(buf, "departureList")){ + // ESP_LOGI(TAG,"tossing ... strstr: %ld", (uint32_t)strstr(buf, "departureList")); + // write_offset=0; + //}else + // ESP_LOGI(TAG,"keeping ... strstr: %ld", (uint32_t)strstr(buf, "departureList")); } while (1); + buf[write_offset+len+1] = 0; esp_tls_conn_destroy(tls); return 0; diff --git a/main/main.c b/main/main.c index 4324810..320f805 100644 --- a/main/main.c +++ b/main/main.c @@ -22,9 +22,10 @@ #include "wlan.h" #include "https.h" -#include "cJSON/cJSON.h" #include "esp_heap_caps.h" +#include "lwjson/lwjson.h" + #ifdef CONFIG_IDF_TARGET_ESP32 #define CHIP_NAME "ESP32" #endif @@ -39,7 +40,7 @@ const esp_timer_create_args_t periodic_timer_args = { }; uint8_t buf_ok = 0; -char bsvg_buf[33000]; +char bsvg_buf[40000]; static void https_request_task(void *pvparameters){ while(https_get_request_using_crt_bundle(bsvg_buf, sizeof(bsvg_buf)) != 0){ ESP_LOGE("bsvg", "Failed to get data from server. Retrying..."); @@ -49,15 +50,47 @@ static void https_request_task(void *pvparameters){ vTaskDelete(NULL); } +//static lwjson_token_t tokens[1100]; +//static lwjson_t lwjson; + uint8_t json_ok = 0; -cJSON *departureList; static void json_parse_task(void *pvparameters){ //uint16_t start_ptr = 0; //while(buf[start_ptr] != '{' && start_ptr < sizeof(buf)) // start_ptr++; char *start = strstr(bsvg_buf, "\"departureList\"") + 17; + //char *start = strstr(bsvg_buf, "{"); ESP_LOGI("bsvg", "start at %ld: \"%.10s\"", (uint32_t)start, start); - departureList = cJSON_Parse(start); + //departureList = cJSON_Parse(start); + + char *item = start; + while((item = strstr(item+1, "{ \"stopID\":")) != 0){ + char *symbol = strstr(item, "\"symbol\""); + char *direction = strstr(item, "\"direction\""); + char *countdown = strstr(item, "\"countdown\""); + if(symbol && direction && countdown) + printf("%.15s\t%.30s\t%.15s\n", symbol, direction, countdown); + else + printf("cant find symbol\n"); + } + + //lwjson_init(&lwjson, tokens, LWJSON_ARRAYSIZE(tokens)); + //if (lwjson_parse(&lwjson, start) == lwjsonOK) { + // const lwjson_token_t* t; + // printf("JSON parsed..\r\n"); + + // /* Find custom key in JSON */ + // if ((t = lwjson_find(&lwjson, "stopID")) != NULL) { + // printf("Key found with data type: %d\r\n", (int)t->type); + // } + + // /* Call this when not used anymore */ + // lwjson_free(&lwjson); + //} + //else{ + // ESP_LOGI("bsvg", "parse fail"); + //} + printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) ); json_ok = 1; @@ -108,41 +141,41 @@ void app_main(void) } ESP_LOGI("bsvg", "Done"); - if (departureList == NULL) - { - ESP_LOGI("bsvg", "did not get list"); - const char *error_ptr = cJSON_GetErrorPtr(); - heap_caps_print_heap_info(MALLOC_CAP_DEFAULT); - if (error_ptr != NULL) - { - fprintf(stderr, "Error at %ld: %.10s\n", (uint32_t)error_ptr-(uint32_t)bsvg_buf, error_ptr); - fprintf(stderr, "%.100s%.100s\n", error_ptr-100, error_ptr); - fprintf(stderr, " ^\n"); - } - else { - fprintf(stderr, "did not get err ptr\n"); - } - }else{ - ESP_LOGI("bsvg", "got list"); - printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) ); - ESP_LOGI("bsvg", "type: %d", departureList->type); - ESP_LOGI("bsvg", "child: %ld", (uint32_t)departureList->child); - if(cJSON_IsArray(departureList)){ - ESP_LOGI("bsvg", "is Array"); - const cJSON *departure = NULL; - cJSON_ArrayForEach(departure, departureList){ - cJSON *number = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(departure, "servingLine"), "number");; - if(number == NULL) - ESP_LOGI("bsvg", "cant get number"); - else if(cJSON_IsNumber(number)) - ESP_LOGI("bsvg", "number (int): %d\n", number->valueint); - else if(cJSON_IsString(number)) - ESP_LOGI("bsvg", "number (str): %.3s\n", number->valuestring); - else - ESP_LOGI("bsvg", "cant get number value"); - } - } - } + //if (departureList == NULL) + //{ + // ESP_LOGI("bsvg", "did not get list"); + // const char *error_ptr = cJSON_GetErrorPtr(); + // heap_caps_print_heap_info(MALLOC_CAP_DEFAULT); + // if (error_ptr != NULL) + // { + // fprintf(stderr, "Error at %ld: %.10s\n", (uint32_t)error_ptr-(uint32_t)bsvg_buf, error_ptr); + // fprintf(stderr, "%.100s%.100s\n", error_ptr-100, error_ptr); + // fprintf(stderr, " ^\n"); + // } + // else { + // fprintf(stderr, "did not get err ptr\n"); + // } + //}else{ + // ESP_LOGI("bsvg", "got list"); + // printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) ); + // ESP_LOGI("bsvg", "type: %d", departureList->type); + // ESP_LOGI("bsvg", "child: %ld", (uint32_t)departureList->child); + // if(cJSON_IsArray(departureList)){ + // ESP_LOGI("bsvg", "is Array"); + // const cJSON *departure = NULL; + // cJSON_ArrayForEach(departure, departureList){ + // cJSON *number = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(departure, "servingLine"), "number");; + // if(number == NULL) + // ESP_LOGI("bsvg", "cant get number"); + // else if(cJSON_IsNumber(number)) + // ESP_LOGI("bsvg", "number (int): %d\n", number->valueint); + // else if(cJSON_IsString(number)) + // ESP_LOGI("bsvg", "number (str): %.3s\n", number->valuestring); + // else + // ESP_LOGI("bsvg", "cant get number value"); + // } + // } + //} for (;;) {