0

I am trying to start a camera recording using Pi Zero W (which acts as MQTT client) upon receiving message and stop the recording on receiving the stop message. Below is my code:

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()

The issue I am facing is that the Pi is recieving the correct message to start recording but it fails to enter the with picamera.PiCamera as camera: loop and the recording doesn't start. The compiler does not show any error in the code. I am unable understand why the recording doesn't start. I have checked the camera and it works fine. Thanks for your help and time in advance.

Shiny
  • 115
  • 1
  • 9
  • How is this different from this question https://stackoverflow.com/questions/60515693/unable-to-control-raspberry-pi-camera-remotely-using-mqtt Using 2 accounts to ask the same question just makes it harder to help you fix the question. – hardillb Mar 05 '20 at 07:11
  • First, you should not be doing blocking long running tasking in the `on_message` callback. Second add try/expect blocks round the code you think is failing and print out the error messages – hardillb Mar 05 '20 at 07:13

0 Answers0