0

我正在尝试在收到消息后使用 Pi Zero W(充当 MQTT 客户端)开始摄像头录制,并在收到停止消息时停止录制。下面是我的代码:

continueRecording = 1

Broker = "192.168.0.105"

pub_topic = "picamera1"

sub_topics = ["Rpi_Master", 0]

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        pass
    else:
        print("Bad Connection with result code: " + str(rc))
    for topic in sub_topics:
        client.subscribe(topic)

def on_message(client, userdata, msg):
    global message_topic, message
    global continueRecording
    message = str(msg.payload.decode("utf-8"))
    print("Received message is" + message)
    message_start = str(message[:4])
    print("Command to start recording is " + message_start)
    if message_start == "shop":
        print("Enter shop")
        with picamera.PiCamera as camera:
            camera.resolution = (640, 480)
            camera.framerate=20
            camera.start_recording("/home/pi/camera-recording/shop/shoprecording.h264")
            time.sleep(0.5)
            while continueRecording == 1:
                camera.wait_recording(.01)
                if message == "OK":
                    print("Stopping to record")
                    camera.stop_recording()
                    continueRecording = 0

def on_publish(mosq, obj, mid):
    pass

# on mqtt disconnection#
def on_disconnect(client, userdata, rc):
    if rc == 0:
        pass
    elif rc != 0:
        print("Unexpected MQTT disconnection. Will try to reconnect")
        try:
            client.username_pw_set(username="ab", password="abcdef")
            client.connect(Broker, 1883, 60)
        except:
            print("Error in trying to reconnect with the Broker")

# mqtt client broker Connection
def clientBrokerConnection():
    print("Client Broker Function Running")
    global client
    client = mqtt.Client("piCamera1")  # creating a new instance
    ##Defining the callback functions
    client.username_pw_set(username="pi", password="lotus56789")
    client.on_connect = on_connect
    client.on_message = on_message
    client.on_publish = on_publish
    client.on_disconnect = on_disconnect
    ##End of callback functions
    client.connect(Broker, 1883, 60)  # Connecting to Broker
    client.loop_start()


clientBrokerConnection()

我面临的问题是 Pi 正在接收正确的消息以开始录制,但它无法进入with picamera.PiCamera as camera:循环并且录制没有开始。编译器不会在代码中显示任何错误。我不明白为什么录音没有开始。我检查了相机,它工作正常。感谢您的帮助和提前的时间。

4

0 回答 0