The ESP8266 is an extremely versatile chip that can do a whole lot more than just sit on an ESP-01 module! The simple ESP8266 Blinking LED project was one of my first goals when getting started with developing with this chip because it assures you that you can compile a dirt simple program – which is quite a feat. I used the ESP8266 FreeRTOS SDK, the official version provided by Espressif Systems. I will add further details to the post as we continue into this.
If you need help compiling programs for the ESP8266 using the FreeRTOS SDK, let me know in a mail or comment and I would write an article on that.
Getting the FreeRTOS based SDK template to compile is quite a challenge for those who have not worked with cross compilers before. I will probably cover setting that up in another article. For now, let’s take a look at what goes into blinking an LED using the ESP8266. The hardware used is the ESP-LAUNCHER, again straight from Espressif Systems. I will switch to a breadboard mounted ESP-12 module soon when i receive my package. π
In FreeRTOS, to blink an LED, you need a task that can toggle the state of a GPIO port periodically. If you are not familiar with RTOS tasks or threads, you should take a look at the FreeRTOS official website, where you can find plenty examples or guidelines to write your programs with FreeRTOS. I recommend getting yourself familiarized with the FreeRTOS before starting off with using it with ESP8266.
This program I wrote works in the following manner:
- The ESP8266 starts execution of user_init () after booting up and completing some internal tasks.
- In user_init (), we create a task that can blink an LED. Before creating the task, we declare the target pin as GPIO.
- The task now created is executed in its time slice as decided by the FreeRTOS scheduler.
So what does the blink function do?
The blink function simply toggles the state of an LED. It is of the form:
{toggle GPIO, delay, toggle GPIO, delay}
And the stuff inside those curly brackets just keeps on repeating forever. Note that the tasks in FreeRTOS must never return. If they do, they must delete themselves. But this is not a concern here as we need an infinitely looping task anyway.
The source code from the program (user_main.c) is shown here. You may copy that or just download the ESP8266 blinking LED source files from here:
Download Code (user_main.c)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/* * Title: Blinking LED example * NOTE: Tested with Espressif FreeRTOS SDK v.1.4.0 * Copyright (c) 2016 Pratik Panda * Designed for IoTBlocks Hardware * (Website: http://www.PratikPanda.com) * * MIT License with additional clause: * The author, Pratik Panda, must be notified if the source code is used * in a commercial product or associated with any service that is not * provided free of cost to the end user.The above copyright notice and * this permission notice shall be included in all copies or substantial * portions of the Software. * * Full License text: * http://www.pratikpanda.com/iotblocks/iotblocks-code-license/ */ #include "esp_common.h" #include "gpio.h" // This task will execute forever and blink LED // Note that internal high priority tasks still // execute - such as WiFi stack routines void LEDBlinkTask (void *pvParameters) { while(1) { // Delay and turn on vTaskDelay (300/portTICK_RATE_MS); GPIO_OUTPUT_SET (12, 1); // Delay and LED off vTaskDelay (300/portTICK_RATE_MS); GPIO_OUTPUT_SET (12, 0); } } // User function // All user code goes here. // Note that the user function should exit and not block execution void user_init(void) { printf("SDK version:%s\n", system_get_sdk_version()); // Config pin as GPIO12 PIN_FUNC_SELECT (PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); // This task blinks the LED continuously xTaskCreate(LEDBlinkTask, (signed char *)"Blink", 256, NULL, 2, NULL); } |
nnj
can you include your esp_common.h file as well? thank you!
pratik
It is the same file, unmodified, as in the FreeRTOS SDK v.1.4.0
If you are getting errors on missing definitions, you should put the files gpio.h and gpio.c in the same folder as user_main.c
You may email me if you still face issues, I’d be able to respond faster. π
Taurn
How to compile and upload the code on ESP 8266?
pratik
You will need to compile using the environment provided by Espressif and upload using the flash download tool. You may try to search the process π
vkthakar
hi pratik,
which RTOS lib you used?
i use 1.4.0.
but when i make this code then gpio state not change.
and yes, i have problem that when i use boot loader inducing in 1.4.0 SDK then device not boot up but if i use boot loader of 1.2.0 SDK then device runs correctly.
UART and wifi related functionality works correctly.
animesh sachan
can you/anybody tell me the steps to compile RTOS sdk based program and how to upload the program to the ESP8266? I am very new and familiar with ESP8266 programming in arduino IDE only.
pratik
Hello Animesh, I’d let you know once I have an article on this in this week π
Vasil
Hi Pratik,
Could you post a link to the article about mentioned in your post?
Adam
hello any update for FreeRTOS ESP8266 with Arduino IDE ??
Milton Morazan
Pratik Hi, I’m new to the ESP8266. I want to use the device as a remote control and at the same time serve as a webserver.
as I install your library?
Regards,
Milton Morazan.
pratik
That should be possible with a bit of coding. Let me know more about your project, and if it is a personal project, I can come up with articles that show how to do what you want to do! π
Vishal
Hi,
I want to use ESP12E for taking some sensors data.
For that i want to use freeRTOS sdk. So which IDE I require to write and compile the code into ESP12E.
Thanks.
moksh
Hi,
i am able to compile and load non OS sdk which are downloaded from websites, but i don’t know what are the steps to take care for programming rtos sdk in eclipse.even i didn’t found any article regarding on that.can someone please give an article in detail………
pratik
That definitely sounds like a good idea. I will research this over this week and will let you know if I write an article related to this. Would you prefer Windows or Linux?
moksh
l prefer linux to work,can i have some detail information about programming of
esp8266RTOS
Chris Sparks
You may want to add that you need to add “-ldriver” to the Makefile. Assuming one has build the driver subdirectory in the SDK.
Chris Sparks
I tried this with my NODEMCU and I get no blinking. The pins mappings are different I think so does anyone have a clue to fix this?
OttoS
Hi Chris,
This is perhaps a bit late for you, but may help someone else with the same issue.
My ESP-12 board (and the NodeMCU board, I think) have the on-board LED connected to GPIO2.
You need to adapt Pratik’s code as follows:
Change line 31: GPIO_OUTPUT_SET (12, 1); to GPIO_OUTPUT_SET (2, 1);
Change line 35: GPIO_OUTPUT_SET (12, 0); to GPIO_OUTPUT_SET (2, 0);
Change line 47: PIN_FUNC_SELECT (PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); to PIN_FUNC_SELECT (PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO2);
The blinking code now works (for me)
Regards
Otto
pratik
Thanks for the info, and yes, I confirm that MOST ESP-12E/F variants have the blue LED connected to GPIO2. The Espressif ESP-Launcher has LED on GPIO12.
moksh
Hi pratik,
i got a issue while uploading the ESP_RTOS_SDK, open_ssl_demo example to esp8266-01. i able to compile with crosstool-ng and getting two bin files (eagle.flash.bin——–>0x00000,eagle.irom0text.bin—->0x40000)
,even iam able to upload those files by using flashdownload(flashmode in esp8266),but module is not responding when i make it in run mode.should we follow any instruction in case of RTOS
pratik
You sure that you need to load the file at 0x40000?
And also, make sure the compiled BIN is small enough to fit on ESP-01 module (which has 4MBit flash).
Narendhar
can you give more details about how to use esp8266 rtos sdk
Narendhar
iam using linux environment
pratik
Can write some tutorials on that, what information do you want? Can you provide me a list of articles that you want to have?
Akhil
Hi pratik,
I have tried rtos sdk sample code compilation and flashed the firmware to module but not booting and also not getting boot messages in serial console. What might be the problem?
pratik
Have you put the ESP8266 in flash mode? Check if power is sufficient and serial connection is working well.
Tiffany
Hi pratik,I wonder to know if you have some information about esp8266 mesh.as we know ,mesh demo is based on Non_os,and I just know Freertos a little.There are some difficulities to understand Non_os code,such as user_webserver.c’s function.hope for your replyγ
pratik
I know, the mesh is not documented that well. I can try to figure out and work on it if you have a commercial project and need consulting help. We can discuss the issue on my email “hello [at] pratikpanda.com”. π
petter
Hi pratik, i don’t know how to make a new project , can you help me , please??
pratik
The easiest way is to create a copy of a simple example project. And then see if you can compile it. If you can, just delete all example sources and leave a clean user_main.c
Now use this as a new project template! This will always work.
petter
hi pratik ,so how to adjust makefile ?and if i don’t copy simple example so how to make new project ?
Gyanesh singh
Hi Pratik,
I tried to modify the RTOS example with the code given here by you and built in eclipse environment and it was built and flashed successfully, but I do not get any output.
I am using esp Launcher board with esp test board connected on it.
below is the log and source code.
Please help me find out where I am doing wrong.Thanks in advance
Gyanesh
/************************** log *********************************************/
17:32:00 **** Build of configuration Default for project esp_rtos_sdk_example_2 ****
mingw32-make.exe -f C:/Espressif/examples/ESP8266/esp_rtos_sdk_example_2/Makefile flash
c:/Espressif/utils/ESP8266/esptool.exe -p COM3 -b 256000 write_flash -ff 40m -fm qio -fs 8m 0x00000 firmware/eagle.flash.bin 0x10000 firmware/eagle.irom0text.bin
esptool.py v1.2-dev
Connecting…
Running Cesanta flasher stub…
Flash params set to 0x0020
Writing 32768 @ 0x0… 0 (0 %)1024 (3 %)2048 (6 %)3072 (9 %)4096 (12 %)5120 (15 %)6144 (18 %)7168 (21 %)8192 (25 %)9216 (28 %)10240 (31 %)11264 (34 %)12288 (37 %)13312 (40 %)14336 (43 %)15360 (46 %)16384 (50 %)17408 (53 %)18432 (56 %)19456 (59 %)20480 (62 %)21504 (65 %)22528 (68 %)23552 (71 %)24576 (75 %)25600 (78 %)26624 (81 %)27648 (84 %)28672 (87 %)29696 (90 %)30720 (93 %)31744 (96 %)32768 (100 %)
Wrote 32768 bytes at 0x0 in 1.4 seconds (192.9 kbit/s)…
Writing 249856 @ 0x10000… 0 (0 %)1024 (0 %)2048 (0 %)3072 (1 %)4096 (1 %)5120 (2 %)6144 (2 %)7168 (2 %)8192 (3 %)9216 (3 %)10240 (4 %)11264 (4 %)12288 (4 %)13312 (5 %)14336 (5 %)15360 (6 %)16384 (6 %)17408 (6 %)18432 (7 %)19456 (7 %)20480 (8 %)21504 (8 %)22528 (9 %)23552 (9 %)24576 (9 %)25600 (10 %)26624 (10 %)27648 (11 %)28672 (11 %)29696 (11 %)30720 (12 %)31744 (12 %)32768 (13 %)33792 (13 %)34816 (13 %)35840 (14 %)36864 (14 %)37888 (15 %)38912 (15 %)39936 (15 %)40960 (16 %)41984 (16 %)43008 (17 %)44032 (17 %)45056 (18 %)46080 (18 %)47104 (18 %)48128 (19 %)49152 (19 %)50176 (20 %)51200 (20 %)52224 (20 %)53248 (21 %)54272 (21 %)55296 (22 %)56320 (22 %)57344 (22 %)58368 (23 %)59392 (23 %)60416 (24 %)61440 (24 %)62464 (25 %)63488 (25 %)64512 (25 %)65536 (26 %)66560 (26 %)67584 (27 %)68608 (27 %)69632 (27 %)70656 (28 %)71680 (28 %)72704 (29 %)73728 (29 %)74752 (29 %)75776 (30 %)76800 (30 %)77824 (31 %)78848 (31 %)79872 (31 %)80896 (32 %)81920 (32 %)82944 (33 %)83968 (33 %)84992 (34 %)86016 (34 %)87040 (34 %)88064 (35 %)89088 (35 %)90112 (36 %)91136 (36 %)92160 (36 %)93184 (37 %)94208 (37 %)95232 (38 %)96256 (38 %)97280 (38 %)98304 (39 %)99328 (39 %)100352 (40 %)101376 (40 %)102400 (40 %)103424 (41 %)104448 (41 %)105472 (42 %)106496 (42 %)107520 (43 %)108544 (43 %)109568 (43 %)110592 (44 %)111616 (44 %)112640 (45 %)113664 (45 %)114688 (45 %)115712 (46 %)116736 (46 %)117760 (47 %)118784 (47 %)119808 (47 %)120832 (48 %)121856 (48 %)122880 (49 %)123904 (49 %)124928 (50 %)125952 (50 %)126976 (50 %)128000 (51 %)129024 (51 %)130048 (52 %)131072 (52 %)132096 (52 %)133120 (53 %)134144 (53 %)135168 (54 %)136192 (54 %)137216 (54 %)138240 (55 %)139264 (55 %)140288 (56 %)141312 (56 %)142336 (56 %)143360 (57 %)144384 (57 %)145408 (58 %)146432 (58 %)147456 (59 %)148480 (59 %)149504 (59 %)150528 (60 %)151552 (60 %)152576 (61 %)153600 (61 %)154624 (61 %)155648 (62 %)156672 (62 %)157696 (63 %)158720 (63 %)159744 (63 %)160768 (64 %)161792 (64 %)162816 (65 %)163840 (65 %)164864 (65 %)165888 (66 %)166912 (66 %)167936 (67 %)168960 (67 %)169984 (68 %)171008 (68 %)172032 (68 %)173056 (69 %)174080 (69 %)175104 (70 %)176128 (70 %)177152 (70 %)178176 (71 %)179200 (71 %)180224 (72 %)181248 (72 %)182272 (72 %)183296 (73 %)184320 (73 %)185344 (74 %)186368 (74 %)187392 (75 %)188416 (75 %)189440 (75 %)190464 (76 %)191488 (76 %)192512 (77 %)193536 (77 %)194560 (77 %)195584 (78 %)196608 (78 %)197632 (79 %)198656 (79 %)199680 (79 %)200704 (80 %)201728 (80 %)202752 (81 %)203776 (81 %)204800 (81 %)205824 (82 %)206848 (82 %)207872 (83 %)208896 (83 %)209920 (84 %)210944 (84 %)211968 (84 %)212992 (85 %)214016 (85 %)215040 (86 %)216064 (86 %)217088 (86 %)218112 (87 %)219136 (87 %)220160 (88 %)221184 (88 %)222208 (88 %)223232 (89 %)224256 (89 %)225280 (90 %)226304 (90 %)227328 (90 %)228352 (91 %)229376 (91 %)230400 (92 %)231424 (92 %)232448 (93 %)233472 (93 %)234496 (93 %)235520 (94 %)236544 (94 %)237568 (95 %)238592 (95 %)239616 (95 %)240640 (96 %)241664 (96 %)242688 (97 %)243712 (97 %)244736 (97 %)245760 (98 %)246784 (98 %)247808 (99 %)248832 (99 %)249856 (100 %)
Wrote 249856 bytes at 0x10000 in 10.1 seconds (198.2 kbit/s)…
Leaving…
17:32:13 Build Finished (took 12s.915ms)
/*****************************************************************************************/
modified code is below:
/*******************************Code *********************************************/
/*
* Title: Blinking LED example
* NOTE: Tested with Espressif FreeRTOS SDK v.1.4.0
* Copyright (c) 2016 Pratik Panda
* Designed for IoTBlocks Hardware
* (Website: http://www.PratikPanda.com)
*
* MIT License with additional clause:
* The author, Pratik Panda, must be notified if the source code is used
* in a commercial product or associated with any service that is not
* provided free of cost to the end user.The above copyright notice and
* this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* Full License text:
* http://www.pratikpanda.com/iotblocks/iotblocks-code-license/
*/
#include “esp_common.h”
#include “gpio.h”
// This task will execute forever and blink LED
// Note that internal high priority tasks still
// execute – such as WiFi stack routines
void LEDBlinkTask (void *pvParameters)
{
while(1)
{
// Delay and turn on
vTaskDelay (300/portTICK_RATE_MS);
GPIO_OUTPUT_SET (12, 1);
// Delay and LED off
vTaskDelay (300/portTICK_RATE_MS);
GPIO_OUTPUT_SET (12, 0);
}
}
/******************************************************************************
* FunctionName : user_rf_cal_sector_set
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
* We add this function to force users to set rf cal sector, since
* we don’t know which sector is free in user’s application.
* sector map for last several sectors : ABCCC
* A : rf cal
* B : rf init data
* C : sdk parameters
* Parameters : none
* Returns : rf cal sector
*******************************************************************************/
uint32 user_rf_cal_sector_set(void)
{
flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;
switch (size_map)
{
case FLASH_SIZE_4M_MAP_256_256:
rf_cal_sec = 128 – 5;
break;
case FLASH_SIZE_8M_MAP_512_512:
rf_cal_sec = 256 – 5;
break;
case FLASH_SIZE_16M_MAP_512_512:
case FLASH_SIZE_16M_MAP_1024_1024:
rf_cal_sec = 512 – 5;
break;
case FLASH_SIZE_32M_MAP_512_512:
case FLASH_SIZE_32M_MAP_1024_1024:
rf_cal_sec = 1024 – 5;
break;
default:
rf_cal_sec = 0;
break;
}
return rf_cal_sec;
}
// User function
// All user code goes here.
// Note that the user function should exit and not block execution
void user_init(void)
{
printf(“SDK version:%s\n”, system_get_sdk_version());
// Config pin as GPIO12
PIN_FUNC_SELECT (PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);
// This task blinks the LED continuously
xTaskCreate(LEDBlinkTask, (signed char *)”Blink”, 256, NULL, 2, NULL);
}
/********************************************************************************/
Joseph
I have a slight experience in installing OS to a board using a cross compiler
but could you provide me with some more detail on
getting FreeRTOS onto nodeMCU??
Here’s my email π
jk_9397@hotmail.com
Nitin
Sir how many bins are needed . Suppose i want to make a access point then what i need to do . I m using rtos sdk
Khalil
Hi Pratik. how do you upload and compile the code into ESP8266? I am very new to this and that would help me a lot. Thank you so much
shivpatil
which IDE you are using for the RTOS SDK for esp8266 Launch pad?.
will you also provide some references related to it?
Ajay Kale
Hello,
Everyone it`s easy to write program of blink LED but it is going to hard when we are include button with that.
this is really hard to write a program led with button
swapnil
Hi Pratik,
Could you let me know, how can I control “ESP8266 FreeRTOS SDK β blinking LED” using Blynk application??
Is it possible?
I kindly request you, please send me your suggestion and some references.
Thanks!
Hope to here you soon.
Swapnil.