メッセージの受信時に 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:
ループに入らず、録音が開始されないことです。コンパイラはコードにエラーを表示しません。録音が開始されない理由がわかりません。カメラを確認しましたが、問題なく動作しています。ご協力いただきありがとうございます。