On Saturday, August 13, 2022 we will be working on the Adafruit Customer Support Forums. If you visit over the weekend and things are not complete yet, please check back later, https://adafruit.com/forumupdates
0

issue when using esp32 and sd card in CircuitPython
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

issue when using esp32 and sd card in CircuitPython

by powersoft on Fri Jun 24, 2022 10:24 am

I use in my program the esp-32 to connect via Pyportal to the internet time, and also use the sd card to load images.
There is an conflict wit the SCK

Traceback (meest recente call laatst):
Bestand "code.py", regel 58, in <module>
ValueError: SCK in gebruik = (SCK in use)

Working with the Titano.

Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio
import digitalio
from adafruit_esp32spi import adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
from adafruit_pyportal import PyPortal

import storage
import adafruit_sdcard
import adafruit_imageload
import time

try:
    from secrets import secrets
except ImportError:
    print("""WiFi settings are kept in secrets.py, please add them there!
the secrets dictionary must contain 'ssid' and 'password' at a minimum""")
    raise
   
esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False)
requests.set_socket(socket, esp)


from moon_angle import *

def set_image(group, filename):
    print("Set image to ", filename)
    if group:
        group.pop()

    if not filename:
        return  # we're done, no icon desired

    #image, palette = adafruit_imageload.load(filename,bitmap=displayio.Bitmap,palette=displayio.Palette)
    #image_sprite = displayio.TileGrid(image, pixel_shader=getattr(image, 'pixel_shader', displayio.ColorConverter()))
    image, palette = adafruit_imageload.load(file_name)
    image_sprite = displayio.TileGrid(image, pixel_shader=palette)

    group.append(image_sprite)

# See if a card is present
card_detect_pin = digitalio.DigitalInOut(board.SD_CARD_DETECT)
card_detect_pin.direction = digitalio.Direction.INPUT
card_detect_pin.pull = digitalio.Pull.UP
print('SD card present: %s' % card_detect_pin.value)

# Try to connect to the SD card
sdcard = adafruit_sdcard.SDCard(
    busio.SPI(board.SCK, board.MOSI, board.MISO),
    digitalio.DigitalInOut(board.SD_CS)
)


The error is coming when try to connect to the SD card. Is there a solution for?

Thanks for any help.
Jan Kromhout
Hellevoetsluis-NL

powersoft
 
Posts: 56
Joined: Tue Aug 30, 2011 1:01 am

Re: issue when using esp32 and sd card in CircuitPython

by dastels on Fri Jun 24, 2022 11:53 am

It's because you are doing this twice:
Code: Select all | TOGGLE FULL SIZE
busio.SPI(board.SCK, board.MOSI, board.MISO),
Just use the spi variable for creating the SD card object:
Code: Select all | TOGGLE FULL SIZE
sdcard = adafruit_sdcard.SDCard(spi, digitalio.DigitalInOut(board.SD_CS))


Dave

dastels
 
Posts: 10578
Joined: Tue Oct 20, 2015 3:22 pm

Re: issue when using esp32 and sd card in CircuitPython

by neradoc on Fri Jun 24, 2022 11:53 am

[edit: jinx]
Note that you should also setup the SD Card first, because the SD Card has to be put into SPI mode, or the other devices could fail.

neradoc
 
Posts: 301
Joined: Wed Apr 27, 2016 2:38 pm

Re: issue when using esp32 and sd card in CircuitPython

by powersoft on Fri Jun 24, 2022 12:55 pm

Thanks for the information, but please can you help me to change the code. I don't understand how to do it.
I have change the code so the sd card code is in front of the code.

Code: Select all | TOGGLE FULL SIZE
# See if a card is present
card_detect_pin = digitalio.DigitalInOut(board.SD_CARD_DETECT)
card_detect_pin.direction = digitalio.Direction.INPUT
card_detect_pin.pull = digitalio.Pull.UP
print('SD card present: %s' % card_detect_pin.value)

# Try to connect to the SD card
sdcard = adafruit_sdcard.SDCard(
    busio.SPI(board.SCK, board.MOSI, board.MISO),
    digitalio.DigitalInOut(board.SD_CS)
)

# Mount the card to a directory
virtual_file_system = storage.VfsFat(sdcard)
storage.mount(virtual_file_system, '/sd')

esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False)
requests.set_socket(socket, esp)


Again thanks for any help.

powersoft
 
Posts: 56
Joined: Tue Aug 30, 2011 1:01 am

Re: issue when using esp32 and sd card in CircuitPython

by dastels on Fri Jun 24, 2022 1:23 pm

This is the relevant section of the code:
Code: Select all | TOGGLE FULL SIZE
# CREATE A SPI OBJECT
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)   

# Try to connect to the SD card
# USE THE SPI OBJECT TO CREATE AN SDCARD OBJECT
sdcard = adafruit_sdcard.SDCard(           
    spi,                                                             
    digitalio.DigitalInOut(board.SD_CS)
)

# Mount the card to a directory
virtual_file_system = storage.VfsFat(sdcard)
storage.mount(virtual_file_system, '/sd')

esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)

# USE THE SPI OBJECT TO CREATE AN ESP OBJECT
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False)

Dave

dastels
 
Posts: 10578
Joined: Tue Oct 20, 2015 3:22 pm

Re: issue when using esp32 and sd card in CircuitPython

by powersoft on Sat Jun 25, 2022 11:26 am

Thans for this, but when I add the pyportal to the code I get an other error

Code: Select all | TOGGLE FULL SIZE
SD card present: True
Traceback (meest recente call laatst):
  Bestand "code.py", regel 1, in <module>
  Bestand "display_moon_picture.py", regel 47, in <module>
  Bestand "adafruit_pyportal/__init__.py", regel 183, in __init__
  Bestand "adafruit_pyportal/peripherals.py", regel 75, in __init__
ValueError: SD_CS in gebruik


and here is the code:

Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio
import digitalio
from adafruit_esp32spi import adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests
from adafruit_pyportal import PyPortal
import displayio
import storage
import adafruit_sdcard
import adafruit_imageload
import time

try:
    from secrets import secrets
except ImportError:
    print("""WiFi settings are kept in secrets.py, please add them there!
the secrets dictionary must contain 'ssid' and 'password' at a minimum""")
    raise
   
# CREATE A SPI OBJECT
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)

# See if a card is present
card_detect_pin = digitalio.DigitalInOut(board.SD_CARD_DETECT)
card_detect_pin.direction = digitalio.Direction.INPUT
card_detect_pin.pull = digitalio.Pull.UP
print('SD card present: %s' % card_detect_pin.value)

# Try to connect to the SD card
sdcard = adafruit_sdcard.SDCard(spi, digitalio.DigitalInOut(board.SD_CS))

# Mount the card to a directory
virtual_file_system = storage.VfsFat(sdcard)
storage.mount(virtual_file_system, '/sd')

# Connect esp32
esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False)
requests.set_socket(socket, esp)

# get time
# initialize pyportal
pyportal = PyPortal(esp=esp,external_spi=spi)
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
    print("ESP32 found and in idle mode")


print("Getting new time from internet...")
pyportal.get_local_time(secrets['timezone'])




please can you help me with this?

Ore is there a simple solution to do:
1. Init SD card
2. Init ESP32
3. Start PyPortal
4. Get the time

Thanks for any help

powersoft
 
Posts: 56
Joined: Tue Aug 30, 2011 1:01 am

Re: issue when using esp32 and sd card in CircuitPython

by dastels on Sat Jun 25, 2022 11:56 am

I think your problem has to do with you setting up the SD and ESP32 AS WELL AS using adafruit_pyportal.PyPortal which takes care of setting it up. As you can see the error is occuring in adafruit_pyportal/peripherals.py, not your code.

Choose one way or the other of using the SD & ESP. I suspect using the PyPortal module will be quicker and easier for you since support is all built in.

Dave

dastels
 
Posts: 10578
Joined: Tue Oct 20, 2015 3:22 pm

Re: issue when using esp32 and sd card in CircuitPython

by powersoft on Sat Jun 25, 2022 1:13 pm

Thanks for the last remarks. Had no idee that using PyPortal avoid a lot of problems so far.
This is my result test code thats read the time, get an image from the sd-card and show it on the screen.
By the way my project is a moon-clock with hi-res moon images. For every moon-angle (0..360) I have
a picture on the sd-card.
Thanks for your quick response han helping hand.

Cheers
Jan

Code: Select all | TOGGLE FULL SIZE
import time
import board
import displayio
import adafruit_imageload
from adafruit_pyportal import PyPortal

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

pyportal = PyPortal(status_neopixel=board.NEOPIXEL,default_bg=0x000000)

display = board.DISPLAY
print("Getting new time from internet...")
pyportal.get_local_time(secrets['timezone'])

#get local time
t = time.localtime()
print(t)
Moongroup = displayio.Group()
Moongroup.x = 112
Moongroup.y =  32
picture_index = 150
file_name = "/sd/MOON/"+str(picture_index)+".bmp"
image, palette = adafruit_imageload.load(file_name)
image_sprite = displayio.TileGrid(image, pixel_shader=palette)
Moongroup.append(image_sprite)
board.DISPLAY.show(Moongroup)
print('ready')
while True:
    pass

powersoft
 
Posts: 56
Joined: Tue Aug 30, 2011 1:01 am

Re: issue when using esp32 and sd card in CircuitPython

by dastels on Sat Jun 25, 2022 1:47 pm

Awesome!

Dave

dastels
 
Posts: 10578
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.