Thonny Ide Esp32

Posted onby admin

Thonny is a Python IDE that keeps the process of programming a microcontroller simple, which is great for beginners. After Thonny is installed, we will go over getting Thonny setup for an ESP32 NodeMCU, flashing the MicroPython firmware, and writing a simple program for the ESP32 device using MicroPython. Thonny Python IDE. A lightweight Python IDE ideal for writing simple Python programs for first time users. Thonny has the following screen areas. Toolbar Script Area Python Shell Interpreter. Thonny runs on Mac, Windows and Linux. Running help You can enter the help function in the main script area and then press the Play button.

I’ve been playing with a few MicroPython projects recently on several different embedded platforms, including a couple of ESP32 WiFi modules. There are various ways to program these modules:

  • Use a serial terminal and ampy (maintained by [devxpy] since being dropped by Adafruit in 2018).
  • If you use Pycom boards or WiFy firmware, there are the pymakr plugins for Atom and Visual Studio.
  • If you prefer the command-line like me, there is rshell by one of the top MicroPython contributors [Dave Hylands].

For over a year, I have been quite happy with rshell until I started working on these wireless nodes. Being lazy, I want to tinker with my ESP32 modules from the sofa, not drag my laptop into the kitchen or balcony to plug up a USB cable. Can’t I work with them wirelessly?

Well, you can use WebREPL. While its functional, it just didn’t strike my fancy for some reason. [Elliot] mentioned in a recent podcast that he’s using telnet to access his wireless nodes, but he’s using esp-link on an ESP8266, which means throwing another chip into the mix.

The Thonny IDE

I had all but given up when by chance I saw this video on the Dronebot Workshop channel about running MicroPython on the new Raspberry Pi Pico boards. Bill was using Thonny, a Python IDE that is popular in the education community. Thonny was introduced in 2015 by Aivar Annamaa of the University of Tartu in Estonia. Thonny was designed to address common issues observed during six years of teaching Python programming classes to beginners. If you read about the project and its development, you’ll see that he’s put a lot of effort into making Thonny, and it shows.

Leaning about Thonny got me curious, and after a little digging I discovered that it has WebREPL support for MicroPython right out-of-the-box. Although this is a new feature and classified as experimental, I found it reasonably stable to use and more than adequate for home lab use.

Accessing internal sensors of ESP32 ( temperature & hall effect sensors). Flashing MicroPython using Thonny IDE. Project description Plug-in for Thonny IDE which adds ESP8266 and ESP32 MicroPython backends. Thonny is Python IDE for beginners. It allows you to program your ESP32 and ESP8266 boards with MicroPython, and it is compatible with Windows, Mac OS X, and Linux. It even comes installed by default with Raspbian OS for Raspberry Pi. Additionally, it's easy to install, so you shouldn’t have problems with the installation process.

Installation and Connection

Instructions are clearly given on the project’s website. There is a downloader link at the top of the page for Windows. For Linux and Mac simply doing sudo apt install thonny or brew install thonny was all I needed. If you have a Raspberry Pi, you will find Thonny is pre-installed — version 3.3.3 was found on a recent Pi 4 in my lab. On an older Pi 3 however, Thonny was too old and simply reinstalling it didn’t work for me. Doing a sudo apt update, sudo apt upgrade, and then reinstalling Thonny resulted in the latest version 3.3.6.

For some reason, the Thonny found in the Ubuntu 20.04 and Debian Buster package repositories also weren’t recent enough. Rather than building from source, I used the provided Linux installation script that downloads and installs the latest release.

In order to connect to a module, you will use the Run -> Select interpreter... menu. Here you pick which type of module to use, and which interface (USB or WebREPL). Note that you have to connect to your module over USB first and make sure that WebREPL is enabled in boot.py. This is the normal method of enabling WebREPL in general — if your module already has WebREPL setup, you don’t need to change anything. Fill in the WebREPL URL and password — you should connect and be good to go.

Tweaks

By default, Thonny’s windows are stacked vertically. If you’re using a modern wide-screen monitor, you will probably want to rearrange the windows in Thonny so the editor and shell are side-by-side (see the feature image). This is done by adding ShellView.location = e to the [view] section of the configuration.ini file (see here). The e means East, and other compass points like se can be specified as well. For the systems I tested, the configuration file can be found in the following places:

  • Raspi, Ubuntu, Debian
    ~/.config/Thonny/configuration.ini
  • Mac
    ~/Library/Thonny/configuration.ini
  • Windows
    C:UsersAppDataRoamingThonnyconfiguration.ini

I had no problems using the REPL manually, but I discovered an issue once you start testing self-starting applications with forever loops. If you are not careful, your application’s main loop can prevent you from regaining REPL control. The solution is to make sure your application sleeps for several milliseconds in the main loop. I tested with 100 ms (see here) and was always able to regain control. This shouldn’t be an issue in most home sensor applications, where the module may sleep for tens of minutes between readings.

By default, a Thonny installation doesn’t make itself visible for command line execution. On the computers I tested, you will find the Thonny executable in the following locations:

  • Raspi /usr/bin/thonny
  • Ubuntu, Debian (installed via installer script)
    ~/apps/thonny/bin/thonny
  • Mac
    /Applications/Thonny.app/Contents/MacOS/thonny
  • Windows
    C:UsersAppDataLocalProgramsThonnythonny.exe

Multiple Modules and Instances

Thonny can support a variety of different modules. But if you want to connect to multiple modules at the same time, you need to run several instances of Thonny simultaneously. By default, only one instance is allowed. In the Tools -> Options -> General menu, untick the Allow only single Thonny instance checkbox.

I noticed one quirk on MacOS, however. For some reason, unchecking that option doesn’t work when launching the app from the GUI. There is a workaround — you can get multiple instances running if you start them from the command line.

One more annoyance — by default, Thonny will use the previous interpreter / connection when starting up. This is reasonable behavior, but causes a hiccup when talking to multiple boards simultaneously. Just ignore the plethora of error messages when Thonny instance #2 tries to connect to an already connected module, and use the menus to connect to the second device. There apparently isn’t any command line argument for a specific connection, but I have an inquiry pending on the GitHub project page.

Tip of the Iceberg

I have only focused on the WebREPL connectivity aspect of Thonny here, a functionality which seems to be uncommon in other MicroPython programming workflows. But this only scratches the surface of Thonny. Under the hood, it has a lot to offer that is hidden when you first open it.

Python thonny ide

And while I have been using its MicroPython mode of operation, Thonny was designed to aid students who are learning to write and debug “normal” Python programs running on desktop computers. If you frequently work in Python, I would encourage you to have a look at these aspects of the program — an object inspector, debugger, single-stepping, and a whole slew of other useful tools.

Thonny is also a multi-platform tool. While preparing this article, I tested it on MacOS, Windows 10, Debian, Ubuntu, and Raspberry Pi computers with no problems. The project is open-sourced under the MIT License.

At the very least, Thonny is a great solution for remote MicroPython programming of WiFi embedded modules. Check it out and give it a test run, especially if you’re lazy like me.

MicroPython on ESP32 using uPyCraft IDE – Getting Started

1. Overview: Getting Started with MicroPython on ESP32 using uPyCraft IDE

In this MicroPython ESP32 Tutorial - getting started with MakePython ESP32, we will learn how to use MicroPython on ESP32 using uPyCraft IDE. We all are familiar with the Python Programming Language. Python is an interpreted, high-level, general-purpose programming language. As Python is a very powerful language it is used for Artificial Intelligence (AI) & Internet of ThingsApplications.

As Python became very popular, the Python community developed MicroPython. MicroPython is a lean and efficient implementation of the Python programming language that includes a small subset of the& Python standard library and is optimized to run on microcontrollers and in constrained environments. Micropython is supported by boards like PyBoard, ESP8266, ESP32 & other boards.

In this tutorial, we will be using MicroPython on ESP32 Board using uPyCraft IDE. uPyCraft is an IDE that works with Windows and Mac and designed with a simple interface which is convenient to use. uPyCraft is developed by DFRobot.


We will take two examples here as a reference to learn MicroPython on ESP32 using uPyCraft IDE.
1. We will first write code to blink an LED which is simply like writing the “Hello World” Program.
2. In the second example, we will make a Web Server to control an LED using Web Page.

2. What is MicroPython?

MicroPython is a software implementation of a programming language largely compatible with Python 3, written in C, that is optimized to run on a microcontroller. The difference between Python & MicroPython is that MicroPython does not come with the full library support as Python. Therefore MicroPython works in constraint conditions only. There are few standard libraries to access and interact with the low-level hardware like GPIOs, Wi-Fi, Networking.

MicroPython is a full Python compiler and runtime that runs on the microcontroller hardware. The user is presented with an interactive prompt (the REPL) to execute supported commands immediately. Included are a selection of core Python libraries; MicroPython includes modules that give the programmer access to low-level hardware.

3. MakePython ESP32 Board

As already explained above, we will first need an ESP32 Board. So I purchased an ESP32 Board specially designed for using Micropython from MakePython ESP32 Board from MakerFabs.

MakePython ESP32 Board has the same ESP32 Chip from espressif. The advantage of this board over the normal ESP32 Board is that, the board has an additional 0.96″ OLED Display added to it.

MakePython ESP32 is a powerful, generic Wifi+ BLE MCU module target to MicroPython learners & developers. It is true “breadboard compatible”, with the very detailed pins explanation, it is easy to learn & use. The MakePython ESP32 is programmed with MicroPython by default, users can begin the MicroPython developing as getting then on hand.

The complete pin description and detail about this board can be found here: MakePython ESP32 Description. You can even purchase the board from the same link.

4. Getting Started with MicroPython on ESP32 using uPyCraft IDE

The section covers:
1. Installing Python 3.8.x on Windows/MAC
2. Installing uPyCraft IDE
3. Flashing the MicroPython Firmware (Bin File) on MakePython ESP32 Board
4. Writing Python Code and uploading the code on ESP32 using uPyCraft IDE
5. Blinking of LED
6. Controlling the LED using Web Server

Step 1: Installing Python 3.8.x on Windows

MicroPython is an implementation of Python so we first need to install Python 3.8.x or above. To install Python 3.8.x download Python from https://www.python.org/downloads/

Python Thonny Ide


After downloading Python 3.8.x, you need to install it.


Once the installation is completed, you can close the installation window.

Step 2. Installing uPyCraft IDE

There are only a few IDEs that support MicroPython like Thonny IDE and uPyCraft IDE. Thonny IDE does not have an option to flash MicroPython firmware on ESP32 but the uPyCraft IDE has inbuilt functionality to flash firmware, programming, and uploading the code in ESP32 board.

Go to this link: uPyCraft IDE Download & download the uPyCraft IDE.

After downloading, you can simply click the .exe and the following window will open.


Now Let’s have a closer look at the uPyCraft IDE and its 4 sections. All 4 sections have their own importance.


1. Folders and files:
This section contains files and folders like Device, SD Folder, uPy_Lib & Workspace. The device folder contains the files stored in the ESP board including boot.py or main.py. SD folder contains the files stored in the SD card. uPy_lib contains in-built MicroPython IDE libraries. The workspace contains the files that will be saved on your PC.

2. Editor Window:
This section is for writing and editing the python code. Multiple python files can be opened in Editor.

3. Tools:
This section contains options for creating a new file, saving the file, uploading the file, connecting/disconnecting the board, clearing shell.

4. MicroPython Shell/Terminal:
This section is for writing and executing the shell commands on the ESP board. This section also shows the status of the program, errors & print messages.

Step 3: Flashing the MicroPython Firmware (Bin File) on MakePython ESP32 Board

Now we need to flash firmware to ESP32 Board so that it can support MicroPython. Visit this link: https://micropython.org/download/esp32/and download the .bin file as shown in the image below. Choose the stable version.


After downloading the firmware, we need to upload it to ESP32 Board using uPyCraft IDE. So first connect the ESP32 Board to your PC using Micro-USB Data Cable.


Now open the uPyCraft IDE and Select Tools → Burn Firmware.


Choose the following options shown in the image below. Select the bin file that you downloaded recently (explained above). And then click on OK.


So the firmware will start uploading. In some ESP32 boards, you need to press the boot button to get the process started.


Once the uploading is done, the window will close automatically.

Step 4. Writing Python Code and uploading the code on ESP32 using uPyCraft IDE

1. Now click on File and create a new file with name main.py or give it any other name.

Install Thonny Ide


2. Then, go to Tools → boards. Choose your board as ESP32.

3. Again go to tools → Serial and choose your COM port.

4. Now, click on the connect icon in the Tools section to make the connection between IDE and ESP32 board.


5. ESP32 LED Blinking with MicroPython using uPyCraft

Now let us do the first getting started the program. For that connect the LED to GPIO5 Pin of MakePython ESP32 Board.

Copy the following MicroPython Code and upload it to the ESP32 Board by clicking on the download button.


As soon as you hit the download button, you can view the details in ;Python shell as shown in the above image. Similarly, the connected LED will start blinking.


6. ESP32 Micropython: Controlling LED using Web Server

Now let us design a small webpage and control the turning ON & OFF of LED using Web Server. You can follow ESP32 Web Server Tutorial if you want to learn more about the Web Server.

Copy the following MicroPython Code and upload it to the ESP32 Board by clicking on the download button again. In the below code change the credentials for wifi SSID & Password.


After code is successfully downloaded, you can see the ESP32 Board connecting to the network in the uPyCraft Shell. Similarly the IP Address of the Board will also be displayed. Copy the IP Address and paste it on any web browser. So the page will open as shown below.


You can now send ON Command or OFF Command to Turn ON or Turn OFF the LED respectively.


This article is originally published on how2electronics.