Book Image

MicroPython Cookbook

By : Marwan Alsabbagh
Book Image

MicroPython Cookbook

By: Marwan Alsabbagh

Overview of this book

MicroPython is an open source implementation of Python 3 that runs in embedded environments. With MicroPython, you can write clean and simple Python code to control hardware instead of using complex low-level languages such as C and C++. This book guides you through all the major applications of the MicroPython platform to build and program projects that use microcontrollers. This MicroPython book covers recipes that will help you experiment with the programming environment and hardware programmed in MicroPython. You'll find tips and techniques for building a variety of objects and prototypes that can sense and respond to touch, sound, position, heat, and light. This book will take you through the uses of MicroPython with a variety of popular input devices and sensors. You'll learn techniques to handle time delays and sensor readings, and apply advanced coding techniques to create complex projects. As you advance, you'll deal with Internet of Things (IoT) devices and integration with other online web services. In addition to this, you'll use MicroPython to make music with bananas and create portable multiplayer video games that incorporate sound and light animations into the gameplay. By the end of this book, you'll have mastered the tips and tricks to troubleshoot your development problems and take your MicroPython project to the next level.
Table of Contents (17 chapters)

Using screen to access the REPL

Linux and macOS have powerful Terminal emulators, such as screen, that can be used to directly connect to the device's Read-Eval-Print Loop (REPL) over a serial (USB) connection. This recipe will show how to connect to the REPL and start running a Python code interactively.

Getting ready

Either macOS or a Linux computer may be used for this recipe and may require the screen command to be available. On macOS, the Screen application is built-in and so requires no installation. On Ubuntu, the Linux Screen can be installed with the apt install screen command.

How to do it...

Let's have a look at how to connect the REPL and run the code:

  1. Open the computer's Terminal application.
  2. List device names before plugging in device by running ls /dev/ttyACM* on Linux or ls /dev/tty.* on macOS.
  3. Connect the board to your computer with a USB cable.
  4. List the device names again with the same command to discover the device name of the board.
  5. If the device name is /dev/ttyACM0, then the screen command would be screen /dev/ttyACM0 115200.
  6. Enter the command in the Terminal and start the Screen application.
  7. If Screen is able to connect successfully, the Python REPL should appear on the Terminal with output similar to the following text:
Adafruit CircuitPython 3.1.2 on 2019-01-07; Adafruit CircuitPlayground Express with samd21g18
>>>
  1. If the prompt doesn't appear, you can try pressing Ctrl + C and then press Enter, which will stop the currently running Python script and run the REPL with the following message:
Press any key to enter the REPL. Use CTRL-D to reload.
  1. Once the REPL prompt appears, we will have to test if the prompt is working by evaluating the 1+1 expression. It should produce the following output:
>>> 1+1
2

How it works...

The Circuit Playground Express exposes a serial device over the USB connection, which can be accessed by a number of different Terminal emulator programs. Besides screen, there are other programs, such as picocom and minicom, that may also be used.

The last parameter that was set as 115,200 in the command sets the baud rate of the connection, which should be set at that speed. Once the connection is successfully established, an interactive session is commenced that allows expressions to be directly evaluated on the device and the output is directly displayed on the Terminal.

There's more...

Many of the recipes in the book will introduce the different parts of a script using the REPL. This will give you a chance to get immediate feedback as you run each snippet of code. Once you've entered the different snippets in the REPL you can also use REPL features to assist in your experimentation with the code. You can use the up and down arrow keys to move through the history of commands that have been entered in the REPL. For example, if you had just executed a line of code in the REPL that turned on a specific pixel on the board, you could press the up key and change which pixel is lit up by editing the line and pressing Enter again.

See also