Micropython Setup - SamsungResearchUK-IoT-Meetup/multimode_sensor_platform GitHub Wiki

Micropython Setup

This describes the tools we use in using micropython. They are not needed to run the software or make the program work, but will make your life easier when developing.

There are many guides on getting started for Micropython. Unfortunately google dominates the listings with the 'ESP and Micropython'. The place to get started with pyboard D, or the default pyboard is here. The guide tells you just about everything. However, to save a bit of pain I'm going to mention about adding your self to a 'group' permissions on Linux. In a nut shell, you will be accessing the device via USB. Using the ttyACM0 or ttyUSB0 device to save python files to your microcontroller or run commands. This means that you need to give the user you are logged in as - permissions to be part of the dialout group since tty devices were originally used as modems. To do this run:

$/> sudo usermod -a -G dialout $USER

You can check you are part of the group by logging out and in then typing:


$/> groups
    nherriot adm dialout cdrom sudo dip plugdev lpadmin sambashare docker

Once you have plugged in your board and got your hello world application running you may get frustrated in continually copying a file over, pressing the restart button on the board and attaching your repl. It takes time! Even though this process is a huge step advantage over most other environments there is a way to do this in a more slick way.

RSHELL Environment

The problem is we need to be able to put new code onto the device and have the code interpreted immediately while the system is still running. We want a way to have a python repl attached to our microcontroller directly and cut'n'paste new code directly onto the device. To do this we will:

  1. Install a new 'shell' which allows us to interact with the board directly. Called 'rshell' and used to interact with our new board quickly.
  2. Copy files to the board, then use the python functions on the board via the repl.
  3. Be able to paste chunks of code into the repl and use the functions that we have newly added.
  4. Re-read files via the command line, but still have our environment usable and running.

Install rshell

   $/> pip install rshell

Copying Files Via rshell

Rshell will provide you with a terminal that mounts the flash drive and SD card on the microcontroller as if it was on your machine. Your flash drive will already be accessible as a USB Mass Storage Device (MSB), but this allows you to use copy and the repl interactively. To start make sure you have already activated your virtual envorionment then do:

   $/> rshell
   (multimode_sensor_platform) nherriot@Zenbook-UX32A$ rshellConnecting to /dev/ttyACM0 (buffer-size 512)...
   Trying to connect to REPL  connected
   Testing if sys.stdin.buffer exists ... Y
   Retrieving root directories ... /flash/
   Setting time ... Apr 23, 2019 14:08:34
   Evaluating board_name ... pyboard
   Retrieving time epoch ... Jan 01, 2000
   Welcome to rshell. Use Control-D (or the exit command) to exit rshell.

Once you see this output you are connected via rshell. You can view files on the pyboard using the ** ls ** command.

   $.> ls -l /flash/
   1727 Apr 17 15:27 README.md
   698 Mar  3 16:45 README.txt
   366 Dec 31 2014  boot.py
   21 Dec 31 1999  hello.py
   34 Dec 31 2014  main.py
   2786 Dec 31 2014  pybcdc.inf
   3721 Sep 20 2018  tile_one-orig.py
   6029 Dec 31 1999  tile_one.py

To copy a file to the board you can simply use the ** cp ** command. I've coped a hello.py file to my board:

   $/> cp hello.py /flash/

My python file looked like:

print("hello world 2")

def hellome(name):

    print("***Test program started***")
    print("the name is: ", name)

Now we can start the repl and use the file like so:

   $/> repl
   Entering REPL. Use Control-X to exit.
   >
   MicroPython v1.9.4-925-g8edf1205f-dirty on 2019-01-16; PYBD_SF2 with STM32F722IEK
   Type "help()" for more information.
   >>>

To prove our file exists and can be used we can simply import it via the repl like so:

   >>> from hello import hellome
   >>> hellome('Nick')
   ***Test program started***
   the name is:  Nick
   >>>

To exit the repl use CTRL+X.

Pasting code via repl

You can override a file dynamically via the repl to test out your changes. Consider we want to change our python function 'hellome'. We want it to print out ** 2019 Test program started **. We can do this with an interactive ** copy ** mode in the repl. By typing CTRL+e in the repl it will be put into a copy text editor mode. Below we hit CTRL+e and paste in our new code. To exit and run the code hit CTRL+d.

   >>> 
   paste mode; Ctrl-C to cancel, Ctrl-D to finish
   === def hellome(name):
   === 
   ===     print("*** 2019 Test program started***")
   ===     print("the name is: ", name)
   === 
   >>> 

Now we are in a position to use the new function.

   >>> hellome('Nick')
   *** 2019 Test program started***
   the name is:  Nick
   >>>

Re-reading code and running without pressing reset

While in the repl simply hit CTRL+d, which will cause the microcontroller to do a soft reset. Pulling in the new files. What you can do in this instance is have to rshell's running. One can have the repl up, while the other can be used to copy new files across.

   >>> 
   PYB: sync filesystems
   PYB: soft reboot
   MicroPython v1.9.4-925-g8edf1205f-dirty on 2019-01-16; PYBD_SF2 with STM32F722IEK
   Type "help()" for more information.
   >>>