In this part you will learn:
How to install MicroPython and the required tools on the (Windows) PC, I have in this tutorial already described. That's why I'll only briefly summarize the steps here:
After the tools (esptool, rshell) are installed on the PC, we delete the memory of the ESP32
and then flash the current one MicroPython image on the ESP32 (use ESP-IDF v3.x, I had problems with v4.x):
To test whether everything really worked, we connect with rshell on the ESP32:
> rshell -p COM5 Using buffer-size of 32 Connecting to COM5 (buffer-size 32)... Trying to connect to REPL . connected Testing if ubinascii.unhexlify exists ... Y Retrieving root directories ... /boot.py/ Setting time ... Feb 11, 2021 22:00:58 Evaluating board_name ... pyboard Retrieving time epoch ... Jan 01, 2000 Welcome to rshell. Use the exit command to exit rshell.
If your output looks something like this and shows no errors, everything worked.
Now that MicroPython is installed, we can begin the actual programming.
First we put the file main.py with the following content:
import network import time wlan = network.WLAN(network.STA_IF) wlan.active(True) # Set DHCP host name to recognize the device in your router wlan.config(dhcp_hostname="Heater") # Replace your SSID and password wlan.connect("<SSID>", "<password>") while not wlan.isconnected(): time.sleep(1) print("WiFi connected")
We then copy this file to the ESP32 (rshell is still running), then switch to the REPL and restart the ESP32 with CTRL + D:
I've written a small logger module with which I can display log outputs more comfortably and, if necessary, have them sent to a SYSLOG server later. You can get this module download here and then copy it to the ESP32 or alternatively with upip to install:
In the main.py we then import the module, initialize the logger and replace the print output.
import network import time import ep_logging wlan = network.WLAN(network.STA_IF) wlan.active(True) # Set DHCP host name to recognize the device in your router wlan.config(dhcp_hostname="Heater") # Replace your SSID and password wlan.connect("<SSID>", "<password>") while not wlan.isconnected(): time.sleep(1) logger = ep_logging.colored_logger(appname="main") logger.notice("WiFi connected")
Saccasner Strasse 19