From 9b78b00bfac21d4a3b0ad6407509add044f3bcc5 Mon Sep 17 00:00:00 2001 From: vaski666 Date: Wed, 14 May 2025 14:36:00 +0200 Subject: [PATCH 1/4] Update and rename gpt_car.py to gpt_car2.py --- gpt_examples/{gpt_car.py => gpt_car2.py} | 55 ++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) rename gpt_examples/{gpt_car.py => gpt_car2.py} (82%) diff --git a/gpt_examples/gpt_car.py b/gpt_examples/gpt_car2.py similarity index 82% rename from gpt_examples/gpt_car.py rename to gpt_examples/gpt_car2.py index e9f78598..bdf8c1a9 100644 --- a/gpt_examples/gpt_car.py +++ b/gpt_examples/gpt_car2.py @@ -16,6 +16,8 @@ import os import sys +import json # Import the json module +import paho.mqtt.client as mqtt # Import the paho-mqtt library os.popen("pinctrl set 20 op dh") # enable robot_hat speake switch current_path = os.path.dirname(os.path.abspath(__file__)) @@ -38,7 +40,7 @@ # ================================================================= openai_helper = OpenAiHelper(OPENAI_API_KEY, OPENAI_ASSISTANT_ID, 'picarx') -LANGUAGE = [] +LANGUAGE = ['no'] # LANGUAGE = ['zh', 'en'] # config stt language code, https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes # VOLUME_DB = 5 @@ -212,7 +214,21 @@ def action_handler(): action_thread = threading.Thread(target=action_handler) action_thread.daemon = True - +# MQTT Functions +# ================================================================= +def on_connect(client, userdata, flags, rc): + """Callback function for when the MQTT client connects.""" + if rc == 0: + print("Connected to MQTT broker") + client.subscribe("picarx/stt") # Subscribe to the topic + else: + print(f"Failed to connect to MQTT broker, rc={rc}") + +def on_message(client, userdata, msg): + """Callback function for when a message is received on the subscribed topic.""" + stt_text = msg.payload + print(f"Received STT text from MQTT: {stt_text}") + # main # ================================================================= def main(): @@ -226,6 +242,38 @@ def main(): speak_thread.start() action_thread.start() + + # Setup MQTT Client + client = mqtt.Client() + client.on_connect = on_connect + client.on_message = on_message + + # MQTT Broker Address + mqtt_broker_address = "IP_ADRESS" # Replace with your broker's address + mqtt_username = "user" + mqtt_password = "pass" + try: + client.username_pw_set(mqtt_username, mqtt_password) + client.connect(mqtt_broker_address, 1883, 60) # Connect to the broker + print("Connecting to MQTT broker...") # Add log + except Exception as e: + print(f"\033[31mERROR: Could not connect to MQTT broker: {e}\033[m") + print("Please check your MQTT broker address and ensure it is running.") + sys.exit(1) + + # client.connect("localhost", 1883, 60) + + client.loop_start() # Start the MQTT loop in a non-blocking thread + + print("Waiting for STT data from Home Assistant via MQTT topic 'picarx/stt'") + try: + while True: + time.sleep(0.1) # Keep the main thread alive + except KeyboardInterrupt: + print("Stopping...") + my_car.reset() + client.loop_stop() # Stop the MQTT loop + client.disconnect() # Disconnect from the broker while True: if input_mode == 'voice': @@ -248,8 +296,7 @@ def main(): # stt # ---------------------------------------------------------------- st = time.time() - _result = openai_helper.stt(audio, language=LANGUAGE) - gray_print(f"stt takes: {time.time() - st:.3f} s") + _result = stt_text(f'\033[1;30m{"intput: "}\033[0m').encode(sys.stdin.encoding).decode('utf-8') if _result == False or _result == "": print() # new line From e742371adf41ca19c8a732a11c3c68565553ac9d Mon Sep 17 00:00:00 2001 From: vaski666 Date: Wed, 14 May 2025 15:05:39 +0200 Subject: [PATCH 2/4] Update gpt_car2.py --- gpt_examples/gpt_car2.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/gpt_examples/gpt_car2.py b/gpt_examples/gpt_car2.py index bdf8c1a9..8efc5f39 100644 --- a/gpt_examples/gpt_car2.py +++ b/gpt_examples/gpt_car2.py @@ -281,17 +281,11 @@ def main(): # listen # ---------------------------------------------------------------- - gray_print("listening ...") + print("listening ...") with action_lock: action_status = 'standby' - _stderr_back = redirect_error_2_null() # ignore error print to ignore ALSA errors - # If the chunk_size is set too small (default_size=1024), it may cause the program to freeze - with sr.Microphone(chunk_size=8192) as source: - cancel_redirect_error(_stderr_back) # restore error print - recognizer.adjust_for_ambient_noise(source) - audio = recognizer.listen(source) # stt # ---------------------------------------------------------------- From 0c0963bcb50348a3e45d8c9986effd1581ed7c0b Mon Sep 17 00:00:00 2001 From: vaski666 Date: Wed, 14 May 2025 18:30:51 +0200 Subject: [PATCH 3/4] Update gpt_car2.py --- gpt_examples/gpt_car2.py | 39 +-------------------------------------- 1 file changed, 1 insertion(+), 38 deletions(-) diff --git a/gpt_examples/gpt_car2.py b/gpt_examples/gpt_car2.py index 8efc5f39..fc6b7618 100644 --- a/gpt_examples/gpt_car2.py +++ b/gpt_examples/gpt_car2.py @@ -84,24 +84,6 @@ time.sleep(.5) print('\n') -# speech_recognition init -# ================================================================= -''' -self.energy_threshold = 300 # minimum audio energy to consider for recording -self.dynamic_energy_threshold = True -self.dynamic_energy_adjustment_damping = 0.15 -self.dynamic_energy_ratio = 1.5 -self.pause_threshold = 0.8 # seconds of non-speaking audio before a phrase is considered complete -self.operation_timeout = None # seconds after an internal operation (e.g., an API request) starts before it times out, or ``None`` for no timeout - -self.phrase_threshold = 0.3 # minimum seconds of speaking audio before we consider the speaking audio a phrase - values below this are ignored (for filtering out clicks and pops) -self.non_speaking_duration = 0.5 # seconds of non-speaking audio to keep on both sides of the recording - -''' -recognizer = sr.Recognizer() -recognizer.dynamic_energy_adjustment_damping = 0.16 -recognizer.dynamic_energy_ratio = 1.6 - # speak_hanlder # ================================================================= speech_loaded = False @@ -226,7 +208,7 @@ def on_connect(client, userdata, flags, rc): def on_message(client, userdata, msg): """Callback function for when a message is received on the subscribed topic.""" - stt_text = msg.payload + stt_text = msg.payload.decode('utf-8') print(f"Received STT text from MQTT: {stt_text}") # main @@ -278,17 +260,10 @@ def main(): while True: if input_mode == 'voice': my_car.set_cam_tilt_angle(DEFAULT_HEAD_TILT) - - # listen - # ---------------------------------------------------------------- print("listening ...") with action_lock: action_status = 'standby' - - - # stt - # ---------------------------------------------------------------- st = time.time() _result = stt_text(f'\033[1;30m{"intput: "}\033[0m').encode(sys.stdin.encoding).decode('utf-8') @@ -296,18 +271,6 @@ def main(): print() # new line continue - elif input_mode == 'keyboard': - my_car.set_cam_tilt_angle(DEFAULT_HEAD_TILT) - - with action_lock: - action_status = 'standby' - - _result = input(f'\033[1;30m{"intput: "}\033[0m').encode(sys.stdin.encoding).decode('utf-8') - - if _result == False or _result == "": - print() # new line - continue - else: raise ValueError("Invalid input mode") From ad87ff9b8d0a1dbd864a8727c4f615af35b8445f Mon Sep 17 00:00:00 2001 From: vaski666 Date: Wed, 14 May 2025 18:39:04 +0200 Subject: [PATCH 4/4] Update gpt_car2.py --- gpt_examples/gpt_car2.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gpt_examples/gpt_car2.py b/gpt_examples/gpt_car2.py index fc6b7618..40c68264 100644 --- a/gpt_examples/gpt_car2.py +++ b/gpt_examples/gpt_car2.py @@ -273,6 +273,7 @@ def main(): else: raise ValueError("Invalid input mode") + print("failing ...") # chat-gpt # ----------------------------------------------------------------