/* Hello World Example 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 #include #include #include #include #include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_timer.h" #include "esp_log.h" #include "display.h" #include "text.h" #include "wlan.h" #include "https.h" #include "esp_heap_caps.h" #include "lwjson/lwjson.h" #ifdef CONFIG_IDF_TARGET_ESP32 #define CHIP_NAME "ESP32" #endif #ifdef CONFIG_IDF_TARGET_ESP32S2BETA #define CHIP_NAME "ESP32-S2 Beta" #endif const esp_timer_create_args_t periodic_timer_args = { .callback = &display_cycle, }; uint8_t buf_ok = 0; 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..."); vTaskDelay(1); } buf_ok = 1; vTaskDelete(NULL); } void get_json_string(char* buf, char* name, char* dest){ char* element = strstr(buf, name); char *ptr= element; while(*ptr != ':' && *ptr != 0) ptr++; while(*ptr != '"' && *ptr != 0) ptr++; ptr++; char *end=ptr; while(*end != '"') end++; strncpy(dest, ptr, end-ptr); dest[end-ptr]=0; } uint8_t json_ok = 0; 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); uint8_t line = 0; char *item = start; char txt[5][50]; while((item = strstr(item+1, "{ \"stopID\":")) != 0){ char symbol[10]; get_json_string(item, "symbol", symbol); char direction[50]; get_json_string(item, "direction", direction); char countdown[10] ; get_json_string(item, "countdown", countdown); if(atoi(countdown)>=60){ uint8_t h = atoi(countdown) / 60; uint8_t m = atoi(countdown) % 60; sprintf(countdown, "%d:%02d", h, m); } if(line < 6){ sprintf(txt[line], " %.4s", countdown); put_line(fb, line, txt[line], 1, 1); sprintf(txt[line], " %.20s", direction); put_line(fb, line, txt[line], 1, 1); sprintf(txt[line], "%.3s", symbol); put_line(fb, line, txt[line], 1, 1); } sprintf(txt[line], "%.3s\t%.20s\t%.3s", symbol, direction, countdown); printf("line = %d: %s\n", line, txt[line]); printf("\n"); line++; } //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"); //} json_ok = 1; vTaskDelete(NULL); } void app_main(void) { printf("Hello world!\n"); printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) ); ESP_LOGI("bsvg", "display init"); display_init(); printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) ); esp_timer_handle_t periodic_timer; ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer)); ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, 500)); put_line(fb, 1, "Moin", 1, 1); wlan_init(); put_line(fb, 2, "wifi", 1, 1); for (;;) { printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) ); xTaskCreate(&https_request_task, "https_get_task", 8192, NULL, 5, NULL); while (!buf_ok) { vTaskDelay(1); } printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) ); ESP_LOGI("bsvg", "got from https server:"); for(uint32_t i=0; itype); // 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"); // } // } //} vTaskDelay(6000); } }