This function worked fine once, but the rest of the time it fails with a 500 error.
void requestOpenAIAudio(const String& text) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(openAIURL);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", apiKey);
String requestData = "{\"model\":\"tts-1\",\"input\":\"" + text + "\",\"voice\":\"alloy\",\"response_format\":\"opus\"}";
Serial.printf("Cadena PayLoad:%s\n", requestData.c_str());
int httpResponseCode = http.POST(requestData);
if (httpResponseCode > 0) { // Check for the returning code
Serial.printf("[HTTP] POST... code: %d\n", httpResponseCode);
if (httpResponseCode == HTTP_CODE_OK) {
String line;
Serial.println("[HTTP] Response:");
// This loop reads the response line by line and prints it
// This is where you process chunks of the response as they arrive, which is similar to streaming
while(http.connected()) {
line = http.getString();
Serial.println(line);
// Here you could process each part of the response (line) as needed
}
}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpResponseCode).c_str());
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}