wESP32 MicroPython demo: dimmable LED light controlled from dweet.io
Various scripts are provided to simplify some of the complexities that come with using a powerful firmware like MicroPython. Note that error handling in these scripts is minimal, this is done to keep them clear so they can be used to learn how each operation works. And because I'm lazy. :)
In the minimum hardware configuration, you can just connect a low power LED with current limiting resistor between IO23 and GND. For practical light output, you can connect IO23 to the PWM control input of a high power LED driver that powers the LED from V+ (12 V).
All my development is done on Ubuntu Linux. If you use a different platform you may need to adjust some of these installation steps and some of the scripts to your platform.
All the helper scripts get their serial port setting from the
wesp32-port.sh script, this makes it easy to adjust the serial port setting to your system by just editing this single file.
Binaries for MicroPython can be found on the MicroPython download page. Download a binary for the ESP32 platform to use on the wESP32.
To be able to flash the MicroPython image to the wESP32, you need to have Espressif's esptool.py installed. This can be done by running:
sudo pip3 install esptool
This depends on Python 3, which likely is present on most Linux systems. If you use Python 2 just use
pip instead of
pip3. If you need to install Python 3, you can do so by running:
sudo apt install python3
A variety of tools exist to manage transfer of files from your host to the wESP32 running MicroPython. The one I used in the scripts for this example is Adafruit's ampy. This can be installed by running:
sudo pip3 install adafruit-ampy
terminal.sh script to get an interactive terminal to the wESP32 uses the
screen command, you can install it by running:
sudo apt install screen
set-light-level.sh script uses
curl to send a value to dweet.io, you can install it by running:
sudo apt install curl
The installation of this demo software to your wESP32 can be done in two steps that are simplified by helper scripts.
A helper script is provided to install a MicroPython image to the wESP32. Just run the following command in the project directory:
Replace the placeholder with the name of your downloaded MicroPython image.
The script not only flashes MicroPython to the wESP32 but it also updates the
boot.py file to automatically configure the ESP32 networking subsystem correctly for the wESP32. This makes it so your own scripts don't even need to bother to do anything to get the LAN connected! You can just access the
lan variable to interact with the
LAN object and get useful information such as the IP address.
Uploading the application files
Since there are no extra modules this project depends on, we can now upload the files for the application to the wESP32. This is automated with another script, so you can just run:
This will send the application file to the wESP32, reset and print the wESP32's IP address, the board's unique ID and a dweet.io URL that can be used to control the light level. It will also write the unique ID to a file called
uid.txt that is used by the
set-light-level.sh script to access the correct URL that will be monitored by the board. The application will automatically start to run after boot.
Other helper scripts
Some other helper scripts are provided that can make your life easier:
get-ip-uid.shprints the IP address, unique ID and dweet.io URL of the connected wESP32. This only works when MicroPython and the
boot.pyscript have been installed.
reset.shcan be used to reset the connected wESP32. This only works when MicroPython has been installed.
screento open an interactive terminal session to the wESP32. Very handy to access the MicroPython REPL command line to interactively get yourself familiar with the environment. If the application is running, you need to hit Ctrl-C to get the command line.
set-light-level.shcan be used to send the provided light level parameter to the dweet.io endpoint monitored by the board. It uses the
uid.txtfile saved by
upload-app.shto construct the correct URL.
Information on the Python code
The Python application code for this demo is quite simple. It depends on the
boot.py code to initialize the Ethernet peripheral correctly for the wESP32. Then it initializes the PWM output that will drive the LED. The ESP32's unique ID is read out and turned into a string.
After this the main loop starts. It constructs the correct URL, then uses the built-in
urequests module to get the data from dweet.io, parses it as JSON and extracts the
light value we're interested in. Then this value is used to set the PWM output.
If anything goes wrong in this sequence, the exception handler makes sure the PWM output doesn't get changed. Whether everything works or not, the loop sleeps for 2 seconds and then does the same thing again.