import time import threading import speech_recognition import pyttsx3 from paho.mqtt import client as mqtt_client broker = 'broker.emqx.io' port = 1883 pubtop = "/boxchat/tu" subtop = "/boxchat/tuan" client_id = f'python-mqtt-{10}' #connect to broker username = 'emqx' password = 'public' # connect client to broker def on_connect(client, userdata, flags, rc): #call to connect function if rc == 0: print("Connected to MQTT Broker!") client.subscribe(subtop) else: print("Failed to connect, return code %d\n", rc) ########################### # message control def on_message(client, userdata, msg): global chat if str(msg.topic) != pubtop: chat = str(msg.payload.decode("utf-8")) chat = str(msg.payload.decode("utf-8")) robot_mouth = pyttsx3.init() robot_mouth.say(chat) robot_mouth.runAndWait() def publish(client): global msg while True: robot_ear = speech_recognition.Recognizer() with speech_recognition.Microphone() as mic: print("robot : im listen") audio = robot_ear.listen(mic) try: msg = robot_ear.recognize_google(audio) except: msg = "" if msg == 'stop' or msg == 'Stop': client.disconnect() print("Disconnected....") break else: client.publish(pubtop,msg,0, retain=False) client = mqtt_client.Client(client_id, clean_session = False,userdata=None) client.username_pw_set(username, password) client.on_connect = on_connect client.on_message = on_message client.connect(broker,port) time.sleep(1) thread1 =threading.Thread(target=publish,args=(client,)) thread1.start() client.loop_forever()