Microbit Notes01:Connect/debug: Difference between revisions

From artserver wiki
m (Text replacement - "HackPact" to "Code Notes")
No edit summary
Line 6: Line 6:
* use the python interactive console
* use the python interactive console


=== find out the connection ===
== find out the connection ==


The board should appear under <code>/dev</code> as <code>tty???</code>, to find out after connecting the board run: <code>sudo dmesg| tail | grep tty</code>
The board should appear under <code>/dev</code> as <code>tty???</code>, to find out after connecting the board run: <code>sudo dmesg| tail | grep tty</code>
Line 24: Line 24:
Notice that <code>/dev/ttyACM3</code> '''is the only device with a baudrate of above 9600. That is the one micropython is running'''.
Notice that <code>/dev/ttyACM3</code> '''is the only device with a baudrate of above 9600. That is the one micropython is running'''.


=== connect ===
== connect with GNU screen ==


I will connect using GNU screen (terminal multiplexer) is able to connect to a serial port, but you can use [https://wiki.archlinux.org/index.php/Working_with_the_serial_console#Command_line other alternatives]
I will connect using GNU screen (terminal multiplexer) is able to connect to a serial port, but you can use [https://wiki.archlinux.org/index.php/Working_with_the_serial_console#Command_line other alternatives]
Line 109: Line 109:


If you didn't disappear within the microprocessor, you should be back in you machine and the microbit should have resume to running the loaded script :)
If you didn't disappear within the microprocessor, you should be back in you machine and the microbit should have resume to running the loaded script :)
==connect with ampy==
Failed so far to use ampy with Microbit.
Successful with ESP8266 boards
[https://github.com/pycampers/ampy ampy] is Adafruit MicroPython Tool - Utility to interact with a MicroPython board over a serial connection.
<code>
ampy
Usage: ampy [OPTIONS] COMMAND [ARGS]...
  ampy - Adafruit MicroPython Tool
  Ampy is a tool to control MicroPython boards over a serial connection.
  Using ampy you can manipulate files on the board's internal filesystem and
  even run scripts.
Options:
  -p, --port PORT    Name of serial port for connected board.  Can optionally
                    specify with AMPY_PORT environment variable.  [required]
  -b, --baud BAUD    Baud rate for the serial connection (default 115200).
                    Can optionally specify with AMPY_BAUD environment
                    variable.
  -d, --delay DELAY  Delay in seconds before entering RAW MODE (default 0).
                    Can optionally specify with AMPY_DELAY environment
                    variable.
  --version          Show the version and exit.
  --help            Show this message and exit.
Commands:
  get    Retrieve a file from the board.
  ls    List contents of a directory on the board.
  mkdir  Create a directory on the board.
  put    Put a file or folder and its contents on the board.
  reset  Perform soft reset/reboot of the board.
  rm    Remove a file from the board.
  rmdir  Forcefully remove a folder and all its children from the board.
  run    Run a script and print its output.
</code>


[[Section::Code Notes]]
[[Section::Code Notes]]
[[Date::2019]]
[[Date::2019]]

Revision as of 14:30, 1 September 2019

connect to the board:

Using a serial connection you can connect to the board to:

  • see print messages of running script
  • use the python interactive console

find out the connection

The board should appear under /dev as tty???, to find out after connecting the board run: sudo dmesg| tail | grep tty

In my system (Debian stretch) get: cdc_acm 1-1.2:1.1: ttyACM3: USB ACM device, so ttyACM3 but is also possibe it appears as ttyUSB under other OSs/versions.

If you ls /dev you see there is a number of /dev/ACM?. dmesg told us it was /dev/ttyACM3, but to be sure we can print the terminal line settings of /dev/ttyACM3 and the other /dev/ttyACM?

stty < /dev/ttyACM0 speed 9600 baud; line = 0;

stty < /dev/ttyACM1 speed 9600 baud; line = 0;

stty < /dev/ttyACM2 speed 9600 baud; line = 0;

stty < /dev/ttyACM3 speed 115200 baud; line = 0;

Notice that /dev/ttyACM3 is the only device with a baudrate of above 9600. That is the one micropython is running.

connect with GNU screen

I will connect using GNU screen (terminal multiplexer) is able to connect to a serial port, but you can use other alternatives

screen /dev/ttyACM3 115200

If a script is running you will see the print messages of the script loaded onto the microbit, being displayed. Useful for debugging.

If no script is loaded, or if you interrupt the running script with Ctl-c you will start python interactive shell from which you can control the microbit or read the help on the microbit modules. Here are a few examples

>>> help()      
Welcome to MicroPython on the micro:bit!

Try these commands:
  display.scroll('Hello')
  running_time()
  sleep(1000)
  button_a.is_pressed()
What do these commands do? Can you improve them? HINT: use the up and down
arrow keys to get your command history. Press the TAB key to auto-complete
unfinished words (so 'di' becomes 'display' after you press TAB). These
tricks save a lot of typing and look cool!

Explore:
Type 'help(something)' to find out about it. Type 'dir(something)' to see what
it can do. Type 'dir()' to see what stuff is available. For goodness sake,
don't type 'import this'.

Control commands:
  CTRL-C        -- stop a running program
  CTRL-D        -- on a blank line, do a soft reset of the micro:bit
  CTRL-E        -- enter paste mode, turning off auto-indent

For a list of available modules, type help('modules')

For more information about Python, visit: http://python.org/
To find out about MicroPython, visit: http://micropython.org/
Python/micro:bit documentation is here: https://microbit-micropython.readthedocs.io/


>>> help('modules')
__main__          love              os                time
antigravity       machine           radio             ucollections
array             math              random            ustruct
audio             microbit          speech            utime
builtins          micropython       struct
collections       music             sys
gc                neopixel          this
Plus any modules on the filesystem

>>> import machine

>>> dir(machine)
['__name__', 'unique_id', 'reset', 'freq', 'disable_irq', 'enable_irq', 'mem8', 'mem16', 'mem32', 'time_pulse_us']

>>> machine.unique_id()
b'\xc3Xp56\x88\x06\xb8'


>>> import microbit
>>> help(microbit)
Useful stuff to control the micro:bit hardware.
>>> dir(microbit) 
['__name__', 'Image', 'display', 'button_a', 'button_b', 'accelerometer', 'compass', 'i2c', 'uart', 'spi', 'reset', 'sleep', 'running_time', 'panic', 'temperature', 'pin0', 'pin1', 'pin2', 'pin3', 'pin4', 'pin5', 'pin6', 'pin7', 'pin8', 'pin9', 'pin10', 'pin11', 'pin12', 'pin13', 'pin14', 'pin15', 'pin16', 'pin19', 'pin20']

>>> temperature()
29

>>> button_a.is_pressed()
False

(press button_a)

>>> button_a.is_pressed()
True

>>> display.show(Image.PACMAN)

After playing around, exit the Python console byt runnig GNU-screen shortcut for killing the window:

Ctl-a + k and to the question Really kill this window [y/n] answer y

If you didn't disappear within the microprocessor, you should be back in you machine and the microbit should have resume to running the loaded script :)


connect with ampy

Failed so far to use ampy with Microbit.

Successful with ESP8266 boards

ampy is Adafruit MicroPython Tool - Utility to interact with a MicroPython board over a serial connection.

ampy Usage: ampy [OPTIONS] COMMAND [ARGS]...

 ampy - Adafruit MicroPython Tool
 Ampy is a tool to control MicroPython boards over a serial connection.
 Using ampy you can manipulate files on the board's internal filesystem and
 even run scripts.

Options:

 -p, --port PORT    Name of serial port for connected board.  Can optionally
                    specify with AMPY_PORT environment variable.  [required]
 -b, --baud BAUD    Baud rate for the serial connection (default 115200).
                    Can optionally specify with AMPY_BAUD environment
                    variable.
 -d, --delay DELAY  Delay in seconds before entering RAW MODE (default 0).
                    Can optionally specify with AMPY_DELAY environment
                    variable.
 --version          Show the version and exit.
 --help             Show this message and exit.

Commands:

 get    Retrieve a file from the board.
 ls     List contents of a directory on the board.
 mkdir  Create a directory on the board.
 put    Put a file or folder and its contents on the board.
 reset  Perform soft reset/reboot of the board.
 rm     Remove a file from the board.
 rmdir  Forcefully remove a folder and all its children from the board.
 run    Run a script and print its output.

Code Notes 2019

Code Notes +
Date"Date" is a type and predefined property provided by Semantic MediaWiki to represent date values.
2019 +