MicroPython ESP32 pro-tip: Use WebREPL within Thonny
The repetitive process of editing your code in Thonny, switching over to the WebREPL window, re-selecting your edited code file, re-sending it to your ESP32, and going back to Thonny to fix any bugs can get cumbersome and slow. Luckily for us, there's a solution!
We've learned how to wirelessly connect and upload new code to an ESP32 using WebREPL before. But often, the repetitive process of:
- editing your code in Thonny,
- switching over to the WebREPL window,
- re-selecting your edited code file,
- re-sending it to your ESP32,
- and going back to Thonny to fix any bugs
can get cumbersome and slow.
Luckily for us, there's an awesome solution - using WebREPL within Thonny itself! And the crazy part is, this feature is already built into Thonny...it's just very well hidden. If you want to speed up your workflow when coding in Micropython, read on (or watch the video) to find out how!
Open up Thonny IDE (if you don't have it, you can download it here). In the menu, click on Preferences and open up the Interpreter tab. At the bottom you will see a dropdown menu named Port or WebREPL.
By default, Thonny is configured to automatically detect any microcontroller you connect using your USB port, and give you access to its files and its REPL shell. To get the same functionality but wirelessly, click on the dropdown menu and change the selected option to <WebREPL>.
Now enter your ESP32's IP address and WebREPL password in the new menu. If you don't know your ESP32's IP address or have no clue how to configure the WebREPL password, fear not! There's a tutorial for that 🙂 You can see an example of my IP address and password below.
Click OK at the bottom and voila! If you set up everything right, you should see your shell pane pop up at the bottom of Thonny with the magic words everyone wants to hear: "WebREPL connected".
(A tip: if your ESP32 was running a server at the time you connected to it, you will need to send one or more Ctrl+Cs to kill the server and get access to the REPL. Unfortunately, this doesn't always work great and you may have to press Enter/Return a few times till its responsive...hey, they did say it's an experimental feature, after all)
Now that you've got a connection, you can run commands on the REPL, of course. But even better is that you can look at the files on your ESP32 and open them up for editing right within Thonny! Click on the "open file" icon at the top of Thonny and it will load your filesystem over websockets.
Once you open a file, it will load that file over websockets into the Thonny editor. Make your changes and click the "save" icon at the top to save it back to the microcontroller. Basically your whole workflow (typing commands, opening files, and saving files) is the same as if you had connected to your ESP32 using USB! Amazing!
I hope this tip is helpful to you as it is to me. It's a great feature that takes an unwieldy process and makes it really smooth - I'm a sucker for speed and efficiency so I always appreciate any opportunity I get to streamline my process.
If you want more Micropython secrets and tips, click Follow/Subscribe below and I'll email you my learnings when I post them. I only post once every 1-2 weeks so don't worry about getting spammed 🙂 If you like learning by watching videos, you can subscribe to my Youtube channel as well. That's it for today! See you next time on...Ssssecretsss of MicroPythththon 🐍