The following code was tested using rshell and repl and was found to work when the three file statements at the end were commented out and replaced by print(datum). When b2 was pressed, the current value of i together with the date and time was printed to screen at the python prompt. This could be done repeatedly, except in rare cases when the usb port spontaneously disconnected (see below). I am using pyboard 1.1 and micropython 1.11 and ubuntu 18.04.
def sd_seq(start =1, end = 100, step = 1, gap = 500):
import pyb
from pyb import Pin
m = Pin('X1', Pin.OPEN_DRAIN)
f = Pin('X2', Pin.OPEN_DRAIN)
m.high()
f.low()
b1 = Pin('X3', Pin.IN, Pin.PULL_UP)
b2 = Pin('X4', Pin.IN, Pin.PULL_UP)
b3 = Pin('X5', Pin.IN, Pin.PULL_UP)
#f = open(diary', 'a')
while True:
if not b1.value():
for i in range(start, end, step):
pyb.delay(gap)
m.low()
f.high()
pyb.delay(i)
m.high()
f.low()
if not b3.value():
break
else:
if not b2.value():
a,b,c,d,e,g,h,j = pyb.RTC().datetime()
datum = '{}msec {},{},{},{},{},{},{},{}\n'.format(i,a,b,c,d,e,g,h,j)
f = open('/sd/log/diary', 'a')
f.write(datum)
f.close()
break
It has not worked when the print statement was replaced by the three indicated file-related statements and the file was placed on the microSD card together with the following files.
The boot.py file was as follows:
# boot.py -- run on boot-up
# can run arbitrary Python, but best to keep it minimal
import machine
import pyb
pyb.main('main.py') # main script to run after this one
#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device
#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse
The main.py file was as follows:
# main.py -- put your code here!
import sd_flseq1
sd_flseq1.sd_seq()
With these files on the microSD card, pressing b2 stops the for-loop and enters a single datum in the target file. After this, the board becomes no longer responsive to button pushes. I used a subdirectory 'log' because in another posting, one user reported that this was either useful or necessary for logging data to microSD card.
In the short term, we need to log data to the microSD card, so our first priority is to get the above code working. However, a long term goal is to control the pyboard from a desktop application with a GUI that will enable the user to select the timing parameters for the sd_seq() function. Since we need a robust system, the spontaneous losses of the usb connection will be problematic. I am wondering if others have experienced this problem and have hopefully found a solution. Online, I see reports of this with Ubuntu and theories about hardware being noncompliant with standards. I would therefore be interested in knowing what hardware and software combinations have been found by others to work.
Problem with logging data to sd card - loss of usb connection in repl mode
Re: Problem with logging data to sd card - loss of usb connection in repl mode
If you're logging to a file, you should make sure that you put in your boot.py file (i.e. you need to disable MSC). If you don't then the host will corrupt your sd filesystem.
Your code does an open of /sd/log/diary. Did you create the log directory on the sd card already? If not, then the open will fail. open doesn't create directories automatically.
Code: Select all
pyb.usb_mode('VCP')
Your code does an open of /sd/log/diary. Did you create the log directory on the sd card already? If not, then the open will fail. open doesn't create directories automatically.
Re: Problem with logging data to sd card - loss of usb connection in repl mode
Thanks very much for your reply! I finally got it to work. As a Smalltalker just learning Python, I was making multiple mistakes in addition to those you pointed out. This became clear when I started using Visual Studio code.dhylands wrote: ↑Wed Dec 11, 2019 7:01 pmIf you're logging to a file, you should make sure that you putin your boot.py file (i.e. you need to disable MSC). If you don't then the host will corrupt your sd filesystem.Code: Select all
pyb.usb_mode('VCP')
Your code does an open of /sd/log/diary. Did you create the log directory on the sd card already? If not, then the open will fail. open doesn't create directories automatically.
Also, the frequent autodisconnects when using repl mode have mysteriously disappeared.
Finally, thanks for giving us rshell!
lclev