0% found this document useful (0 votes)
61 views

MicroPython On ESP32 Using uPyCraft IDE

This document discusses using MicroPython on an ESP32 board with the uPyCraft IDE. It covers installing Python, uPyCraft, flashing firmware onto the ESP32, and writing code to blink an LED and control an LED using a web server.

Uploaded by

Chaztan Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

MicroPython On ESP32 Using uPyCraft IDE

This document discusses using MicroPython on an ESP32 board with the uPyCraft IDE. It covers installing Python, uPyCraft, flashing firmware onto the ESP32, and writing code to blink an LED and control an LED using a web server.

Uploaded by

Chaztan Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

MicroPython on ESP32 using uPyCraft

IDE (EXAMPLE: LED BLINKING).


SOURCE: https://how2electronics.com/esp32-micropython-upycraft-getting-started/

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.

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/

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 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.

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.

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

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.
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

2 from machine import Pin

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

5 while True:

6 led.value(1) #turn off

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

5 from machine import Pin

6 try:

7 import usocket as socket

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

17 SSID = "************" #Modify here with SSID

18 PASSWORD = "*************" #Modify here with PWD

19 led = Pin(5, Pin.OUT)

20

21 def web_page():

22 html = """<html><head><meta name="viewport" content="width=device-width, initial-


scale=1"></head>
23
<body><h1>ESP32 Web Server</h1><a href=\"?led=on\"><button>OFF</button></a>&nbsp;
24 <a href=\"?led=off\"><button>ON</button></a></body></html>"""

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")

34 mac = ('%02x:%02x:%02x:%02x:%02x:%02x').upper() %(s[0],s[1],s[2],s[3],s[4],s[5])

35 print("Local MAC:"+mac) #get mac

36 wlan.connect(ssid, psw)

37 if not wlan.isconnected():

38 print('connecting to network...' + ssid)

39 wlan.connect(ssid, psw)

40

41 start = time.ticks_ms() # get millisecond counter

42 while not wlan.isconnected():

43 time.sleep(1) # sleep for 1 second

44 if time.ticks_ms()-start > 20000:

45 print("connect timeout!")

46 break

47

48 if wlan.isconnected():

49 print('network config:', wlan.ifconfig())

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:

61 conn, addr = s.accept()

62 print('Got a connection from %s' % str(addr))

63 request = conn.recv(1024)

64 request = str(request)

65 print('Content = %s' % 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()

75 conn.send('HTTP/1.1 200 OK\n')

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.

You might also like