This board is also supported by MicroPython. This page covers the basics to get you started.
Load MicroPython using esptool
Before you can load MicroPython, you'll need to install esptool
. Follow the instructions found here.
Once you have esptool
installed, you will first want to erase the flash on your QT Py. You can do so by running something similar to the following command. Make sure you update the port to match the serial port to which your board is connected, i.e. change /dev/tty.wchusbserial54780341901
to match your connection.
esptool.py --chip esp32 --port /dev/tty.wchusbserial547803419010 erase_flash
Once the flash is successfully erased, you can load MicroPython by running something similar to the command below. Make sure you update the following:
- The port to match the serial port to which your board is connected, i.e. change /dev/tty.wchusbserial54780341901 to match your connection.
- The firmware.bin file name to match the MicroPython firmware you downloaded, i.e. change firmware.bin to something like esp32-20220117-v1.18.bin.
Load MicroPython using something similar to the following command, with the above changes:
esptool.py --chip esp32 --port /dev/tty.wchusbserial547803419010 write_flash -z 0x1000 firmware.bin
MicroPython Code
To manage and run code, you can use Thonny - set up to be a MicroPython (ESP32) board, then pick the matching serial port.
That's all there is to getting Thonny set up to work with MicroPython!
MicroPython Blink
The first example is blinking the onboard NeoPixel LED.
Download the following code and save it to your QT Py as main.py
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT import time from machine import Pin from neopixel import NeoPixel power_pin = Pin(8, Pin.OUT) # NeoPixel power is on pin 8 power_pin.on() # Enable the NeoPixel Power pin = Pin(5, Pin.OUT) # Onboard NeoPixel is on pin 5 np = NeoPixel(pin, 1) # create NeoPixel driver on pin 5 for 1 pixel while True: np.fill((0, 0, 150)) # Set the NeoPixel blue np.write() # Write data to the NeoPixel time.sleep(0.5) # Pause for 0.5 seconds np.fill((0, 0, 0)) # Turn the NeoPixel off np.write() # Write data to the NeoPixel time.sleep(0.5) # Pause for 0.5 seconds
Once you have the main.py file on your computer, open the main.py file into Thonny, and click Save.
The first time you do this, you will receive the following popup. Click MicroPython Device.
The following window will show up next, allowing you to save the main.py file to your QT Py.
Once saved to your QT Py, in Thonny, click STOP (red button) followed by Run (green arrow button) to load the main.py file and run it.
The NeoPixel LED will begin blinking blue.
That's all there is to blinking the onboard NeoPixel LED using MicroPython!
First, install the urequests
library via the package manager. Click Tools > Manage Packages to open the package manager. Type urequests into the search bar, and click Search on PyPI.
You may be greeted with the following popup. Click Yes.
Once installed, the package manager will show it as installed.
Click Close.
Download the following code and save it to your ESP32 as main.py
Don't forget to change MY_SSID
and MY_PASSWORD
to your WiFi access point name and credentials.
# SPDX-FileCopyrightText: 2022 Ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """MicroPython simple WiFi connection demo""" import time import network import urequests station = network.WLAN(network.STA_IF) station.active(True) # Network settings wifi_ssid = "MY_SSID" wifi_password = "MY_PASSWORD" url = "http://wifitest.adafruit.com/testwifi/index.html" print("Scanning for WiFi networks, please wait...") authmodes = ['Open', 'WEP', 'WPA-PSK', 'WPA2-PSK4', 'WPA/WPA2-PSK'] for (ssid, bssid, channel, RSSI, authmode, hidden) in station.scan(): print("* {:s}".format(ssid)) print(" - Channel: {}".format(channel)) print(" - RSSI: {}".format(RSSI)) print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid)) print(" - Auth: {}".format(authmodes[authmode])) print() # Continually try to connect to WiFi access point while not station.isconnected(): # Try to connect to WiFi access point print("Connecting...") station.connect(wifi_ssid, wifi_password) time.sleep(10) # Display connection details print("Connected!") print("My IP Address:", station.ifconfig()[0]) # Perform HTTP GET request on a non-SSL web response = urequests.get(url) # Display the contents of the page print(response.text)
Follow the same instructions above to load the WiFi code onto your QT Py.
In the Shell, you should see something like the following indicating it could connect to WiFI and read the data from our webserver.
Text editor powered by tinymce.