lwjson also does not work, parsing manualy
This commit is contained in:
@@ -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
|
REQUIRES esp_driver_gpio esp_wifi nvs_flash esp_timer esp-tls
|
||||||
INCLUDE_DIRS "")
|
INCLUDE_DIRS "." "lwjson/lwjson/src/include")
|
||||||
|
|||||||
21
main/https.c
21
main/https.c
@@ -10,12 +10,12 @@
|
|||||||
|
|
||||||
static const char *TAG = "https";
|
static const char *TAG = "https";
|
||||||
|
|
||||||
//#define WEB_SERVER "bsvg.efa.de"
|
#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_PORT "443"
|
#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"
|
static const char REQUEST[] = "GET " WEB_URL " HTTP/1.1\r\n"
|
||||||
"Host: "WEB_SERVER"\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;
|
len = ret;
|
||||||
ESP_LOGI(TAG, "%d bytes read", len);
|
ESP_LOGI(TAG, "%d bytes read", len);
|
||||||
|
|
||||||
if(!strstr(buf, "departureList")){
|
//if(!strstr(buf, "departureList")){
|
||||||
ESP_LOGI(TAG,"tossing ... strstr: %ld", (uint32_t)strstr(buf, "departureList"));
|
// ESP_LOGI(TAG,"tossing ... strstr: %ld", (uint32_t)strstr(buf, "departureList"));
|
||||||
write_offset=0;
|
// write_offset=0;
|
||||||
}else
|
//}else
|
||||||
ESP_LOGI(TAG,"keeping ... strstr: %ld", (uint32_t)strstr(buf, "departureList"));
|
// ESP_LOGI(TAG,"keeping ... strstr: %ld", (uint32_t)strstr(buf, "departureList"));
|
||||||
|
|
||||||
} while (1);
|
} while (1);
|
||||||
|
buf[write_offset+len+1] = 0;
|
||||||
|
|
||||||
esp_tls_conn_destroy(tls);
|
esp_tls_conn_destroy(tls);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
111
main/main.c
111
main/main.c
@@ -22,9 +22,10 @@
|
|||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include "https.h"
|
#include "https.h"
|
||||||
|
|
||||||
#include "cJSON/cJSON.h"
|
|
||||||
#include "esp_heap_caps.h"
|
#include "esp_heap_caps.h"
|
||||||
|
|
||||||
|
#include "lwjson/lwjson.h"
|
||||||
|
|
||||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||||
#define CHIP_NAME "ESP32"
|
#define CHIP_NAME "ESP32"
|
||||||
#endif
|
#endif
|
||||||
@@ -39,7 +40,7 @@ const esp_timer_create_args_t periodic_timer_args = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
uint8_t buf_ok = 0;
|
uint8_t buf_ok = 0;
|
||||||
char bsvg_buf[33000];
|
char bsvg_buf[40000];
|
||||||
static void https_request_task(void *pvparameters){
|
static void https_request_task(void *pvparameters){
|
||||||
while(https_get_request_using_crt_bundle(bsvg_buf, sizeof(bsvg_buf)) != 0){
|
while(https_get_request_using_crt_bundle(bsvg_buf, sizeof(bsvg_buf)) != 0){
|
||||||
ESP_LOGE("bsvg", "Failed to get data from server. Retrying...");
|
ESP_LOGE("bsvg", "Failed to get data from server. Retrying...");
|
||||||
@@ -49,15 +50,47 @@ static void https_request_task(void *pvparameters){
|
|||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//static lwjson_token_t tokens[1100];
|
||||||
|
//static lwjson_t lwjson;
|
||||||
|
|
||||||
uint8_t json_ok = 0;
|
uint8_t json_ok = 0;
|
||||||
cJSON *departureList;
|
|
||||||
static void json_parse_task(void *pvparameters){
|
static void json_parse_task(void *pvparameters){
|
||||||
//uint16_t start_ptr = 0;
|
//uint16_t start_ptr = 0;
|
||||||
//while(buf[start_ptr] != '{' && start_ptr < sizeof(buf))
|
//while(buf[start_ptr] != '{' && start_ptr < sizeof(buf))
|
||||||
// start_ptr++;
|
// start_ptr++;
|
||||||
char *start = strstr(bsvg_buf, "\"departureList\"") + 17;
|
char *start = strstr(bsvg_buf, "\"departureList\"") + 17;
|
||||||
|
//char *start = strstr(bsvg_buf, "{");
|
||||||
ESP_LOGI("bsvg", "start at %ld: \"%.10s\"", (uint32_t)start, start);
|
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) );
|
printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) );
|
||||||
|
|
||||||
json_ok = 1;
|
json_ok = 1;
|
||||||
@@ -108,41 +141,41 @@ void app_main(void)
|
|||||||
}
|
}
|
||||||
ESP_LOGI("bsvg", "Done");
|
ESP_LOGI("bsvg", "Done");
|
||||||
|
|
||||||
if (departureList == NULL)
|
//if (departureList == NULL)
|
||||||
{
|
//{
|
||||||
ESP_LOGI("bsvg", "did not get list");
|
// ESP_LOGI("bsvg", "did not get list");
|
||||||
const char *error_ptr = cJSON_GetErrorPtr();
|
// const char *error_ptr = cJSON_GetErrorPtr();
|
||||||
heap_caps_print_heap_info(MALLOC_CAP_DEFAULT);
|
// heap_caps_print_heap_info(MALLOC_CAP_DEFAULT);
|
||||||
if (error_ptr != NULL)
|
// if (error_ptr != NULL)
|
||||||
{
|
// {
|
||||||
fprintf(stderr, "Error at %ld: %.10s\n", (uint32_t)error_ptr-(uint32_t)bsvg_buf, error_ptr);
|
// 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, "%.100s%.100s\n", error_ptr-100, error_ptr);
|
||||||
fprintf(stderr, " ^\n");
|
// fprintf(stderr, " ^\n");
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
fprintf(stderr, "did not get err ptr\n");
|
// fprintf(stderr, "did not get err ptr\n");
|
||||||
}
|
// }
|
||||||
}else{
|
//}else{
|
||||||
ESP_LOGI("bsvg", "got list");
|
// ESP_LOGI("bsvg", "got list");
|
||||||
printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) );
|
// printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) );
|
||||||
ESP_LOGI("bsvg", "type: %d", departureList->type);
|
// ESP_LOGI("bsvg", "type: %d", departureList->type);
|
||||||
ESP_LOGI("bsvg", "child: %ld", (uint32_t)departureList->child);
|
// ESP_LOGI("bsvg", "child: %ld", (uint32_t)departureList->child);
|
||||||
if(cJSON_IsArray(departureList)){
|
// if(cJSON_IsArray(departureList)){
|
||||||
ESP_LOGI("bsvg", "is Array");
|
// ESP_LOGI("bsvg", "is Array");
|
||||||
const cJSON *departure = NULL;
|
// const cJSON *departure = NULL;
|
||||||
cJSON_ArrayForEach(departure, departureList){
|
// cJSON_ArrayForEach(departure, departureList){
|
||||||
cJSON *number = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(departure, "servingLine"), "number");;
|
// cJSON *number = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(departure, "servingLine"), "number");;
|
||||||
if(number == NULL)
|
// if(number == NULL)
|
||||||
ESP_LOGI("bsvg", "cant get number");
|
// ESP_LOGI("bsvg", "cant get number");
|
||||||
else if(cJSON_IsNumber(number))
|
// else if(cJSON_IsNumber(number))
|
||||||
ESP_LOGI("bsvg", "number (int): %d\n", number->valueint);
|
// ESP_LOGI("bsvg", "number (int): %d\n", number->valueint);
|
||||||
else if(cJSON_IsString(number))
|
// else if(cJSON_IsString(number))
|
||||||
ESP_LOGI("bsvg", "number (str): %.3s\n", number->valuestring);
|
// ESP_LOGI("bsvg", "number (str): %.3s\n", number->valuestring);
|
||||||
else
|
// else
|
||||||
ESP_LOGI("bsvg", "cant get number value");
|
// ESP_LOGI("bsvg", "cant get number value");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|||||||
Reference in New Issue
Block a user