├── .DS_Store ├── .gitattributes ├── Schematic_ChatGPTV2.pdf ├── ESP32_Speech_to_Text ├── .DS_Store ├── I2S.h ├── CloudSpeechClient.h ├── Credentials.h ├── Audio.h ├── ESP32_Speech_to_Text.ino ├── I2S.cpp ├── Audio.cpp ├── network_param.h └── CloudSpeechClient.cpp ├── README.md └── ESP32_Text_to_Speech ├── debug_custom.json ├── debug.cfg └── ESP32_Text_to_Speech.ino /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/ESP32-ChatGPT-V2/main/.DS_Store -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Schematic_ChatGPTV2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/ESP32-ChatGPT-V2/main/Schematic_ChatGPTV2.pdf -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/ESP32-ChatGPT-V2/main/ESP32_Speech_to_Text/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ESP32 Boards Package Link - https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json 2 | -------------------------------------------------------------------------------- /ESP32_Text_to_Speech/debug_custom.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"Arduino on ESP32", 3 | "toolchainPrefix":"xtensa-esp32-elf", 4 | "svdFile":"esp32.svd", 5 | "request":"attach", 6 | "postAttachCommands":[ 7 | "set remote hardware-watchpoint-limit 2", 8 | "monitor reset halt", 9 | "monitor gdb_sync", 10 | "thb setup", 11 | "c" 12 | ], 13 | "overrideRestartCommands":[ 14 | "monitor reset halt", 15 | "monitor gdb_sync", 16 | "thb setup", 17 | "c" 18 | ] 19 | } -------------------------------------------------------------------------------- /ESP32_Text_to_Speech/debug.cfg: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: GPL-2.0-or-later 2 | # 3 | # Example OpenOCD configuration file for ESP32-WROVER-KIT board. 4 | # 5 | # For example, OpenOCD can be started for ESP32 debugging on 6 | # 7 | # openocd -f board/esp32-wrover-kit-3.3v.cfg 8 | # 9 | 10 | # Source the JTAG interface configuration file 11 | source [find interface/ftdi/esp32_devkitj_v1.cfg] 12 | set ESP32_FLASH_VOLTAGE 3.3 13 | # Source the ESP32 configuration file 14 | source [find target/esp32.cfg] 15 | -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/I2S.h: -------------------------------------------------------------------------------- 1 | #ifndef _I2S_H 2 | #define _I2S_H 3 | #include 4 | #include "freertos/FreeRTOS.h" 5 | #include "freertos/task.h" 6 | #include "driver/i2s.h" 7 | #include "esp_system.h" 8 | 9 | enum MicType { 10 | ADMP441, 11 | ICS43434, 12 | M5GO, 13 | M5STACKFIRE 14 | }; 15 | 16 | class I2S { 17 | i2s_bits_per_sample_t BITS_PER_SAMPLE; 18 | public: 19 | I2S(MicType micType); 20 | int Read(char* data, int numData); 21 | int GetBitPerSample(); 22 | }; 23 | 24 | #endif // _I2S_H 25 | -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/CloudSpeechClient.h: -------------------------------------------------------------------------------- 1 | #ifndef _CLOUDSPEECHCLIENT_H 2 | #define _CLOUDSPEECHCLIENT_H 3 | #include 4 | #include "Audio.h" 5 | 6 | enum Authentication { 7 | USE_ACCESSTOKEN, 8 | USE_APIKEY 9 | }; 10 | 11 | class CloudSpeechClient { 12 | WiFiClientSecure client; 13 | void PrintHttpBody2(Audio* audio); 14 | Authentication authentication; 15 | 16 | public: 17 | CloudSpeechClient(Authentication authentication); 18 | ~CloudSpeechClient(); 19 | void Transcribe(Audio* audio); 20 | }; 21 | 22 | #endif // _CLOUDSPEECHCLIENT_H 23 | -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/Credentials.h: -------------------------------------------------------------------------------- 1 | // Provide all the crdentials here 2 | 3 | 4 | 5 | // Your WiFi Credentials 6 | const char *ssid = "SSID"; // WiFi SSID Name 7 | const char *password = "PASSWORD";// WiFi Password ( Keep it blank if your WiFi router is open ) 8 | 9 | 10 | // Parameters of Google Speech to Text API 11 | const String ApiKey = "GOOGLE_API";// Google Speech to Text API key 12 | String SpeechtoText_Language = "en-IN"; // Language 13 | 14 | // Parameters of OpenAI API 15 | const char* chatgpt_token = "OPENAI API"; // OpenAI API Key 16 | String OpenAI_Model = "gpt-3.5-turbo-instruct"; // Model 17 | String OpenAI_Temperature = "0.20"; // temperature 18 | String OpenAI_Max_Tokens = "40"; //Max Tokens 19 | -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/Audio.h: -------------------------------------------------------------------------------- 1 | #ifndef _AUDIO_H 2 | #define _AUDIO_H 3 | 4 | #include 5 | #include "I2S.h" 6 | 7 | // 16bit, monoral, 16000Hz, linear PCM 8 | class Audio { 9 | I2S* i2s; 10 | static const int headerSize = 44; 11 | static const int i2sBufferSize = 12000; 12 | char i2sBuffer[i2sBufferSize]; 13 | void CreateWavHeader(byte* header, int waveDataSize); 14 | 15 | public: 16 | static const int wavDataSize = 90000; // It must be multiple of dividedWavDataSize. Recording time is about 1.9 second. 17 | static const int dividedWavDataSize = i2sBufferSize/4; 18 | char** wavData; // It's divided. Because large continuous memory area can't be allocated in esp32. 19 | byte paddedHeader[headerSize + 4] = {0}; // The size must be multiple of 3 for Base64 encoding. Additional byte size must be even because wave data is 16bit. 20 | 21 | Audio(MicType micType); 22 | ~Audio(); 23 | void Record(); 24 | }; 25 | 26 | #endif // _AUDIO_H 27 | -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/ESP32_Speech_to_Text.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This is the code for Speech to Text conversion and 3 | Requesting OpenAi API to ask any questions. 4 | 5 | To use this code succesfully: 6 | 1. You need to downgrade your ESP32 Boards Package to version 1.0.6. 7 | 2. Keep all the files attached with this zip in one single folder named ESP32_Speech_to_Text. 8 | 3. Add all your Credentials like WiFi SSID Name, Password, Google Cloud Key & 9 | OpenAI API Key into Credentials.h Header File 10 | 4. Tested with Arduino IDE Verison 1.8.19 11 | 12 | 13 | If you still facing any issues, kindly watch out our video about this project on our YouTube channel 14 | YouTube channel - https://www.youtube.com/techiesms 15 | 16 | 17 | */ 18 | 19 | //IR Sensor acting as WakeUp Button 20 | #define button 23 21 | 22 | // RGB LEDs for status indication 23 | #define led_1 15 24 | #define led_2 2 25 | #define led_3 4 26 | 27 | // UART Pins of Other ESP32 for Text to Speech 28 | #define RXp2 16 29 | #define TXp2 17 30 | 31 | // Necessary Libraries 32 | #include "Audio.h" 33 | #include "CloudSpeechClient.h" 34 | 35 | 36 | int i = 0; 37 | void setup() 38 | { 39 | pinMode(button, INPUT); 40 | pinMode(led_1, OUTPUT); 41 | pinMode(led_2, OUTPUT); 42 | pinMode(led_3, OUTPUT); 43 | Serial.begin(115200); 44 | Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2); 45 | Serial2.println("Intialising"); 46 | // Serial.println(My_Data); 47 | } 48 | 49 | void loop() 50 | { 51 | 52 | digitalWrite(led_1, 0); 53 | digitalWrite(led_2, 0); 54 | digitalWrite(led_3, 0); 55 | 56 | if (i == 0) { 57 | Serial.println("Press button"); 58 | i = 1; 59 | } 60 | // if(i==1){delay(1);} 61 | 62 | delay(500); 63 | if (digitalRead(button) == 0) 64 | { 65 | Serial2.println("\r\nPlease Ask!\r\n"); 66 | // Green LED ON 67 | digitalWrite(led_1, 1); 68 | digitalWrite(led_2, 0); 69 | digitalWrite(led_3, 0); 70 | delay(2100); 71 | Serial.println("\r\nRecord start!\r\n"); 72 | Audio* audio = new Audio(ADMP441); 73 | audio->Record(); 74 | Serial.println("Processing your Audio File"); 75 | // Blue LED ON 76 | digitalWrite(led_1, 0); 77 | digitalWrite(led_2, 1); 78 | digitalWrite(led_3, 0); 79 | 80 | CloudSpeechClient* cloudSpeechClient = new CloudSpeechClient(USE_APIKEY); 81 | cloudSpeechClient->Transcribe(audio); 82 | delete cloudSpeechClient; 83 | delete audio; 84 | i = 0; 85 | } 86 | if (digitalRead(button) == 1) 87 | { 88 | delay(1); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /ESP32_Text_to_Speech/ESP32_Text_to_Speech.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This is the code for Text to Speech conversion of 3 | String respose coming from other ESP32 board. 4 | 5 | To use this code succesfully: 6 | 1. You need to downgrade your ESP32 Boards Package to version 1.0.6. 7 | 2. You need to download and install the Audio.h library mentioned in this code itself and remove other 8 | Audio.h libraries if you have already installed. 9 | 3. Add your Credentials like WiFi SSID Name, Password below to make this project 10 | connect with Internet 11 | 4. Tested with Arduino IDE version 1.8.19 12 | 13 | If you still facing any issues, kindly watch out our video about this project on our YouTube channel 14 | YouTube channel - https://www.youtube.com/techiesms 15 | 16 | 17 | */ 18 | 19 | #include "Arduino.h" 20 | #include "WiFi.h" 21 | #include "Audio.h" // Download this Library -> https://github.com/schreibfaul1/ESP32-audioI2S 22 | 23 | #define uart_en 15 24 | #define RXp2 16 25 | #define TXp2 17 26 | #define I2S_DOUT 25 27 | #define I2S_BCLK 27 28 | #define I2S_LRC 26 29 | 30 | // Your WiFi Credentials 31 | const char *ssid = "SSID"; // WiFi SSID Name 32 | const char *password = "PASS";// WiFi Password ( Keep it blank if your WiFi router is open ) 33 | 34 | Audio audio; 35 | 36 | 37 | void setup() 38 | { 39 | 40 | Serial.begin(115200); 41 | Serial2.begin(115200,SERIAL_8N1, RXp2, TXp2); 42 | 43 | WiFi.disconnect(); 44 | WiFi.mode(WIFI_STA); 45 | WiFi.begin( ssid, password); 46 | 47 | while (WiFi.status() != WL_CONNECTED) 48 | delay(1500); 49 | 50 | audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); 51 | audio.setVolume(100); 52 | audio.connecttospeech("Starting", "en"); // Google TTS 53 | } 54 | 55 | 56 | void loop() 57 | 58 | { 59 | 60 | if (Serial2.available()) { 61 | String Answer = Serial2.readString(); 62 | 63 | //----- 64 | // Split the answer into chunks and send each chunk to connecttospeech 65 | size_t chunkSize = 80; // Define chunk size (adjust if necessary) 66 | for (size_t i = 0; i < Answer.length(); i += chunkSize) { 67 | 68 | String chunk = Answer.substring(i, (i + chunkSize)); 69 | Serial.println(chunk); 70 | audio.connecttospeech(chunk.c_str(), "en"); 71 | 72 | while(audio.isRunning()){ 73 | audio.loop(); 74 | 75 | 76 | } 77 | //-------- 78 | 79 | } 80 | 81 | 82 | } 83 | audio.loop(); 84 | 85 | } 86 | 87 | void audio_info(const char *info) { 88 | 89 | } -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/I2S.cpp: -------------------------------------------------------------------------------- 1 | #include "I2S.h" 2 | #define SAMPLE_RATE (16000) 3 | #define PIN_I2S_BCLK 26 4 | #define PIN_I2S_LRC 22 5 | #define PIN_I2S_DIN 34 6 | #define PIN_I2S_DOUT 25 7 | 8 | // This I2S specification : 9 | // - LRC high is channel 2 (right). 10 | // - LRC signal transitions once each word. 11 | // - DATA is valid on the CLOCK rising edge. 12 | // - Data bits are MSB first. 13 | // - DATA bits are left-aligned with respect to LRC edge. 14 | // - DATA bits are right-shifted by one with respect to LRC edges. 15 | I2S::I2S(MicType micType) { 16 | if (micType == M5GO || micType == M5STACKFIRE ) { 17 | BITS_PER_SAMPLE = I2S_BITS_PER_SAMPLE_16BIT; 18 | i2s_config_t i2s_config = { 19 | .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN), 20 | .sample_rate = SAMPLE_RATE, 21 | .bits_per_sample = BITS_PER_SAMPLE, 22 | .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, 23 | .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S_MSB), 24 | .intr_alloc_flags = 0, 25 | .dma_buf_count = 2, 26 | .dma_buf_len = 1024 27 | }; 28 | i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); 29 | i2s_set_adc_mode(ADC_UNIT_1, ADC1_CHANNEL_6); 30 | i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, BITS_PER_SAMPLE, I2S_CHANNEL_STEREO); 31 | i2s_adc_enable(I2S_NUM_0); 32 | } 33 | else if (micType == ADMP441 || micType == ICS43434 ) { 34 | BITS_PER_SAMPLE = I2S_BITS_PER_SAMPLE_32BIT; 35 | i2s_config_t i2s_config = { 36 | .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX), 37 | .sample_rate = SAMPLE_RATE, 38 | .bits_per_sample = BITS_PER_SAMPLE, 39 | .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, 40 | .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB), 41 | .intr_alloc_flags = 0, 42 | .dma_buf_count = 16, 43 | .dma_buf_len = 60 44 | }; 45 | i2s_pin_config_t pin_config; 46 | pin_config.bck_io_num = PIN_I2S_BCLK; 47 | pin_config.ws_io_num = PIN_I2S_LRC; 48 | pin_config.data_out_num = I2S_PIN_NO_CHANGE; 49 | pin_config.data_in_num = PIN_I2S_DIN; 50 | i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); 51 | i2s_set_pin(I2S_NUM_0, &pin_config); 52 | i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, BITS_PER_SAMPLE, I2S_CHANNEL_STEREO); 53 | } 54 | } 55 | 56 | int I2S::Read(char* data, int numData) { 57 | return i2s_read_bytes(I2S_NUM_0, (char *)data, numData, portMAX_DELAY); 58 | } 59 | 60 | int I2S::GetBitPerSample() { 61 | return (int)BITS_PER_SAMPLE; 62 | } 63 | -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/Audio.cpp: -------------------------------------------------------------------------------- 1 | #include "Audio.h" 2 | 3 | Audio::Audio(MicType micType) { 4 | wavData = new char*[wavDataSize/dividedWavDataSize]; 5 | for (int i = 0; i < wavDataSize/dividedWavDataSize; ++i) wavData[i] = new char[dividedWavDataSize]; 6 | i2s = new I2S(micType); 7 | } 8 | 9 | Audio::~Audio() { 10 | for (int i = 0; i < wavDataSize/dividedWavDataSize; ++i) delete[] wavData[i]; 11 | delete[] wavData; 12 | delete i2s; 13 | } 14 | 15 | void Audio::CreateWavHeader(byte* header, int waveDataSize){ 16 | header[0] = 'R'; 17 | header[1] = 'I'; 18 | header[2] = 'F'; 19 | header[3] = 'F'; 20 | unsigned int fileSizeMinus8 = waveDataSize + 44 - 8; 21 | header[4] = (byte)(fileSizeMinus8 & 0xFF); 22 | header[5] = (byte)((fileSizeMinus8 >> 8) & 0xFF); 23 | header[6] = (byte)((fileSizeMinus8 >> 16) & 0xFF); 24 | header[7] = (byte)((fileSizeMinus8 >> 24) & 0xFF); 25 | header[8] = 'W'; 26 | header[9] = 'A'; 27 | header[10] = 'V'; 28 | header[11] = 'E'; 29 | header[12] = 'f'; 30 | header[13] = 'm'; 31 | header[14] = 't'; 32 | header[15] = ' '; 33 | header[16] = 0x10; // linear PCM 34 | header[17] = 0x00; 35 | header[18] = 0x00; 36 | header[19] = 0x00; 37 | header[20] = 0x01; // linear PCM 38 | header[21] = 0x00; 39 | header[22] = 0x01; // monoral 40 | header[23] = 0x00; 41 | header[24] = 0x80; // sampling rate 16000 42 | header[25] = 0x3E; 43 | header[26] = 0x00; 44 | header[27] = 0x00; 45 | header[28] = 0x00; // Byte/sec = 16000x2x1 = 32000 46 | header[29] = 0x7D; 47 | header[30] = 0x00; 48 | header[31] = 0x00; 49 | header[32] = 0x02; // 16bit monoral 50 | header[33] = 0x00; 51 | header[34] = 0x10; // 16bit 52 | header[35] = 0x00; 53 | header[36] = 'd'; 54 | header[37] = 'a'; 55 | header[38] = 't'; 56 | header[39] = 'a'; 57 | header[40] = (byte)(waveDataSize & 0xFF); 58 | header[41] = (byte)((waveDataSize >> 8) & 0xFF); 59 | header[42] = (byte)((waveDataSize >> 16) & 0xFF); 60 | header[43] = (byte)((waveDataSize >> 24) & 0xFF); 61 | } 62 | 63 | void Audio::Record() { 64 | CreateWavHeader(paddedHeader, wavDataSize); 65 | int bitBitPerSample = i2s->GetBitPerSample(); 66 | if (bitBitPerSample == 16) { 67 | for (int j = 0; j < wavDataSize/dividedWavDataSize; ++j) { 68 | i2s->Read(i2sBuffer, i2sBufferSize/2); 69 | for (int i = 0; i < i2sBufferSize/8; ++i) { 70 | wavData[j][2*i] = i2sBuffer[4*i + 2]; 71 | wavData[j][2*i + 1] = i2sBuffer[4*i + 3]; 72 | } 73 | } 74 | } 75 | else if (bitBitPerSample == 32) { 76 | for (int j = 0; j < wavDataSize/dividedWavDataSize; ++j) { 77 | i2s->Read(i2sBuffer, i2sBufferSize); 78 | for (int i = 0; i < i2sBufferSize/8; ++i) { 79 | wavData[j][2*i] = i2sBuffer[8*i + 2]; 80 | wavData[j][2*i + 1] = i2sBuffer[8*i + 3]; 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/network_param.h: -------------------------------------------------------------------------------- 1 | #ifndef _NETWORK_PARAM_H 2 | #define _NETWORK_PARAM_H 3 | 4 | const char* server = "speech.googleapis.com"; 5 | 6 | // To get the certificate for your region run: 7 | // openssl s_client -showcerts -connect speech.googleapis.com:443 8 | // Copy the certificate (all lines between and including ---BEGIN CERTIFICATE--- 9 | // and --END CERTIFICATE--) to root.cert and put here on the root_cert variable. 10 | const char* root_ca= 11 | "-----BEGIN CERTIFICATE-----\n" 12 | "MIIFljCCA36gAwIBAgINAgO8U1lrNMcY9QFQZjANBgkqhkiG9w0BAQsFADBHMQsw\n" 13 | "CQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEU\n" 14 | "MBIGA1UEAxMLR1RTIFJvb3QgUjEwHhcNMjAwODEzMDAwMDQyWhcNMjcwOTMwMDAw\n" 15 | "MDQyWjBGMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZp\n" 16 | "Y2VzIExMQzETMBEGA1UEAxMKR1RTIENBIDFDMzCCASIwDQYJKoZIhvcNAQEBBQAD\n" 17 | "ggEPADCCAQoCggEBAPWI3+dijB43+DdCkH9sh9D7ZYIl/ejLa6T/belaI+KZ9hzp\n" 18 | "kgOZE3wJCor6QtZeViSqejOEH9Hpabu5dOxXTGZok3c3VVP+ORBNtzS7XyV3NzsX\n" 19 | "lOo85Z3VvMO0Q+sup0fvsEQRY9i0QYXdQTBIkxu/t/bgRQIh4JZCF8/ZK2VWNAcm\n" 20 | "BA2o/X3KLu/qSHw3TT8An4Pf73WELnlXXPxXbhqW//yMmqaZviXZf5YsBvcRKgKA\n" 21 | "gOtjGDxQSYflispfGStZloEAoPtR28p3CwvJlk/vcEnHXG0g/Zm0tOLKLnf9LdwL\n" 22 | "tmsTDIwZKxeWmLnwi/agJ7u2441Rj72ux5uxiZ0CAwEAAaOCAYAwggF8MA4GA1Ud\n" 23 | "DwEB/wQEAwIBhjAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwEgYDVR0T\n" 24 | "AQH/BAgwBgEB/wIBADAdBgNVHQ4EFgQUinR/r4XN7pXNPZzQ4kYU83E1HScwHwYD\n" 25 | "VR0jBBgwFoAU5K8rJnEaK0gnhS9SZizv8IkTcT4waAYIKwYBBQUHAQEEXDBaMCYG\n" 26 | "CCsGAQUFBzABhhpodHRwOi8vb2NzcC5wa2kuZ29vZy9ndHNyMTAwBggrBgEFBQcw\n" 27 | "AoYkaHR0cDovL3BraS5nb29nL3JlcG8vY2VydHMvZ3RzcjEuZGVyMDQGA1UdHwQt\n" 28 | "MCswKaAnoCWGI2h0dHA6Ly9jcmwucGtpLmdvb2cvZ3RzcjEvZ3RzcjEuY3JsMFcG\n" 29 | "A1UdIARQME4wOAYKKwYBBAHWeQIFAzAqMCgGCCsGAQUFBwIBFhxodHRwczovL3Br\n" 30 | "aS5nb29nL3JlcG9zaXRvcnkvMAgGBmeBDAECATAIBgZngQwBAgIwDQYJKoZIhvcN\n" 31 | "AQELBQADggIBAIl9rCBcDDy+mqhXlRu0rvqrpXJxtDaV/d9AEQNMwkYUuxQkq/BQ\n" 32 | "cSLbrcRuf8/xam/IgxvYzolfh2yHuKkMo5uhYpSTld9brmYZCwKWnvy15xBpPnrL\n" 33 | "RklfRuFBsdeYTWU0AIAaP0+fbH9JAIFTQaSSIYKCGvGjRFsqUBITTcFTNvNCCK9U\n" 34 | "+o53UxtkOCcXCb1YyRt8OS1b887U7ZfbFAO/CVMkH8IMBHmYJvJh8VNS/UKMG2Yr\n" 35 | "PxWhu//2m+OBmgEGcYk1KCTd4b3rGS3hSMs9WYNRtHTGnXzGsYZbr8w0xNPM1IER\n" 36 | "lQCh9BIiAfq0g3GvjLeMcySsN1PCAJA/Ef5c7TaUEDu9Ka7ixzpiO2xj2YC/WXGs\n" 37 | "Yye5TBeg2vZzFb8q3o/zpWwygTMD0IZRcZk0upONXbVRWPeyk+gB9lm+cZv9TSjO\n" 38 | "z23HFtz30dZGm6fKa+l3D/2gthsjgx0QGtkJAITgRNOidSOzNIb2ILCkXhAd4FJG\n" 39 | "AJ2xDx8hcFH1mt0G/FX0Kw4zd8NLQsLxdxP8c4CU6x+7Nz/OAipmsHMdMqUybDKw\n" 40 | "juDEI/9bfU1lcKwrmz3O2+BtjjKAvpafkmO8l7tdufThcV4q5O8DIrGKZTqPwJNl\n" 41 | "1IXNDw9bg1kWRxYtnCQ6yICmJhSFm/Y3m6xv+cXDBlHz4n/FsRC6UfTd\n" 42 | "-----END CERTIFICATE-----\n"; 43 | 44 | // Getting Access Token : 45 | // At first, you should get service account key (JSON file). 46 | // Type below command in Google Cloud Shell to get AccessToken: 47 | // $ gcloud auth activate-service-account --key-file=KEY_FILE (KEY_FILE is your service account key file) 48 | // $ gcloud auth print-access-token 49 | // The Access Token is expired in an hour. 50 | // Google recommends to use Access Token. 51 | //const String AccessToken = ""; 52 | 53 | // It is also possible to use "API Key" instead of "Access Token". It doesn't have time limit. 54 | //const String ApiKey = "Your_API_Key"; 55 | 56 | // see https://cloud.google.com/docs/authentication?hl=ja#getting_credentials_for_server-centric_flow 57 | // see https://qiita.com/basi/items/3623a576b754f738138e (Japanese) 58 | 59 | #endif // _NETWORK_PARAM_H 60 | -------------------------------------------------------------------------------- /ESP32_Speech_to_Text/CloudSpeechClient.cpp: -------------------------------------------------------------------------------- 1 | #include "CloudSpeechClient.h" 2 | #include "network_param.h" 3 | #include "Credentials.h" 4 | 5 | #include 6 | #include 7 | #define USE_SERIAL Serial 8 | #include 9 | #include 10 | //#define uart_en 15 11 | #define led_3 4 12 | #define led_1 15 13 | #define led_2 2 14 | //#include 15 | ////SoftwareSerial (D4, D2); 16 | //const char* chatgpt_token = "Your_ChatGPT_Token"; 17 | CloudSpeechClient::CloudSpeechClient(Authentication authentication) { 18 | this->authentication = authentication; 19 | WiFi.begin(ssid, password); 20 | // while (WiFi.status() == WL_CONNECTED){ digitalWrite(led_3,1);} 21 | while (WiFi.status() != WL_CONNECTED) delay(1000); 22 | client.setCACert(root_ca); 23 | 24 | 25 | if (!client.connect(server, 443)) Serial.println("Connection failed!"); digitalWrite(led_3, 1); digitalWrite(led_1, 0); digitalWrite(led_2, 0); 26 | } 27 | 28 | String ans; 29 | 30 | CloudSpeechClient::~CloudSpeechClient() { 31 | client.stop(); 32 | WiFi.disconnect(); 33 | } 34 | 35 | void CloudSpeechClient::PrintHttpBody2(Audio* audio) 36 | { 37 | String enc = base64::encode(audio->paddedHeader, sizeof(audio->paddedHeader)); 38 | enc.replace("\n", ""); // delete last "\n" 39 | client.print(enc); // HttpBody2 40 | char** wavData = audio->wavData; 41 | for (int j = 0; j < audio->wavDataSize / audio->dividedWavDataSize; ++j) { 42 | enc = base64::encode((byte*)wavData[j], audio->dividedWavDataSize); 43 | enc.replace("\n", "");// delete last "\n" 44 | client.print(enc); // HttpBody2 45 | } 46 | } 47 | 48 | void CloudSpeechClient::Transcribe(Audio* audio) { 49 | String HttpBody1 = "{\"config\":{\"encoding\":\"LINEAR16\",\"sampleRateHertz\":16000,\"languageCode\":\"" + SpeechtoText_Language + "\"},\"audio\":{\"content\":\""; 50 | String HttpBody3 = "\"}}\r\n\r\n"; 51 | int httpBody2Length = (audio->wavDataSize + sizeof(audio->paddedHeader)) * 4 / 3; // 4/3 is from base64 encoding 52 | String ContentLength = String(HttpBody1.length() + httpBody2Length + HttpBody3.length()); 53 | String HttpHeader; 54 | // if (authentication == USE_APIKEY) 55 | HttpHeader = String("POST /v1/speech:recognize?key=") + ApiKey 56 | + String(" HTTP/1.1\r\nHost: speech.googleapis.com\r\nContent-Type: application/json\r\nContent-Length: ") + ContentLength + String("\r\n\r\n"); 57 | // else if (authentication == USE_ACCESSTOKEN) 58 | // HttpHeader = String("POST /v1beta1/speech:syncrecognize HTTP/1.1\r\nHost: speech.googleapis.com\r\nContent-Type: application/json\r\nAuthorization: Bearer ") 59 | // + AccessToken + String("\r\nContent-Length: ") + ContentLength + String("\r\n\r\n"); 60 | client.print(HttpHeader); 61 | client.print(HttpBody1); 62 | PrintHttpBody2(audio); 63 | client.print(HttpBody3); 64 | String My_Answer = ""; 65 | while (!client.available()); 66 | 67 | while (client.available()) 68 | { 69 | char temp = client.read(); 70 | My_Answer = My_Answer + temp; 71 | // Serial.write(client.read()); 72 | } 73 | client.stop(); 74 | // Serial.print("My Answer - ");Serial.println(My_Answer); 75 | int postion = My_Answer.indexOf('{'); 76 | // Serial.println(postion); 77 | ans = My_Answer.substring(postion); 78 | Serial.print("Json daata--"); 79 | //Serial.print(ans); 80 | 81 | DynamicJsonDocument doc(384); 82 | 83 | //StaticJsonDocument<384> doc; 84 | 85 | DeserializationError error = deserializeJson(doc, ans); 86 | 87 | if (error) { 88 | Serial.print("deserializeJson() failed: "); 89 | Serial.println(error.c_str()); 90 | return; 91 | } 92 | 93 | JsonObject results_0 = doc["results"][0]; 94 | //const char* 95 | const char* chatgpt_Q = results_0["alternatives"][0]["transcript"]; 96 | const char* a = "light on"; 97 | const char* b = "light off"; 98 | //String chatgpt_Q = a+ans+b; 99 | //Serial.println(ans); 100 | Serial.print(chatgpt_Q); Serial.println("-"); 101 | /////////////////////////////////////////////////////////// 102 | 103 | { 104 | Serial.println("Asking Chat GPT"); 105 | HTTPClient https; 106 | 107 | Serial.print("[HTTPS] begin...\n"); 108 | if (https.begin("https://api.openai.com/v1/completions")) { // HTTPS 109 | 110 | https.addHeader("Content-Type", "application/json"); 111 | String token_key = String("Bearer ") + chatgpt_token; 112 | https.addHeader("Authorization", token_key); 113 | 114 | String payload = String("{\"model\": \"" + OpenAI_Model + "\", \"prompt\": ") + "\"" + chatgpt_Q + "\"" + String(", \"temperature\": " + OpenAI_Temperature + ", \"max_tokens\": " + OpenAI_Max_Tokens + "}"); //Instead of TEXT as Payload, can be JSON as Paylaod 115 | 116 | Serial.print("[HTTPS] GET...\n"); 117 | 118 | // start connection and send HTTP header 119 | int httpCode = https.POST(payload); 120 | 121 | // httpCode will be negative on error 122 | // file found at server 123 | if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { 124 | String payload = https.getString(); 125 | Serial.println(payload); 126 | // Serial2.println(payload); 127 | ////////////////////////////////////////////////// 128 | StaticJsonDocument<2000> doc2; 129 | 130 | DeserializationError error = deserializeJson(doc2, payload); 131 | 132 | if (error) { 133 | Serial.print("deserializeJson() failed: "); 134 | Serial.println(error.c_str()); 135 | return; 136 | 137 | } 138 | JsonObject choices_0 = doc2["choices"][0]; 139 | const char* only_ans = choices_0["text"]; 140 | Serial.println("Only ans:-"); Serial.print(only_ans); 141 | Serial2.print(only_ans); 142 | digitalWrite(led_1, 1); 143 | digitalWrite(led_2, 1); 144 | //digitalWrite(uart_en, LOW); 145 | delay(1); 146 | 147 | //digitalWrite(uart_en,HIGH); 148 | ///////////////////////////////////////////////////////// 149 | } 150 | else { 151 | Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); 152 | } 153 | https.end(); 154 | } 155 | else { 156 | Serial.printf("[HTTPS] Unable to connect\n"); 157 | } 158 | 159 | Serial.print("To ask again"); 160 | //delay(10000); 161 | 162 | } 163 | 164 | 165 | 166 | 167 | /////////////////////////////////////////////////////////// 168 | /* 169 | 170 | 171 | */ 172 | } 173 | --------------------------------------------------------------------------------