IOT

How to use MicroPython on ESP32 using uPyCraft IDE

In this blog, we’ll learn how to utilize MicroPython on ESP32 with the uPyCraft IDE. Python is a programming language that we are all familiar with. Python is a high-level, interpreted programming language for general-purpose applications. Python is utilized for Artificial Intelligence (AI) and Internet of Things applications since it is a powerful language.

The Python community developed MicroPython as Python grew in popularity. MicroPython is a lean and efficient Python programming language implementation that includes only a small fraction of the Python standard library and is targeted for microcontrollers and limited situations. Boards like PyBoard, ESP8266, ESP32, and others support Micropython.

In this article, we’ll use the uPyCraft IDE to run MicroPython on an ESP32 board. uPyCraft is a Windows and Mac-compatible integrated development environment with a user-friendly interface. DfRobot is the creator of uPyCraft.

What is MicroPython?

MicroPython is a Python interpreter designed for microcontrollers and embedded devices. Damien P. George created it, and it is written in C. MicroPython is a Python 3 compatible compiler and runtime that may be run on microcontrollers.

Although it is a subset of Python 3, it does not include all of Python’s standard libraries because it was designed for limited systems. Only a few carefully chosen Python libraries are supplied, while the rest are created specifically for MicroPython (mostly for low-level hardware access).

If you’re a software developer who’s never dealt with hardware but wants to build programs for microcontroller-based embedded systems (for personal or professional projects), MicroPython is the way to go.

Components Required

  1. ESP32 Dev board
  2. LED
  3. Jumper Wires

MakePython ESP32 Board

ESP32 Board is specially designed for using Micropython from MakePython ESP32 Board from MakerFabs.

ESP32 MakePython

The ESP32 Chip from espressif is used on the MakePython ESP32 Board. The advantage of this board over the standard ESP32 Board is that it includes an additional 0.96′′ OLED Display.

MakePython ESP32 is a powerful, generic Wifi+ BLE MCU module for MicroPython developers and learners. It is truly “breadboard compatible,” and it is simple to learn and operate thanks to the extremely clear pins explanation. By default, the MakePython ESP32 is coded in MicroPython, so users can start programming in MicroPython right away.

MakePython ESP32 Pins

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

  • Since MicroPython is a Python implementation, we must first install Python 3.8.x or above. Python 3.8.x can be downloaded from the Python website.https://www.python.org/downloads/

.

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

You can close the installation window once the installation is finished.

Step 2. Installing uPyCraft IDE

Thonny IDE and uPyCraft IDE are two of the few IDEs that support MicroPython. Although the Thonny IDE lacks the ability to flash MicroPython firmware on the ESP32, the uPyCraft IDE includes the capability for flashing firmware, programming, and uploading code to the ESP32 board.

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

After downloading, simply double-click the.exe to launch the following window.

Let’s take a deeper look at the four sections of the uPyCraft IDE. Each of the four divisions has its unique significance.

1. Folders and files:

Device, SD Folder, uPy Lib, and Workspace are among the files and directories in this section. The device folder contains files like as boot.py and main.py that are kept on the ESP board. The files on the SD card are stored in the SD folder. MicroPython IDE libraries are included in uPy lib. The files that will be saved on your computer are stored in the workspace. ​

2. Editor Window:

This part is where you’ll write and edit your Python code. Editor allows you to open multiple Python files.

3. Tools:

This section includes options for creating a new file, saving it, uploading it, connecting and disconnecting the board, and cleaning the shell.

4. MicroPython Shell/Terminal:

The shell commands on the ESP board are written and executed in this part. This area also displays the program’s status, problems, and print messages.

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

Now we must flash firmware to the ESP32 Board to enable MicroPython functionality. Visit this link: https://micropython.org/download/esp32/ and download the .bin file as shown in the image below. Choose the stable version.

  • After obtaining the firmware, we must use the uPyCraft IDE to upload it to the ESP32 board. So, using a Micro-USB Data Cable, connect the ESP32 Board to your PC.
ESP32 Micropython upycraft
  • Select Tools >>>Burn Firmware from the uPyCraft IDE.
  • Choose from the options listed in the image below. Select the bin file that you recently downloaded (explained above). After that, click OK.

As a result, the firmware will begin to upload. To start the procedure on various ESP32 boards, you must hit the boot button.

The window will automatically close after the uploading is completed.

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

1. Now go to File and create a new file with the name main.py or any other name you choose.

2. Then, go to Tools->boards. Choose your board as esp32

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

4. Now, in the Tools area, click the connect icon to link the IDE to the ESP32 board.

ESP32 LED Blinking with MicroPython using uPyCraft

Let’s get started with the first program. Connect the LED to the MakePython ESP32 Board’s GPIO5 Pin.

Copy the MicroPython code below and use the download button to upload it to the ESP32 board.

import time
from machine import Pin
led=Pin(5,Pin.OUT) #create LED object from pin5,Set Pin5 to output

while True:
led.value(1) #turn off
time.sleep(0.5)
led.value(0) #turn on
time.sleep(0.5)
ESP32 MicroPython uPyCraft LED Blink Code

You may view the details in the Python shell as soon as you hit the download button, as shown in the above image. The associated LED will also begin to blink.

ESP32 Micropython: Controlling LED using Web Server

Let’s create a small webpage to control the on/off of LEDs using a Web Server. If you want to learn more about the Web Server, check out the ESP32 Web Server Tutorial.

Copy the following MicroPython code and use the download button to upload it to the ESP32 Board. Change the wifi SSID and password credentials in the code below.

#webserver.py
import network
import webrepl
import time
from machine import Pin
try:
import usocket as socket
except:
import socket

AUTH_OPEN = 0
AUTH_WEP = 1
AUTH_WPA_PSK = 2
AUTH_WPA2_PSK = 3
AUTH_WPA_WPA2_PSK = 4

SSID = “************” #Modify here with SSID
PASSWORD = “*************” #Modify here with PWD
led = Pin(5, Pin.OUT)

def web_page():
html = “””<html><head><meta name=”viewport” content=”width=device-width, initial-scale=1″></head>
<body><h1>ESP32 Web Server</h1><a href=\”?led=on\”><button>OFF</button></a>&nbsp;
<a href=\”?led=off\”><button>ON</button></a></body></html>”””
return html

def do_connect(ssid,psw):
#import network
#import time

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
s = wlan.config(“mac”)
mac = (‘%02x:%02x:%02x:%02x:%02x:%02x’).upper() %(s[0],s[1],s[2],s[3],s[4],s[5])
print(“Local MAC:”+mac) #get mac
wlan.connect(ssid, psw)
if not wlan.isconnected():
print(‘connecting to network…’ + ssid)
wlan.connect(ssid, psw)

start = time.ticks_ms() # get millisecond counter
while not wlan.isconnected():
time.sleep(1) # sleep for 1 second
if time.ticks_ms()-start > 20000:
print(“connect timeout!”)
break

if wlan.isconnected():
print(‘network config:’, wlan.ifconfig())
return wlan

def connect():
do_connect(SSID,PASSWORD)

def app():
connect()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((”, 80))
s.listen(5)
while True:
conn, addr = s.accept()
print(‘Got a connection from %s’ % str(addr))
request = conn.recv(1024)
request = str(request)
print(‘Content = %s’ % request)
led_on = request.find(‘/?led=on’)
led_off = request.find(‘/?led=off’)
if led_on == 6:
print(‘LED ON’)
led.value(0)
if led_off == 6:
print(‘LED OFF’)
led.value(1)
response = web_page()
conn.send(‘HTTP/1.1 200 OK\n’)
conn.send(‘Content-Type: text/html\n’)
conn.send(‘Connection: close\n\n’)
conn.sendall(response)
conn.close()

app()

In the uPyCraft Shell, you can see the ESP32 Board connecting to the network after the code has been successfully downloaded. Similarly, the Board’s IP Address will be displayed.

ESP32 MicroPython uPyCraft Web Server
  • Copy and paste the IP address into any web browser. As a result, the page will appear as shown below.
  • You can now use the ON and OFF commands to turn the LED on and off, accordingly.

Conclusion

I hope all of you had understand how to use MicroPython on ESP32 using uPyCraft IDE. We will be back soon with more informative blogs.

Leave a Reply

Your email address will not be published.