Try   HackMD

Getting started with IoT devices

tags: Tools Labs basica

Code referenced in this doc can be found here: https://github.com/pmanzoni/hackmd_code

The HW we use is based on a LoPy4 with a PySense board.

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 →

LoPys are based on MicroPython. MicroPython is a full Python compiler and runtime that runs on the bare-metal. You get an interactive prompt, called the REPL ("Read Evaluate Print Loop"), to execute commands immediately, along with the ability to run and import scripts from the built-in filesystem. The REPL has history, tab completion, auto-indent and paste mode.
More infos about REPL can be found here: https://docs.pycom.io/pymakr/toolsfeatures/

To connect to the LoPy's REPL you will use a Python tool called mpy-repl-tool. You will learn how to use it in this section.

Now, plug-in your LoPy device in a USB port and wait a few seconds until the LED starts blinking in blue. Before, you'll probably be asked whether you want to use the device in the VM

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 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. If everything is fine you should see something like:

/dev/ttyACM0 - Pysense - Pysense

To get access to the REPL prompt, write:

  • python3 -m there -i

You'll get the >>> REPL prompt (see below). To exit from the REPL you have to type Ctrl+]

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 →

The basic commands:

These are the basic commands that you will have to use:

  • getting a list of the files on the LoPy:
$ 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 is inserted, obviously)
  • coping file _filename_ from your computer to the LoPy:
    python3 -m there push _filename_ /flash

  • wildcards can be used to copy multiple files from your computer to the LoPy. For example:
    python3 -m there push *.py /flash

  • reading the contents of file _filename_ on the LoPy:
    python3 -m there cat /flash/_filename_

  • removing file _filename_ on the LoPy:
    python3 -m there rm /flash/_filename_
    wildcards can be used to delete multiple files.

A detailed list of commands can be found here: mpy-repl-tool.readthedocs

BTW; it can be useful to create the alias:
$ alias pyt="python3 -m there"
so not to type too much

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 →

Typical Lopy device workflow

A typical workflow is the following:

  1. Write the micropython code in your computer
  2. Copy the code file or files from your computer to the LoPy, e.g.,:
    $ python3 -m there push *.py /flash
  3. Start a serial terminal and get access to the REPL:
    $ python3 -m there -i
  4. Execute the code in file _filename_.py on the LoPy, with:
  • >>> import _filename_

Accessing the sensors of the Pysense board

The PySense offers various embedded sensors, namely:

  • Ambient light sensor
  • Barometric pressure sensor
  • Temperature sensor
  • Humidity sensor
  • 3 axis 12-bit accelerometer

Take a look at the code in file "reads.py" to understand how to use them. As you can see, various python libraries are necessary, they are all in the lib folder. To copy the whole lib folder to your LoPy, you have to execute:
python3 -m there push -r lib/* /flash/lib

Appendix

LoPy's documentation is here: https://docs.pycom.io/index.html

Problem "pushing" a file to the LoPy

Sometime you get an error message when "push"ing a file to the LoPy, something like this:

$ python3 -m there push led_blink.py /flash
ERROR: action or command failed: execution failed: : Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: _h

the solution is to use the --force option. That is:

$ python3 -m there push --force led_blink.py /flash

Resetting the LoPy

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 while in the REPL.

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()