AI Smart Home is an open source smart home system developed in Python, featuring an AI-powered voice assistant (powered by GPT4o) and a modular architecture. The system integrates various functionalities such as music playback, diary entry creation and playback, sensor readings (temperature, humidity, battery status), alarm clock management, light control (via Tinytuya), and a web console for remote monitoring and control.
This project was developed as part of a Bachelor’s degree in Computer Science and is primarily designed to run on a Raspberry Pi 4B, though it can be adapted to work with other hardware setups.
- Features
- Technologies
- Installation
- Configuration
- Usage
- File Structure
- Troubleshooting
- Contributing
- License
- Future Improvements
- About
-
Voice Assistant:
- Activated with a wake word (default is "assistant").
- Processes voice commands and integrates with OpenAI GPT4o for intelligent responses.
-
Music Player:
- Streams and plays music from YouTube using
yt-dlp
andmpv
. - Example command: "Play music [Song Name]".
- Streams and plays music from YouTube using
-
Diary Module:
- Records diary entries via audio.
- Converts recordings to text and saves both audio and text files with the current date.
- Supports playback of diary entries.
-
Sensor Module (Hygrometer):
- Retrieves temperature, humidity, and battery status from a Bluetooth-enabled hygrometers from Govee.
- Uses asynchronous BLE scanning with the
bleak
library.
-
Alarm Clock:
- Schedules and manages alarms using the
schedule
module. - Triggers alarms with voice notifications and custom alarm sounds.
- Commands for setting and deleting alarms.
- Schedules and manages alarms using the
-
Light Control:
- Controls Tuya-compatible smart lights via the Tuya Cloud API (using
tinytuya
). - Supports adjusting color, brightness, and operating mode.
- Controls Tuya-compatible smart lights via the Tuya Cloud API (using
-
Web Console:
- Provides a web interface for remote system monitoring.
- Programming Language: Python 3.11
- Libraries:
speech_recognition
for voice command detection.pygame
for audio playback and TTS.gTTS
for text-to-speech conversion.yt-dlp
andmpv
for streaming and playing YouTube audio.bleak
for Bluetooth Low Energy (BLE) scanning.tinytuya
for controlling Tuya-compatible devices.OpenAI
for GPT4o integration.
- Target Hardware:
- Raspberry Pi 4B (primarily)
- Razer Seiren Mini Microphone
- Soundcore Boom 2 Bluetooth Speaker
- Govee H5075
- A Raspberry Pi 4B running Raspberry Pi OS (or compatible hardware).
- Python 3.11 installed.
- Miniforge3 (or an alternative Python environment manager).
- Internet access for API queries and music streaming.
-
Clone the Repository:
git clone https://github.com/Luke-Werle-99/AI_Smart_Home.git cd AI_Smart_Home
-
Create and Activate a Python Environment:
conda create -n AI_Smart_Home python=3.11 conda activate AI_Smart_Home
-
Install Dependencies:
cd installation sh install_dependencies.sh sh install_libraries.sh
-
Configure API Keys:
- Create or edit the
.env
file in the project root:OPENAI_API_KEY=your_openai_api_key_here
- Also ensure that any other required configurations (e.g., Tuya credentials) in the corresponding modules are correctly set.
- Create or edit the
- Environment Variables:
The.env
file contains sensitive data such as the OpenAI API key. Make sure this file is configured correctly. - Audio Settings:
- Ensure the correct audio devices (microphone and speakers) are selected.
- On the Raspberry Pi, additional configuration (e.g., PulseAudio settings) might be necessary.
- Bluetooth & Sensors:
- Enable Bluetooth and ensure your Govee hygrometer is properly paired.
- Tuya Cloud:
- Update your Tuya Cloud credentials (DEVICE_ID, ACCESS_ID, ACCESS_SECRET, API_REGION) in the relevant modules.
-
Start the Assistant:
python main.py
The voice assistant will start and listen for the wake word (default "assistant").
-
Available Voice Commands:
- Wake the Assistant: "assistant" (optionally followed by a command).
- Play Music: "Play music [Song Name]".
- Diary Functions: "create diary" to start a new diary entry, "read diary" to playback an entry.
- Sensor Readings: "temperature", "humidity", or "battery" to fetch current sensor values.
- Alarm Clock: "set alarm" to create an alarm and "delete alarm" to remove an alarm.
- Light Control: "set light" followed by commands such as "on", "off", color names (e.g., "red", "blue"), or brightness percentages.
- General Queries: All other queries are forwarded to OpenAI GPT4o.
-
Systemd Service (Optional):
To have the assistant start automatically on system boot, set up a systemd service as described below.
- Say "Assistant" to wake up the system.
- Use commands like:
- "Play music Believer by Imagine Dragons."
You also wake the system and input your command in one sentence like: "Assistant Play music Believer by Imagine Dragons"
Alternatively, you can configure it to start automatically using a systemd service file (see below).
To run the assistant on system startup:
- Create a systemd service file:
sudo nano /etc/systemd/system/autostart.service
- Add the following content:
[Unit] Description=Start AI Smart Home Assistant After=sound.target bluetooth.target network.target [Service] User=your-username WorkingDirectory=/home/your-username/ai-smart-home ExecStart=/home/your-username/miniforge3/condabin/conda run -n AI_Smart_Home --no-capture-output python main.py Environment="XDG_RUNTIME_DIR=/run/user/1000" Environment="DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus" Restart=on-failure [Install] WantedBy=multi-user.target
- Enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable autostart.service sudo systemctl start autostart.service
- Additinally you can set up automatic bluetooth pairing in case you are using a bluetooth speaker like this:
echo "Connecting to Bluetooth speaker..." for i in {1..5}; do echo "Attempt $i: Connecting to F4:2B:7D:29:50:41..." bluetoothctl -- connect F4:2B:7D:29:50:41 && break sleep 5 done
AI_Smart_Home/
├── main.py
├── Modules/
│ ├── init.py
│ ├── music_player.py
│ ├── diary.py
│ ├── hygrometer.py
│ ├── alarm_clock.py
│ ├── light_control.py
│ └── web_console.py
├── diary_entries/
│ ├── YYYY-MM-DD.txt
│ ├── YYYY-MM-DD.wav
├── installation/
│ ├── install_dependencies.sh
│ └── install_libraries.sh
├── .env
└── README.md
-
Audio Playback:
- Ensure
pygame
is properly initialized. - If you encounter issues in subprocesses, consider using threads instead of processes to share the Pygame mixer context.
- Ensure
-
Microphone Issues:
- Verify the device index using
arecord -l
(on Linux) and adjust the settings in thelisten()
function if needed. - Adjust parameters like
energy_threshold
ordynamic_energy_threshold
in thespeech_recognition
library.
- Verify the device index using
-
Bluetooth and Sensor Readings:
- Ensure Bluetooth is enabled and your hygrometer is paired correctly.
- Watch for potential conflicts between asynchronous BLE scans (using
bleak
) and other async operations.
-
Bluetooth Speaker Issues:
- Ensure the speaker is paired and set as the default audio sink using:
pactl set-default-sink [sink-name]
- Ensure the speaker is paired and set as the default audio sink using:
-
OpenAI API:
- Confirm that your API key is correct and that you have an active network connection.
- Check logs for any error messages related to API requests.
-
General Debugging:
- Monitor console logs to identify error messages.
- Review the specific module in the
Modules/
directory for module-related issues.
Contributions to AI Smart Home are welcome!
To contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes with clear, descriptive messages.
- Open a Pull Request describing your changes and the benefits they provide.
For any questions or suggestions, please open an issue on the GitHub repository.
This project is licensed under the MIT License.
AI Smart Home was developed by Luke Werle as a Bachelor’s degree project in Computer Science. The aim is to provide a privacy-friendly and modular alternative to commercial smart home systems. Feedback and suggestions for improvements are always welcome!
Enjoy your AI Smart Home Assistant!