problems with esp-32 coding
Posted: Sat Jun 03, 2023 9:05 pm
Hi I'm working on a project where my ESP-32 Lolin32 Lite connects to the ChatGPT APi and takes the input from bluethooth where the question to ChatGPT is given from and lastly the answer coming from ChatGPT is displayed in the Serial Monitor. The code I have wrote for now works but it keeps sending the request to ChatGPT again and again, and it uses the API credit I have. Here's the code:
If I understood this is because the question part is in the loop, so it's normal that it keeps repeating it, but if I'm not wrong if I put everything in the setup part I can send only one question from the bluethooth input, except if I keep restarting the esp-32 so how do I make it do the request only when it receives a question from Bluetooth and don't keep asking it again and again.
Oh and I wanted to make it possible to send the result that the esp-32 took from ChatGPT to the bluethooth device, so what do I have to change in the code? Is it possible to connect the device with usb instead of bluethooth so that it powers the esp-32 too? I was thinking on this last option but I have to hurry up because it's for my last year's exam and it's in a week and some days.
Code: Select all
/*
* Project ChatGPT Client For ESP32
* Description: For HTTPS connection using WiFiClientSecure
*/
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <ChatGPT.hpp>
#include <BluetoothSerial.h> //aggiunto
String datibt = "What is sun"; //aggiunto
BluetoothSerial SerialBT; //aggiunto
#define RXp2 16 //aggiunto
#define TXp2 17 //aggiunto
static const char *ssid = "my wifi's ssid";
static const char *password = "my password";
WiFiClientSecure client;
ChatGPT<WiFiClientSecure> chat_gpt(&client, "v1", "...my api key...");
void setup() {
SerialBT.begin("ESP32 Bluetooth"); //aggiunto
Serial2.begin(9600, SERIAL_8N1, RXp2, TXp2); //aggiunto
Serial.begin(115200);
Serial.print("Connecting to WiFi network: ");
Serial.print(ssid);
Serial.println("'...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting...");
delay(500);
}
Serial.println("Connected!");
}
void loop() {
if (SerialBT.available()) { // Check if there is data available from Bluetooth
Serial.println("Receiving question");//
datibt = SerialBT.readString(); // Read the received data as a string
Serial.println("Received data: " + datibt); // Print the received data for debugging
}
// Ignore SSL certificate validation
client.setInsecure();
String result;
Serial.println("[ChatGPT] Only print a content message");
if (chat_gpt.simple_message("gpt-3.5-turbo-0301", "user", datibt, result)) { //modificato
Serial.println("===OK===");
Serial.println(result);
} else {
Serial.println("===ERROR===");
Serial.println(result);
}
}
If I understood this is because the question part is in the loop, so it's normal that it keeps repeating it, but if I'm not wrong if I put everything in the setup part I can send only one question from the bluethooth input, except if I keep restarting the esp-32 so how do I make it do the request only when it receives a question from Bluetooth and don't keep asking it again and again.
Oh and I wanted to make it possible to send the result that the esp-32 took from ChatGPT to the bluethooth device, so what do I have to change in the code? Is it possible to connect the device with usb instead of bluethooth so that it powers the esp-32 too? I was thinking on this last option but I have to hurry up because it's for my last year's exam and it's in a week and some days.