genFile on SDcard ?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
jym92
Posts: 3
Joined: Fri Sep 11, 2015 2:30 pm

genFile on SDcard ?

Post by jym92 » Fri Sep 11, 2015 2:52 pm

Hi everybody !

I am new guy in this community, I am an engineer in embedded electronic and I work principaly in Python langage.


Now, I ask my question :

I have already work on module TELIT or on directly on IDLE. I want to use the SD card like a safe data bank.

To explain my project, I want to combine the pyboard with an inertial central in i2C and to save inertial informations on the SDcard under a text file, after if it's possible, i want to extract this text file by usb ...

Do you think it's possible ?


Normally, I use this typical command to create a file : genFile.write(str(a)+','+str(b)+'\n')

But I am not sure that's work and I don't know where my file was saved ....

How I can saved my text file on sd card ?


If you have any remarks on my project, I listen it with a great pleasure ! :)




Thanks to help me,

Ps: sorry for my bad english but I am a french guy from a small town ....

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

Re: genFile on SDcard ?

Post by dhylands » Fri Sep 11, 2015 4:34 pm

When you open a file you can specify a path. /flash is the path which gets you to the root of the internal flash drive, and /sd gets you to the root of the sdcard. So if you do something like this:

Code: Select all

>>> file = open('/sd/myfile.txt', 'wb')
>>> file.write('This is a test\n')
15
>>> file.close()
then it will create 'myfile.txt' on the sdcard. You can examine the contents by doing:

Code: Select all

>>> file = open('/sd/myfile.txt', 'rb')
>>> lines = file.readall()
>>> lines
b'This is a test\n'
>>> file.close()

jym92
Posts: 3
Joined: Fri Sep 11, 2015 2:30 pm

Re: genFile on SDcard ?

Post by jym92 » Fri Sep 11, 2015 8:17 pm

Thanks for your help,

I writte this code in main.py normally it work but the script don't start ...

Code: Select all

# --------------- importation des librairies Micropython nécéssaires :
import pyb

sw = pyb.Switch()
led = pyb.LED(4)
sw_was_pressed = False

# --------- initialisation des classes et des périphériques ----------
sd = pyb.SD(('GP15', 8, 'GP10', 6, 'GP11', 6))
sd.mount()

# main.py -- put your code here!
# -------------  début du prog principal ------------------------------
# ----- init -----------

file = open('/sd/myfile.txt', 'wb')

while (1):
    for i in range(3):
        led.on()
        pyb.delay(100)
        led.off()
        for i in range(9):
            if sw():
                sw_was_pressed = True
                break
            pyb.delay(100)
            if sw_was_pressed:
                file.write('accX ; accY ; accZ \n')
                accel = pyb.Accel()
                file.write(str(accel))
                file.close()
                break
            if not sw_was_pressed:
                print("Switch wasn't pressed - running main program")
                import main_program
            else:
                print("Switch was pressed - dropping into REPL")
        
I just save this without the sd card, I insert it and I push on reset and after on sw but this don't work...

The problem was in my code or in my method ?

thanks,

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

Re: genFile on SDcard ?

Post by dhylands » Fri Sep 11, 2015 8:39 pm

To debug this type of thing, I normally recommend that you don't call main.py until you have everything working.

Instead call it myfile.py (or something) and then boot to the REPL. From the REPL then do:

Code: Select all

import myfile
This way you'll see any errors. Is this running on the pyboard? If so, then mounting the sdcard isn't required, its done automatically, and from what I can tell pyb.SD() takes no arguments (unless maybe you have some really old firmware)

jym92
Posts: 3
Joined: Fri Sep 11, 2015 2:30 pm

Re: genFile on SDcard ?

Post by jym92 » Sun Sep 13, 2015 1:08 pm

Hi,

I tried to use the first code of introduction, It work's great !

Code: Select all

# --------------- importation des librairies Micropython nécéssaires :

import pyb

switch = pyb.Switch()
leds = [pyb.LED(i+1) for i in range(4)]
accel = pyb.Accel()


i = 0
# --------- initialisation des classes et des périphériques ----------
#sd = pyb.SD(('GP15', 8, 'GP10', 6, 'GP11', 6))
# sd.mount()

# main.py -- put your code here!
# -------------  début du prog principal ------------------------------
# ----- init -----------

#file = open('/myfile.txt', 'wb')
#file.write('accX ; accY ; accZ \n')
#file.write('This is a test\n')
#file.close()

while not switch():
    x = accel.x()
    y = accel.y()
    z = accel.z()
    i = (i + (1 if y > 0 else -1)) % len(leds)
    leds[i].toggle()
    #file.write( str(x)+' ;' + str(y)+' ;' +str(z)+'  \n')
    
    
    pyb.delay(10 * max(1, 30 - abs(y)))
    
#file.close()
But when I just discomment four lines to write directly on the memory the board without sd card, 2 leds toggle 2 times and stop... I don't understand for what ?

Code: Select all

# --------------- importation des librairies Micropython nécéssaires :

import pyb

switch = pyb.Switch()
leds = [pyb.LED(i+1) for i in range(4)]
accel = pyb.Accel()


i = 0
# --------- initialisation des classes et des périphériques ----------
#sd = pyb.SD(('GP15', 8, 'GP10', 6, 'GP11', 6))
# sd.mount()

# main.py -- put your code here!
# -------------  début du prog principal ------------------------------
# ----- init -----------

file = open('/myfile.txt', 'wb')
file.write('accX ; accY ; accZ \n')
file.write('This is a test\n')
file.close()

while not switch():
    x = accel.x()
    y = accel.y()
    z = accel.z()
    i = (i + (1 if y > 0 else -1)) % len(leds)
    leds[i].toggle()
    #file.write( str(x)+' ;' + str(y)+' ;' +str(z)+'  \n')
    
    
    pyb.delay(10 * max(1, 30 - abs(y)))
    
#file.close()
Remarks: 1 - I prefer to write directly on the main.py to this project and to use 2 or 3 threads....
2 - Sometimes, when I use the button reset when the card was connected to the pc,
pyboard clear my main.pyprogram!
==> how I can block that ?

Thanks,

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

Re: genFile on SDcard ?

Post by dhylands » Sun Sep 13, 2015 3:34 pm

There is an issue using USB Mass Storage. The protocol was designed to only allow exclusive access, but the way its implemented in the pyboard, both the pyboard and the host think they have exclusive access to the flash.

So I tend to disabled USB Mass Storage by putting

Code: Select all

pyb.usb_mode('CDC') # act as a serial only
in my boot.py and then use rshell to copy files into and out of the internal flash drive.

You can find rshell here: https://github.com/dhylands/upy-shell/t ... ter/rshell

Note that pressing the switch while the pyboard boots will put it into a special boot mode. See: http://docs.micropython.org/en/latest/p ... boot-modes

So if you want to use the switch when your script starts, you may want to flash a LED to indicate that your code is running and then add a small delay to allow you to press the switch to start (or something)

Post Reply