Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MicroPython Cookbook

You're reading from   MicroPython Cookbook Over 110 practical recipes for programming embedded systems and microcontrollers with Python

Arrow left icon
Product type Paperback
Published in May 2019
Publisher Packt
ISBN-13 9781838649951
Length 452 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Marwan Alsabbagh Marwan Alsabbagh
Author Profile Icon Marwan Alsabbagh
Marwan Alsabbagh
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting started with MicroPython FREE CHAPTER 2. Controlling LEDs 3. Creating Sound and Music 4. Interacting with Buttons 5. Reading Sensor Data 6. Button Bash Game 7. Fruity Tunes 8. Let's Move It, Move It 9. Coding on the micro:bit 10. Controlling the ESP8266 11. Interacting with the Filesystem 12. Networking 13. Interacting with the Adafruit FeatherWing OLED 14. Building an Internet of Things (IoT) Weather Machine 15. Coding on the Adafruit HalloWing Microcontroller 16. Other Books You May Enjoy

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

You have been reading a chapter from
MicroPython Cookbook
Published in: May 2019
Publisher: Packt
ISBN-13: 9781838649951
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image