Trending

Python for NodeMCU / ESP8266

Buy NodeMCU From Aliexpress
Want to program ESP8266 in glorious python language.

Intro

MicroPython is an implementation of Python 3 programming language Optimised to run on microcontrollers and in constrained environments.
It also includes a small subset of the Python standard library and
MicroPython is packed full of advanced features such as an interactive prompt, arbitrary precision integer, closures, list comprehension, generators, exception handling and more. Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM.

MicroPython on ESP8266 Boards

Step 1: Download Firmware

Download MicroPython for ESP8266 board https://micropython.org/download

Step 2: Download NodeMCU flasher

https://github.com/nodemcu/nodemcu-flasher

Step 3: Flashing Firmware

Open NodeMCU flasher located in folder "downloaded zip\Win64\Release\ESP8266Flasher.exe"
In the config, tab pastes the location of the downloaded firmware file (i.e file location of .bin file).
2018-06-19 11_53_59-NODEMCU FIRMWARE PROGRAMMER
In Advanced tab set baud rate to 115200.
2018-06-19 11_55_18-NODEMCU FIRMWARE PROGRAMMER
Finally, in Operation Tab Select the port and Flash.
2018-06-19 11_55_45-Untitled - Open Live Writer

Programming

MicroPython REPL prompt

REPL stands for reading Evaluate Print Loop. It is an Interactive MicroPython prompt that you can access on the ESP8266.
Two ways to access REPL
  • wired connection through the UART serial port
  • via WiFi
To access REPL through serial port simply open Arduino Serial Monitor
And set the baud rate to “115200” and line ending to "Both NL & CR"

Using REPL

Remember type this is Serial Monitor
Just like Python

>>> print('hello esp8266!')
hello esp8266!
>>> 1 + 2
3
>>> 1 / 2
0.5
>>> 12**34
4922235242952026704037113243122008064
To make a pin output
# GPIO 2 is internal LED Pin on NodeMCU
pin = machine.Pin(2, machine.Pin.OUT)
# Set its value by
pin.value(0)
pin.value(1)
# OR
pin.off()
pin.on()

No comments