Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32 Wi-Fi Controlled Smart Light System with Voice Assistant

View and Edit Project In Cirkit DesignerImage of ESP32 Wi-Fi Controlled Smart Light System with Voice Assistant

Circuit Documentation

Summary

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.

Component List

  1. INMP441

    • Description: MEMS microphone
    • Pins: GND, VDD, SD, L/R, WS, SCK
  2. Toggle Switch

    • Description: A simple toggle switch
    • Pins: Vcc, Sig, Gnd
  3. LED: Two Pin (green)

    • Description: Green LED
    • Pins: cathode, anode
  4. LED: Two Pin (yellow)

    • Description: Yellow LED
    • Pins: cathode, anode
  5. LED: Two Pin (red)

    • Description: Red LED
    • Pins: cathode, anode
  6. Battery 12V

    • Description: 12V battery
    • Pins: +, -
  7. 7805

    • Description: Voltage regulator
    • Pins: Vin, Gnd, Vout
  8. LED: Four Pin

    • Description: RGB LED
    • Pins: red cathode, common anode, green cathode, blue cathode
  9. ESP32 Wroom Dev Kit

    • Description: ESP32 microcontroller development board
    • Pins: 3V3, EN, VP, VN, GPIO 34, GPIO 35, GPIO 32, GPIO 33, GPIO 25, GPIO 26, GPIO 27, GPIO 14, GND, GPIO 13, SD2, SD3, CMD, V5, GPIO 23, GPIO 22, TXD, RXD, GPIO 21, GPIO 19, GPIO 18, GPIO 5, GPIO 17, GPIO 16, GPIO 4, GPIO 0, GPIO 2, GPIO 15, SD1, SD0, CLK

Wiring Details

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

Code Documentation

ESP32 Wroom Dev Kit (Instance 1)

#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