NodeMCU Sample Projects

First step in exploring the state of firmware already developed is to run a few sample codes on a stable ESP8266 device. I will be compiling and running a few NodeMCU sample projects to familiarize myself with the workflow.

See the NodeMCU in action in the video below:

This post assumes that you have already set up the toolchain.

Blinky on NodeMCU – ESP8266 SDK

Using the ESP8266 SDK provided by Espressif enables us to run a number of already provided sample projects. The most fundamental of programs for an embedded system is a Blinky project (i.e. connecting a LED to a GPIO and toggling the GPIO at a fixed interval to show the embedded system’s “heartbeat”).

Some basic examples are shipped with ESP8266 SDK which was downloaded during the toolchain setup process. You can also look at them at https://github.com/esp8266/source-code-examples

Move to blinky project directory anddit the required paths in project Makefile, namely XTENSA_TOOLS_ROOT and SDK_BASE, to point to the relevant directories. Hence, run the make command:

talha@talha-workstation-1:~/scratch/Projects/dev/esp8266/source-code-examples/blinky$ make
 LD build/app.out
 FW firmware/
 esptool.py v1.2-dev

One the firmware has compiled, the next step is to burn it to flash. Makefile takes care of the addresses on flash to burn the firmware to.

talha@talha-workstation-1:~/scratch/Projects/dev/esp8266/source-code-examples/blinky$ make flash
esptool.py --port /dev/ttyUSB0 write_flash 0x00000 firmware/0x00000.bin 0x40000 firmware/0x40000.bin
esptool.py v1.2-dev
Connecting...
Running Cesanta flasher stub...
Flash params set to 0x0000
Writing 28672 @ 0x0... 28672 (100 %)
Wrote 28672 bytes at 0x0 in 2.5 seconds (91.9 kbit/s)...
Writing 188416 @ 0x40000... 188416 (100 %)
Wrote 188416 bytes at 0x40000 in 16.3 seconds (92.2 kbit/s)...
Leaving...

After a succesful flash, LED connected to GPIO2 can be seen to flash continually. Your embedded system is alive!

Multi-threading Example on NodeMCU – ESP8266_RTOS_SDK

Espressif Git repo provides a freeRTOS port for ESP8266 devices. This enables a whole range of capabilities to be implemented on ESP8266 based device employing the freeRTOS framework. Drivers for UART, GPIO and HW Timer are also provided.

A very good programming guide can be found at this link: http://bbs.espressif.com/viewtopic.php?f=51&t=1023 https://espressif.com/sites/default/files/documentation/20a-esp8266_rtos_sdk_programming_guide_en.pdf

I modified one of its example programs to develop a multi-threading example on NodeMCU. A snippet of the user_init function (program entry point), annotated with comments, is copied below. Do not forget to change the DEMO_AP_SSID and DEMO_AP_PASSWORD for your own WiFi network.

// Init UART0 and set baud rate to 115200 bps
uart_init_new();

printf("SDK version:%s\n", system_get_sdk_version());

// Connect to WiFi AP and print the IP address assigned using DHCP.
wifi_set_opmode(STATION_MODE);
struct station_config * config = (struct station_config *)zalloc(sizeof(struct
station_config));
sprintf(config->ssid, DEMO_AP_SSID);
sprintf(config->password, DEMO_AP_PASSWORD);
wifi_station_set_config(config);
free(config);
wifi_station_connect();

// Set GPIO2 as output.
GPIO_AS_OUTPUT(LED_GPIO_NUMBER);

// Create tasks
xTaskCreate(task_blink, "blink", 256, NULL, 2, NULL);
xTaskCreate(task_hello_keepalive, "hello_keepalive", 256, NULL, 2, NULL);

This sample can be found on my fork of the ESP8266_RTOS_SDK repo. We start with cloning the git repository.

git clone https://github.com/timran1/ESP8266_RTOS_SDK.git

Navigate to examples/multi_thread_blinky directory. Set the SDK_PATH (ESP8266_RTOS_SDK directory) and BIN_PATH (firmare ouput directory) using the following commands:

export SDK_PATH=<sdk-path-here>
export BIN_PATH =<bin-path-here>

Execute the firmware generation script for an interactive screen:

./gen_misc.sh

Alternatively, you can run the make command directly. The following command has my configurations (with 1MB of flash on my ESP8266 device):

make clean && make COMPILE=gcc BOOT=new APP=0 SPI_SPEED=40 SPI_MODE=DIO SPI_SIZE_MAP=2

The following output confirms a successful build:

!!!
 SDK_PATH: /home/talha/scratch/Projects/dev/esp8266/toolchain/ESP8266_RTOS_SDK
 BIN_PATH: /home/talha/scratch/Projects/dev/esp8266/projs/ESP8266_RTOS_SDK-projs/blinky-wifi/bin
No boot needed.
 Generate eagle.flash.bin and eagle.irom0text.bin successully in BIN_PATH
 eagle.flash.bin-------->0x00000
 eagle.irom0text.bin---->0x20000
!!!

Finally, following the directions provided by successful build output, I used the following command to burn the firmware at the appropriate addresses of the flash:

 esptool.py --port /dev/ttyUSB0 write_flash 0x00000 eagle.flash.bin 0x20000 eagle.irom0text.bin

And you are done with the software side of things. Make sure GPIO2 has a LED connected to it (NodeMCU has a blue LED connected already). Also, you can read the messages printed to UART0 by opening the serial port at baud rate of 115200bps. Both these operations are being carried out in their task functions in parallel (thanks to FreeRTOS).

4 comments

  1. I’m newbie in Linux. I followed step by step instructions from you and led flashing successfully with multithread (on / off).

    What is C/C++ IDE which support for debug on nodemcu esp6288?

    Thank you for sharing this knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *