Kick-Start To MicroPython Using ESP32 ES - Harish Kondoor
Kick-Start To MicroPython Using ESP32 ES - Harish Kondoor
Kick-Start To MicroPython Using ESP32 ES - Harish Kondoor
to
MicroPython
ESP32 / ESP8266
Harish Kondoor
Disclaimer
This eBook has been written for information purposes only. Main aim for
the eBook is to educate MicroPython. Content included is at the best and
tries to minimise errors and be as accurate as possible.
The author (Mr.Harish Kondoor) shall not be responsible for any errors or
omissions.
The author (Mr.Harish Kondoor) is not liable for any damage / loss caused
or alleged directly or indirectly by this eBook.
The external links (video/ resource/ website url) provided throughout the
eBook can be removed or content can be changed / updated at any time
from third-party websites. The author shall not be responsible for that.
The Windows 10 operating system is used for course explanation along
with ESP32 Dev as a development board. Other operating systems like Mac
and Linux will support it if Thonny IDE and Python 3 is installed to the
computer. 80 % of the course also supports ESP8266 development board.
Please check the above compatibility before pursuing this course. Author
shall not be responsible for any technical issues with your computer and its
configuration.
This eBook has been created with the assumption that readers have basic
knowledge in electronics.
Copyright @ 2020 Harish Kondoor, All Rights Reserved
About the Author
Annexure-1
Resources
Module1- Introduction to course
This course is a kick-start to MicroPython. This course has been divided
into different modules and includes required external links.
Why MicroPython?
MicroPython was created by Damien George from Australia and initiated
the ‘Kickstart’ project and backed in 2013 and developed STM32F4 based
development board called ‘PyBoard’ for MicroPython.
MicroPython is the recreated version of Python 3 that runs in the memory
restricted microcontrollers with a minimum of 256KB of ROM and 16KB
of RAM . MicroPython supports chips like ESP32, ESP8266, STM32,
nRF52, W600 etc . MicroPython follows Python 3 syntax which makes it
easy to programme for microcontrollers.The hardware APIs are capable of
handling GPIO pins in microcontrollers. In this course we discuss the
ESP32 dev module as the main controller which has a high level of
flexibility in connecting with sensors, on chip capabilities with onboard
WiFi.
As a maker MicroPython is very easy to learn. If you know Python 3, you
can jump into embedded systems in a short time. And you can develop your
projects in a short period of time compared with other platforms.
Currently MicroPython is used in different companies ranging from STEM
based products to R&D and consumer products.
For the electronics components required for the course, visit Annexure1
Module2- Setting up software for the course
Following software is required to be installed into your PC for this course.
● Python 3
● Thonny IDE
Note: Mentioned software will support in Windows ,Linux and Mac
operating systems. This course mainly discusses using the Windows10
operating system.
2.1 Installing Python 3
Python 3 is mandatory to install to your PC. For the Windows operating
system follow the steps.
Step:1
➢ Check python 3 is installed into your PC by opening command
prompt
➢ Type the command ‘ python --version ’
Step3:
➢ From the ‘Downloads’ tab you can download the latest version of
Python 3 respective to your operating system.
Step4:
➢ Once download is completed. Go to ‘Downloads’ folder in the PC
and find the downloaded ‘Python3.8.*.exe’ file.
Step5:
➢ Right click on the file and click on ‘Run as an administrator’ .
Step6:
➢ Tick on ‘Add Python 3.8 to PATH’ then click on ‘Install Now’
Step7:
➢ Wait for a few minutes until installation is completed.
Step8:
➢ Once installation is completed click on ‘Close’ Tab
Step9:
➢ Check python is installed by following step1
In command prompt if you got response ‘ Python 3.8.* ‘ , then you have
successfully installed Python to your PC.
Note: If this point of time python is not recognized in the command
prompt , then set the path of python3 properly.
2.2 Installing Thonny IDE
Thonny IDE is the one of the simplest IDE available for MicroPython. You
can create code, manage codes, debug, and will be able to flash
MicroPython firmware using Thonny IDE. Other popular IDEs are
uPyCraft and PyCharm.
Step1:
➢ Visit the website: https://thonny.org/
➢ Browse the the folder you want to install and click on ‘Next >’
➢ Wait until installation completes.
Step4:
➢ Click on ‘Finish’ once the installation is successfully completed
Step5:
➢ You can find the Thonny IDE icon from the desktop or find the
application from the ‘Start menu’ . Launch the application.
2.3 Thonny IDE Interface
Step2:
➢ Open Thonny IDE.
➢ Navigate to Tools → Manage plug-ins → Search for ‘esptool’ and
install it.
Step4:
➢ From Port option select the port ( eg:- COM6)
Note: If the port number is not visible, check the quality of the USB
cable used and install the Device driver CP210x or CH340 to PC.
Step5:
➢ Click on the firmware option .
Step6:
➢ From the new window select the Port (ex:- COM6)
➢ Browse to downloaded .bi n file → click on ‘Open’
Step7:
➢ Tick the option ‘Erase flash before installing’
➢ Press the BOOT button in the ESP32 (FLASH button in the case of
ESP8266)
➢ Click on the Install option in the dialog box.
➢ Few seconds(2-3 s) later release the BOOT button from ESP32.
➢ New prompt will open there and you can see the progress of flashing.
Initially it erases the flash memory then installs the MicroPython
firmware .
➢ Once firmware is 100% loaded press on the EN / RESET pin in the
ESP32 (RST in the case of ESP8266).
➢ You have successfully flashed MicroPython firmware to ESP32.
➢ Close the Thonny IDE and disconnect ESP32 from PC.
Step8:
➢ Reconnect ESP32 to your PC
➢ Open Thonny IDE
➢ Go Tool s → Interpreter → Select MicroPython(ESP32 ) →
from Port select the allocated Port (ex:- COM6 )
➢ Press EN / RESET pin in the ESP32
➢ Micropython REPL ( read–eval–print loop ) prompt will load in the
Thonny IDE
For string concatenation, coma (,) or plus (+) sign can be used. See the
second line from the above example.
➢ To execute and save the script, press on the green play icon from
the toolbar of Thonny IDE or use ‘F5’ .
➢ From the Run tab ‘Run current script’ option also can be used.
➢ If the script is not saved, from the new prompt window, you can
choose where to save the script. Is it to the computer or to the device.
➢ Make sure you are providing a .py file extension to the script. (ex:-
MyFirstCode.py )
➢ Then click ‘OK’
Note: The input receiving from the user will save as string by default. If
you want to convert into integer or float in that case refer to line number
5 from the above script.
➢ Output in the REPL
➢ Variable name is a string
➢ Variable age is a string
➢ Variable age_1 converted as an integer using int() before input()
function.
➢ Line 2 and 3 make the x will start from 0, exclude 10 and print the
numbers upto 9 in REPL.
➢ Line 5 and 6 make the x will start from 2, exclude 10 and print the
numbers upto 9 in REPL.
➢ Line 8 and 9 make the x will start from 2, exclude 26 and print the
numbers in REPL with an increment of 3.
If you use another GPIO pin, use a 220 ohm resistor in series to the LED.
for the same project. Which will protect the LED from burning / damage.
➢ See the Inbuilt LED in GPIO2, how it is working in your project.
5.5 DHT11 for measuring temperature and humidity
DHT11 is an ultra-low cost temperature and humidity sensor. MicroPython
firmware for ESP32 comes with an inbuilt module for DHT11. With 5%
tolerance DHT11 can read and measure temperature from a range 0 to 50
degree celsius and humidity with 2 % tolerance and a range 20 to 80%. The
DHT11 module works in a range of 3V to 5V. DHT11 uses 1 wire
communication. If you want to use more accuracy and range go with the
DHT22 sensor module.
Keep a magnet very close to the ESP32 module (top of the metal shield )
then the value will change from above shown value to closer to 25
depending on the strength of the magnet.
5.8 MultiTreading
MultiTreading is a powerful tool that can be used in efficient and high end
programming. ESP32 chips have the capability of executing different
functions virtually at the same time. Advantage is that multiple threads can
work independently at the same time. Here we will be discussing how to
control 3 LEDs blinking at different rates of speed and each and every LED
will be controlled by different threads.
Syntax:
_thread.start_new_tread (name of the function ,(pass parameters as tuple))
➢ Import _thread function. For the easiness of calling it _thread
function we will call it as th
➢ Import sleep function
➢ Import Pin function
➢ Created 3 LED objects to GPIO pins 25, 33, 2 (Inbuilt LED is at
GPIO2)
➢ Created different functions using the keyword def . Functions are
function_led1, function_led2, function_led3
➢ From the multi threading documents it is mandatory to pass
arguments.
➢ Two arguments ‘message ’ and ‘t ’ have been created. You can give
any name instead of ‘message’ and ‘t’ . Where ‘message’ is used to
display LED name and ‘t’ is used to determine the delay time in our
code.
➢ Write the necessary script inside the created function. In this case the
function makes the LED turn it ON and turn it OFF with a delay.
➢ Call the function using _thread syntax.
➢ In the code used try: and except: method to avoid crashing the code.
➢ _thread.error is the possible error when we try for MultiTreading.
➢ See the output in the REPL and how the LEDs are blinking.
Step2:
➢ Create a new file and save to the MicroPython device( ESP32) as
ConnectWiFi.py (You can give any name).
➢ Import the sleep function
➢ Create a new function called connect()
➢ Import the network module
➢ Assigning SSID and Password of your wifi to two variables
➢ Create a WiFi object with Station Mode (station is the object name
here)
Syntax: network.WLAN (network.STA_IF)
➢ Activate the WiFi object using .active (True) command
➢ Connect to WiFi using .connect( ssid, password) method
➢ Check ESP32 is connected to WiFi using .isconnected method
➢ Get the local IP address using .ipconfig() method
Step3:
➢ Create a new file called main.py and save to MicroPython
device(ESP32)
➢ Import the previously created file called ConnectWiFi.py to
main.py using the command import ConnectWiFi
➢ Call the function connect() from ConnectWiFi using the command
ConnectWiFi.connect()
Note: make sure you have saved both files ConnectWiFi.py and main.py
in the ESP32 device.
➢ Disconnect ESP32 and close Thonny IDE.
➢ Reconnect ESP32 and open Thonny IDE.
➢ Select the port from tools tab of Thonny IDE
➢ Press the EN / RESET button in the ESP32 and see the output in the
REPL to get your local ip address allocated by your WiFi router to
ESP32
Step1:
➢ Create a free account in OpenWeatherMap
:https://openweathermap.org/
➢ You will receive a confirmation email from OpenWeatherMap with
an API key to the registered email.
➢ It takes 1 to 2 hours to activate your API key .
➢ Visit the link to know how to call current data using API key
:https://openweathermap.org/current
➢ We can try with the first method.
➢ City name replace with your city name
➢ Your api key is replaced with the API key which you receive from
OpenWeatherMap and paste to the browser url to see the json file
from the service provider.
➢ Suggest to use firefox browser to see extracted json file
➢ Example like temperature, humidity and pressure comes under
‘main’
➢ City name is under ‘name’
Step2:
➢ Keep all the files from 6.1 Auto connect to WiFi in the ESP32
➢ Create another file weatherData.py and save in the ESP32
➢ Make sure the module named urequests or requests is present in the
modules using the command in the REPL, help(‘modules’)
Step3:
➢ Follow the code
Step4:
➢ Open main.py file and connect the weatherData to data function
➢ Used while loop to print this value in a certain interval of time
➢ Save all the codes in the ESP32 and press EN/RESET button in the
ESP32.
➢ See the output in the REPL
Annexure 1
Components list