Skip to content

[PXCT-198] review ecosystem #2271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
---
featured: micropython-101
title: '2. Micropython Environment - Editor'
title: 'Code Editor'
description: 'Learn the basics for loops on MicroPython.'
author: 'Pedro Lima'
hero_image: "./hero-banner.png"
tags: [MicroPython, Arduino Lab for MicroPython]
---

Arduino Labs for MicroPython includes a user-friendly code editor that helps us write, format, and run MicroPython code with ease. In this article, we’ll explore how the code editor works, key formatting rules for MicroPython, and some useful tips to keep our code clean and error-free.
Arduino Lab for MicroPython includes a user-friendly code editor that helps us write, format, and run MicroPython code with ease. In this article, we’ll explore how the code editor works, key formatting rules for MicroPython, and some useful tips to keep our code clean and error-free.

## The Code Editor in Arduino Labs for MicroPython
## Requirements

- [Arduino Lab for MicroPython]()
- **MicroPython compatible board** (not required for using the editor, but for running any code).

***Note that the Arduino Lab for MicroPython is now also available online, as part of the Arduino Cloud platform. You will find it here: [Arduino Cloud - Arduino Lab for MicroPython]().***

## Arduino Labs for MicroPython

The code editor in Arduino Labs is designed to streamline our coding experience, providing tools to:

Expand All @@ -18,6 +24,24 @@ The code editor in Arduino Labs is designed to streamline our coding experience,

As we get familiar with the editor, remember that MicroPython has a few syntax rules that differ from other languages we might know.

### Overview

Text / bullet points here summarizing e.g. buttons and icons in the editor.

![Overview of the code editor.]()

### Files

Text / bullet points here for how to manage files (and point to the file system tutorial)

![Managing files in Arduino Lab for MicroPython]()

### REPL

Text / bullet points here for where the REPL is and how it works (and point to the REPL tutorial)

![The REPL.]()

## Indentation

In MicroPython, indentation (using spaces or tabs) is essential for defining the structure of our code. Unlike many other languages, MicroPython relies on indentation instead of symbols like `{ }` to define code blocks.
Expand Down Expand Up @@ -104,9 +128,4 @@ Here are a few quick tips to make the most of our time in the code editor:
- **Use Descriptive Names**: Name variables and functions clearly to make the code readable.
- **Keep It Modular**: Split complex code into functions for easier maintenance.
- **Comment as We Go**: Leave helpful comments explaining why we wrote certain sections of code.
- **Indent Carefully**: Pay attention to indentation for clean, error-free code.

## Try It Out

- **Experiment with Indentation**: Try writing a few functions with nested loops or conditionals to get comfortable with indentation.
- **Add Comments**: Document a small code snippet with comments explaining each part.
- **Indent Carefully**: Pay attention to indentation for clean, error-free code.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
title: 'File System'
description: 'Learn how to use the File Sytem in MicroPython.'
author: 'Pedro Lima'
tags: [MicroPython, REPL]
tags: [MicroPython, File System]
---

When working with MicroPython, we’re not limited to a single program file like in traditional Arduino sketches (using C++). Instead, MicroPython provides a file system, enabling us to store and manage multiple files on our microcontroller. This opens up powerful capabilities for organizing code, managing assets, and creating modular projects.

When working with MicroPython, we’re not limited to a single program like in traditional Arduino sketches. Instead, MicroPython provides a file system, enabling us to store and manage multiple files on our microcontroller. This opens up powerful capabilities for organizing code, managing assets, and creating modular projects.

In this article, we’ll explore how the MicroPython file system works, how to organize files effectively, and the typical structure of MicroPython projects.
In this article, we'll explore how the MicroPython file system works, how to organize files effectively, and the typical structure of MicroPython projects.

## The MicroPython File System: Key Differences

In traditional Arduino programming, we upload a single compiled file directly to the microcontroller, where it runs immediately. With MicroPython, however, we work within a file system that can store multiple files. This file system allows us to:
In traditional Arduino programming, we upload a single compiled file directly to the microcontroller, where it runs immediately. In MicroPython we work within a file system that can store multiple files. This file system allows us to:

- **Upload and Download Files**: We can save individual scripts, libraries, and assets directly on the device.
- **Organize Project Files**: Create folders, save multiple scripts, and organize files for a modular approach.
- **Edit Files Directly on the Device**: Modify files without needing to overwrite everything, making adjustments faster and more flexible.
- **Run Different Scripts**: while we have `boot.py` (runs at start) and `main.py` (runs after start), we can also create more scripts that we can run, either from the editor, or from the board itself.

## Accessing the MicroPython File System

Expand All @@ -26,9 +26,11 @@ To interact with the MicroPython file system, we’ll use Arduino Labs for Micro
2. **Upload and Download Files**: Use the file manager to upload files from our computer to the microcontroller or download files back to our computer.
3. **Organize Files**: We can create folders and store multiple files, making it easy to organize our project.

![Accessing the file system.]()

## Basic MicroPython File Structure

A typical MicroPython project includes a main file, boot script, libraries, and any supporting files our project needs. Here’s a standard layout:
A typical MicroPython project includes a `main.py` file, `boot.py` file, libraries, and any supporting files our project needs. Here’s a standard layout:

```
/ (Root Directory)
Expand All @@ -41,59 +43,64 @@ A typical MicroPython project includes a main file, boot script, libraries, and
- **`boot.py`**: Runs once at startup, before `main.py`, and is typically used for system configurations that need to be set when the device first powers on.
- **`main.py`**: This is the primary script, similar to the `setup()` and `loop()` structure in Arduino. It runs automatically after `boot.py` finishes.

## Example: Switching Execution from `main.py` to `favorite_things.py`
## Example: Importing Code from Scripts

Let’s say we’re creating a project where `main.py` runs and, at a certain point, hands off control to `favorite_things.py` to perform a specific task. Here’s how we might organize it.
With the MicroPython file system, we can create our own scripts and import them in the `main.py` script. This can be helpful in avoiding long scripts, as we instead can store the code in other files. This approach also makes it more modular.

1. **Write the Main Script** (`main.py`): This script runs some initial code and then switches to executing `favorite_things.py`.
2.Create the file "favourite_things.py".
3. **Add Content to `favorite_things.py`**: In this case, we’ll simply print a message from `favorite_things.py`.
To run code from a separate script in our `main.py` file, we can follow the instructions below:

### Sample `main.py` Code
1. Create a file named `my_new_script.py`, and add the following function:

Here’s how `main.py` might look, printing an introductory message and then executing `favorite_things.py`:
```python
def test():
print("This runs from my_new_script.py")
```

```python
def main():
print("Getting the list of my favourite things...")
2. In `main.py`, we run some initial code and then switches to executing a function from `my_new_script.py`. Here's an example:

# Now execute favorite_things.py
print("Switch to favorite_things.py...")
try:
with open("favorite_things.py") as f:
exec(f.read())
except OSError:
print("Error: Could not open favorite_things.py")

if __name__ == "__main__":
main()
```
```python
import my_new_script
print("This runs from main.py")

### Sample `favorite_things.py` Code
my_new_script.test()
```

In `favorite_things.py`, we’ll keep it simple with a message:
3. Check the REPL, we should see:

```python
# favorite_things.py
print("Bears. Beets. Battlestar Galactica.")
```
```bash
This runs from main.py # executed from main.py
This runs from my_new_script.py #executed from my_new_script.py
```

### Explanation
Essentially, this is how [modules]() work. You import a module, and use a function from that module.

- **Switching Execution**: `main.py` starts by printing an introductory message, and then uses `exec(f.read())` to read and run the content of `favorite_things.py`.
- **Running Another Script**: The `exec()` function allows `main.py` to execute `favorite_things.py` as if it were part of `main.py`, printing “Bears. Beets. Battlestar Galactica.” directly.
![Import code from a script.]()

### Expected Output
## Example: Directly Executing a Script

When you run `main.py`, the output should look like this:
We can also directly execute another script stored on the device. For this example, let's create a script and name it `run_directly.py`.

```
Getting the list of my favourite things...
Switch to favorite_things.py...
Bears. Beets. Battlestar Galactica.
```
1. In the script, store this code:

```python
print("I was run from main.py")
```

2. Then in `main.py`, we will use the `open()`, `exec()` and `read()` functions.

```python
with open("run_directly.py") as f:
exec(f.read())
```

- `open()` - opens a file
- `exec()` - executes a file
- `read()` - reads a file

As a result, we should read `"I was run from main.py"` in the REPL. How this differs from the previous example, is that the `run_directly.py` script was just run from top to bottom, as opposed to importing a specific segment of code.

![Executing a script directly.]()

This setup demonstrates how to use MicroPython’s file system to organize and execute multiple scripts, allowing for modular and readable code.

## Organizing Code with Modules and Libraries

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: 'Modules'
description: 'Understanding Modules and how to use them.'
description: 'Understanding modules in MicroPython and how to use them.'
author: 'Pedro Lima'
tags: [MicroPython, Modules]
---


In this article, we’ll cover how modules work in MicroPython, explore a few built-in modules, and demonstrate how to install an external package like Modulino to extend our MicroPython project’s functionality.
In this guide, we’ll cover how modules work in MicroPython, explore a few built-in modules, and demonstrate how to install an external package like Modulino to extend our MicroPython project’s functionality.

## What Are Modules?

Expand Down Expand Up @@ -38,7 +38,7 @@ In this example, `time.sleep()` introduces a delay. Built-in modules like `time`

## External Modules

Some modules aren’t included with the default MicroPython installation and need to be installed separately. External modules, often provided by the community or specific hardware packages, extend MicroPython’s functionality. For example, the **Modulino** library is an external module that provides tools for working with Arduino Modulinos.
Some modules aren’t included with the default MicroPython installation and need to be installed separately. External modules, often provided by the community or specific hardware packages, extend MicroPython’s functionality. For example, the [Modulino library]() is an external module that provides tools for working with Arduino Modulinos.

To demonstrate how to use external modules, we’ll go through the steps to install the Modulino package on an Arduino board.

Expand All @@ -51,37 +51,41 @@ Before we can install external modules, we need to have MicroPython running on o
- Press the "Refresh" button if the board does not appear.
- Click "**Install MicroPython**" and wait for the installation to complete.

***For more details, visit the [MicroPython installation guide]()***

### Step 2: Install the Modulino Package

To install the Modulino package, we’ll use `mpremote`, a tool that allows us to install MicroPython packages directly onto our board from the command line.

1. **Install `mpremote`**: Make sure Python is installed on your computer, then open a terminal and type:
1. Make sure Python is installed on your computer
2. Open a terminal on your machine.
3. Run the following command to install `mpremote`.

```bash
pip install mpremote
```

2. **Connect to Your Board**: Find your boards serial port by running:
4. With `mpremote` installed, run the following script to find our board's serial port.

```bash
mpremote connect list
```

This command should return something like:
This command should return something akin to:

```bash
/dev/cu.usbmodem101 ecda3b60a4dccb3f 2341:056b Arduino Nano ESP32
```

- Copy the port, e.g., `/dev/cu.usbmodem101`.

3. **Install the Modulino Package**: Use the following command to install the Modulino package, replacing `<PORT>` with your board’s port:
5. Use the following command to install the Modulino package (or any other package we want to install), replacing `<PORT>` with our board’s port retrieved in the previous step.

```bash
mpremote connect <PORT> mip install github:arduino/arduino-modulino-mpy
```

4. **Verify Installation**: After installation, check Arduino Labs for MicroPython. You should see a `/lib` folder with the Modulino library inside, indicating a successful installation.
6. After the installation, open Arduino Labs for MicroPython, and connect your board. In the board's files, we should see a `/lib` folder with the Modulino library inside, indicating a successful installation.

![MicroPython Lab Files](./assets/microPythonLabsFiles.png)

Expand Down
Loading