No title

 #include <WiFi.h>

#include <BluetoothSerial.h>

#include "Audio.h"


// -------- Bluetooth ----------

BluetoothSerial SerialBT;


// -------- Audio --------------

Audio audio;


// -------- WiFi Credentials ---

const char* ssid     = "YOUR_WIFI_NAME";

const char* password = "YOUR_WIFI_PASSWORD";


// -------- I2S Pins (MAX98357) -

#define I2S_BCLK 26

#define I2S_LRC  25

#define I2S_DOUT 22


// -------- URL Encode Function -

String urlEncode(String str) {

  String encoded = "";

  char c;

  char bufHex[4];


  for (int i = 0; i < str.length(); i++) {

    c = str.charAt(i);

    if (isalnum(c)) {

      encoded += c;

    } else if (c == ' ') {

      encoded += "%20";

    } else {

      sprintf(bufHex, "%%%02X", c);

      encoded += bufHex;

    }

  }

  return encoded;

}


void setup() {

  Serial.begin(115200);


  // Bluetooth start

  SerialBT.begin("ESP32_TTS");

  Serial.println("Bluetooth Started: ESP32_TTS");


  // WiFi connect

  WiFi.begin(ssid, password);

  Serial.print("Connecting WiFi");

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  Serial.println("\nWiFi Connected");


  // Audio setup

  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);

  audio.setVolume(15);   // 0–21


  Serial.println("ESP32 Online TTS Ready");

  Serial.println("Send text from Bluetooth Terminal");

}


void loop() {

  audio.loop();


  if (SerialBT.available()) {

    String text = SerialBT.readStringUntil('\n');

    text.trim();


    if (text.length() == 0) return;


    // Show on Serial Monitor

    Serial.println("Received Text:");

    Serial.println(text);


    // Encode text for URL

    String encodedText = urlEncode(text);


    // Google TTS URL (Female-like voice)

    String ttsURL =

      "https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=" +

      encodedText;


    // Play audio

    audio.connecttohost(ttsURL.c_str());

  }

}

Post a Comment

Previous Post Next Post

Contact Form