Skip to content

Commit cf331dd

Browse files
committed
add micropython
1 parent b1be839 commit cf331dd

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed
100 KB
Loading
121 KB
Loading
130 KB
Loading

docs/vscode/migrating.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ This checklist of steps should help you overcome the most common issues. It is a
1616
Using **MicroPython**? The setup for MicroPython projects is quite different - please see the [Vs Code for MicroPython page](vscode-micropython.md).
1717
:::
1818

19-
2019
## Download the project source
2120

2221
To start, the first step is to save your online project. From the same menu, under the save button, you can then select to download a `.zip` archive of the project. This will contain useful files such as any source code and the `diagram.json` file used by Wokwi.

docs/vscode/vscode-micropython.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: MicroPython projects in VS Code
3+
sidebar_label: MicroPython in VS Code
4+
description: Migrate or create MicroPython projects incorporating the Wokwi simulator in a local VS Code environment
5+
keywords: [Visual Studio Code, VS Code, embedded simulation, IoT simulation, MicroPython]
6+
---
7+
8+
MicroPython projects differ from those in most of the other frameworks, because in this case the majority of the firmware to be installed on your hardware device (or Wokwi simulator) is MicroPython itself. This firmware, the `main.py` you create and any additional libraries are the components which need to be uploaded.
9+
When the device or simulator is initialized or rebooted, it will first load MicroPython, then run the `main.py` code, and then (assuming the main code exits) respond to further interaction using the [REPL](https://docs.micropython.org/en/latest/esp8266/tutorial/repl.html) prompt.
10+
In the case of the Wokwi simulator, this means, in addition to the code you have created, it also requires a version of the firmware to run the simulation on.
11+
12+
## Before you start
13+
14+
There are three things you should install before migrating your project to VS Code (or starting a new one).
15+
16+
1. Make sure you have the VS Code extension for Wokwi (See the [getting started guide](./getting-started#installation)).
17+
1. Install the **mpremote** software (see the [MicroPython documentation](https://docs.micropython.org/en/latest/reference/mpremote.html) for this step). This software uses a serial connection to your MicroPython-enabled hardware (or the Wokwi simulator) to manage the filesystem and perform tasks such as rebooting or entering the REPL interactive mode.
18+
:::warning
19+
To avoid compatibility issues, it is strongly advised to install **a version of mpremote of the same version as your firmware**. The default firmware in the project repository is currently `1.23`. To install the matching mpremote with `pip` for example:
20+
21+
```bash
22+
pip install mpremote==1.23
23+
```
24+
25+
...or adjust for whatever method you are using for installing `mpremote`.
26+
:::
27+
1. Clone the [example project from GitHub](https://github.com/wokwi/wokwi-vscode-micropython). This project has been set up with the relevant files that the Wokwi simulator requires for different types of hardware. You can visit the link to clone or download the repository, or [download it via this link](https://github.com/wokwi/wokwi-vscode-micropython/archive/refs/heads/main.zip) as a zip file. Remember to uncompress the file somewhere you want to work with it!
28+
29+
## Open the repository in Visual Studio Code
30+
31+
Run Visual Studio Code and then use 'File>Open folder' to open the directory containing the example project.
32+
You'll find the project contains a useful README, a `main.py` file and some hardware specific directories (`esp32`, `rp2040`, and so on, for different microcontrollers).
33+
34+
![Screenshot showing the VS Code directory structure](./images/vs-micropython-01.png)
35+
36+
Each of the hardware directories contains the relevant firmware file for MicroPython, a Wokwi `diagram.json` file and the `wokwi.toml` file used to configure the simulator.
37+
38+
Select the `diagram.json` file, and a new view should open showing a board and the familiar Wokwi simulation display. Click on the play button to run the simulation and test that it works.
39+
40+
![Running the demo code](./images/vs-micropython-02.png)
41+
42+
Once the simulator starts, it will also open a terminal window which will show the startup messages and then start the interactive REPL. You can enter MicroPython commands here to further test the simulation.
43+
44+
What you may have noticed is that it didn't execute the code in `main.py`. That's because the local version of `main.py` hasn't been uploaded to the filesystem of MicroPython in the simulator - it's just running the bare MicroPython firmware.
45+
46+
## (Optional) Add your own files
47+
48+
If you have downloaded a MicroPython project from the Wokwi online simulator, you should add these files to the project, replacing the default files. The download will contain at a minimum the `main.py` file and a `diagram.json` file.
49+
50+
Replace `main.py` in the main directory of the project with your own. If you used any additional files (for example, extra python modules or data files), these should also be added here.
51+
52+
The `diagram.json` file needs to be in the hardware specific directories. Copy it into each of the hardware directories (or just the ones you intend to use).
53+
54+
You can select the `diagram.json` file again to open the simulator and confirm that your circuit is displayed.
55+
56+
## Uploading files to the Wokwi simulator
57+
58+
Before we can upload anything, we need to start the simulation. When it's running, Wokwi also opens a serial connection through a server on a local port. We can then use this connection to upload the files.
59+
60+
:::note
61+
The serial port configuration for the simulator can be found in the `wokwi.toml` file in eache hardware specific directory:
62+
```
63+
rfc2217ServerPort = 4000
64+
```
65+
This designates port '4000' for the connection. You may wish to change this if you have other services already using this port.
66+
:::
67+
68+
To upload the `main.py` file, open a new terminal (you can add an additional terminal within VS Code and switch between it and the Wokwi REPL) and enter the command to upload the `main.py` file:
69+
70+
```
71+
mpremote connect port:rfc2217://localhost:4000 fs cp main.py :main.py
72+
```
73+
74+
:::note
75+
Sometimes the command will return an error which ends with `TransportError("could not enter raw repl")`. If you get this error you can force the REPL into 'Raw' mode by activating the Wokwi REPL window and pressing <kbd>Ctrl</kbd> + <kbd>A</kbd>, then go back to your terminal and run the command again.
76+
:::
77+
78+
:::tip
79+
On Unix based systems (e.g. Mac or Linux), you can create a shortcut for connecting to the simulator by running the following command:
80+
```
81+
mkdir -p ~/.config/mpremote
82+
echo 'config={"wokwi": "connect port:rfc2217://localhost:4000"}' > ~/.config/mpremote/config.py
83+
```
84+
After running this command, you can connect to the simulator by running `mpremote wokwi`.
85+
:::
86+
87+
You can also use `mpremote` to install libraries which are part of the MicroPython `mip` package library. For example, to install the `ssd1306` library:
88+
89+
```
90+
mpremote connect port:rfc2217://localhost:4000 mip install ssd1306
91+
```
92+
93+
For your own additional files, simply upload with the syntax:
94+
95+
```
96+
mpremote connect port:rfc2217://localhost:4000 fs cp <filename> :<filename>
97+
```
98+
99+
You can confirm what files have been uploaded with the `ls` command:
100+
101+
```
102+
mpremote connect port:rfc2217://localhost:4000 ls
103+
```
104+
105+
Once the required files are uploaded, activate the Wokwi REPL again and enter the following:
106+
107+
```python
108+
import machine
109+
machine.reset()
110+
```
111+
112+
This will perform a hard reboot of the simulation and automatically run the `main.py` file.
113+
114+
![Automatically running the main.py code on reset](./images/vs-micropython-03.png)
115+
116+
:::warning
117+
The filesystem stored in the simulator is not persistent. If you stop the simulation or close the simulation window in VS Code, you will need to repeat the steps to upload any files or libraries again.
118+
:::

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ module.exports = {
9393
'vscode/debugging',
9494
'vscode/offline-mode',
9595
'vscode/migrating',
96+
'vscode/vscode-micropython',
9697
],
9798
'Wokwi CI': [
9899
'wokwi-ci/getting-started',

0 commit comments

Comments
 (0)