Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Home Automation Voice Assistant #530

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions home_automation_voice_assistant/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# 🔊 Home Automation Voice Assistant

> An AI-powered voice assistant for home automation, with features like context-aware commands, scheduling, real-time device state management, and flexible device configuration.

---

## 🌟 Features

- **Enhanced Natural Language Processing (NLP):** Understands commands intuitively, even with varying phrases.
- **Context-Aware Commands:** Controls multiple devices in a single command.
- **Scheduled Actions:** Automate your devices on a delay or schedule.
- **Real-Time Device State Feedback:** Keeps track of each device's state and responds accordingly.
- **Easy Device Expansion:** Add new devices and actions via a JSON configuration file.
- **Voice Feedback:** Confirms actions with text-to-speech.

---

## 📋 Requirements

- **Python**: >= 3.7
- **Libraries**: Install using the requirements file
- **MQTT Broker**: [HiveMQ](https://www.hivemq.com/) (or your choice of MQTT broker)
- **Microphone**: For voice input

### Installation

1. **Clone the repository**:
```bash
git clone https://github.com/dino65-dev/Home_automation-voice-assistant.git
cd home_automation-voice-assistant
2. **Install dependencies**:
```bash
pip install -r requirements.txt
3. **Edit the Device Configuration: Update ```devices.json``` to add or modify devices and actions.**
```json
{
"lights": ["on", "off", "dim", "brighten"],
"fan": ["on", "off", "speed up", "slow down"],
"tv": ["on", "off", "volume up", "volume down"],
"ac": ["on", "off", "temperature up", "temperature down"],
"curtains": ["open", "close"]
}
```
4. **Configure the MQTT Broker: Edit the ```MQTT_BROKER``` and ```MQTT_PORT``` values in the code to point to your MQTT broker.**
5. **Run the voice assistant:**
```bash
python home_automation-voice-assistant.py
---
### 🔧 Usage
This assistant listens for voice commands to control home automation devices. It recognizes various commands for different devices based on the configuration in ```devices.json```.

**Available Commands**
1. **Basic Commands:**
- "Turn on the lights."
- "Switch off the fan."
- "Set the AC to temperature up."

2. **Multi-Device Commands:**
- "Turn off the lights and fan."
- "Set TV to volume up and AC to temperature down."

3. **Scheduled Commands:**
- "Turn on the lights in 10 minutes."
- "Switch off the fan in 1 hour."
---
### 🎛️ Advanced Features
1. **Scheduling:**
- Commands like "Turn off the lights in 5 minutes" will execute with a delay.
2. **State Awareness:**
- Responds with messages like "The lights are already on" if you try to turn on a device that’s already active.
3. **Extensible Configuration:**
- Easily add new devices or actions without modifying the code. Just update ```devices.json```!
4. **Interactive Feedback:**
- The assistant provides voice feedback after each command, confirming the action.
---
### 🛠 Troubleshooting
1. **Issue with Voice Recognition?**
- Make sure your microphone is connected and set as the default recording device.
- Speak clearly and check for ambient noise that may interfere.
2. **Error Connecting to MQTT?**
- Double-check the ```MQTT_BROKER``` and ```MQTT_PORT```.
- Ensure your MQTT broker is running and accessible.
3. **New Device Not Recognized?**
- Ensure the device and its actions are correctly added to ```devices.json```.
- Restart the program after making changes to the configuration.
---
## 📖 Commands Overview

| Command Example | Description |
|------------------------------------------|------------------------------------------------|
| `Turn on the lights` | Activates the lights |
| `Switch off the fan` | Deactivates the fan |
| `Turn on the AC in 10 minutes` | Schedules the AC to turn on after 10 minutes |
| `Set TV to volume down and AC to off` | Controls multiple devices in one command |
| `Increase heater temperature` | Increases heater setting (custom actions) |

---
## 📦 Extending the System

You can expand the system by editing `devices.json`. Each device can have unique actions. Just add entries for each device and action in JSON format:

```json
{
"smart_light": ["on", "off", "dim", "brighten"],
"smart_blinds": ["open", "close", "adjust"]
}
```
---
## 🧩 Contributing

Feel free to contribute to this project! Open an issue or submit a pull request to suggest or implement new features.

---

## 🎉 Final Tips

1. **Clear Commands**: Try to give clear and distinct commands.
2. **Add Custom Responses**: Customize `respond()` function in the code for unique voice responses.
3. **Experiment with Scheduling**: Test different scheduled times to see how the assistant handles delays.
---
## ✨ Created by Dinmay Brahma
**Star ⭐ the repository if you found this project helpful!**

> _Automate your home with your voice and experience the power of an intelligent assistant!_


---

Feel free to add more information or edit it to match the specifics of your project. This README template is designed to be visually appealing, easy to navigate, and informative for users on GitHub. Let me know if you’d like further customization!


8 changes: 8 additions & 0 deletions home_automation_voice_assistant/devices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"lights": ["on", "off", "dim", "brighten"],
"fan": ["on", "off", "speed up", "slow down"],
"tv": ["on", "off", "volume up", "volume down"],
"ac": ["on", "off", "temperature up", "temperature down"],
"curtains": ["open", "close"],
"heater": ["on", "off", "increase", "decrease"]
}
168 changes: 168 additions & 0 deletions home_automation_voice_assistant/home_automation-voice-assistant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import paho.mqtt.client as mqtt
import speech_recognition as sr
import pyttsx3
import json
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import time
import random
import re
from datetime import datetime, timedelta
import threading

# Initialize Text-to-Speech Engine
tts_engine = pyttsx3.init()
tts_engine.setProperty('rate', 150)

# Load stopwords for NLP processing
try:
stop_words = set(stopwords.words("english"))
except:
import nltk
nltk.download("stopwords")
stop_words = set(stopwords.words("english"))
nltk.download('punkt')

# MQTT Configuration
MQTT_BROKER = "broker.hivemq.com"
MQTT_PORT = 1883
MQTT_TOPIC_PREFIX = "home/automation"

# Device Configuration (Loaded from JSON for easy expansion)
with open("devices.json", "r") as f:
DEVICES = json.load(f)

# Initialize MQTT Client
mqtt_client = mqtt.Client()
connected = False

# Device State Management
device_states = {device: "off" for device in DEVICES}

# MQTT Connection Functions
def on_connect(client, userdata, flags, rc):
global connected
if rc == 0:
print("Connected to MQTT Broker!")
connected = True
else:
print(f"Failed to connect, return code {rc}")
connected = False

def on_disconnect(client, userdata, rc):
global connected
print("Disconnected from MQTT Broker")
connected = False
reconnect()

def reconnect():
global connected
while not connected:
try:
mqtt_client.connect(MQTT_BROKER, MQTT_PORT, 60)
mqtt_client.loop_start()
connected = True
except:
print("Reconnection attempt failed. Trying again in 5 seconds...")
time.sleep(5)

mqtt_client.on_connect = on_connect
mqtt_client.on_disconnect = on_disconnect
mqtt_client.connect(MQTT_BROKER, MQTT_PORT, 60)
mqtt_client.loop_start()

# Advanced Command Parsing with Context
def process_command(command):
command = command.lower()
words = word_tokenize(command)
filtered_words = [word for word in words if word not in stop_words]

found_device = None
found_action = None
scheduled_time = None

# Recognize devices and actions
for device, actions in DEVICES.items():
if device in filtered_words:
found_device = device
for action in actions:
if action in command:
found_action = action
break
break

# Detect scheduled commands (e.g., "in 10 minutes")
time_match = re.search(r"in (\d+) (minute|minutes|hour|hours)", command)
if time_match:
time_value = int(time_match.group(1))
time_unit = time_match.group(2)
scheduled_time = datetime.now() + timedelta(minutes=time_value) if "minute" in time_unit else datetime.now() + timedelta(hours=time_value)

# Execute or schedule the command
if found_device and found_action:
if scheduled_time:
schedule_command(found_device, found_action, scheduled_time)
else:
execute_command(found_device, found_action)
else:
respond("Sorry, I didn't understand the command. Please try again.")

# Command Execution
def execute_command(device, action):
if device_states[device] == action:
respond(f"The {device} is already {action}.")
else:
device_states[device] = action
publish_mqtt(device, action)
respond(f"{device.capitalize()} is now set to {action}.")

# Schedule a Command for Later
def schedule_command(device, action, scheduled_time):
delay = (scheduled_time - datetime.now()).total_seconds()
threading.Timer(delay, execute_command, args=(device, action)).start()
respond(f"{device.capitalize()} will be set to {action} at {scheduled_time.strftime('%H:%M')}.")

# Publish MQTT Messages
def publish_mqtt(device, action):
topic = f"{MQTT_TOPIC_PREFIX}/{device}"
mqtt_client.publish(topic, action)
print(f"Sent '{action}' command to {device}.")

# Text-to-Speech Feedback
def respond(message):
print(message)
tts_engine.say(message)
tts_engine.runAndWait()

# Voice Recognition with retry mechanism
def listen_for_command():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening for command...")
recognizer.adjust_for_ambient_noise(source, duration=1)
audio = recognizer.listen(source)

try:
command = recognizer.recognize_google(audio).lower()
print(f"Recognized command: '{command}'")
process_command(command)
except sr.UnknownValueError:
respond("Sorry, I didn't catch that. Could you please repeat?")
except sr.RequestError:
respond("Could not request results; check your internet connection.")

# Main loop
if __name__ == "__main__":
print("Advanced Home Automation Controller is running...")
try:
while True:
if connected:
listen_for_command()
else:
reconnect()
time.sleep(1)
except KeyboardInterrupt:
print("Shutting down.")
finally:
mqtt_client.loop_stop()
mqtt_client.disconnect()
4 changes: 4 additions & 0 deletions home_automation_voice_assistant/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
paho-mqtt
SpeechRecognition
python3-pyaudio
pyttsx3