ESP32-CAM Revisited [SOLVED]

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 4161
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ESP32-CAM Revisited

Post by pythoncoder » Wed May 13, 2020 2:11 pm

@tve Thanks for that. Reading the docs should carry a health warning ;)

@ebolisa I have used sdcard.py in the last week on ESP8266 and ESP32 and can report that it works with standard MicroPython firmware.

That said, if your hardware shares the SPI bus between the SD card and other devices there is a known issue.
Peter Hinch

User avatar
ebolisa
Posts: 24
Joined: Thu Feb 21, 2019 11:43 am
Location: Madrid, Spain

Re: ESP32-CAM Revisited

Post by ebolisa » Wed May 13, 2020 3:12 pm

@pythoncoder Thanks for the confirmation, I've not tried the code on other devices as I don't have an external SD card reader/writer.

The ESP32-CAM module has a built in SD card slot so is the one I'm using.

It's possible the SPI bus is shared with the camera but @LeMaRiva got it working modifing the firmware at https://lemariva.com/blog/2020/02/micro ... -esp32-cam and it's the procedure I'm following with no avail.

User avatar
pythoncoder
Posts: 4161
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ESP32-CAM Revisited

Post by pythoncoder » Thu May 14, 2020 7:13 am

If you think it might be a bus sharing problem you could try this sdcard.py which has a fix.
Peter Hinch

User avatar
ebolisa
Posts: 24
Joined: Thu Feb 21, 2019 11:43 am
Location: Madrid, Spain

Re: ESP32-CAM Revisited

Post by ebolisa » Thu May 14, 2020 9:08 am

Thank you for helping out @pythoncoder.

I'd like to think that I have a bogus module, but I'm resisting to believe it cause the Arduino code works.

So what I've done is the following:
Flashed the module with the latest firmware.

Code: Select all

esptool.py --chip esp32 --port com4 write_flash -z 0x1000 esp32-idf3-20191220-v1.12.bin
Added the new sdcard.py library and used the following test code (the boot.py file is empty)

Code: Select all

import machine, sdcard, gc
gc.collect()

try:
    print('*** Setup ***')

#    ESP32-CAM board uses  the SD card to the following pins:
#    SD Card | ESP32    |esp32-cam
#       D2       -          -
#       D3       SS         gpio13
#       CMD      MOSI       gpio15
#       VSS      GND        gnd
#       VDD      3.3V       3.3v
#       CLK      SCK        gpio14
#       VSS      GND        gnd
#       D0       MISO       gpio2
#       D1       -          gpio4 + LED flash also  :(
#   FLASHLED                gpio4
#   red led                 gpio33 (mini smd led below ESP32-controler)
#       SD card socket : pin 9 is SD ( = CARD DETECTION , is a card inserted ?)
  
    
    spi = machine.SPI(1, baudrate=100000, phase=0, polarity=0, sck=machine.Pin(14), mosi=machine.Pin(15), miso=machine.Pin(2)) 
    sd = sdcard.SDCard(spi, machine.Pin(13))
    
except AssertionError as e:
    print(e)
    machine.reset()

--------
MPY: soft reboot
*** Using new sdcard lib ***
*** Setup ***
Traceback (most recent call last):
  File "main.py", line 24, in <module>
  File "sdcard.py", line 50, in __init__
  File "sdcard.py", line 78, in init_card
OSError: no SD card
MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32
At this point I may be using the wrong pins to initialize the SPI bus but, even though, I've been swapping them around.
I also noticed that the SPI bus is used within the sdcard.py lib so, I modified the SPI initialize code with

Code: Select all

#spi = machine.SPI(1, baudrate=100000, phase=0, polarity=0, sck=machine.Pin(14), mosi=machine.Pin(15), miso=machine.Pin(2)) 
sd = sdcard.SDCard(machine.SPI(1), machine.Pin(13)) #SPI(-1) and anything above 2, fails
but... no luck.

My question is, how to properly initialize the SPI bus?
TIA

User avatar
lemariva
Posts: 4
Joined: Sun Oct 21, 2018 9:33 am
Location: Hannover, Germany
Contact:

Re: ESP32-CAM Revisited

Post by lemariva » Wed May 20, 2020 10:56 am

Did you flash the firmware that I've uploaded (https://github.com/lemariva/uPyCam/blob ... python.bin)? Try with that firmware and the repository files (https://github.com/lemariva/uPyCam/tree ... pse-camera). It should work from the box. Keep me updated! ;)

User avatar
ebolisa
Posts: 24
Joined: Thu Feb 21, 2019 11:43 am
Location: Madrid, Spain

Re: ESP32-CAM Revisited

Post by ebolisa » Wed May 20, 2020 11:50 am

Hi @lemariva,

Yes, I tried it several times being last time, few minutes ago.

This' what I get:
MicroPython v1.10-128-g584bc5b2a on 2019-09-01; ESP32 module with ESP32

Type "help()" for more information.
>>>

MPY: soft reboot
network config: ('192.168.0.69', '255.255.255.0', '192.168.0.1', '192.168.0.200')
I (61600) gpio: GPIO[32]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (61680) gpio: GPIO[35]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61680) gpio: GPIO[34]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61690) gpio: GPIO[39]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61700) gpio: GPIO[36]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61710) gpio: GPIO[21]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61720) gpio: GPIO[19]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61720) gpio: GPIO[18]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61730) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61740) gpio: GPIO[25]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61750) gpio: GPIO[23]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61760) gpio: GPIO[22]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (61770) camera: Allocating 1 frame buffers (234 KB total)
I (61810) camera: Allocating 234 KB frame buffer in OnBoard RAM
E (61840) gpio: gpio_install_isr_service(394): GPIO isr service already installed
I (62050) spi_master: Allocate TX buffer for DMA
I (62050) spi_master: Allocate TX buffer for DMA
I (62050) spi_master: Allocate TX buffer for DMA
I (62060) spi_master: Allocate TX buffer for DMA
I (62060) spi_master: Allocate TX buffer for DMA
no SD card
Restarting...
I (62070) wifi: state: run -> init (0)
I (62070) wifi: pm stop, total sleep time: 29298470 us / 58451294 us

I (62080) wifi: new:<1,0>, old:<1,0>, ap:<255,255>, sta:<1,0>, prof:1
I (62100) wifi: flush txq
I (62100) wifi: stop sw txq
I (62100) wifi: lmac stop hw txq
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5200
ho 0 tail 12 room 4
load:0x40078000,len:9616
load:0x40080400,len:7328
entry 0x40081758
I (485) spiram: Found 64MBit SPI RAM device
I (485) spiram: SPI RAM mode: flash 40m sram 40m
I (485) spiram: PSRAM initialized, cache is in normal (1-core) mode.
I (491) cpu_start: Pro cpu up.
I (495) cpu_start: Application information:
I (500) cpu_start: Compile time: 21:48:02
I (505) cpu_start: Compile date: Sep 1 2019
I (510) cpu_start: ESP-IDF: v3.3-beta1-268-g5c88c5996
I (517) cpu_start: Single core mode
I (1383) spiram: SPI SRAM memory test OK
I (1383) heap_init: Initializing. RAM available for dynamic allocation:
I (1384) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (1390) heap_init: At 3FFB9870 len 00026790 (153 KiB): DRAM
I (1396) heap_init: At 3FFE0440 len 0001FBC0 (126 KiB): D/IRAM
I (1403) heap_init: At 40078000 len 00008000 (32 KiB): IRAM
I (1409) heap_init: At 40097338 len 00008CC8 (35 KiB): IRAM
I (1415) cpu_start: Pro cpu start user code
I (1420) spiram: Adding pool of 4096K of external SPI memory to heap allocator
I (99) cpu_start: Starting scheduler on PRO CPU.
I (100) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations
I (120) main: Allocated 2048K for micropython heap at 0x3f940020
I (180) wifi: wifi driver task: 3ffcc68c, prio:23, stack:3584, core=0
I (180) wifi: wifi firmware version: 38e2484
I (180) wifi: config NVS flash: enabled
I (180) wifi: config nano formating: disabled
I (190) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (200) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (210) wifi: Init dynamic tx buffer num: 32
I (210) wifi: Init data frame dynamic rx buffer num: 32
I (220) wifi: Init management frame dynamic rx buffer num: 32
I (220) wifi: Init static tx buffer num: 16
I (230) wifi: Init static rx buffer size: 1600
I (230) wifi: Init static rx buffer num: 10
I (230) wifi: Init dynamic rx buffer num: 32
connecting to network...
I (300) phy: phy_version: 4007, 9c6b43b, Jan 11 2019, 16:45:07, 0, 0
I (300) wifi: mode : sta (24:6f:28:16:38:08)
I (300) wifi: STA_START
I (430) wifi: new:<1,0>, old:<1,0>, ap:<255,255>, sta:<1,0>, prof:1
I (1110) wifi: state: init -> auth (b0)
I (1120) wifi: state: auth -> assoc (0)
I (1170) wifi: state: assoc -> run (10)
I (1380) wifi: connected with PHONE_HOME, channel 1, bssid = 58:90:43:4c:65:d6
I (1390) wifi: pm start, type: 1

I (1390) network: CONNECTED
I (4280) event: sta ip: 192.168.0.69, mask: 255.255.255.0, gw: 192.168.0.1
I (4280) network: GOT_IP
The only change I made in main.py is to print the exception so I can see what's going on:

Code: Select all

except Exception as e:
    print(e)
    print('Restarting...')
    machine.reset()
EDIT:
If I comment the sdcard init lines...

Code: Select all

#     sd = sdcard.SDCard(spi, machine.Pin(13))
#     os.mount(sd, '/sd')
#     os.listdir('/')
I get these results:
MPY: soft reboot
network config: ('192.168.0.69', '255.255.255.0', '192.168.0.1', '192.168.0.200')
I (104060) gpio: GPIO[32]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (104140) gpio: GPIO[35]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104140) gpio: GPIO[34]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104150) gpio: GPIO[39]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104160) gpio: GPIO[36]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104170) gpio: GPIO[21]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104180) gpio: GPIO[19]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104190) gpio: GPIO[18]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104200) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104210) gpio: GPIO[25]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104220) gpio: GPIO[23]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104220) gpio: GPIO[22]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (104230) camera: Allocating 1 frame buffers (234 KB total)
I (104270) camera: Allocating 234 KB frame buffer in OnBoard RAM
E (104300) gpio: gpio_install_isr_service(394): GPIO isr service already installed
[Errno 110] ETIMEDOUT
Restarting...
I (105720) wifi: state: run -> init (0)
I (105720) wifi: pm stop, total sleep time: 63719911 us / 102142158 us

I (105720) wifi: new:<1,0>, old:<1,0>, ap:<255,255>, sta:<1,0>, prof:1
I (106510) wifi: flush txq
I (106510) wifi: stop sw txq
I (106510) wifi: lmac stop hw txq
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5200
ho 0 tail 12 room 4
load:0x40078000,len:9616
load:0x40080400,len:7328
entry 0x40081758
I (485) spiram: Found 64MBit SPI RAM device
I (485) spiram: SPI RAM mode: flash 40m sram 40m
I (485) spiram: PSRAM initialized, cache is in normal (1-core) mode.
I (491) cpu_start: Pro cpu up.
I (495) cpu_start: Application information:
I (500) cpu_start: Compile time: 21:48:02
I (505) cpu_start: Compile date: Sep 1 2019
I (510) cpu_start: ESP-IDF: v3.3-beta1-268-g5c88c5996
I (517) cpu_start: Single core mode
I (1383) spiram: SPI SRAM memory test OK
I (1383) heap_init: Initializing. RAM available for dynamic allocation:
I (1384) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (1390) heap_init: At 3FFB9870 len 00026790 (153 KiB): DRAM
I (1396) heap_init: At 3FFE0440 len 0001FBC0 (126 KiB): D/IRAM
I (1403) heap_init: At 40078000 len 00008000 (32 KiB): IRAM
I (1409) heap_init: At 40097338 len 00008CC8 (35 KiB): IRAM
I (1415) cpu_start: Pro cpu start user code
I (1420) spiram: Adding pool of 4096K of external SPI memory to heap allocator
I (99) cpu_start: Starting scheduler on PRO CPU.
I (100) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations
I (110) main: Allocated 2048K for micropython heap at 0x3f940020
I (180) wifi: wifi driver task: 3ffcc710, prio:23, stack:3584, core=0
I (180) wifi: wifi firmware version: 38e2484
I (180) wifi: config NVS flash: enabled
I (180) wifi: config nano formating: disabled
I (190) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (200) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (210) wifi: Init dynamic tx buffer num: 32
I (210) wifi: Init data frame dynamic rx buffer num: 32
I (220) wifi: Init management frame dynamic rx buffer num: 32
I (220) wifi: Init static tx buffer num: 16
I (220) wifi: Init static rx buffer size: 1600
I (230) wifi: Init static rx buffer num: 10
I (230) wifi: Init dynamic rx buffer num: 32
connecting to network...
I (300) phy: phy_version: 4007, 9c6b43b, Jan 11 2019, 16:45:07, 0, 0
I (310) wifi: mode : sta (24:6f:28:16:38:08)
I (310) wifi: STA_START
I (430) wifi: new:<1,0>, old:<1,0>, ap:<255,255>, sta:<1,0>, prof:1
I (1120) wifi: state: init -> auth (b0)
I (1120) wifi: state: auth -> assoc (0)
I (1190) wifi: state: assoc -> run (10)
I (1340) wifi: connected with PHONE_HOME, channel 1, bssid = 58:90:43:4c:65:d6
I (1340) wifi: pm start, type: 1

I (1340) network: CONNECTED
I (3320) event: sta ip: 192.168.0.69, mask: 255.255.255.0, gw: 192.168.0.1
I (3320) network: GOT_IP
network config: ('192.168.0.69', '255.255.255.0', '192.168.0.1', '192.168.0.200')
I (3610) modsocket: Initializing
I (4040) gpio: GPIO[32]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (4120) gpio: GPIO[35]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4120) gpio: GPIO[34]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4130) gpio: GPIO[39]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4140) gpio: GPIO[36]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4150) gpio: GPIO[21]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4150) gpio: GPIO[19]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4160) gpio: GPIO[18]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4170) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4180) gpio: GPIO[25]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4190) gpio: GPIO[23]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4200) gpio: GPIO[22]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (4210) camera: Allocating 1 frame buffers (234 KB total)
I (4250) camera: Allocating 234 KB frame buffer in OnBoard RAM
E (4280) gpio: gpio_install_isr_service(394): GPIO isr service already installed
MicroPython v1.10-128-g584bc5b2a on 2019-09-01; ESP32 module with ESP32
Type "help()" for more information.

>>> FTP Server started

User avatar
lemariva
Posts: 4
Joined: Sun Oct 21, 2018 9:33 am
Location: Hannover, Germany
Contact:

Re: ESP32-CAM Revisited

Post by lemariva » Thu May 21, 2020 12:10 pm

I've just upgraded the repository and included the driver from @pythoncoder. I flashed the firmware included and the project is working out of the box.
I think the problem could be the microSD card. I tested this with an 8GB and a 16GB microSD formatted using FAT (required!). Do you have another microSD card that you can test? Keep me updated! :)

User avatar
ebolisa
Posts: 24
Joined: Thu Feb 21, 2019 11:43 am
Location: Madrid, Spain

Re: ESP32-CAM Revisited

Post by ebolisa » Thu May 21, 2020 3:13 pm

@lemariva I didn't think to change the SD card as it worked with the Arduino code and, Yes, it was the culprit. Thank you!!

Post Reply