Python is one of the best programming languages around, it’s easy and efficient. Let’s leave c/c++ behind and program NodeMCU with Python. See how.
Most probably you were programming NodeMCU with Arduino platform (Arduino Language in other words).
Similarly, there is MicroPython Framework using that you can program NodeMCU to the full extent. For that, you have to follow these steps,
- Download firmware file for ESP8266 board.
- Flash NodeMCU with the firmware file using a tool NodeMCU Flasher.
- Download IDE for programming or program directly through Serial (Not recommended).
- Learn the basics of MicroPython.
Flashing the Firmware
1. Downloading the firmware
Open up the MicroPython website and go to download section — http://micropython.org/download.
You will find firmware for few popular boards, go to “Firmware for ESP8266 boards” (NodeMCU is based in esp8266 board) and download the latest version (that is at the top).
2.Flashing the Firmware
Using NodeMCU Flasher (On Windows only)
This is the simplest tool to flash ESP8266 boards.
- To flash downloaded (or any firmware to NodeMCU), download NodeMCU Flasher Directly from here.
- Open NodeMCU Flasher. Select a port where NodeMCU is connected.
- Go to config tab and paste the location of firmware file (without quotes).
- Flash from Operation tab by clicking Flash.
Using esptool (Windows, Linux, Mac)
This is a cross platform tool. It’s actually a python script.
- Install python if not already installed From here.
- Install esptool from
Command prompt or PowerShell in Windows
Terminal in Linux and Mac
pip install esptool
- Erase the flash on esp8266
python -m esptool --port <port> erase_flash
Where <port> is the port where NodeMCU is connected You can check which port it’s connected in Arduino IDE.
- Now flash the firmware
python -m esptool --port <port> --baud 460800 write_flash --flash_size=detect -fm dio 0 <firmware_file_location>
If errors like espcomm_mem_failed occur, change the baud rate by replacing “–baud 460800” to sometimes like “–baud 115200” or “–baud 9600”.
NOTE – Some NodeMCU variants may not require “-fm dio” option or try which method works for you.
Where <port> is the port where NodeMCU is connected.
Here comes the coolest part, programming MicroPython is the easiest.
Two ways to program
1. Using REPL
REPL over the Serial Port
Just fire up the Arduino IDE open Serial Monitor set baud rate to 115200 and line ending to Carriage Return or Both NL or CR.
Now just type python codes as you do in Python command line.
Try this code,
pin = machine.Pin(2, machine.Pin.OUT) # Inbuilt LED pin, GPIO02 pin.value(0) # Turn On the LED pin.value(1) # Turn Off #OR pin.off() pin.on()
WebREPL — Prompt over Wi-Fi
After fresh install, the device will open Wi-Fi Access Point (AP), with ESSID MicroPython- with password “micropythoN”, no need to go more here we will configure this with Serial prompt, it’s easier.
By Uploading files.
You can just place python files like any other files on your PC, including folders (Yippee).
For that, we need to configure MicroPython so it will connect to your Wi-Fi and enable WebREPL (disabled initially for security reason)
Using Serial Prompt, type this
import network # STA_IF Interface (Station Interface) sta_if = network.WLAN(network.STA_IF) # Activate station interface: sta_if.active(True) # Then connect to your Wi-Fi sta_if.connect('Your_SSID', 'WiFi_Password') # Check if connected sta_if.isconnected() # If connected check config sta_if.ifconfig()
NOTE DOWN THE IP ADDRESS, WE WILL NEED IT LATER
Remember, just like python command line all code is erased on closing the console, same happens hereafter restart, but Wi-Fi and WebREPL configuration will stay.
So after a restart, it will automatically connect to your Wi-Fi.
If you type following code again
#Check if connected sta_if.isconnected() #If connected check config sta_if.ifconfig()
It will show this
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'sta_if' isn't defined
It means objects (or variables) are erased but the configuration like this is saved.
You will be prompted to enable or disable WebREPL and to set a password (REMEMBER THE PASSWORD). Like this
So to solve this we will upload/modify python files on ESP8266. Let’s see how.
Check which files are currently in the device.
import os os.listdir() # Currently existing files will be printed like this >>> ['boot.py']
boot.py is the main file and is executed first so we will edit this file.
Let’s upload files
Download WebREPL Client From Here. This tool has GUI as well as CLI. We will be exploring GUI.
Open webrepl.html that is located in that folder.
- Enter your NodeMCU password and type REPL Password (input will not be shown) you configured earlier.
- In Get a file text box type boot.py, this will download boot.py file to your PC.
- Edit this file, Fading example below, add these lines at end of the file.
We will create blink function because it will block the command line till it’s execution.
However, it can be stopped by pressing CTRL+C (Just like in python) but Arduino IDE doesn’t support sending Key Combination (or signal) from the serial monitor.
You can use other software like Putty
from machine import Pin import time # for delay function #Create PWM object led = Pin(2, Pin.OUT) # Pin 2 (D4) is inbuilt LED # on ESP8266 def blink(): i=0 for x in range(5): # blink 10 times led.on() time.sleep_ms(200) led.off() time.sleep_ms(200) blink()
- Then Upload file
- Restart by pressing the reset button.
To continue exploring more head over to MicroPython ESP8266 Documentation page by clicking here.
MicroPython is a great way to program NodeMCU, especially if you are a beginner in electronics and don’t know much.
However, python performance is very lower than C/C++, means Arduino language is faster than MicroPython.
Also, the community size of MicroPython is still lower than Arduino, so you may struggle to find libraries or solution to a bug if encountered.
And thanks for reading this article, share it to friends, Follow our Social Media Pages.