Skip to content

How to program an ESP8266

It is not difficult to program an ESP8266, especially if you already have some experience with embedded devices such as the Arduino. An ESP8266 actually works quite similar to an Arduino. Although you can barely speak about an operating system, your program is embedded in firmware and using some hooks, it is possible to run custom code.

In most Arduino sketches the most important hooks are setup() and loop(). Setup is called when the Arduino is booted up (or reset), while the loop routine runs over and over again forever.

The ESP8266 works in a more or less similar way, except that in only offers one hook, which is called during boot-up of the chip: user_init(), which should be written in file user/user_main.c. To get a loop running, similar to the loop on Arduino, you need to tell the firmware to queue the execution of another function. And if this function eventually asks the OS to reschedule the same function, you will effectively create a similar loop at on the Arduino.

In the example above, the user_init is called automatically by the firmware. In this function, a task is created using statement system_os_task, which points to the loop function (user_procTask). Once the task is created, it is scheduled for execution using the system_os_post statement.

Once the user_procTask function is running, it is supposed to do its stuff and reschedule itself using the system_os_post statement. A os_delay_us is added to give the OS the chance to do other things as well, such as communication with the Wifi module and serial port.

Running an indefinite loop in the user_procTask function is a bad idea, as the chip’s OS will never get the chance to run its internal process, which may result in e.g. not getting any output on the serial bus.

If you’re wondering what the ICACHE_FLASH_ATTR means in earlier example; it’s an instruction for the linker, indicating that the function should be stored in flash memory instead of RAM. It is generally advisable to prefix most functions with ICACHE_FLASH_ATTR in order to save RAM.

Published inTechnoblog


  1. carlos carlos

    Im jus starting working with the ESP8266. The blinky demo worked fine. But now I want to work with UDP Multicast. I have been looking for examples but most of them are programmed using the arduino language or lua. I wonder if you know of an example using the SDK c programming.

  2. chuc chuc

    Thanks for explain, please recommend document for to start programing ESP8266 using SDK. I don’t want programing ESP8266 using Arduino. Thank you so much!

Leave a Reply

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

seventeen − 6 =