This circuit involves multiple components including LEDs, a toggle switch, a microphone, a voltage regulator, a battery, and ESP32 microcontrollers. The circuit is designed to control LEDs based on commands received via serial communication and to convert text to speech using Google Cloud's Text-to-Speech API.
INMP441
Toggle Switch
LED: Two Pin (green)
LED: Two Pin (yellow)
LED: Two Pin (red)
Battery 12V
7805
LED: Four Pin
ESP32 Wroom Dev Kit
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <I2S.h> // For audio output over I2S (adjust if using PWM or other methods)
// WiFi credentials
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
// Google Cloud API endpoint and API key
const String apiEndpoint = "https://texttospeech.googleapis.com/v1/text:synthesize";
const String apiKey = "YOUR_GOOGLE_CLOUD_API_KEY";
// Pin definitions for audio output
#define I2S_DOUT_PIN 25 // I2S Data out pin for audio
#define I2S_BCLK_PIN 26 // I2S Bit clock pin
#define I2S_LRC_PIN 27 // I2S Left/Right clock pin
// Setup WiFi
void setup() {
// Start serial communication for debugging
Serial.begin(115200);
Serial.println("Connecting to WiFi...");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi connected");
// Initialize I2S for audio output
I2S.begin(I2S_PHILIPS_MODE, 16000, 16); // 16KHz, 16-bit depth for audio output
// Example: Send a simple text to be converted to speech
String textToConvert = "Hello, I am your assistant. How can I help you today?";
convertTextToSpeech(textToConvert);
}
void loop() {
// Add code to handle interactions here (e.g., listening for commands)
// The voice output can be triggered based on your application logic
}
// Function to send text to Google Cloud Text-to-Speech API and get audio
void convertTextToSpeech(String text) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected");
return;
}
// Prepare HTTP request
HTTPClient http;
http.begin(apiEndpoint);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Bearer " + apiKey); // Use API Key for authorization
// Prepare the request payload (JSON)
String payload = "{"
"\"input\": {\"text\": \"" + text + "\"},"
"\"voice\": {\"languageCode\": \"en-US\", \"ssmlGender\": \"NEUTRAL\"},"
"\"audioConfig\": {\"audioEncoding\": \"MP3\"}"
"}";
// Send POST request
int httpResponseCode = http.POST(payload);
String response = http.getString();
http.end();
// Check for successful response
if (httpResponseCode == 200) {
Serial.println("Response from API received successfully.");
playAudio(response); // Play the audio
} else {
Serial.println("Error in API response: " + String(httpResponseCode));
}
}
// Function to parse the