audio error in Running Chatgpt on ESP32 with audio output

Agitha
Posts: 1
Joined: Thu Nov 02, 2023 3:56 pm

audio error in Running Chatgpt on ESP32 with audio output

Postby Agitha » Thu Nov 02, 2023 4:27 pm

i have done this Running chatgpt on esp32 with audio output , the project worked well, I have attached outputs as well, but now when I asked question the answer only appears in serial monitor the audio output is not appearing , in earlier output there was lot of data about audio appeared but now simply question and answer is appearing,I have attached that output screenshot as well , how to rectify this

Code: Select all

#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>

#include <ArduinoJson.h>
#include "Audio.h"

const char* ssid = "AMhd";
const char* password = "agithamhd";
const char* chatgpt_token = "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";
const char* temperature = "0";
const char* max_tokens = "45";
String Question = "";

#define I2S_DOUT      25
#define I2S_BCLK      27
#define I2S_LRC       26

Audio audio;

void setup()
{
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();


  while (!Serial);

  // wait for WiFi connection
  WiFi.begin(ssid, password);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.setVolume(100);
}

void loop()
{

  Serial.print("Ask your Question : ");
  while (!Serial.available())
  {
    audio.loop();
  }
  while (Serial.available())
  {
    char add = Serial.read();
    Question = Question + add;
    delay(1);
  }
  int len = Question.length();
  Question = Question.substring(0, (len - 1));
  Question = "\"" + Question + "\"";
  Serial.println(Question);

  HTTPClient https;

  //Serial.print("[HTTPS] begin...\n");
  if (https.begin("https://api.openai.com/v1/completions")) {  // HTTPS

    https.addHeader("Content-Type", "application/json");
    String token_key = String("Bearer ") + chatgpt_token;
    https.addHeader("Authorization", token_key);

    String payload = String("{\"model\": \"text-davinci-003\", \"prompt\": ") + Question + String(", \"temperature\": ") + temperature + String(", \"max_tokens\": ") + max_tokens + String("}"); //Instead of TEXT as Payload, can be JSON as Paylaod

    //Serial.print("[HTTPS] GET...\n");

    // start connection and send HTTP header
    int httpCode = https.POST(payload);

    // httpCode will be negative on error
    // file found at server
    if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
      String payload = https.getString();
      //Serial.println(payload);

      DynamicJsonDocument doc(1024);


      deserializeJson(doc, payload);
      String Answer = doc["choices"][0]["text"];
      Answer = Answer.substring(2);
      Serial.print("Answer : "); Serial.println(Answer);
      audio.connecttospeech(Answer.c_str(), "en");

    }
    else {
      Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
    }
    https.end();
  }
  else {
    Serial.printf("[HTTPS] Unable to connect\n");
  }

  Question = "";
}

void audio_info(const char *info) {
  Serial.print("audio_info: "); Serial.println(info);
}
.
Attachments
WhatsApp Image 2023-11-02 at 9.55.58 PM.jpeg
WhatsApp Image 2023-11-02 at 9.55.58 PM.jpeg (80.41 KiB) Viewed 1246 times
Screenshot 2023-11-02 at 9.52.48 PM.png
Screenshot 2023-11-02 at 9.52.48 PM.png (83.44 KiB) Viewed 1246 times
Screenshot 2023-11-02 at 9.51.53 PM.png
Screenshot 2023-11-02 at 9.51.53 PM.png (55.73 KiB) Viewed 1246 times

Who is online

Users browsing this forum: No registered users and 123 guests