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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | static void user_procTask(os_event_t *events); // Loop function static void ICACHE_FLASH_ATTR user_procTask(os_event_t *events) { // Do stuff os_delay_us(1000); system_os_post(user_procTaskPrio, 0, 0 ); } // Setup function void ICACHE_FLASH_ATTR user_init() { system_os_task(loop, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen); system_os_post(user_procTaskPrio, 0, 0 ); } |
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.
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.
You’re right. UDP examples are hard to come by. This page may be of use though: http://www.limpkin.fr/index.php?post/2014/12/07/First-Steps-with-the-ESP8266-03-Development-Board
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!