How to Program NodeMCU (ESP8266) in Python with Zerynth
As reported in this thread in the Zerynth Community Forum, we’re happy to announce that Zerynth officially supports one of the most widely used microcontrollers for IoT and Embedded applications: the insanely inexpensive ESP8266!
The supported board is the NodeMCU v2 but more ESP based boards are coming in the next weeks. In fact, we are expanding the number of supported boards and we want to involve you: can you help us understand the next board to make programmable in Python? You can vote here. The list is very, very huge. Choose your preferred board and try to win it!
About NodeMCU and ESP8266
The ESP8266 is a low-cost Wi-Fi chip with full TCP/IP stack and microcontroller capability produced by Shanghai-based Chinese manufacturer, Espressif Systems.
- 32-bit RISC CPU: Tensilica Xtensa LX106 running at 80 MHz*
- 64 KiB of instruction RAM, 96 KiB of data RAM
- External QSPI flash – 512 KiB to 4 MiB* (up to 16 MiB is supported)
- IEEE 802.11 b/g/n Wi-Fi
- 16 GPIO pins
- SPI, I²C,
- I²S interfaces with DMA (sharing pins with GPIO)
- UART on dedicated pins, plus a transmit-only UART can be enabled on GPIO2
- 1 10-bit ADC
NodeMCU is an open source IoT platform including a firmware which runs on the ESP8266 with the Espressif Non-OS SDK, and hardware based on the ESP-12 module. The board features 4MB of SPI flash, 80MHz of system clock, around 50k of usable RAM and an on chip Wifi Transceiver. Official reference for NodeMCU can be found here.
Connect, Virtualize and Program NodeMCU in Python
First of all, of course, you have to download and install Zerynth Studio.
Then connect the board to your PC. Drivers for the bridge can be downloaded here and are needed for Windows and Mac.
To start using NodeMCU with Zerynth Studio, you have to update the Zerynth Standard Boards package, where you can find all the modules needed.
Then you can flash the Zerynth Virtual Machine into the board. There’s no need to run creepy scripts or to copy/paste code from here and there in the web. Thanks to Zerynth, you can make your board programmable in Python, just with a few clicks. Follow this nice tutorial to get started.
In the Zerynth Official Documentation, you can find the details of the Zerynth Virtual Machine dedicated to this board. In the figure below is reported the board pinout.
“Hello World” with NodeMCU in Python
Now you can start to program your NodeMCU in Python!
Let’s start with the “Hello Zerynth” project:
- open the Examples browser clicking on the “light bulb” icon on the left toolbar of the ZERYNTH IDE;
- select the example you prefer or use the search bar. In this case, “Hello Zerynth”;
- click on the chosen example;
- clone it by clicking on the dedicated button;
- At this stage, Zerynth Studio converts the example in a new project giving you the possibility to edit the Title, the Description and the Tags. Remember that Zerynth Studio doesn’t allow two projects to have the same name. If you are trying to clone an example that you already cloned in the past leaving the default Title, then you need to give a different title to this new project.
- click on “Clone” once again and you are done!
Now you have to uplink the project to the board clicking the dedicated button on the upper left toolbar.
The example is very simple: it just opens the default serial port and prints the string “Hello ZERYNTH!” every 1000 ms on the serial console.
ESP8266 Wifi Support
The Wifi chip of the NodeMCU can be used by installing lib.espressif.esp8266wifi and using it like this:
# import the wifi interface
from wireless import wifi
# import wifi support
from espressif.esp8266wifi import esp8266wifi as wifi_driver
# init the wifi driver!
# The driver automatically registers itself to the wifi interface
# with the correct configuration for the selected board
# use the wifi interface to link to the Access Point
# change network name, security and password as needed
# FOR THIS EXAMPLE TO WORK, "Network-Name" AND "Wifi-Password" MUST BE SET
# TO MATCH YOUR ACTUAL NETWORK CONFIGURATION
except Exception as e:
print("ooops, something wrong while linking :(", e)
Program your NodeMCU in Python!
Now it’s your turn! You have the tools to program this amazing board with the simplicity of Python.
Follow the Zerynth Programming Guide hints and tips and show us what you can do!