ESP32 Micropython Programs: A Powerful Combination for IoT
The Micropython language is a powerful but compact subset of the Python v3 programming language used to program the very advanced ESP32 microcontroller from Espressif®. Used as a sensor node, or "mote", it is perfect for the Internet of Things (IoT).
Micropython was originally developed in late 2013 as a KickStarter campaign by Damien George, from Cambridge University in England.
The ESP32 is a very advanced microprocessor that actually has an operating system when flashed with Micropython. It is so fast it can boot from a cold start and connect to your WiFi network in less than a second!
The ESP32 is also a champion when it comes to power consumption. It has a deep sleep mode that consumes only 0.01 mA (1 mA = 1 milliAmp = 1 one thousandth of an Amp).
That's a theoretical minimum current requirement of 10 microamps, with the ability to operate as a remote sensor on simple AA batteries for years. Having worked with microcontrollers for many years, we think this is a remarkable development, and a substantial paradigm shift from the ease-of-use point of view. If you're familiar with the Arduino revolution, Micropython running on the very powerful ESP32 is arguably as significant.
We have developed an educational kit that includes these components, with a remote control shade motor as a practical outcome that you can actually use in your home. This makes a great gift for anyone interested in learning how to use microcontrollers!
Have you got a geek in your life? Please see the links at the bottom of this page for more info; here is a wiring diagram:
Connecting The ESP32 to Your WiFi Network
When you first apply power, the OLED screen (if it has one) will display this message on the screen, prompting you to connect your WiFi to the AP (Access Point) mode of the ESP32.
It may take up to a minute for the SSID to show up in your list of networks, but if you disconnect from your current WiFi it will show up faster (the disconnect prompts a refresh of the WiFi available network list). The next step is to connect your laptop or PC to this network; here is the connection info you will need ...
SSID Network Name: IoT-xxxxxxx
(xxxxxxx represents the unique chip ID)
Password: aabbccddee
Once you have connected successfully to the ESP32 AP WiFi mode, a wifi configuration screen will appear in your browser at this location (pictured):
IP address: 192.168.4.1
Please type this address into your browser address bar (where you would normally type a website URL).
The ESP32 will have already scanned the local radio wave emissions to generate a list of WiFi access points, and this information will appear in the dropdown selector that you see as the first item on the above WiFi config page.
When you have selected your network SSID and entered your WiFi password, please press the SUBMIT button; this will cause a Javascript pop-up dialog to appear, giving you a final chance to check your entries. Click OK to save this configuation and reboot the ESP32 microcontroller.
When it starts up again, the ESP32 will attempt to connect to your specified network in 'Station' mode. This is normally a very fast sequence, less than a second, and the OLED display will also indicate that you are successfully connected to your WiFi network (pictured).
The ESP32 will also detect the new IP address that the DHCP system in your router assigned to it. It isn't an urgent consideration, but we reccommend that you bypass the DHCP system and assign a static IP address so that it never changes. You will need to login to the administrative section in your router to do this, and although it is generally similar, the process will vary from manufacturer to manufacturer. For more instructions, please search the internet for your router model number, or consult your user manual.
If you have entered incorrect WiFi connection information, or the device cannot connect (for whatever reason), the ESP32 will make 30 attemps to connect (in case you happened to cycle power on your WiFi router) before it gives up. It will timeout at that point and reboot, bringing up the WiFi config screen again. It will continue this loop procedure until you input the correct configuration settings. We will later see where you can simply edit a file with WiFi settings, if you prefer.
There are 3 Micropython files involved in this procedure:
- connect_wifi.py
- config_wifi.txt
- config_wifi.py
Here is the sequence of events and what actually happens when you first apply power to the ESP32 ...
- The auto-execute file (main.py) loads the connect_wifi.py program
- The connect_wifi.py program opens the config_wifi.txt file (it has a JSON key/value structure) and looks for SSID and PWD
- It does not find those settings as they have not yet been entered, so it launches the config_wifi.py file that allows you to set the parameters.
You can examine the code in the above files as an example of how readable Micropython is. We have commented the code carefully to help you understand the program branching.
This is a flowchart showing the details of the process of connecting to your WiFi router.
Please note that there are several different ways to program the ESP32, and we chose Micropython for a number of reasons, as it is easy to understand the code syntax, and the operating system provides very useful advantages.
You can purchase educational kits for blinds and window openers, and our control products in our online DIY automation store.
Stay tuned, if you're interested; we are taking the journey into IoT and automation, writing about every step we take!
Adrian Biffen
Senior Partner
RollerTrol Automation Systems
We sincerely hope you enjoy our advanced technology products; if you have any questions, please contact us at any time!
Articles in this series:
Part 1: Micropython & the ESP32 Microcontroller: Connecting to Your WiFi Router Network (you are here)
Part 2: Micropython for the ESP32 Microcontroller: REPL Communications
Part 3: Micropython for the ESP32: FTP Fast File Transfer
Part 4: Micropython for the ESP32: Pre-loaded Programs
Part 5: Micropython for the ESP32: DC Motor Reversing Switch
Part 6: Micropython for the ESP32: WiFi Blind Motor Educational Kit Components
Part 7: Micropython for the ESP32: Error Codes and Definitions