Best way to use esp-32 cam as a USB camera?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
lorenz
Posts: 8
Joined: Sat Dec 12, 2020 8:11 pm

Best way to use esp-32 cam as a USB camera?

Post by lorenz » Sat Apr 10, 2021 10:57 am

Hi,
I have mounted two esp-32 ai thinker cams with the esp-cam-mb board on a robot and my cnc router to be able to remotely see what they are doing. As I have a Raspberry Pi close by I would like to stream the pictures from the esp board via a usb cable to the raspberry pi.

I have written the micropython code below and I gate a frame rate of ca 5pics per second (with 320x240 QVGA frames) which is fine for my purposes. On the Pi, I have a small python program that parses the pictures, puts them into redis from where I can stream it via a small flask server.

But I am not sure this is the best approach.

Here are my questions:
1) does anybody know if there is a driver for the esp32cam so the it shows up as `/dev/videox` on the pi when you plug it in?
2) I am using the repl uart to send the pictures and I have added an if clausue if I send an "s" back, the esp-32 will do a hard reset. I can then see the repl output, but I cannot enter anything any more. I have played with uos.dupterm but did not have any luck. Does anybody have any ideas here?
3) When using baudrates higher than 230400, it was still sending things but the python program on the pi could not decode it any more. My cable length was ca 0.5m and the esp-cam-mb has the CH340 usb to serial chip on it. Any hints here?

Code: Select all

import camera, machine, time, json

def send_pictures():
    uart = machine.UART(1, baudrate=230400, rx=3, tx=1)
    camera.init(0, format=camera.JPEG, framesize=5)
    camera.quality(30)
    t_send_prev = 0
    c = 0
    while True:
        t0 = time.ticks_ms()
        pic = camera.capture()
        t1 = time.ticks_ms()
        c += 1
        info = {
            "len": len(pic),
            "t_cap": t1 - t0,
            "t_send_prev": t_send_prev,
            "c": c,
        }
        uart.write("\r\nstart-%s-end\r\n" % json.dumps(info))
        uart.write(pic)
        t2 = time.ticks_ms()
        t_send_prev = t2 - t1
        r = uart.read()
        # todo: this does not fully work yet
        if r is not None and 's' in r:
            uart.deinit()
            machine.reset()

# have the sleep time at the beginning to be able to upload py files / stop the pgrogram. 
sleep_time_sec = 15
print("will start sending pictures in %d seconds" % sleep_time_sec)
time.sleep(sleep_time_sec)
send_pictures()
Thanks a lot in advance,
Lorenz

Here are two pictures of the setup:
https://ibb.co/3YwGXzG
https://ibb.co/Rc0P3JQ

Post Reply