Logging data to file on SD-card

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
Turbinenreiter
Posts: 288
Joined: Sun May 04, 2014 8:54 am

Logging data to file on SD-card

Post by Turbinenreiter » Sun May 04, 2014 8:58 am

... is not working?

i tried this example: https://github.com/micropython/micropyt ... ccellog.py
obviously some stuff has changed (time, accel), and i fixed that, so now it looks like that:

Code: Select all

import pyb

accel = pyb.Accel()
f = open('motion.dat', 'w')                 # open the file for writing

for i in range(60):                         # loop 60 times
    time = pyb.millis()                       # get the current time
    x, y, z = accel.filtered_xyz()                     # get the accelerometer data

    # write time and x,y,z values to the file
    f.write('{} {} {} {}\n'.format(time, x, y, z))
    pyb.delay(1000)                         # wait 1000 ms = 1 second

f.close()                                   # close the file
but there is no file showing up on the SD-card. only file on SD-card is main.py.
any ideas?


BobM
Posts: 3
Joined: Thu Oct 30, 2014 7:16 pm

Re: Logging data to file on SD-card

Post by BobM » Thu Oct 30, 2014 8:59 pm

A belated update from a newbie.
In spite of what's been written elsewhere, you can write a file to the SD card when it's in mass storage mode. You won't see the file in the host computer's view of the SD card until after the next hard reset. (No, I don't know why.)
What my experiments have shown is that writing to a file in a sub-directory of /sd works absolutely fine. Writing to /sd always gives me an empty file.
I used

Code: Select all

import os
os.mkdir('log')
at the REPL prompt. The subdirectory showed up in Windows Explorer (my host) after a reset.
I modified your code to

Code: Select all

# main.py -- put your code here!
import pyb
accel = pyb.Accel()
l = pyb.LED(3)
l.on()
f = open('log/motion.dat', 'w')                 # open the file for writing

for i in range(5):                         # loop 5 times only for testing
	time = pyb.millis()                       # get the current time
	x, y, z = accel.filtered_xyz()                     # get the accelerometer data

	# write time and x,y,z values to the file
	f.write('{} {} {} {}\n'.format(time, x, y, z))
	pyb.delay(1000)                         # wait 1000 ms = 1 second

f.close()                                   # close the file
l.off()
And /sd/log/motion.dat showed up and was not empty.

Code: Select all

287 4 0 22
1292 7 1 44
2293 12 2 66
3294 16 2 88
4294 14 2 88
All interesting, and puzzling.
Bob, Yateley UK

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

Re: Logging data to file on SD-card

Post by dhylands » Fri Oct 31, 2014 12:15 am

What's happening (when pyboard and host are both seeing /sd) is that the host thinks it has exclusive access to the media.

So it will read in stuff from the disk, probably the root directory, and cache it in memory.

The pyboard then comes along, and it also thinks that it has exclusive access to the file, so it goes ahead and reads and caches the data as well.

Now, if the pyboard goes and adds something to the root directory, and then after that you also added something to the root directory on the host, then the host changes would probably just wipe out the changes done by the pyboard. Whoever writes last will wind up winning.

So, if you're planning on using the sdcard to log data to, you should probably put it into CDC+HID mode.

You might want to add a switch and have your boot.py look at the switch, and if its in logging mode, then you set the usb mode to CDC+HID and you go ahead and write your logging data. If its in "non-logging" mode, then you set it up for CDC+UMS mode, and you don't write your logging data.

Then you shouldn't experience any corruption.

Turbinenreiter
Posts: 288
Joined: Sun May 04, 2014 8:54 am

Re: Logging data to file on SD-card

Post by Turbinenreiter » Fri Oct 31, 2014 7:23 am

There is an sddatalogger in the examples if the repo and an article in the wiki which implements what dhylands described.

BobM
Posts: 3
Joined: Thu Oct 30, 2014 7:16 pm

Re: Logging data to file on SD-card

Post by BobM » Fri Oct 31, 2014 8:46 pm

Thanks, both, very helpful.
I think that I misinterpreted some other forum comments to mean that you couldn't successfully write to the SD card in CDC+MSC mode.
For getting to grips with the beast, I find logging to a subdirectory useful and I treat the subdirectory as read-only anyway from the host. Thus I'm quite pleased to find that it works.
Bob, Yateley UK

Post Reply