08.
21 2:24 PM
import json
import queue
import sounddevice as sd
import vosk
import serial
import threading
import pystray
from PIL import Image, ImageDraw
import pyttsx3
# ------------------- Arduino Setup -------------------
try:
arduino = [Link]('COM14', 9600, timeout=1)
print("Connected to Arduino on COM14")
except:
arduino = None
print("Arduino not connected!")
# ------------------- Speech Model -------------------
print("Loading Vosk model...")
model = [Link]("model")
recognizer = [Link](model, 16000)
print("Model loaded!")
# ------------------- Queue for audio -------------------
q = [Link]()
# ------------------- TTS -------------------
engine = [Link]()
[Link]('rate', 150)
def speak(text):
[Link](target=lambda: [Link](text) or
[Link]()).start()
# ------------------- Command Processing -------------------
def process_command(raw_text):
print(f"[DEBUG] Raw text in process_command: {raw_text}")
text = raw_text.lower()
# Light
if any(phrase in text for phrase in ["light on", "turn on light", "switch on
light"]):
if arduino:
[Link](b'L1')
speak("Turning on the light")
elif any(phrase in text for phrase in ["light off", "turn off light", "switch
off light"]):
if arduino:
[Link](b'L0')
speak("Turning off the light")
# Fan
elif any(phrase in text for phrase in ["fan on", "turn on fan", "switch on
fan"]):
if arduino:
[Link](b'F1')
speak("Turning on the fan")
elif any(phrase in text for phrase in ["fan off", "turn off fan", "switch off
fan"]):
if arduino:
[Link](b'F0')
speak("Turning off the fan")
# TV
elif any(phrase in text for phrase in ["tv on", "turn on tv", "switch on tv"]):
if arduino:
[Link](b'T1')
speak("Turning on the TV")
elif any(phrase in text for phrase in ["tv off", "turn off tv", "switch off
tv"]):
if arduino:
[Link](b'T0')
speak("Turning off the TV")
# Charger
elif any(phrase in text for phrase in ["charger on", "turn on charger", "switch
on charger"]):
if arduino:
[Link](b'C1')
speak("Turning on the charger")
elif any(phrase in text for phrase in ["charger off", "turn off charger",
"switch off charger"]):
if arduino:
[Link](b'C0')
speak("Turning off the charger")
else:
print(f"[INFO] Command not recognized: {text}")
# ------------------- Audio Callback -------------------
def callback(indata, frames, time, status):
if status:
print(status)
[Link](bytes(indata))
# ------------------- Listening Thread -------------------
def listen():
with [Link](samplerate=16000, blocksize=8000, dtype='int16',
channels=1, callback=callback):
print("Jarvis: Listening...")
while True:
data = [Link]()
if [Link](data):
result = [Link]([Link]())
text = [Link]("text", "")
if text:
print(f"You said: {text}")
process_command(text)
# ------------------- Tray Icon -------------------
def create_image():
# Create a simple red circle icon
image = [Link]('RGB', (64, 64), (255, 255, 255))
draw = [Link](image)
[Link]((16, 16, 48, 48), fill=(255, 0, 0))
return image
icon = [Link]("Jarvis", create_image(), "Jarvis Voice Control")
def run_tray():
[Link]()
# ------------------- Main -------------------
if __name__ == "__main__":
[Link](target=listen, daemon=True).start()
run_tray()