MicroPython On ESP32 Using uPyCraft IDE
MicroPython On ESP32 Using uPyCraft IDE
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.
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 the 4 sections have its own
importance.
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.
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.
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.
2. Then, go to Tools->boards. Choose your board as esp32
4. Now, click on the connect icon in the Tools section to make the connection between IDE
and ESP32 board.
ESP32 LED Blinking with MicroPython using uPyCraft
Now let us do the first getting started program. For that connect the LED to GPIO5 Pin of
MakePython ESP32 Board.
Now let us do the first getting started 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.
1 import time
5 while True:
7 time.sleep(0.5)
8 led.value(0) #turn on
9 time.sleep(0.5)
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.
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.
1 #webserver.py
2 import network
3 import webrepl
4 import time
6 try:
8 except:
9 import socket
10
11 AUTH_OPEN = 0
12 AUTH_WEP = 1
13 AUTH_WPA_PSK = 2
14 AUTH_WPA2_PSK = 3
15 AUTH_WPA_WPA2_PSK = 4
16
20
21 def web_page():
25 return html
26
27 def do_connect(ssid,psw):
28 #import network
29 #import time
30
31 wlan = network.WLAN(network.STA_IF)
32 wlan.active(True)
33 s = wlan.config("mac")
36 wlan.connect(ssid, psw)
37 if not wlan.isconnected():
39 wlan.connect(ssid, psw)
40
45 print("connect timeout!")
46 break
47
48 if wlan.isconnected():
50 return wlan
51
52 def connect():
53 do_connect(SSID,PASSWORD)
54
55 def app():
56 connect()
57 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
58 s.bind(('', 80))
59 s.listen(5)
60 while True:
63 request = conn.recv(1024)
64 request = str(request)
66 led_on = request.find('/?led=on')
67 led_off = request.find('/?led=off')
68 if led_on == 6:
69 print('LED ON')
70 led.value(0)
71 if led_off == 6:
72 print('LED OFF')
73 led.value(1)
74 response = web_page()
76 conn.send('Content-Type: text/html\n')
77 conn.send('Connection: close\n\n')
78 conn.sendall(response)
79 conn.close()
80
81 app()
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.