From f12c812d73635a773d16a3ec209f2a05809acbc9 Mon Sep 17 00:00:00 2001 From: Adam Bissen Date: Thu, 5 Nov 2026 20:26:59 -0600 Subject: [PATCH] Stream json into parser. Print city on display. --- src/F1_api.h | 110 ++++++++++++++++++++++++++++++++++----------------- src/main.cpp | 28 +++++++------ 2 files changed, 90 insertions(+), 48 deletions(-) diff --git a/src/F1_api.h b/src/F1_api.h index 67c307b..32c63f9 100644 --- a/src/F1_api.h +++ b/src/F1_api.h @@ -5,37 +5,68 @@ #include "secrets.h" #include "ArduinoJson.h" +struct race_info { + char session_name[50]; + char date_start[32]; + char circuit_short_name[50]; + uint16_t session_key; + uint16_t meeting_key; + uint16_t circuit_key; +}; + +extern race_info next_race; + +static int get_race_query_url(char* target, size_t target_size) { + if (target == NULL) return -1; + + 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]; + char fdate_buffer[12]; + Serial.print(""); + strftime(date_buffer, sizeof(date_buffer), "%Y-%m-%d", &timeinfo); + Serial.printf("Current Date: %s\n", date_buffer); + + strftime(fdate_buffer, sizeof(fdate_buffer), "%Y-%m-%d", &futureTime); + Serial.printf("Future Date: %s\n", fdate_buffer); + + snprintf(target, target_size, "https://api.openf1.org/v1/sessions?date_start>=%s&date_start<=%s&session_type=Race", + date_buffer, + fdate_buffer); + + return 0; + +} + +int parse_session_json() { + +} + 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( - "https://api.openf1.org/v1/sessions?date_start%3E=2026-11-01&date_start%3C=2026-12-01&session_type=Race"); + + char query_url[103]; + get_race_query_url(query_url, sizeof(query_url)); + Serial.print(""); + Serial.print("Query: "); + Serial.println(query_url); + + const bool requestReady = http.begin(query_url); if (!requestReady) { Serial.print(""); Serial.println("Failed to initialize OpenF1 request."); @@ -43,32 +74,37 @@ void get_next_race(void *pvParameters) { 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); + const DeserializationError 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"]; + JsonObject nextRaceJson = sessions[0]; + const char* sessionName = nextRaceJson[F("session_name")]; + const char* dateStart = nextRaceJson[F("date_start")]; + const char* circuitName = nextRaceJson[F("circuit_short_name")]; + next_race.session_key = nextRaceJson[F("session_key")]; + next_race.meeting_key = nextRaceJson[F("meeting_key")]; + next_race.circuit_key = nextRaceJson[F("circuit_key")]; + if (sessionName != NULL) { + strlcpy(next_race.session_name, sessionName, sizeof(next_race.session_name)); + } + if (dateStart != NULL) { + strlcpy(next_race.date_start, dateStart, sizeof(next_race.date_start)); + } + if (circuitName != NULL) { + strlcpy(next_race.circuit_short_name, circuitName, sizeof(next_race.circuit_short_name)); + } + Serial.print(""); Serial.print("Next race: "); - Serial.print(sessionName); + Serial.print(next_race.session_name); Serial.print(" at "); - Serial.print(circuitName); + Serial.print(next_race.circuit_short_name); Serial.print(" on "); - Serial.println(dateStart); + Serial.println(next_race.date_start); } } else { Serial.print(""); diff --git a/src/main.cpp b/src/main.cpp index 2d72c84..35c6ca9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,7 @@ static constexpr UWORD COUNTER_HEIGHT = 24; static constexpr UWORD COUNTER_Y = 20; static UBYTE counterBuffer[(COUNTER_WIDTH / 8) * COUNTER_HEIGHT]; static int counter = 0; +race_info next_race; void wifi_connect() { WiFi.setHostname(DEVICE_HOSTNAME); @@ -89,9 +90,14 @@ void display_loop(void *pvParameters) { lastRefresh = millis(); counter++; - snprintf(counterStr, sizeof(counterStr), "Counter: %d", counter); - Serial.print(""); - Serial.println(counterStr); + + if (next_race.session_key == 0) { + snprintf(counterStr, sizeof(counterStr), "Counter: %d", counter); + Serial.print(""); + Serial.println(counterStr); + } else { + snprintf(counterStr, sizeof(counterStr), "%s", next_race.circuit_short_name); + } Paint_Clear(WHITE); Paint_DrawString_EN(10, 0, counterStr, &Font16, WHITE, BLACK); @@ -117,7 +123,14 @@ void setup() { EPD_4IN2_V2_Init(); Serial.println("e-Paper initialization complete; clearing display..."); - EPD_4IN2_V2_Clear(); + //EPD_4IN2_V2_Clear(); + + 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 Booting...", &Font16, WHITE, BLACK); + Paint_DrawString_EN(10, COUNTER_Y, "Counter: 0", &Font16, WHITE, BLACK); + EPD_4IN2_V2_Display(displayBuffer); DEV_Delay_ms(500); @@ -125,13 +138,6 @@ void setup() { sync_time(); printf("Begin display loop.\n"); - 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, 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);