repl on the BBC:Microbit

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Jim.S
Posts: 80
Joined: Fri Dec 18, 2015 7:36 pm

repl on the BBC:Microbit

Post by Jim.S » Sat Aug 06, 2016 12:56 pm

I'm a bit confused how to get started with programming the BBC:Microbit in micropython like I would with the pyboard.
1. Do I need to download specific micropythyon firmware before I can get a prompt in the serial terminal? (i.e from screen /dev/ttyACM0)
2. If so, while I have installed yotta on my linux Mint box, I'm not sure I understand the instructions on "http://microbit-micropython.readthedocs ... mware.html"
3. Does rshell work with the microbit, or is there a trick to make it work?

(Currently I am trying to avoid the web based programming interface. The mu editor might be my next step if it is specifically tailored to the microbit but If I would prefer to have the interface the same as the pyboard i.e use rshell)

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

Re: repl on the BBC:Microbit

Post by deshipu » Sat Aug 06, 2016 5:15 pm

You need to flash the firnware. The simplest way to do that is to just copy the hex file onto the disk that appears when you connect your microbit. After that, you can use the repl over serial.

However, I think the Mu editor is the simplest way to use the microbit with micropython, as it does it all for you, and also lets you upload files.

Jim.S
Posts: 80
Joined: Fri Dec 18, 2015 7:36 pm

Re: repl on the BBC:Microbit

Post by Jim.S » Sat Aug 06, 2016 6:40 pm

That's what I have discovered but I cant find a rebuilt microbit firware anywhere

I have discovered the following
1. if you go to the "https://www.microbit.co.uk/" you can use the on line editor to create python, compile it to to a .hex file, then drag the file to the MICROBIT directory/folder and it runs. This is good for testing that python works the way you think it should. [tick!]

2. I'm assuming that I can do something similar with the micropython runtime/interpreter, i.e. compile it to a hex file and then just drag it to the MICROBIT directory? (There is nothing that suggests that I have to use DFU mode)

3. if so, the instructions for creating the run time on a linux box using yotta ( http://microbit-micropython.readthedocs ... ation.html ) are not clear and explicit. The first issue that I had was figuring out that I had to download the source from git hub (https://github.com/bbcmicrobit/micropython)... then install "makeupsafe" (which is probably specific to my Mint 17.3 system..) Then I had to create an account with arm embed to allow the build to progress. (Really irritating!!!!!) .. Currently I am failing with something to with the nrf51822 stuff...
[100/291] Building CXX object ym/ble-nrf51822/source/CMakeFiles/ble...t/micropython-master/yotta_modules/ble-nrf51822/source/nRF5xn.cpp.o
ninja: build stopped: subcommand failed.
error: command ['ninja'] failed
Any ideas on where to go next? I cant help feeling that this is too complex and that have overlooked something obvious. (After all this hardware is aimed at 11-12 children....)

Jim.S
Posts: 80
Joined: Fri Dec 18, 2015 7:36 pm

Re: repl on the BBC:Microbit

Post by Jim.S » Tue Aug 09, 2016 7:55 pm

Getting there! There are subtle differences between the pyboard and the microbit that make the transition between them confusing.

Firmware
To flash the firmware on the pyboard you download the latest (from http://micropython.org/download/), put the board into DFU mode (using a hardware jumper) and flash it using the appropriate dfu programming software (dfu-util pydfu..)

To flash the firmware on the BBC Microbit you just have to copy a firmware .hex file onto the board. There seem to be many ways to get the firmware hex, the simplest way I found was to go to https://www.microbit.co.uk/create-code and create a python program with only "from microbit import *" (but you probably do not need that line), then download that .hex file onto the microbit.

Yotta
There are instructions here (https://github.com/bbcmicrobit/micropython and http://microbit-micropython.readthedocs ... mware.html) on how to use yotta to build firmware using the source from the git repository. But I can't get this to work using Linux Mint 17.3. (see my post above) This might be a problem with certain flavours of linux because there is another thread on the microbit@python.org forum with a similar failure mode on Xubunu16.04

Repl
To get the REPL prompt on the pyboard from linux you type "screen /dev/ttyACM0"

but to get the REPL on the microbit you must add the baud rate, i.e you must type "screen /dev/ttyACM0 115200"

Rshell
Cant get rshell to work yet, but cant see any reason why it wouldn’t work

Mu
As Deshipu wrote above, mu is probably the neatest solution. I haven’t tried it yet and I'm interested to know if works with the pyboard because I'd like the same system for both boards.

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

Re: repl on the BBC:Microbit

Post by deshipu » Tue Aug 09, 2016 9:33 pm

There is also uflash utility (https://github.com/ntoll/uflash and https://pypi.python.org/pypi/uflash/1.0.1) which you can install with pip and use to flash python code on the micro:bit from the command line (or just flash naked micropython interpreter). Both Mu and uflash will give you the latest and greatest version (including the radio and speech modules), whereas the online tools give you a somewhat older version (they are maintained by the BBC and they are slow to update).

Jim.S
Posts: 80
Joined: Fri Dec 18, 2015 7:36 pm

Re: repl on the BBC:Microbit

Post by Jim.S » Wed Aug 10, 2016 9:16 pm

I understand how to use uFlash to get a REPL or to flash python scripts onto the board, but how can I get a create firmware on the microbit that will run a "main.py" script that is just copied onto the board's memory rather than flashed with uFlash (i.e using the command uflash main.py)

Is there a microbit equivalent of the pyb.main('main.py')

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

Re: repl on the BBC:Microbit

Post by deshipu » Thu Aug 11, 2016 7:14 am

I *think* that main.py is getting executed automatically on startup if there is no program flashed with the firmware -- at least that's what I remember seeing somewhere. Failing that, you can always import it...

Jim.S
Posts: 80
Joined: Fri Dec 18, 2015 7:36 pm

Re: repl on the BBC:Microbit

Post by Jim.S » Thu Aug 11, 2016 7:50 pm

The behaviour of the microbit seems to be VERY different from the pyboard.

If I flash the microbit with the only the command "uflash" (i.e without a python script as an option) it flashes micropython.hex to the pyboard. I then copy a the following main.py script to the board

Code: Select all

from microbit import *
display.show(Image.HAPPY)
This gives a REPL prompt but does not execute main.py on a soft reset (i.e CTRL-D or using the microbit reset switch)

If I do a hard reset by ejecting the microbit and reconnecting it to the usb, main.py has been deleted from the microbit memory!

This isn’t really a problem for software development but does seem to be that the approach for the microbit might be different from the pyboard

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

Re: repl on the BBC:Microbit

Post by deshipu » Thu Aug 11, 2016 10:57 pm

Jim.S wrote: If I do a hard reset by ejecting the microbit and reconnecting it to the usb, main.py has been deleted from the microbit memory!
That shouldn't happen. Are you sure it was successfully copied in there in the first place?
Only re-flashing a hex should delete the files you uploaded.

Jim.S
Posts: 80
Joined: Fri Dec 18, 2015 7:36 pm

Re: repl on the BBC:Microbit

Post by Jim.S » Fri Aug 12, 2016 7:21 pm

I tried the following experiment
uflash main.py
Flashing Python to: /media/skelton/MICROBIT/micropython.hex
<result happy face on microbit>

uflash
Flashing Python to: /media/skelton/MICROBIT/micropython.hex
<result nothing on microbit LED array>

cd /media/xxx/MICROBIT/
$ ls
AUTO_RST.CFG DETAILS.TXT MICROBIT.HTM

cp main.py /media/xxx/MICROBIT/
ls /media/xxx/MICROBIT/
AUTO_RST.CFG DETAILS.TXT main.py MICROBIT.HTM

I use eject from the Linux GUI to unmount the microbit, I disconnect the usb lead then reconnect

ls /media/skelton/MICROBIT/
AUTO_RST.CFG DETAILS.TXT MICROBIT.HTM

<main.py has vanished>
I then check the version of microbit firmware
screen /dev/ttyACM0 115200
>>>
soft reboot
MicroPython v1.7-9-gbe020eb on 2016-04-18; micro:bit with nRF51822
Type "help()" for more information.
>>>

I tried the same thing again disconnecting the drive using sudo umount and main.py vanishes again!
repeated a third time with the same result

Any ideas?

Post Reply