Start of json parsing. Sometimes http get only gets partial json.
This commit is contained in:
1 parent
42e51a4a1c
commit
5e6606e6cc
4 files changed
+66
-9
No files matched your search
Vendored
+3
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"cmake.ignoreCMakeListsMissing": true
|
||||||
|
}
|
||||||
+4
-3
@@ -13,7 +13,8 @@ platform = https://github.com/maxgerhardt/platform-raspberrypi.git
|
|||||||
board = rpipicow
|
board = rpipicow
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
build_flags =
|
build_flags =
|
||||||
-I include
|
-I include
|
||||||
-DPIO_FRAMEWORK_ARDUINO_ENABLE_FREERTOS
|
-DPIO_FRAMEWORK_ARDUINO_ENABLE_FREERTOS
|
||||||
build_src_filter = +<main.cpp>
|
build_src_filter = +<main.cpp>
|
||||||
|
lib_deps = bblanchon/ArduinoJson@7.4.3
|
||||||
+55
-2
@@ -3,15 +3,38 @@
|
|||||||
#include "FreeRTOS.h"
|
#include "FreeRTOS.h"
|
||||||
#include "task.h"
|
#include "task.h"
|
||||||
#include "secrets.h"
|
#include "secrets.h"
|
||||||
|
#include "ArduinoJson.h"
|
||||||
|
|
||||||
void get_next_race(void *pvParameters) {
|
void get_next_race(void *pvParameters) {
|
||||||
TickType_t retryDelay = pdMS_TO_TICKS(10000);
|
TickType_t retryDelay = pdMS_TO_TICKS(10000);
|
||||||
while (true) {
|
while (true) {
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
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);
|
retryDelay = pdMS_TO_TICKS(10000);
|
||||||
|
|
||||||
|
WiFiClient client;
|
||||||
|
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
|
http.setTimeout(10000);
|
||||||
http.setCACert(OPENF1_ROOT_CA);
|
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");
|
"https://api.openf1.org/v1/sessions?date_start%3E=2026-11-01&date_start%3C=2026-12-01&session_type=Race");
|
||||||
if (!requestReady) {
|
if (!requestReady) {
|
||||||
Serial.print("");
|
Serial.print("");
|
||||||
@@ -19,10 +42,40 @@ void get_next_race(void *pvParameters) {
|
|||||||
} else {
|
} else {
|
||||||
const int httpResponseCode = http.GET();
|
const int httpResponseCode = http.GET();
|
||||||
if (httpResponseCode > 0) {
|
if (httpResponseCode > 0) {
|
||||||
|
Stream& responseStream = http.getStream();
|
||||||
String response = http.getString();
|
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.print("");
|
||||||
Serial.println("Received response from OpenF1 API:");
|
Serial.println("Received response from OpenF1 API:");
|
||||||
Serial.println(response);
|
Serial.println(response);
|
||||||
|
|
||||||
|
JsonDocument json;
|
||||||
|
DeserializationError err;
|
||||||
|
err = deserializeJson(json, responseStream);
|
||||||
|
if (err == DeserializationError::Ok) {
|
||||||
|
JsonArray sessions = json.as<JsonArray>();
|
||||||
|
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);
|
retryDelay = pdMS_TO_TICKS(900000);
|
||||||
} else {
|
} else {
|
||||||
Serial.print("");
|
Serial.print("");
|
||||||
@@ -36,7 +89,7 @@ void get_next_race(void *pvParameters) {
|
|||||||
http.end();
|
http.end();
|
||||||
} else {
|
} else {
|
||||||
Serial.print("");
|
Serial.print("");
|
||||||
Serial.println("WiFi not connected.");
|
Serial.println("WiFi not connecteFormattedd.");
|
||||||
}
|
}
|
||||||
vTaskDelay(retryDelay);
|
vTaskDelay(retryDelay);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -94,7 +94,7 @@ void display_loop(void *pvParameters) {
|
|||||||
Serial.println(counterStr);
|
Serial.println(counterStr);
|
||||||
|
|
||||||
Paint_Clear(WHITE);
|
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(
|
EPD_4IN2_V2_PartialDisplay(
|
||||||
counterBuffer,
|
counterBuffer,
|
||||||
@@ -128,13 +128,13 @@ void setup() {
|
|||||||
Paint_NewImage(displayBuffer, EPD_4IN2_V2_WIDTH, EPD_4IN2_V2_HEIGHT, ROTATE_0, WHITE);
|
Paint_NewImage(displayBuffer, EPD_4IN2_V2_WIDTH, EPD_4IN2_V2_HEIGHT, ROTATE_0, WHITE);
|
||||||
Paint_SelectImage(displayBuffer);
|
Paint_SelectImage(displayBuffer);
|
||||||
Paint_Clear(WHITE);
|
Paint_Clear(WHITE);
|
||||||
Paint_DrawString_EN(10, 0, "F1 Display Test", &Font16, BLACK, WHITE);
|
Paint_DrawString_EN(10, 0, "F1 Display Test", &Font16, WHITE, BLACK);
|
||||||
Paint_DrawString_EN(10, COUNTER_Y, "Counter: 0", &Font16, BLACK, WHITE);
|
Paint_DrawString_EN(10, COUNTER_Y, "Counter: 0", &Font16, WHITE, BLACK);
|
||||||
EPD_4IN2_V2_Display(displayBuffer);
|
EPD_4IN2_V2_Display(displayBuffer);
|
||||||
|
|
||||||
Paint_NewImage(counterBuffer, COUNTER_WIDTH, COUNTER_HEIGHT, ROTATE_0, WHITE);
|
Paint_NewImage(counterBuffer, COUNTER_WIDTH, COUNTER_HEIGHT, ROTATE_0, WHITE);
|
||||||
Paint_SelectImage(counterBuffer);
|
Paint_SelectImage(counterBuffer);
|
||||||
|
|
||||||
xTaskCreate(display_loop, "display_loop", 1024, NULL, 5, NULL);
|
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);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user