You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.
i have got error in LED bulletin line in cloudspeechclient.cpp so I have removed all led lines from speech to text and cloudspecchclient.cpp
here is the new program
#define button 23 //IR Sensor
#define RXp2 16
#define TXp2 17
#include "Audio.h"
#include "CloudSpeechClient.h"
int i=0;
void setup() {
pinMode(button, INPUT);
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXp2,TXp2);
Serial2.println("Intialising");
// Serial.println(My_Data);
}
void loop() {
if(i==0){
Serial.println("Press button");
i=1;
}
// if(i==1){delay(1);}
delay(500);
if(digitalRead(button)==0){
Serial2.println("\r\nPlease Ask!\r\n");
delay(2100);
Serial.println("\r\nRecord start!\r\n");
//Serial2.println("\r\nRecord start!\r\n");
Audio* audio = new Audio(ADMP441);
//Audio* audio = new Audio(M5STACKFIRE);
audio->Record();
Serial.println("Recoding Complited Processing");
CloudSpeechClient* cloudSpeechClient = new CloudSpeechClient(USE_APIKEY);
cloudSpeechClient->Transcribe(audio);
delete cloudSpeechClient;
delete audio;
i=0;
}
if(digitalRead(button)==1){
delay(1);
}
cloudspecchclient.cpp
#include "CloudSpeechClient.h"
#include "network_param.h"
#include <base64.h>
#include <ArduinoJson.h>
#define USE_SERIAL Serial
#include <Arduino.h>
#include <HTTPClient.h>
//#define uart_en 15
//#include <SoftwareSerial.h>
////SoftwareSerial (D4, D2);
const char* chatgpt_token = "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";
CloudSpeechClient::CloudSpeechClient(Authentication authentication) {
this->authentication = authentication;
WiFi.begin(ssid, password);
// while (WiFi.status() == WL_CONNECTED){ digitalWrite(led_3,1);}
while (WiFi.status() != WL_CONNECTED) delay(1000);
client.setCACert(root_ca);
if (!client.connect(server, 443)) Serial.println("Connection failed!");
}
String ans;
CloudSpeechClient::~CloudSpeechClient() {
client.stop();
WiFi.disconnect();
}
void CloudSpeechClient::PrintHttpBody2(Audio* audio)
{
String enc = base64::encode(audio->paddedHeader, sizeof(audio->paddedHeader));
enc.replace("\n", ""); // delete last "\n"
client.print(enc); // HttpBody2
char** wavData = audio->wavData;
for (int j = 0; j < audio->wavDataSize / audio->dividedWavDataSize; ++j) {
enc = base64::encode((byte*)wavData[j], audio->dividedWavDataSize);
enc.replace("\n", "");// delete last "\n"
client.print(enc); // HttpBody2
}
}
void CloudSpeechClient::Transcribe(Audio* audio) {
String HttpBody1 = "{"config":{"encoding":"LINEAR16","sampleRateHertz":16000,"languageCode":"en-IN"},"audio":{"content":"";
String HttpBody3 = ""}}\r\n\r\n";
int httpBody2Length = (audio->wavDataSize + sizeof(audio->paddedHeader)) * 4 / 3; // 4/3 is from base64 encoding
String ContentLength = String(HttpBody1.length() + httpBody2Length + HttpBody3.length());
String HttpHeader;
// if (authentication == USE_APIKEY)
HttpHeader = String("POST /v1/speech:recognize?key=") + ApiKey
+ String(" HTTP/1.1\r\nHost: speech.googleapis.com\r\nContent-Type: application/json\r\nContent-Length: ") + ContentLength + String("\r\n\r\n");
// else if (authentication == USE_ACCESSTOKEN)
// HttpHeader = String("POST /v1beta1/speech:syncrecognize HTTP/1.1\r\nHost: speech.googleapis.com\r\nContent-Type: application/json\r\nAuthorization: Bearer ")
// + AccessToken + String("\r\nContent-Length: ") + ContentLength + String("\r\n\r\n");
client.print(HttpHeader);
client.print(HttpBody1);
PrintHttpBody2(audio);
client.print(HttpBody3);
String My_Answer="";
while (!client.available());
while (client.available())
{
char temp = client.read();
My_Answer = My_Answer + temp;
// Serial.write(client.read());
}
// Serial.print("My Answer - ");Serial.println(My_Answer);
int postion = My_Answer.indexOf('{');
// Serial.println(postion);
ans = My_Answer.substring(postion);
Serial.print("Json daata--");
//Serial.print(ans);
DynamicJsonDocument doc(384);
//StaticJsonDocument<384> doc;
DeserializationError error = deserializeJson(doc, ans);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
JsonObject results_0 = doc["results"][0];
//const char*
const char* chatgpt_Q = results_0["alternatives"][0]["transcript"];
const char* a= "light on";
const char* b= "light off";
//String chatgpt_Q = a+ans+b;
//Serial.println(ans);
Serial.print(chatgpt_Q);Serial.println("-");
///////////////////////////////////////////////////////////
{
HTTPClient http;
} {
Serial.println("Asking Chat GPT");
HTTPClient https;
DeserializationError error = deserializeJson(doc2, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
JsonObject choices_0 = doc2["choices"][0];
const char* only_ans = choices_0["text"];
Serial.println("Only ans:-");Serial.print(only_ans);
Serial2.print(only_ans);
//digitalWrite(uart_en, LOW);
delay(1);
//digitalWrite(uart_en,HIGH);
/////////////////////////////////////////////////////////
}
else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
}
else {
Serial.printf("[HTTPS] Unable to connect\n");
}
Serial.print("To ask again");
//delay(10000);
}
///////////////////////////////////////////////////////////
/*
*/
}
now the issue is the chatgpt API request is failing , how to solve this issue
serial monitor output:
Record start!
Recoding Complited Processing
<title>Example Domain</title> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style>Json daata---
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 200
<!doctype html>
Example Domain
This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.
More information...
Asking Chat GPT
[HTTPS] begin...
[HTTPS] GET...
[HTTPS] GET... failed, error: connection refused
To ask againPress button
The text was updated successfully, but these errors were encountered: