2

I am new to BBC micro:bit, so I have the following Problem:

I want to read movements and write it to a file on the m:b and after all I want to download it from there to the pc to work on it.

I wrote the file like that:

from microbit import *

with open('FileName.txt', 'w') as my_file:
    my_file.write('text to write down')

I couldn't see the file, when I used the m:b as USB- device. But when I programmed the m:b to list all files it wrote short before the file was on it.

I know, m:b has no disk operating System, so I tried to pull it with python code, I started the following python code on a Windows pc: (see: Docs to microfs)

import microfs
print microfs.ls()

But I got the error IOError: Could not find micro:bit.

The m:b is not found, I suppose. What am I doing wrong? What else could I try?

am2
  • 380
  • 5
  • 21

3 Answers3

1

Sometimes if the micro:bit cannot be found by scripts like uFlash or MicroFs it helps if you unplug the USB cable, wait a few seconds and plug it again (an additional note for Linux users, although I am aware that this is not the case for you, on Linux is also helpful to wait until the micro:bit drive has been mounted).

You are in the right track using MicroFs to access the MicroPython files, as they are in the microcontroller flash, and not accessible through the USB mass storage interface. Remember that writing a new program into the micro:bit does erase all of the flash contents, including any files your previous program might have created.

For easy of use I would recommend using the Mu editor (https://codewith.mu), as it offers you a GUI to move files in and out of the micro:bit. It's worth noting that Mu uses uFlash and MicroFs internally, so it will give you the same results as using those individual command line tools.

carlosperate
  • 594
  • 4
  • 11
  • Thank you for your answer. Th editor is nice, f.e. you can easy find Syntax Errors. But my problem is still the same: As often I diconnect and connect, as often I load programs to the m:b -> as long, as I write code or look there via USB the m:b is "on", if I try to have a look via "Files" I get the answer: "Could not find an attached BBC microbit. Please make sure the device is plugged into this Computer. The device must have MicroPython flashed onto it before the file system will work. ..." Maybe I have to Flash MicroPython onto the m:b? How to do this? – am2 Apr 18 '17 at 11:56
  • Yes, MicroPython had to be flashed into the micro:bit first for the FileSystem to be accessed. You can flash an empty script with Mu (I assume it is able to load programs into the micro:bit, correct?) or if using uFlash you can just type the `uflash` command on its own and it will do the same. – carlosperate Apr 18 '17 at 21:34
  • I'm afraid, 1st way doesn't work. I used mu, opened a new (empty) sheet and flashed it to the m:b. The LED was blinking about 10 seconds, the message 'Flashing "untitled" was flashed onto the m:b' came, the USB- device did reboot but when I tried to open "Files" (mu), the same message "Could not find ..." appeared. How can I find out, whether the MicroPython was flashed onto the m:b? How can I use uFlash? – am2 Apr 19 '17 at 14:27
1

I, too, have spent several hours searching for the answer to getting microfs and mu to read a file from the microbit, after receiving the 'can't find it' message.

I have just found a solution: update the microbit firmware.

I have flashed an empty Python file from mu to the microbit and then used the command line ufs put <path to file>\main.py to copy over code that creates a text file and displays a heart on the microbit.

Now the Files option in mu correctly displays main.py and the created text file on my microbit.

I hope this helps.

chb
  • 1,727
  • 7
  • 25
  • 47
Big G
  • 11
  • 1
1

As mentioned, you can use the command line ufs to put, get, list and delete files on the microbit.

pip install microfs --user

Then use ufs to list, delete, put and get files on to and off the microbit. github site: https://github.com/ntoll/microfs. The commands are:

ufs ls #list files on the card
ufs rm <filename> # remove a file
ufs put <filename> # write a file to the micro:bit
ufs get <filename> # get a file from the micro:bit

First of all put a blank .py file on to the micro:bit. In Linux you can create this using:

touch empty.py

Connect with microbit by double clicking on it using your file browser (e.g. Nautilus in Linux). Use mu to flash empty.py onto the microbit. Then write your Micropython code and call it main.py. Use ufs to write main.py to the micro:bit.

ufs main.py

This file will run at reset and restart. You can have your main.py file import and use other Micropython files on the micro:bit. Put these onto the micro:bit using ufs.

ufs <file to import to main.py>.py

e.g.

ufs put utilities.py

The files can be overwritten using ufs put. You can't use ufs and a repl at the same time. Now you are in a position to write and read a text file. Please find two example functions I have used for this.

def read_file(filename):
    with open(filename, 'r') as my_file:
        read_value = my_file.read()
        return read_value

def write_file(filename, value):
    with open(filename, 'w') as my_file:
        my_file.write(str(value))
        my_file.close()

The files are written to the microbit and the data remains intact after repowering the device. The data file can be seen using:

ufs ls

Then copied to your local machine using:

ufs get <filename>
Oppy
  • 2,662
  • 16
  • 22
  • ufs put and ufs get accept a second file name the target. so something like `ufs put my_script.py main.py` will copy file my_script.py from your computer to the microbit but on the microbit it will be called main.py – phil Aug 19 '18 at 20:49