Yeah I know I would have done it, but this week I had everyday a different last test for every subject, and I had to revise everything we've done in the last 3 yrs and then next monday we will have 20 minutes each person were you get a word and you have to connect it to each subject so I'll talk for my example about AI in different subjects like technology, geography, science, italian, english... I mean this is how it works here in Italy, that's why I had to pause
Ok, I understood.
Here's the code:
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <ChatGPT.hpp>
#include <BluetoothSerial.h>
static const char *ssid = "Walid's Galaxy A53 5G";
static const char *password = "bngs5227";
WiFiClientSecure client;
ChatGPT<WiFiClientSecure> chat_gpt(&client, "v1", "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");
String result;
boolean newData = false;
BluetoothSerial SerialBT;
const byte numChars = 32;
char receivedChars[numChars]; //first type of data, which will be a text
char receivedCharForMoving; //second type of data which Can be : F, B, R, L, S
const int MR1 = 12; //ESP32 pins (MR=Right Motor) (ML=Left Motor) (1=Forward) (2=Backward)
const int MR2 = 14;
const int ML1 = 27;
const int ML2 = 26;
void setup() {
//______________________________WiFi______________________________________________
delay(1000);
Serial.begin(115200);
delay(1000);
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 to Wifi");
// Ignore SSL certificate validation
client.setInsecure();
//______________________________Bluetooth___________________________________________
SerialBT.begin("Galaxy A53 di Walid");
//______________________________Motors______________________________________________
pinMode(MR1, OUTPUT);
pinMode(MR2, OUTPUT);
pinMode(ML1, OUTPUT);
pinMode(ML2, OUTPUT);
}
void Forward(){
//RIGHT MOTOR
digitalWrite(MR1,HIGH);//MOVE FRONT
digitalWrite(MR2,LOW); //MOVE BACK
//LEFT MOTOR
digitalWrite(ML1,LOW);//MOVE BACK
digitalWrite(ML2,HIGH);//MOVE FRONT
}
void Backward(){
digitalWrite(MR1,LOW);
digitalWrite(MR2,HIGH);
digitalWrite(ML1,HIGH);
digitalWrite(ML2,LOW);
}
void Left(){
digitalWrite(MR1,HIGH);
digitalWrite(MR2,LOW);
digitalWrite(ML1,HIGH);
digitalWrite(ML2,LOW);
}
void Right(){
digitalWrite(MR1,LOW);
digitalWrite(MR2,HIGH);
digitalWrite(ML1,LOW);
digitalWrite(ML2,HIGH);
}
void Stop(){
digitalWrite(MR1,LOW);
digitalWrite(MR2,LOW);
digitalWrite(ML1,LOW);
digitalWrite(ML2,LOW);
}
void loop() {
receivedCharForMoving =(char)SerialBT.read();
if (SerialBT.available()) {
if(receivedCharForMoving == 'F')
{
Forward();
}
if(receivedCharForMoving == 'B')
{
Backward();
}
if(receivedCharForMoving == 'L')
{
Left();
}
if(receivedCharForMoving == 'R')
{
Right();
}
if(receivedCharForMoving == 'S')
{
Stop();
}
}
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '/';
char rc;
while (SerialBT.available() > 0 && newData == false) {
rc = SerialBT.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.println("[ChatGPT]");
if (chat_gpt.simple_message("gpt-3.5-turbo-0301", "user", receivedChars, result)) {
// Connect to the other phone via Bluetooth
BluetoothSerial bt;
bt.begin("Tab A 8.0"); // Set Bluetooth name of the other phone
// Send the data to the other phone
bt.println(result);
// Disconnect from the other phone
bt.end();
} else {
Serial.println("===ERROR===");
Serial.println(result);
}
newData = false;
}
}
I've worked on it for some hours, step by step like @StefanL38 and @GolamMostafa told me, and this should connect to both the BT devices, but I think(not sure about this) that at the begin it (the ESP32) will be able to receive the moving commands but after I send a question to ChatGPT it won't be able to receive the data anymore because the endmarker will be activated, or I am wrong?
Is everything else fine?
(P.S.) At line 17-18 there are the 2 chars for the 2 types of input:
receivedChars is the text that will be sent to ChatGPT
receivedCharForMoving is one only char where:
F is for Forward
B is for Backward
L is for Left
R is for Right
S is for Stop