PyCom basic HowTo

tags: Tools Labs basica

This document describes:

  1. the basic information required to set up a development environment, and
  2. the programming workflow for a Pycom device, in our case the LoPys.

The main goal is to gain access to the REPL. REPL stands for Read Evaluate Print Loop, and is the name given to the interactive MicroPython prompt that is accessible on the Pycom Devices. Using the REPL is by far the easiest way to test out python code and run commands.
More infos about REPL can be found here: https://docs.pycom.io/chapter/gettingstarted/programming/repl/

You must have Python3 and pip installed in your computer.

  • installing Python3 -> https://www.python.org/download/releases/3.0/

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • installing pip -> sudo apt-get install python3-pip

Installing mpy-repl-tool

mpy-repl-tool is the software tool that we will use to connect and control a LoPy (http://mpy-repl-tool.readthedocs.io/en/latest/index.html).

To install it you have to execute:

$ python3 -m pip install mpy-repl-tool

Getting started with mpy-repl-tool

Plug-in your LoPy device in a USB port and wait a few seconds until the LED starts blinking in blue.

NOTE: If you are using an OS inside a virtual machine, remember to tell the host machine to associate the device with your VM

Then execute:

$ python3 -m there detect

This command will list the serial ports and "hopefully"

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
will automatically find your LoPy board.

OK, now you are ready to start!

Some usage examples

  • to get a list of the files on the LoPy do: $ python3 -m there ls -l /flash/*
    for example:
$ python3 -m there ls -l /flash/*
----------    0    0     34B 1980-01-01 01:09:16 /flash/main.py
----------    0    0     29B 1980-01-01 01:09:18 /flash/boot.py

The filesystem has / as the root directory and the available physical drives are accessible from here. They are:

/flash – the internal flash filesystem

/sd – the SD card (if it exists)

  • read the contents of a file on the LoPy:
    python3 -m there cat /flash/somefile

  • copy multiple files from your computer to the LoPy:
    python3 -m there push *.py /flash

  • backup all the files on the LoPy to your computer
    python3 -m there pull -r \* backup

  • finally, to start a serial terminal and get access to the REPL prompt add, exec:
    python3 -m there -i

A detailed list of commands can be found here: http://mpy-repl-tool.readthedocs.io/en/latest/commandline.html

Typical Lopy device workflow

A typical workflow is the following:

  1. Create a folder on your computer
  2. Plug your device into your USB port and exec:
    $ python3 -m there detect
  3. Write your code on your computer
  4. Copy the code files from your computer to the LoPy
    $ python3 -m there push *.py /flash
  5. Start a serial terminal and get access to the REPL:
    $ python3 -m there -i
  6. Execute the code on the LoPy: e.g., >>> import file

Exercise: Let's see if it works

Execute the following code on your LoPy

import pycom import time RED = 0xFF0000 YELLOW = 0xFFFF33 GREEN = 0x007F00 OFF = 0x000000 def set_led_to(color=GREEN): pycom.heartbeat(False) # Disable the heartbeat LED pycom.rgbled(color) def flash_led_to(color=GREEN, t1=1): set_led_to(color) time.sleep(t1) set_led_to(OFF) flash_led_to(RED) flash_led_to(YELLOW) set_led_to(OFF)

Hint: save the code in a file named "test.py" for example and, in the REPL console, write import test



How to

Below you'll find some generic information about how to connect and work with LoPys.

Connect to a LoPy via WiFi

You can have access to the REPL also via a WiFi connection. Your LoPy by default works as a WiFi access point. Search an SSID that looks like lopy-wlan-XXXX; the password is www.pycom.io:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Then, you can access via telnet or via tools like Filezilla.

Accesing via telnet:

Simply execute:
telnet 192.168.4.1and use as user/password the values micro/python.

Accesing via Filezilla

If you want to interchange files with your LoPy you can use a tool like Filezilla:

  1. Connect to device's WiFi (see above):

  2. Go to top menu File > Site Manager... and use as the user/password pair the values micro/python:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Resetting

They are different ways to reset your device. Pycom devices support both soft and hard resets.

A soft reset clears the state of the MicroPython virtual machine but leaves hardware peripherals unaffected. To do a soft reset,

  • press Ctrl+D on the REPL or
  • execute:
>>> import sys
>>> sys.exit()

A hard reset is the same as performing a power cycle to the device. In order to hard reset the device, press the reset switch or run:

>>> import machine
>>> machine.reset()

If a device’s filesystem gets corrupted, it can format it by running:

>>> import os
>>> os.mkfs('/flash')

then reboot.

More details here: https://docs.pycom.io/chapter/toolsandfeatures/bootmodes.html