problems with esp-32 coding

wolly09
Posts: 2
Joined: Sat Jun 03, 2023 8:34 pm

problems with esp-32 coding

Postby wolly09 » 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:

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.

a2800276
Posts: 78
Joined: Sat Jan 23, 2016 1:59 pm

Re: problems with esp-32 coding

Postby a2800276 » Sun Jun 04, 2023 8:25 am

The problem is that you're checking IF new data is available from Bluetooth and saving that data to a variable `datibt`.

In a second step, you are sending the string stored in `dataibt` to the API. The problem is that the second step is not constrained by the original IF. The data is sent to the API whether new data was received or not.

This is a pretty simple mistake and I assume you are new to programming. Some suggestions:

- instead of calling the expensive API, write a function that pretends to be the API and call that until you've worked out the bugs.

- you could add a second variable called `last_query` and compare it to the data you last sent to the API to see if you have a new query. And only call the API in that case.

- concerning how to return data via Bluetooth, that's not really a yes or no question. You will have to look at some Bluetooth tutorials and learn how to do it yourself! It also really depends on the app here using two communicate via Bluetooth on the other end.

Good luck and have fun!

wolly09
Posts: 2
Joined: Sat Jun 03, 2023 8:34 pm

Re: problems with esp-32 coding

Postby wolly09 » Sun Jun 04, 2023 4:36 pm

Oh I understood, thank you. Now I realised that I need to not send the data by bluethooth but I need to use usb because on my tablet I don't have BLE 5 and I have to keep it connected with another thing, so I have to create an android app that sends data to Esp-32 and then get some back. Do you know something on how to do it? (did I mention I use MIT app inventor?)

MicroController
Posts: 1702
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: problems with esp-32 coding

Postby MicroController » Sun Jun 04, 2023 8:33 pm

send the result that the esp-32 took from ChatGPT to the bluethooth device, so what do I have to change in the code?
You should be able to just use

Code: Select all

SerialBT.println(...);
to send stuff back over Bluetooth.

Who is online

Users browsing this forum: No registered users and 41 guests