ESP32 using a Camera and saving to an SD

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
modulusmath
Posts: 30
Joined: Thu Jun 30, 2022 3:21 am

ESP32 using a Camera and saving to an SD

Post by modulusmath » Sun Jul 10, 2022 4:25 am

Good Day All,

Is there anyone who is using an ESP32 (any type) with a camera (any type) that is also saving to an sd card (any type), willing to share the precise pieces of hardware and what firmware .bin file they are using? I shall then purchase them and use those (and will hold you blameless for any outcomes).

I was making some progress in that I was able to take 1 picture and save it to an sd card using:
- ESP32-CAM-MB
- https://github.com/Lennyz1988/micropyth ... rmware.bin
- 32 GB SanDisk.

HOWEVER, only if my steps are like this:

Code: Select all

camera.init()
img = camera.capture()
camera.deinit()

uos.mount(machine.SDCard(), "/sd") 
now_file = '-'.join([ str(x) for x in time.localtime()[:6] ]) + '.jpg'
imgFile = open("/sd/" + now_file, "wb")
imgFile.write(img)
imgFile.close()
uos.umount("/sd")

The problem is if I try to take a second pic just with:

Code: Select all

camera.init()
img = camera.capture()
even after unmounting the sd, it fails (hangs forever) on

Code: Select all

camera.capture()
.

I have gone so far as to also try:

Code: Select all

del uos
del camera 
In my mind, it appears the HW (esp32-cam (I have tried 2 diff ones) / sd card (tried at least 2 diff manufacturers)/ usb cable (tried a few diff ones) are all working.

I do fear it's an spi bus sharing issue but I was unable to get lemariva's fw to work and really I would have thought the camera.deinit() would allow me to take another shot.


However, I can take a bunch of pictures on the non-sd '/' and then use a cp/rm loop workaround:

Code: Select all


### Doing this Allows me to take multiple photos and ultimately get them to the sd card...

count=3
sleep=7
while count > 0:
    import time
    import camera
    camera.init()
    img = camera.capture()
    camera.deinit()
    now_file = '-'.join([ str(x) for x in time.localtime()[:6] ]) + '.jpg'
    imgFile = open(now_file, "wb")
    imgFile.write(img)
    imgFile.close()
    time.sleep(sleep)
    count-=1
       
import uos
uos.mount(machine.SDCard(), "/sd")
from upysh import cp
from upysh import rm
[ cp(x,'/sd/'+x ) for x in os.listdir() if x.startswith('2022') or x.endswith('.jpg') ]
[ rm(x) for x in os.listdir() if x.startswith('2022') or x.endswith('.jpg') ]
#NB: mv yielded 'OSError: [Errno 1] EPERM' for some reason

#Verify
os.listdir('/')
os.listdir('/sd/')
#Unmount
uos.umount("/sd")



It get's it done and if it's what I need to do, so be it, but it just like there's got to be a better way? (or maybe it's me?)

As frustrating as this can be I am having lots of fun w/all this and am appreciative of the giant's whose shoulders I am standing upon to even get this far! Thanks in advance for any insights.

tepalia02
Posts: 99
Joined: Mon Mar 21, 2022 5:13 am

Re: ESP32 using a Camera and saving to an SD

Post by tepalia02 » Mon Jul 11, 2022 11:01 am

In your code, if you try to give your JPEG file a shorter name, does your code work?

modulusmath
Posts: 30
Joined: Thu Jun 30, 2022 3:21 am

Re: ESP32 using a Camera and saving to an SD

Post by modulusmath » Mon Jul 11, 2022 11:50 pm

Thanks for the thoughts. Sadly no it does not help.
I tried, but it's an excellent thought and I should probably shorten it for FAT.

I actually included a machine.reset to completely separate camera ops and sdcard ops, but that did not help either.

And now I can't write consistently to the sdcard.
I've tried higher quality sd's like Sandisk and less known vendors alike.

Some Redditors suggest:

Code: Select all

...the consensus was that only particular sd cards work with micropython and the sd card library on micropython is not that great...
https://www.reddit.com/r/raspberrypipic ... 19_enodev/

It certainly lines up with my experience but really wondering how others are faring w/this.

Thanks!

uncoded0123
Posts: 6
Joined: Wed Aug 12, 2020 11:09 am

Re: ESP32 using a Camera and saving to an SD

Post by uncoded0123 » Fri Feb 03, 2023 4:39 am

I don't know if this will help, it helped me with something similar. Have you tried try / except clause, with machine.reset() in except (while making sure to put sleep before it resets so it doesn't get stuck in auto infinite loop)?

Post Reply