Espressif IoT SDK:
Programming Guide
Status Released
Current version V0.9.1
Author Fei Yu
Completion Date 2014.9.23
Reviewer Jiangang Wu Completion Date 2014.9.23
[ ] CONFIDENTIAL
[ ] INTERNAL
[ ] PUBLIC
Version Info
Date Version Author Comments/Changes 2013.12.25 0.1 Jiangang Wu Draft
2013.12.25 0.1.1Han Liu Revise Json APIs
2014.1.15 0.2 Jiangang Wu Revise some APIs
2014.1.29 0.3 Han Liu Add client/server APIs
2014.3.20 0.4 Jiangang Wu
/ Han Liu 1.Add UART APIs;
2.Add i2c master APIs;
3.Revise client/server APIs;
4.Add secure espconn APIs;
5.Add upgrade APIs
2014.4.17 0.5 Jiangang Wu
/ Han Liu 1.Revise espconn APIs;
2.Add gpio APIs instruction;
3.Add some other instructions;
2014.5.14 0.6 Jiangang Wu Add some APIs
2014.6.18 0.7 Jiangang Wu 1.Add dns APIs;
2.Revise save-param APIs;
3.Add task APIs;
4.Add API to get connected station’s
info when ESP8266 in softAP mode;
5. Add API to get free heap size.
6.Others
2014.7.10 0.8 Fei Yu 1.Add OTA APIs;
2.Add API to ON/OFF printf;
3.Add wifi_station_get_connect_status
4.Add TCP Server(SSL) APIs
2014.8.13 0.9 Fei Yu 1.Revise espconn APIs;
2.Add sniffer APIs;
3.Add system_get_chip_id;
4.Add APIs to read/ write mac&ip;2014.9.23 0.9.1 Fei Yu1、Add system_deep_sleep;
2、Revise APIs to read/write flash;
3、Add APIs for AP_CHE
4、Revise UDP APIs
Disclaimer and Copyright Notice
Information in this document, including URL references, is subject to change without notice.
THIS DOCUMENT IS PROVIDED "AS IS" WITH NO WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR ANY PARTICULAR PURPOSE, OR ANY WARRANTY OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE. All liability, including liability for infringement of any proprietary rights, relating to use of information in this document is disclaimed. No licenses express or implied, by estoppel or otherwise, to any intellectual property rights are granted herein.
The Wi-Fi Alliance Member Logo is a trademark of the Wi-Fi Alliance.
All trade names, trademarks and registered trademarks mentioned in this document are property of their respective owners, and are hereby acknowledged.
Copyright ?2013 Espressif Systems Inc. All rights reserved.
Table of Contents
Version Info (2)
Table of Contents (4)
1.Foreword (8)
2.Overview (9)
3.Application Programming Interface (APIs) (10)
3.1.Timer (10)
3.1.1.os_timer_arm (10)
3.1.2.os_timer_disarm (10)
3.1.3.os_timer_setfn (11)
3.2.System APIs (11)
3.2.1.system_restore (11)
3.2.2.system_restart (11)
3.2.3.system_get_chip_id (12)
3.2.4.system_deep_sleep (12)
3.2.5.system_upgrade_userbin_check (12)
3.2.6.system_upgrade_start (13)
3.2.7.system_upgrade_reboot (13)
3.2.8.system_set_os_print (13)
3.2.9.system_timer_reinit (14)
3.2.10.system_print_meminfo (14)
3.2.11.system_get_free_heap_size (15)
3.2.12.system_os_task (15)
3.2.13.system_os_post (16)
3.2.14.spi_flash_erase_sector (17)
3.2.15.spi_flash_write (17)
3.2.16.spi_flash_read (18)
3.2.17.wifi_get_opmode (18)
3.2.18.wifi_set_opmode (19)
3.2.19.wifi_station_get_config (19)
3.2.20.wifi_station_set_config (20)
3.2.21.wifi_station_connect (20)
3.2.22.wifi_station_disconnect (21)
3.2.23.wifi_station_get_connect_status (21)
3.2.24.wifi_station_scan (22)
3.2.25.scan_done_cb_t (22)
3.2.26.wifi_station_ap_number_set (23)
3.2.27.wifi_station_ap_change (23)
3.2.28.wifi_station_get_current_ap_id (23)
3.2.29.wifi_softap_get_config (24)
3.2.30.wifi_softap_set_config (24)
3.2.31.wifi_softap_get_station_info (24)
3.2.32.wifi_softap_free_station_info (25)
3.2.33.wifi_get_ip_info (26)
3.2.34.wifi_set_ip_info (26)
3.2.35.wifi_set_macaddr (27)
3.2.36.wifi_get_macaddr (28)
3.2.37.wifi_status_led_install (28)
3.2.38.wifi_promiscuous_enable (29)
3.2.39.wifi_set_promiscuous_rx_cb (29)
3.2.40.wifi_get_channel (30)
3.2.41.wifi_set_channel (30)
3.3.Espconn APIs (30)
3.3.1.General APIs (31)
3.3.1.1.espconn_gethostbyname (31)
3.3.1.2.espconn_port (32)
3.3.1.3.espconn_regist_sentcb (32)
3.3.1.4.espconn_regist_recvcb (33)
3.3.1.5.espconn_sent_callback (33)
3.3.1.6.espconn_recv_callback (33)
3.3.1.7.espconn_sent (34)
3.3.2.TCP APIs (34)
3.3.2.1.espconn_accept (34)
3.3.2.2.espconn_secure_accept (35)
3.3.2.3.espconn_regist_time (35)
3.3.2.4.espconn_get_connection_info (36)
3.3.2.5.espconn_connect (36)
3.3.2.6.espconn_connect_callback (36)
3.3.2.7.espconn_disconnect (37)
3.3.2.8.espconn_regist_connectcb (37)
3.3.2.9.espconn_regist_reconcb (38)
3.3.2.10.espconn_regist_disconcb (38)
3.3.2.11.espconn_secure_connect (39)
3.3.2.12.espconn_secure_sent (39)
3.3.2.13.espconn_secure_disconnect (39)
3.3.3.UDP APIs (40)
3.3.3.1.espconn_create (40)
3.3.3.2.espconn_delete (40)
3.4.json APIs (41)
3.4.1.jsonparse_setup (41)
3.4.2.jsonparse_next (41)
3.4.3.jsonparse_copy_value (41)
3.4.4.jsonparse_get_value_as_int (42)
3.4.5.jsonparse_get_value_as_long (42)
3.4.6.jsonparse_get_len (42)
3.4.7.jsonparse_get_value_as_type (43)
3.4.8.jsonparse_strcmp_value (43)
3.4.9.jsontree_set_up (43)
3.4.10.jsontree_reset (44)
3.4.11.jsontree_path_name (44)
3.4.12.jsontree_write_int (44)
3.4.13.jsontree_write_int_array (45)
3.4.14.jsontree_write_string (45)
3.4.15.jsontree_print_next (46)
3.4.16.jsontree_find_next (46)
4.Structure definition (47)
4.1.Timer (47)
4.2.Wifi parameters (47)
4.2.1.station parameters (47)
4.2.2.softap parameters (47)
4.2.3.scan parameters (48)
4.3.json related structure (48)
4.3.1.json structure (48)
4.3.2.json macro definition (50)
4.4.espconn parameters (51)
4.4.1callback function (51)
4.4.2espconn (51)
5.Driver (54)
5.1.GPIO APIs (54)
5.1.1.PIN setting macro (54)
5.1.2.gpio_output_set (54)
5.1.3.GPIO input and output macro (55)
5.1.4.GPIO interrupt (55)
5.1.5.gpio_pin_intr_state_set (56)
5.1.6.GPIO interrupt handler (56)
5.2.UART APIs (56)
5.2.1.uart_init (57)
5.2.2.uart0_tx_buffer (57)
5.2.3.uart0_rx_intr_handler (58)
5.3.i2c master APIs (58)
5.3.1.i2c_master_gpio_init (58)
5.3.2.i2c_master_init (58)
5.3.3.i2c_master_start (59)
5.3.4.i2c_master_stop (59)
5.3.5.i2c_master_setAck (59)
5.3.6.i2c_master_getAck (60)
5.3.7.i2c_master_readByte (60)
5.3.8.i2c_master_writeByte (60)
5.4.pwm (61)
5.4.1.pwm_init (61)
5.4.2.pwm_start (61)
5.4.3.pwm_set_duty (61)
5.4.4.pwm_set_freq (62)
5.4.5.pwm_get_duty (62)
5.4.6.pwm_get_freq (62)
6.Appendix (63)
A.ESPCONN Programming (63)
A.1.TCP Client Mode (63)
A.1.1.Instructions (63)
A.1.2.Steps (63)
A.2.TCP Server Mode (64)
A.2.1.Instructions (64)
A.2.2.Steps (64)
1.Foreword
The SDK based on ESP8266 IoT platform offers users an easy, fast and efficient way to develop IoT devices.
The programming guide provides overview of the SDK as well as details on the API. It is written for embedded software developers to help them program on ESP8266 IoT platform.
2.Overview
The SDK provides a set of interfaces for data receive and transmit functions over the Wi-Fi and TCP/IP layer so programmers can focus on application development on the high level. Users can easily make use of the corresponding interfaces to realize data receive and transmit.
All networking functions on the ESP8266 IoT platform are realized in the library, and are not transparent to users. Instead, users can initialize the interface in user_main.c
void usre_init(void) is the default method provided. Users can add functions like firmware initialization, network parameters setting, and timer initialization in the interface.
The SDK provides an API to handle json, and users can also use self-defined data types to handle the them.
3.Application Programming Interface (APIs) 3.1. Timer
Locate in “\esp_iot_sdk\include\osapi.h”
3.1.1. os_timer_arm
Function: arm timer
Prototype:
void os_timer_arm(ETSTimer *ptimer, uint32_t milliseconds, boolrepeat_flag) Input parameters:
ETSTimer*ptimer——Timer structure
uint32_t milliseconds——Timing, Unit: milisecond
boolrepeat_flag——Whether to repeat the timing
Return:
null
3.1.2. os_timer_disarm
Function: Disarm timer
Prototype:
void os_timer_disarm (ETSTimer *ptimer)
Input parameters:
ETSTimer*ptimer——Timer structure
Return:
null
3.1.3. os_timer_setfn
Function: Set timer callback function
Prototype:
void os_timer_setfn (ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg) Input parameters:
ETSTimer*ptimer——Timer structure
TESTimerFunc*pfunction——timer callback function
void*parg——callback function parameter
Return:
null
3.2. System APIs
Locate in “\esp_iot_sdk \include\user_interface.h”。
3.2.1. system_restore
Function: Reset to default settings
Prototype:
void system_restore(void)
Input parameters:
null
Return:
null
3.2.2. system_restart
Function: Restart
Prototype:
void system_restart(void)
Input parameters:
null
Return:
null
3.2.3. system_get_chip_id
Function: Get chip id
Prototype:
uint32 system_get_chip_id (void)
Input parameters:
null
Return:
Chip id
3.2.
4. system_deep_sleep
Function: Set for deep-sleep mode. Device in deep-sleep mode automatically, every X us wake up once. Everytime device wakes up, it starts from user_init.
Prototype:
void system_deep_sleep(uint32 time_in_us)
parameters:
uint32 time_in_us – during the time (us) device is in deep-sleep
Return:
NULL
3.2.5. system_upgrade_userbin_check
Function: Check userbin
Function definition:
uint8 system_upgrade_userbin_check()
Input parameters:
null
Return:
0x00 : UPGRADE_FW_BIN1 , i.e., user1.bin
0x01 : UPGRADE_FW_BIN2 , i.e., user2.bin
3.2.6. system_upgrade_start
Function: Configure parameters and start upgrade
Function definition:
bool system_upgrade_start (struct upgrade_server_info *server) Parameters:
struct upgrade_server_info *server – server related parameters Return
true: start upgrade
false: upgrade can’t be started.
3.2.7. system_upgrade_reboot
Function: reboot system and use new version
Function definition:
void system_upgrade_reboot (void)
Input parameters:
null
Return:
null
3.2.8. system_set_os_print
Function: Turn on/off print logFunction
Function definition:
void system_set_os_print (uint8onoff)
Input parameters:
uint8 onoff — turn on/off print function;
0x00 : print function off
0x01: print function on
Defaut: print function on
Return:
null
3.2.9. system_timer_reinit
Function: Reinitiate the timer when you need to use microsecond timer Not es: 1. Define USE_US_TIMER;
2. Put system_timer_reinit at the beginning and user_init in the first
sentence.
Function definition:
void system_timer_reinit (void)
Input parameters:
null
Return:
null
3.2.10. system_print_meminfo
Function: Print memory information, including data/rodata/bss/heap Function definition:
void system_print_meminfo (void)
Input parameters:
null
Return:
null
3.2.11. system_get_free_heap_size
Function: Get free heap size
Function definition:
uint32 system_get_free_heap_size(void)
Input parameters:
null
Return:
uint32 ——available heap size
3.2.12. system_os_task
Function: Set up tasks
Function definition:
void system_os_task(os_task_t task, uint8 prio, os_event_t *queue, uint8 qlen)
Input parameters:
os_task_t task——task function
uint8 prio——task priority. 3 priorities are supported, 0/1/2, 0 is the lowest priority.
os_event_t *queue——message queue pointer
uint8 qlen——message queue depth
Return:
null
Example:
#define SIG_RX 0
#define TEST_QUEUE_LEN 4
os_event_t *testQueue;
void test_task (os_event_t *e)
{
switch (e->sig) {
case SIG_RX:
os_printf(“sig_rx %c\n”, (char)e->par);
break;
default:
break;
}
}
void task_init(void)
{
lwipIf0EvtQueue = (os_event_t *)os_malloc(sizeof(os_event_t)*TEST_QUEUE_LEN); system_os_task(test_task, USER_TASK_PRIO_0, testQueue, TEST_QUEUE_LEN); }
3.2.13. system_os_post
Function: send message to task
Function definition:
void system_os_post (uint8 prio, os_signal_t sig, os_param_t par)
Input parameters:
uint8 prio——task priority, corresponding to that you set up
os_signal_t sig——message type
os_param_t par——message parameters
Return:
null
Refer to the example above:
void task_post(void)
{
system_os_post(USER_TASK_PRIO_0, SIG_RX, ‘a’);
}
Printout: sig_rx a
3.2.1
4. spi_flash_erase_sector
Function: erase sector in flash
Note: More details in document “”
Prototype:
SpiFlashOpResult spi_flash_erase_sector (uint16 sec)
Parameters:
uint16 sec –Sector number, the count starts at sector 0, 4KB per sector. Return:
Typedef enum{
SPI_FLASH_RESULT_OK,
SPI_FLASH_RESULT_ERR,
SPI_FLASH_RESULT_TIMEOUT
}SpiFlashOpResult;
3.2.15. spi_flash_write
Function:Write data to flash。
Note: More details in document “”
Prototype:
SpiFlashOpResult spi_flash_write (uint32 des_addr, uint32 *src_addr, uint32 size)
Parameters:
uint32 des_addr - destination address in flash.
uint32 *src_addr – source address of the data.
Uint32 size - length of data
Return:
Typedef enum{
SPI_FLASH_RESULT_OK,
SPI_FLASH_RESULT_ERR,
SPI_FLASH_RESULT_TIMEOUT
}SpiFlashOpResult;
3.2.16. spi_flash_read
Function:Read data from flash.
Note: More details in document “”
Prototype:
SpiFlashOpResult spi_flash_read(uint32 src_addr, uint32 * des_addr, uint32 size)
Parameters:
uint32 src_addr - source address in flash
uint32 * des_addr – destination address to keep data.
Uint32 size - length of data
Return:
Typedef enum{
SPI_FLASH_RESULT_OK,
SPI_FLASH_RESULT_ERR,
SPI_FLASH_RESULT_TIMEOUT
}SpiFlashOpResult;
3.2.17. wifi_get_opmode
Function: get wifi working mode
Function definition:
uint8 wifi_get_opmode (void)
Input parameters:
null
Return:
Wifi working modes:
0x01 means STATION_MODE,
0x02 means SOFTAP_MODE,
0x03 means STATIONAP_MODE.
3.2.18. wifi_set_opmode
Function: set wifi working mode as STATION, SOFTAP or STATION+SOFTAP Note:
Versions before esp_iot_sdk_v0.9.2, need to call system_restart() after this api; after esp_iot_sdk_v0.9.2, need not to restart.
Function definition:
bool wifi_set_opmode (uint8 opmode)
Input parameters:
uint8 opmode——Wifi working modes: 0x01 means STATION_MODE, 0x02 means SOFTAP_MODE, 0x03 means STATIONAP_MODE. Return:
True - succeed;
False - fail.
3.2.19. wifi_station_get_config
Function: get wifi station configuration
Function definition:
bool wifi_station_get_config (struct station_config *config)
Input parameters:
struct station_config *config——wifi station configuration pointer Return:
True - succeed;
False - fail.
3.2.20. wifi_station_set_config
Function: Set wifi station configuration
Note: If wifi_station_set_config is called in user_init , there is no need to call wifi_station_connect after that, ESP8266 will connect to router automatically; otherwise, need wifi_station_connect to connect.
Function definition:
bool wifi_station_set_config (struct station_config *config)
Input parameters:
struct station_config *config——wifi station configuration pointer
Return:
True - succeed;
False - fail.
3.2.21. wifi_station_connect
Function: wifi station connected AP
Note:if ESP8266 has already connected to a router,it‘s necessary to call wifi_station_disconnect first,then call wifi_station_connect to connect.
Function definition:
bool wifi_station_connect (void)
Input parameters:
null
Return:
True - succeed;
False - fail.