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
ebolisa
Posts: 55
Joined: Thu Feb 21, 2019 11:43 am
Location: Madrid, Spain

ESP32-CAM Revisited [SOLVED]

Post by ebolisa » Mon May 11, 2020 5:01 pm

Hi,

I cannot communicate with the board's sd card using Micropython however, it works fine with Arduino's code. The camera works fine with Micropython.

I'm probably using the wrong pins but I ran out of ideas.

I appreciate any help.
TIA

PS
Yes, the sd card is plugged in and FAT32 formatted.

Code: Select all

import machine, sdcard, os

try:
    print('*** Setup ***')
    
#     CLK GPIO 14
#     CMD GPIO 15
#     DATA0   GPIO 2
#     DATA1 / flashlight  GPIO 4
#     DATA2   GPIO 12
#     DATA3   GPIO 13
#     RED LED GPIO 33
#     SD card socket : pin 9 is SD
    
    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))
    os.mount(sd, '/sd')
    os.listdir('/')
    
    print('*** Done ***')

except AssertionError as e:
    print(e)
    machine.reset()
    
MPY: soft reboot
*** Setup ***
I (52270) gpio: GPIO[15]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (52270) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (52280) gpio: GPIO[14]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (52300) spi_master: Allocate TX buffer for DMA
I (52320) spi_master: Allocate TX buffer for DMA
I (52350) spi_master: Allocate TX buffer for DMA
I (52370) spi_master: Allocate TX buffer for DMA
I (52400) spi_master: Allocate TX buffer for DMA
Traceback (most recent call last):
File "main.py", line 27, in <module>
File "main.py", line 19, in <module>
File "sdcard.py", line 48, in __init__
File "sdcard.py", line 76, in init_card
OSError: no SD card
MicroPython v1.10-128-g584bc5b2a on 2019-09-01; ESP32 module with ESP32
Type "help()" for more information.
Last edited by ebolisa on Thu May 21, 2020 3:13 pm, edited 1 time in total.

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: ESP32-CAM Revisited

Post by tve » Tue May 12, 2020 2:43 am

What is this sdcard module? You're not using machine.SDCard?

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

Re: ESP32-CAM Revisited

Post by ebolisa » Tue May 12, 2020 7:29 am

Hi,

Sorry, I failed to mention in my post that I'm following this procedure https://lemariva.com/blog/2019/09/micro ... hoto-esp32 and the sdcard lib was taken from here https://github.com/lemariva/uPyCam/tree ... pse-camera.

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

Re: ESP32-CAM Revisited

Post by pythoncoder » Tue May 12, 2020 8:58 am

Two comments. Firstly I'd use the official version of sdcard.py here. The one you're using looks the same, but I've only glanced at the code. The official version works.

Secondly the line

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))
looks wrong. SPI should be initialised either as soft SPI or hard SPI. In soft SPI you get to specify the pins, but the first argument should be -1. Your arg of 1 specifies hard SPI #1 which uses different pins defined in hardware. See http://docs.micropython.org/en/latest/l ... e.SPI.html

So as a first step change the 1 to -1.
Peter Hinch
Index to my micropython libraries.

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

Re: ESP32-CAM Revisited

Post by ebolisa » Tue May 12, 2020 9:37 am

Ok, I updated the sdcard.py file with the official code and changed the bit from 1 to -1 with no avail.

In both cases (1 and -1) the code stops when reaches this line:

Code: Select all

sd = sdcard.SDCard(spi, machine.Pin(13))
No errors with the following code which indicates that the SPI is initializing ok:

Code: Select all

import machine, sdcard, os

try:
    print('*** Setup ***')
    
    spi = machine.SPI(1, baudrate=100000, polarity=0, phase=0, bits=8, sck=machine.Pin(14), mosi=machine.Pin(15), miso=machine.Pin(2))
    #sd = sdcard.SDCard(spi, machine.Pin(13))
    #os.mount(sd, '/sd')
    #os.listdir('/')
    
    print('*** Done ***')

except AssertionError as e:
    print(e)
    machine.reset()

MPY: soft reboot
*** Setup ***
*** Done ***
MicroPython v1.10-128-g584bc5b2a on 2019-09-01; ESP32 module with ESP32
Type "help()" for more information.
However, if I uncomment that line, the code crushes...

Code: Select all

import machine, sdcard, os

try:
    print('*** Setup ***')
    
    spi = machine.SPI(1, baudrate=100000, polarity=0, phase=0, bits=8, sck=machine.Pin(14), mosi=machine.Pin(15), miso=machine.Pin(2))
    sd = sdcard.SDCard(spi, machine.Pin(13))
    #os.mount(sd, '/sd')
    #os.listdir('/')
    
    print('*** Done ***')

except AssertionError as e:
    print(e)
    machine.reset()

MPY: soft reboot
*** Setup ***
I (2096420) spi_master: Allocate TX buffer for DMA
I (2096420) spi_master: Allocate TX buffer for DMA
I (2096430) spi_master: Allocate TX buffer for DMA
I (2096430) spi_master: Allocate TX buffer for DMA
I (2096440) spi_master: Allocate TX buffer for DMA
Traceback (most recent call last):
  File "main.py", line 24, in <module>
  File "main.py", line 16, in <module>
  File "sdcard.py", line 48, in __init__
  File "sdcard.py", line 76, in init_card
OSError: no SD card
MicroPython v1.10-128-g584bc5b2a on 2019-09-01; ESP32 module with ESP32
Type "help()" for more information.

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

Re: ESP32-CAM Revisited

Post by ebolisa » Tue May 12, 2020 11:57 am

BTW: This' the Arduino test code which works with this module (ESP32-CAM).

Code: Select all

#include "FS.h"                // SD Card ESP32
#include "SD_MMC.h"            // SD Card ESP32

void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

void appendFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}

void setup() {
  Serial.begin(115200);
  Serial.println();

  //Serial.println("Starting SD Card");
  if (!SD_MMC.begin()) {
    Serial.println("SD Card Mount Failed");
    return;
  }

  uint8_t cardType = SD_MMC.cardType();
  if (cardType == CARD_NONE) {
    Serial.println("No SD Card attached");
    return;
  }

  // If the data.txt file doesn't exist
  // Create a file on the SD card and write the data labels
  File file = SD_MMC.open("/data.txt");
  if (!file) {
    Serial.println("File doens't exist");
    Serial.println("Creating file...");
    writeFile(SD_MMC, "/data.txt", "Reading ID, Date, Hour, Temperature \r\n");
  }
  else {
    Serial.println("File already exists");

    int count = 0;
    String dataMessage = "";

    //write some to card
    for (int i = 0; i < 10; i++) {
      dataMessage = String(count) + "\r\n";
      appendFile(SD_MMC, "/data.txt", dataMessage.c_str());
      Serial.printf("Writing to file: %d\n", count);
      count += 1;
      delay(1000);
    }
    file.close();

  }
}

void loop() {}

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: ESP32-CAM Revisited

Post by tve » Tue May 12, 2020 6:23 pm

pythoncoder wrote:
Tue May 12, 2020 8:58 am
In soft SPI you get to specify the pins, but the first argument should be -1. Your arg of 1 specifies hard SPI #1 which uses different pins defined in hardware. See http://docs.micropython.org/en/latest/l ... e.SPI.html
Are you sure? On the ESP32 as far as I can tell you can use arbitrary pins for HW SPI.

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

Re: ESP32-CAM Revisited

Post by pythoncoder » Wed May 13, 2020 5:11 am

tve wrote:
Tue May 12, 2020 6:23 pm
pythoncoder wrote:
Tue May 12, 2020 8:58 am
...
Are you sure? On the ESP32 as far as I can tell you can use arbitrary pins for HW SPI.
There are two questions here. Does the hardware support it? And does the machine module?

I'm no ESP32 guru but I believe the hardware supports arbitrary pin assignments. However the docs lead me to believe that machine does not:
Arbitrary pin assignments are possible only for a bitbanging SPI driver (id = -1).
In any event I'd try changing the ID to -1 as a test. Soft SPI with arbitrary pins and hard SPI with fixed pins have both been well tested.
Peter Hinch
Index to my micropython libraries.

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: ESP32-CAM Revisited

Post by tve » Wed May 13, 2020 7:52 am

pythoncoder wrote:
Wed May 13, 2020 5:11 am
tve wrote:
Tue May 12, 2020 6:23 pm
Are you sure? On the ESP32 as far as I can tell you can use arbitrary pins for HW SPI.
There are two questions here. Does the hardware support it? And does the machine module?
What I meant is the machine module. Looking at the code it supports arbitrary pin assignments. As usual, the docs apply to pyboard...

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

Re: ESP32-CAM Revisited

Post by ebolisa » Wed May 13, 2020 11:56 am

I think that may be my problem. The sdcard.py library works only on the pyboard and not on the ESP32-CAM module. However, I can assure you that the Arduino code shown here works well on said module.

Post Reply