microSD card - clarification

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
philwilkinson40
Posts: 46
Joined: Tue Nov 14, 2017 3:11 am
Location: Perth, Australia

microSD card - clarification

Post by philwilkinson40 » Tue Feb 13, 2018 4:51 am

Please can someone clarify the correct approach/best approach to interact with a microSD card with a ESP8266.
My use case is to save sensor data as files to a microSD card. Periodically, connecting to wifi and sending the data files back to a remote MQTT broker.
I am using a Wemos D1Mini and have a shield attached. The SPI connections are standard. The latest version of micropython 1.9.3 is installed. The microSD card is a decent brand, has been checked as OK and has been formatted.

-On initial boot the microSD card is not added to the filesystem; of course.
-Adding the MicroPython driver for SD cards using SPI bus returns an error at the unmount function when trying the suggested sample code

Code: Select all

import machine, sdcard, os

sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15))
os.umount()
os.VfsFat(sd, "")
os.listdir()

Code: Select all

TypeError: function takes 1 positional arguments but 0 were given
the sdtest.py file that comes with the driver uses the PYB module so I assumed it was not suitable.

the excellent esp8266 tutorial also fails at the same point of unmounting.

The micropython UOS module for the ESP8266 does not contain mount/unmount functions so I am not sure what to do. Should I be creating a SPI object ?

User avatar
deshipu
Posts: 1332
Joined: Thu May 28, 2015 5:54 pm

Re: microSD card - clarification

Post by deshipu » Tue Feb 13, 2018 8:50 am

That's just the normal practice of MicroPython changing the API without updating the documentation or examples. Better get used to it.

User avatar
philwilkinson40
Posts: 46
Joined: Tue Nov 14, 2017 3:11 am
Location: Perth, Australia

Re: microSD card - clarification

Post by philwilkinson40 » Tue Feb 13, 2018 1:51 pm

:D ok thanks, i thought it must be the usual technical gap in my knowledge! I still have much to learn about the micropython project...

OutoftheBOTS_
Posts: 417
Joined: Mon Nov 20, 2017 10:18 am

Re: microSD card - clarification

Post by OutoftheBOTS_ » Tue Feb 13, 2018 7:49 pm

Micropython is very new and fast developing. The docs definitely trail far behind the new development that happens everyday.

But on the up side the authors of the new development r often on this forum and can answer your questions.

I haven't used this exact library but looking at the error it is expecting a argument to be given at os.umount(), the libiary that I use for the ESP32 you can pass True and it will mount the SD card and change directories to it so maybe try os.umount(True) and see what happens :)

User avatar
philwilkinson40
Posts: 46
Joined: Tue Nov 14, 2017 3:11 am
Location: Perth, Australia

Re: microSD card - clarification

Post by philwilkinson40 » Wed Feb 14, 2018 12:44 pm

sadly os.unmount(True) returns the old

Code: Select all

OSError: [Errno 22] EINVAL
...invalid argument!

I have also tried following the Adafruit tutorial and that also fails on

Code: Select all

>>> vfs=os.VfsFat(sd, "")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes 1 positional arguments but 2 were given
removing the second argument "" removes the error, but of course that was the argument that was the path on the filesystem to mount the card, so that did nothing either.

Does anyone have any experience on using SDs on ESP8266? Ideas of other approaches?

JDRBoston
Posts: 3
Joined: Mon Feb 19, 2018 9:43 pm

Re: microSD card - clarification

Post by JDRBoston » Mon Feb 19, 2018 10:03 pm

Hi Phil,

I'm working on a similar project - I am trying to use the Huzzah ESP8266 to collect data from a BME280 breakout board and an LDR, get a time-stamp from a RTC, and write the information to a SD card. I'm not sure whether this helps, but I have been able to mount an SD card using the following:

def sdmount():
import machine, sdcard, os
sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15))
os.umount('/')
vfs = os.VfsFat(sd)
os.mount(vfs, '/sd')
return

I'm using the esp8266-20170823-v1.9.2 build.

User avatar
philwilkinson40
Posts: 46
Joined: Tue Nov 14, 2017 3:11 am
Location: Perth, Australia

Re: microSD card - clarification

Post by philwilkinson40 » Fri Feb 23, 2018 8:14 am

great thanks @JDRBoston, that worked!

so, just to summarise for those visiting this thread later.

I was using a Wemos D1 Mini uSD shield shown below and the SD driver from the micropython github repository.
this allows

Code: Select all

>>> from machine import Pin, SPI
>>> import os
>>> import sdcard
>>> sd = sdcard.SDCard(SPI(1), Pin(15))
>>> os.umount('/')
>>> vfs=os.VfsFat(sd)
>>> os.mount(vfs, '/sd')
>>> os.listdir()
['sd']
>>> 
from which point you can open and write files as per usual to the uSD card. Don't forget to use the full path.

Code: Select all

>>> o = open('/sd/test.txt', 'w')
>>> o.write('Smurf')
5
>>> o.close()
>>> p = open('/sd/test.txt', 'r')
>>> p.read()
'Smurf'
>>> p.close()
>>> 
IMG_20180223_17283.jpg
IMG_20180223_17283.jpg (54.21 KiB) Viewed 1981 times

JDRBoston
Posts: 3
Joined: Mon Feb 19, 2018 9:43 pm

Re: microSD card - clarification

Post by JDRBoston » Sun Mar 18, 2018 8:28 pm

Hi Phil,

I am glad that it helped! Thank you for the example on how to write a file into the sd directory and then read it.

Do you know how one can get access to libraries and run micropython code that has been transferred to the sd card? In my case, I would like to run a program that will access libraries that will allow me to connect to i2c-sensors, get the data, and then store it on the sd card. After formatting the sd card, I backed out and tried to use ampy to transfer the files to the esp8266 but this was not successful - I don't think that ampy likes working with the esp8266 after it has setup the sd directory. Also, I tried loading a py file containing the python program onto the sd card using my computer and then getting it to run after I put the sd card in the esp8266 and setup the sd card but I was unsuccessful. The esp sees the py file but I could not load it as a library and execute the program.

I would appreciate any advice. Thank you, Jay

Name of file saved via computer onto sd card:
led_2.py

File:
# LED functions
#
# pin 0 is the big red LED


def blink(durON,durOFF,n):
from machine import Pin
import time
p0 = Pin(0, Pin.OUT)
for i in range(n):
p0.off()
time.sleep(durON)
p0.on()
time.sleep(durOFF)
return

REPL input/output after the sdcard has been loaded using the method described in the thread above:

>>> o=open('/sd/led_2.py','r')
>>> o.read()
'# LED functions\n#\n# pin 0 is the big red LED\n\n\ndef blink(durON,durOFF,n):\n\tfrom machine import Pin\n\timport time\n\tp0 = Pin(0, Pin.OUT)\n\tfor i in range(n):\n \tp0.off()\n \ttime.sleep(durON)\n \tp0.on()\n \ttime.sleep(durOFF)\n return\n\n\t\n'
>>> import led_2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'led_2'
>>> import /sd/led_2
Traceback (most recent call last):
File "<stdin>", line 1
SyntaxError: invalid syntax

User avatar
dhylands
Posts: 2881
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: microSD card - clarification

Post by dhylands » Sun Mar 18, 2018 11:50 pm

In MicroPython (and CPython) when you use import it searches the directories listed in the sys.path variable to find the modules.

So if you do something like

Code: Select all

import sys
sys.path.append('/sd')
then you should be able to use

Code: Select all

import led_2
and it will find led_2.py in the /sd directory.

JDRBoston
Posts: 3
Joined: Mon Feb 19, 2018 9:43 pm

Re: microSD card - clarification

Post by JDRBoston » Mon Mar 19, 2018 10:50 am

This worked GREAT Dave - thank you, very much! Best wishes, Jay

Post Reply