From 5e6606e6cc5c757313623b9e1e7b8605904ce55a Mon Sep 17 00:00:00 2001 From: Adam Bissen Date: Thu, 5 Nov 2026 18:44:41 -0600 Subject: [PATCH] Start of json parsing. Sometimes http get only gets partial json. --- .vscode/settings.json | 3 +++ platformio.ini | 7 +++--- src/F1_api.h | 57 +++++++++++++++++++++++++++++++++++++++++-- src/main.cpp | 8 +++--- 4 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9ddf6b2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cmake.ignoreCMakeListsMissing": true +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index eb6fbc7..4827216 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,7 +13,8 @@ platform = https://github.com/maxgerhardt/platform-raspberrypi.git board = rpipicow framework = arduino monitor_speed = 115200 -build_flags = - -I include - -DPIO_FRAMEWORK_ARDUINO_ENABLE_FREERTOS +build_flags = + -I include + -DPIO_FRAMEWORK_ARDUINO_ENABLE_FREERTOS build_src_filter = + +lib_deps = bblanchon/ArduinoJson@7.4.3 diff --git a/src/F1_api.h b/src/F1_api.h index 1ec7de1..67c307b 100644 --- a/src/F1_api.h +++ b/src/F1_api.h @@ -3,15 +3,38 @@ #include "FreeRTOS.h" #include "task.h" #include "secrets.h" +#include "ArduinoJson.h" void get_next_race(void *pvParameters) { TickType_t retryDelay = pdMS_TO_TICKS(10000); while (true) { if (WiFi.status() == WL_CONNECTED) { + + time_t now = time(nullptr); + struct tm timeinfo, futureTime; + + localtime_r(&now, &timeinfo); + + futureTime = timeinfo; + futureTime.tm_mon += 2; + mktime(&futureTime); + + char date_buffer[12]; + Serial.print(""); + strftime(date_buffer, sizeof(date_buffer), "%Y-%m-%d", &timeinfo); + Serial.printf("Current Date: %s\n", date_buffer); + + strftime(date_buffer, sizeof(date_buffer), "%Y-%m-%d", &futureTime); + Serial.printf("Future Date: %s\n", date_buffer); + retryDelay = pdMS_TO_TICKS(10000); + + WiFiClient client; + HTTPClient http; + http.setTimeout(10000); http.setCACert(OPENF1_ROOT_CA); - const bool requestReady = http.begin( + const bool requestReady = http.begin( "https://api.openf1.org/v1/sessions?date_start%3E=2026-11-01&date_start%3C=2026-12-01&session_type=Race"); if (!requestReady) { Serial.print(""); @@ -19,10 +42,40 @@ void get_next_race(void *pvParameters) { } else { const int httpResponseCode = http.GET(); if (httpResponseCode > 0) { + Stream& responseStream = http.getStream(); String response = http.getString(); + Serial.printf("HTTP Size: %d\n", http.getSize()); + Serial.printf("Response Size: %d\n", response.length()); + if (http.connected()) Serial.println("Still Connected."); + else Serial.println("Not Connected"); Serial.print(""); Serial.println("Received response from OpenF1 API:"); Serial.println(response); + + JsonDocument json; + DeserializationError err; + err = deserializeJson(json, responseStream); + if (err == DeserializationError::Ok) { + JsonArray sessions = json.as(); + if (sessions.size() != 0) { + JsonObject nextRace = sessions[0]; + const char* sessionName = nextRace["session_name"]; + const char* dateStart = nextRace["date_start"]; + const char* circuitName = nextRace["circuit_short_name"]; + Serial.print(""); + Serial.print("Next race: "); + Serial.print(sessionName); + Serial.print(" at "); + Serial.print(circuitName); + Serial.print(" on "); + Serial.println(dateStart); + } + } else { + Serial.print(""); + Serial.print("Failed to parse JSON response: "); + Serial.println(err.c_str()); + } + retryDelay = pdMS_TO_TICKS(900000); } else { Serial.print(""); @@ -36,7 +89,7 @@ void get_next_race(void *pvParameters) { http.end(); } else { Serial.print(""); - Serial.println("WiFi not connected."); + Serial.println("WiFi not connecteFormattedd."); } vTaskDelay(retryDelay); } diff --git a/src/main.cpp b/src/main.cpp index d8a2343..2d72c84 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -94,7 +94,7 @@ void display_loop(void *pvParameters) { Serial.println(counterStr); Paint_Clear(WHITE); - Paint_DrawString_EN(10, 0, counterStr, &Font16, BLACK, WHITE); + Paint_DrawString_EN(10, 0, counterStr, &Font16, WHITE, BLACK); EPD_4IN2_V2_PartialDisplay( counterBuffer, @@ -128,13 +128,13 @@ void setup() { Paint_NewImage(displayBuffer, EPD_4IN2_V2_WIDTH, EPD_4IN2_V2_HEIGHT, ROTATE_0, WHITE); Paint_SelectImage(displayBuffer); Paint_Clear(WHITE); - Paint_DrawString_EN(10, 0, "F1 Display Test", &Font16, BLACK, WHITE); - Paint_DrawString_EN(10, COUNTER_Y, "Counter: 0", &Font16, BLACK, WHITE); + Paint_DrawString_EN(10, 0, "F1 Display Test", &Font16, WHITE, BLACK); + Paint_DrawString_EN(10, COUNTER_Y, "Counter: 0", &Font16, WHITE, BLACK); EPD_4IN2_V2_Display(displayBuffer); Paint_NewImage(counterBuffer, COUNTER_WIDTH, COUNTER_HEIGHT, ROTATE_0, WHITE); Paint_SelectImage(counterBuffer); xTaskCreate(display_loop, "display_loop", 1024, NULL, 5, NULL); - xTaskCreate(get_next_race, "get_next_race", 4096, NULL, 5, NULL); + xTaskCreate(get_next_race, "get_next_race", 2048, NULL, 5, NULL); }