Menu

Project Documentation: Integrating Voice Assistant with IoT Microcontroller

This documentation explains how the voice assistant can be used to control IoT devices by integrating microcontroller functions with voice commands. The system works by parsing the voice commands through a server and executing the appropriate function on your IoT device based on the server's response.

Overview of the System

The code provided will be used to extract the functions implemented in your microcontroller and allow you to control the IoT devices using voice commands. The voice assistant interacts with the server, which processes the spoken command and identifies the appropriate action by matching it with the predefined functions in your code.

Voice Integration with Your Microcontroller

To enable voice control, you need to integrate the URL provided by the server with your microcontroller project. The URL is specific to your project and is shared with you during the setup. The URL is where you will send a GET request to retrieve the current command that has been set in the database by the voice assistant.

Your microcontroller's firmware should periodically poll the server for the current command and execute the corresponding function. If the current command returned from the server matches a function in your microcontroller code, that function should be executed.

Handling Non-Executable Functions

Certain functions in your microcontroller code (such as loop(), setup(), or start()) may not be directly related to the bot's actionable commands. These are part of initialization or continuous logic and should not be executed as part of a voice command. You can skip executing these functions when they are returned by the server.

Server Response Time and Future Enhancements

The system's performance in executing commands depends heavily on the server's response time. After you speak a command to the voice assistant, the server processes it and stores the result in the database. Your microcontroller polls the server to check for new commands.

As of now, this process is based on HTTP requests, which may introduce a slight delay between issuing the command and seeing the action on your IoT device. In future iterations, the system will adopt a faster communication method such as UDP or WebSockets for real-time, low-latency control of the bot.

Example of Code Integration

Original Microcontroller Code

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (buttonPressed()) {
    turnOnLed();
  }
}

void turnOnLed() {
  digitalWrite(LED_BUILTIN, HIGH);
}

void turnOffLed() {
  digitalWrite(LED_BUILTIN, LOW);
}

Voicified Code Example

Below is the same code modified to respond to voice commands. The microcontroller now polls the server at the provided URL to check for a new command. If the command matches the name of a function, that function is executed:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  // Initialize WiFi connection
}

void loop() {
  String command = getCommandFromServer(); // Poll the server for the current command
  if (command == "turnOnLed") {
    turnOnLed();
  } else if (command == "turnOffLed") {
    turnOffLed();
  }
}

void turnOnLed() {
  digitalWrite(LED_BUILTIN, HIGH);
}

void turnOffLed() {
  digitalWrite(LED_BUILTIN, LOW);
}

String getCommandFromServer() {
  // Make a GET request to the server URL
  // Parse the response and return the command as a string
}

How to Use the Voicified Code

To integrate voice control into your existing IoT project:

  • Send a GET request to the provided URL at regular intervals to check for new commands.
  • Compare the command from the server to the function names in your code.
  • Execute the corresponding function when the command matches.
  • Ensure non-executable functions like loop() and setup() are not treated as bot commands.

Future Plans: Real-Time Communication

Currently, the communication between the voice assistant and the bot is done via HTTP GET requests, which may introduce a slight delay depending on network conditions. In the future, this will be improved by introducing UDP or WebSocket-based communication for faster, real-time command execution with minimal latency.

We hope this guide helps you integrate voice control seamlessly with your microcontroller project. Feel free to customize the system to suit your project's needs.