You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.5 KiB
C

/* 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 <stdint.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#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++;
while(*ptr != '"') 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(line < 6){
sprintf(txt[line], " %.3s", 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);
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; i<sizeof(bsvg_buf); i++){
putchar(bsvg_buf[i]);
if(i%1000==0)
vTaskDelay(1);
}
putchar('\n');
printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) );
put_line(fb, 3, "https", 1, 1);
xTaskCreate(&json_parse_task, "json_parse_task", 8192, NULL, 5, NULL);
ESP_LOGI("bsvg", "Parsing JSON...");
while (!json_ok) {
printf("free heap: %d\n", heap_caps_get_free_size( MALLOC_CAP_DEFAULT) );
vTaskDelay(1);
}
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");
// }
// }
//}
for (;;) {
vTaskDelay(1);
}
}