2C-ESP8266 SDK Programming Guide en v1.4
2C-ESP8266 SDK Programming Guide en v1.4
2C-ESP8266 SDK Programming Guide en v1.4
Version 1.4
Espressif Systems IOT Team
Copyright 2015
Espressif Systems
2/183
Espressif Systems
Table of Content
1.
Preambles .................................................................................................14
2.
Overview ....................................................................................................15
3.
1.
os_timer_arm ..........................................................................................16
2.
os_timer_disarm .....................................................................................17
3.
os_timer_setfn ........................................................................................17
4.
system_timer_reinit .................................................................................17
5.
os_timer_arm_us ....................................................................................18
3.2.
1.
hw_timer_init ..........................................................................................19
2.
hw_timer_arm ........................................................................................19
3.
hw_timer_set_func ..................................................................................19
4.
3.3.
1.
system_get_sdk_version .........................................................................21
2.
system_restore .......................................................................................21
3.
system_restart ........................................................................................21
4.
system_init_done_cb ..............................................................................22
5.
system_get_chip_id ................................................................................22
6.
system_get_vdd33 .................................................................................23
7.
system_adc_read ...................................................................................23
8.
system_deep_sleep ................................................................................24
9.
system_deep_sleep_set_option ..............................................................24
3/183
Espressif Systems
1.
spi_flash_get_id ......................................................................................42
2.
spi_flash_erase_sector ............................................................................42
3.
spi_flash_write ........................................................................................42
4/183
Espressif Systems
4.
spi_flash_read .........................................................................................43
5.
system_param_save_with_protect .........................................................44
6.
system_param_load ...............................................................................44
7.
spi_flash_set_read_func ..........................................................................45
3.5.
1.
wifi_get_opmode ....................................................................................47
2.
wifi_get_opmode_default ........................................................................47
3.
wifi_set_opmode .....................................................................................48
4.
wifi_set_opmode_current ........................................................................48
5.
wifi_station_get_config ............................................................................48
6.
wifi_station_get_config_default ................................................................49
7.
wifi_station_set_config ............................................................................49
8.
wifi_station_set_config_current ...............................................................50
9.
wifi_station_connect ...............................................................................51
5/183
Espressif Systems
6/183
Espressif Systems
1.
wifi_set_user_fixed_rate ..........................................................................80
2.
wifi_get_user_fixed_rate ..........................................................................81
3.
wifi_set_user_sup_rate ............................................................................81
4.
wifi_set_user_rate_limit ...........................................................................82
5.
wifi_set_user_limit_rate_mask .................................................................83
6.
wifi_get_user_limit_rate_mask .................................................................84
3.7.
1.
wifi_fpm_open .......................................................................................85
2.
wifi_fpm_close .......................................................................................85
3.
wifi_fpm_do_wakeup .............................................................................85
4.
wifi_fpm_do_sleep .................................................................................86
5.
wifi_fpm_set_sleep_type ........................................................................86
6.
wifi_fpm_get_sleep_type ........................................................................87
7.
Example .................................................................................................87
3.8.
1.
esp_now_init ...........................................................................................88
2.
esp_now_deinit .......................................................................................88
3.
esp_now_register_recv_cb .....................................................................89
4.
esp_now_unregister_recv_cb ..................................................................89
5.
esp_now_register_send_cb ....................................................................89
6.
esp_now_unregister_send_cb .................................................................90
7.
esp_now_send .......................................................................................90
8.
esp_now_add_peer ................................................................................91
9.
esp_now_del_peer ..................................................................................92
7/183
Espressif Systems
1.
system_upgrade_userbin_check .............................................................98
2.
system_upgrade_flag_set .......................................................................98
3.
system_upgrade_flag_check ...................................................................98
4.
system_upgrade_start ............................................................................99
5.
system_upgrade_reboot .........................................................................99
3.10.
1.
wifi_promiscuous_enable ......................................................................100
2.
wifi_promiscuous_set_mac ...................................................................100
3.
wifi_set_promiscuous_rx_cb .................................................................101
4.
wifi_get_channel ...................................................................................101
5.
wifi_set_channel ...................................................................................101
3.11.
1.
smartconfig_start ..................................................................................102
2.
smartconfig_stop ..................................................................................104
3.
smartconfig_set_type ............................................................................104
3.12.
1.
sntp_setserver ......................................................................................106
2.
sntp_getserver ......................................................................................106
3.
sntp_setservername .............................................................................106
4.
sntp_getservername .............................................................................107
5.
sntp_init ................................................................................................107
6.
sntp_stop .............................................................................................107
8/183
4.
Espressif Systems
7.
sntp_get_current_timestamp ................................................................107
8.
sntp_get_real_time ................................................................................108
9.
TCP/UDP APIs.........................................................................................111
4.1.
1.
espconn_delete ....................................................................................111
2.
espconn_gethostbyname .....................................................................111
3.
espconn_port .......................................................................................112
4.
espconn_regist_sentcb .........................................................................113
5.
espconn_regist_recvcb .........................................................................113
6.
espconn_sent_callback ........................................................................113
7.
espconn_recv_callback .........................................................................114
8.
espconn_send ......................................................................................114
9.
espconn_sent .......................................................................................115
4.2.
1.
espconn_accept ...................................................................................116
2.
espconn_regist_time .............................................................................116
3.
espconn_get_connection_info ..............................................................117
4.
espconn_connect .................................................................................117
5.
espconn_connect_callback ...................................................................118
6.
espconn_regist_connectcb ...................................................................118
7.
espconn_set_opt ..................................................................................119
8.
espconn_clear_opt ...............................................................................120
9.
espconn_set_keepalive .........................................................................120
9/183
Espressif Systems
1.
espconn_create ....................................................................................135
2.
espconn_sendto ...................................................................................135
3.
espconn_igmp_join ...............................................................................136
3.
espconn_igmp_leave ............................................................................136
4.
espconn_dns_setserver ........................................................................136
4.4.
1.
espconn_mdns_init ...............................................................................137
2.
espconn_mdns_close ...........................................................................138
3.
espconn_mdns_server_register ............................................................138
4.
espconn_mdns_server_unregister .........................................................138
5.
espconn_mdns_get_servername ..........................................................139
6.
espconn_mdns_set_servername ..........................................................139
7.
espconn_mdns_set_hostname .............................................................139
8.
espconn_mdns_get_hostname .............................................................140
9.
espconn_mdns_disable ........................................................................140
10/183
!
5.
Espressif Systems
AT APIs ................................................................................................141
1.
at_response_ok ....................................................................................141
2.
at_response_error .................................................................................141
3.
at_cmd_array_regist .............................................................................141
4.
at_get_next_int_dec ..............................................................................142
5.
at_data_str_copy ..................................................................................142
6.
at_init ....................................................................................................143
7.
at_port_print .........................................................................................143
8.
at_set_custom_info ...............................................................................143
9.
at_enter_special_state ..........................................................................144
1.
jsonparse_setup ...................................................................................147
2.
jsonparse_next .....................................................................................147
3.
jsonparse_copy_value ...........................................................................147
4.
jsonparse_get_value_as_int ..................................................................148
5.
jsonparse_get_value_as_long ...............................................................148
6.
jsonparse_get_len .................................................................................148
7.
jsonparse_get_value_as_type ...............................................................149
8.
jsonparse_strcmp_value .......................................................................149
9.
jsontree_set_up ....................................................................................149
11/183
!
6.
Espressif Systems
Timer ....................................................................................................153
6.2.
1.
2.
3.
4.
5.
6.3.
1.
2.
6.4.
1.
2.
espconn ...............................................................................................159
6.5.
7.
1.
2.
gpio_output_set ....................................................................................164
3.
4.
5.
gpio_pin_intr_state_set .........................................................................165
6.
7.2.
1.
uart_init .................................................................................................167
2.
uart0_tx_buffer ......................................................................................167
3.
uart0_rx_intr_handler ............................................................................168
7.3.
1.
i2c_master_gpio_init .............................................................................169
2.
i2c_master_init ......................................................................................169
3.
i2c_master_start ...................................................................................169
4.
i2c_master_stop ...................................................................................169
5.
i2c_master_send_ack ...........................................................................170
12/183
Espressif Systems
6.
i2c_master_send_nack .........................................................................170
7.
i2c_master_checkAck ...........................................................................170
8.
i2c_master_readByte ............................................................................171
9.
i2c_master_writeByte ............................................................................171
7.4.
8.
1.
pwm_init ...............................................................................................172
2.
pwm_start ............................................................................................173
3.
pwm_set_duty ......................................................................................173
4.
pwm_get_duty ......................................................................................173
5.
pwm_set_period ..................................................................................174
6.
pwm_get_period ..................................................................................174
7.
get_pwm_version ................................................................................174
Appendix..................................................................................................175
8.1.
1.
2.
3.
8.2.
8.3.
8.4.
8.5.
13/183
1.
Espressif Systems
Preambles
ESP8266 WiFi SoC offers a complete and self-contained Wi-Fi networking solution; it can be used to
host applications or to offload Wi-Fi networking functions from another application processor. When
the ESP8266 hosts application, it boots up directly from an external flash. It has an integrated cache
to improve the performance of systems running applications. Alternately, serving as a Wi-Fi adapter,
wireless internet access can be added into any microcontroller-based design with simple
connectivity through UART interface or the CPU AHB bridge interface.
ESP8266EX is amongst the most integrated WiFi chip in the industry; it integrates the antenna
switches, RF balun, power amplifier, low noise receive amplifier, filters, power management modules,
it requires minimal external circuitry, and the entire solution, including front-end module, is designed
to occupy minimal PCB area.
ESP8266EX also integrates an enhanced version of Tensilicas L106 Diamond series 32-bit processor,
with on-chip SRAM, on top of its Wi-Fi functionalities. ESP8266EX is often integrated with external
sensors and other application specific devices through its GPIOs. Codes for such applications are
provided in examples in the SDK.
Sophisticated system-level features include fast sleep/wake switching for energy-efficient VoIP,
adaptive radio biasing for low-power operations, advanced signal processing, spur cancellation and
radio co-existence features for common cellular, Bluetooth, DDR, LVDS, LCD interference mitigation.
The SDK based on ESP8266 IoT platform offers users an easy, fast and efficient way to develop IoT
devices. This 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.
14/183
Espressif Systems
!
2.
Overview
The SDK provides a set of interfaces for data receive and transmit functions over the Wi-Fi and TCP/
IP layers so programmers can focus on application development at a higher level. Users can easily
make use of the corresponding interfaces to receive and transmit data.
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 user_init(void) is the default method provided. Users can add functions like firmware
disabled, ESP8266 station and soft-AP will both be disabled, so please dont call related APIs, and WiFi function can not be used either.
The SDK provides APIs to handle JSON, and users can also use self-defined data types to handle the
them.
Notice
Using non-OS SDK which is single-threaded, the CPU should not take long to execute tasks:
If a task occupies the CPU too long, ESP8266 can't feed the dog, it will cause a watchdog
reset
If interrupt is disabled, CPU can only be occupied in us range and the time should not be
more than 10 us; if interrupt is not disabled, it is suggested that CPU should not be
occupied more than 500 ms.
We suggest using a timer to check periodically, if users need to call os_delay_us or function
while, or function for in timer callback, please do not occupy CPU more than 15 ms.
Using non-OS SDK, please do not call any function defined with ICACHE_FLASH_ATTR in the
interrupt handler.
Read and write RAM has to be aligned by 4 bytes, so please do not cast pointer directly, for
example, please use os_memcpy instead of float temp = *((float*)data);.
If users need to print logs in interrupt handler, please use API os_printf_plus.
15/183
Espressif Systems
3.
Software Timer
Timer APIs can be found in: /esp_iot_sdk/include/osapi.h. Please note that os_timer APIs listed
below are software timers executed in task, hence timer callbacks may not be precisely executed at
the right time; it depends on priority. If you need a precise timer, please use a hardware timer which
can be executed in hardware interrupt. Please refer to hw_timer.c.
For the same timer, os_timer_arm (or os_timer_arm_us) cannot be invoked repeatedly.
os_timer_disarm should be invoked first.
os_timer_setfn can only be invoked when the timer is not enabled, i.e., after
os_timer_disarm or before os_timer_arm (or os_timer_arm_us).
1.
os_timer_arm
Function:
Enable a millisecond timer.
Prototype:
void os_timer_arm (
os_timer_t *ptimer,
uint32_t milliseconds,
bool repeat_flag
)
Parameters:
os_timer_t *ptimer : Timer structure
uint32_t milliseconds : Timing, Unit: millisecond
16/183
Espressif Systems
2.
os_timer_disarm
Function:
Disarm timer
Prototype:
void os_timer_disarm (os_timer_t *ptimer)
Parameters:
os_timer_t *ptimer : Timer structure
Return:
null
3.
os_timer_setfn
Function:
Set timer callback function.
For enabled timer, timer callback has to be set.
Prototype:
void os_timer_setfn(
os_timer_t *ptimer,
os_timer_func_t *pfunction,
void *parg
)
Parameters:
os_timer_t *ptimer : Timer structure
os_timer_func_t *pfunction : timer callback function
void *parg : callback function parameter
Return:
null
4.
system_timer_reinit
Function:
Reinitiate the timer when you need to use microsecond timer
Notes:
1. Define USE_US_TIMER;
2. Put system_timer_reinit at the beginning of user_init , in the first
sentence.
Prototype:
void system_timer_reinit (void)
17/183
Espressif Systems
Parameters:
null
Return:
null
5.
os_timer_arm_us
Function:
Enable a microsecond timer.
Notes:
1. Define USE_US_TIMER;
2. Put system_timer_reinit at the beginning of user_init , in the first
sentence.
Prototype:
void os_timer_arm_us (
os_timer_t *ptimer,
uint32_t microseconds,
bool repeat_flag
)
Parameters:
os_timer_t *ptimer : Timer structure
uint32_t microseconds : Timing, Unit: microsecond, the minimum value is
0x64, the maximum value allowed to input is 0xFFFFFFF
bool repeat_flag : Whether the timer will be invoked repeatedly or not
Return:
null
3.2.
Hardware Timer
APIs in hw_timer.c can not be called when PWM APIs are in use, because they all use the
same hardware timer.
18/183
Espressif Systems
1.
hw_timer_init
Function:
InitializethehardwareISRtimer
Prototype:
void hw_timer_init (
FRC1_TIMER_SOURCE_TYPEsource_type,
u8 req
)
Parameters:
FRC1_TIMER_SOURCE_TYPEsource_type : ISR source of timer
FRC1_SOURCE, timeruseFRC1ISRasISRsource.
NMI_SOURCE, timeruseNMI ISRasISRsource.
u8 req :
0, not autoload
1, autoload mode
Return:
none
2.
hw_timer_arm
Function:
Setatriggertimerdelayto enablethistimer.
Prototype:
void hw_timer_arm (uint32val)
Parameters:
uint32val: Timing
inautoloadmode :
Return:
none
3.
hw_timer_set_func
Function:
Set timer callback function.
19/183
Espressif Systems
4.
#define REG_READ(_r)
#define WDEV_NOW()
uint32 tick_now2 = 0;
void hw_test_timer_cb(void)
{
staticuint16j=0;
j++;
if((WDEV_NOW()-tick_now2)>=1000000)
{
staticu32idx=1;
tick_now2=WDEV_NOW();
os_printf("b%u:%d\n",idx++,j);
j=0;
}
}
voidICACHE_FLASH_ATTRuser_init(void)
{
hw_timer_init(FRC1_SOURCE,1);
hw_timer_set_func(hw_test_timer_cb);
hw_timer_arm(100);
}
20/183
Espressif Systems
3.3.
1.
System APIs
system_get_sdk_version
Function:
none
Return:
SDK version
Example
os_printf(SDK version: %s \n, system_get_sdk_version());
2.
system_restore
Function:
Reset to default settings of following APIs wifi_station_set_auto_connect,
wifi_set_phy_mode, wifi_softap_set_config related, wifi_station_set_config
related, wifi_set_opmode, and APs information recorded by #define AP_CACHE
Note
Call system_restart to restart after reset by system_restore.
Prototype:
void system_restore(void)
Parameters:
null
Return:
null
3.
system_restart
Function:
Restart
Prototype:
void system_restart(void)
21/183
Espressif Systems
Parameters:
null
Return:
null
4.
system_init_done_cb
Function:
Call this API in user_init to register a system-init-done callback.
Note:
wifi_station_scan has to be called after system init done and station
enable.
Prototype:
void system_init_done_cb(init_done_cb_t cb)
Parameter:
init_done_cb_t cb : system-init-done callback
Return:
null
Example:
void to_scan(void)
{ wifi_station_scan(NULL,scan_done); }
void user_init(void)
wifi_set_opmode(STATION_MODE);
system_init_done_cb(to_scan);
}
5.
system_get_chip_id
Function:
Get chip ID
Prototype:
uint32 system_get_chip_id (void)
Parameters:
null
Return:
Chip ID
22/183
Espressif Systems
6.
system_get_vdd33
Function:
Prototype:
uint16 system_get_vdd33(void)
Parameter:
none
Return:
7.
system_adc_read
Function:
Prototype:
uint16 system_adc_read(void)
Parameter:
none
Return:
23/183
Espressif Systems
8.
system_deep_sleep
Function:
Configures chip for deep-sleep mode. When the device is in deep-sleep, it
automatically wakes up periodically; the period is configurable. Upon waking
up, the device boots up from user_init.
Note:
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
9.
system_deep_sleep_set_option
Function:
Call this API before system_deep_sleep to set whether the chip will do RF
calibration or not when next deep-sleep wake up. The option is 1 by default.
Prototype:
bool system_deep_sleep_set_option(uint8 option)
Parameter:
uint8 option :
0 : RF calibration after deep-sleep wake up depends on both the times of
entering deep-sleep (deep_sleep_number, returns to 0 in every power up) and
the byte 108 of esp_init_data_default.bin0127byte.
24/183
Espressif Systems
Return:
true
: succeed
false : fail
10. system_phy_set_rfoption
Function:
Prototype:
uint8 option :
0 :RF calibration after deep-sleep wake up depends on both the times of
entering deep-sleep (deep_sleep_number, returns to 0 in every power up)
and the byte 108 of esp_init_data_default.bin(0127byte).
25/183
Espressif Systems
4 :Disable RF after deep-sleep wake up, just like modem sleep; this has
the least current consumption; the device is not able to transmit or
receive data after wake up.
Return:
none
11. system_phy_set_powerup_option
Function:
Set whether the chip will do RF calibration or not when power up. The option
is 0 by default.
Prototype:
Parameter:
Return:
null
12. system_phy_set_max_tpw
Function:
Prototype:
uint8 max_tpw : maximum value of RF Tx Power, unit : 0.25dBm, range [0, 82]
it can be set refer to the 34th byte (target_power_qdb_0) of
esp_init_data_default.bin(0127byte)
26/183
Espressif Systems
Return:
none
13. system_phy_set_tpw_via_vdd33
Function:
Note:
Prototype:
none
14. system_set_os_print
Function:
Turn on/off print logFunction
Prototype:
void system_set_os_print (uint8 onoff)
Parameters:
uint8 onoff
Note:
onoff==0: print function off
onoff==1: print function on
Default:
print function on
Return:
null
15. system_print_meminfo
Function:
Print memory information, including data/rodata/bss/heap
27/183
Espressif Systems
Prototype:
void system_print_meminfo (void)
Parameters:
null
Return:
null
16. system_get_free_heap_size
Function:
Get free heap size
Prototype:
uint32 system_get_free_heap_size(void)
Parameters:
null
Return:
uint32 : available heap size
17. system_os_task
Function:
Set up tasks
Prototype:
bool system_os_task(
os_task_t
task,
uint8
prio,
os_event_t
*queue,
uint8
qlen
)
Parameters:
os_task_t task
: task function
uint8 prio
the lowest priority. This means only 3 tasks are allowed to set up.
os_event_t *queue
uint8 qlen
28/183
Espressif Systems
Return:
true:
succeed
false: fail
Example:
#define SIG_RX
#define TEST_QUEUE_LEN
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) {
testQueue=(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);
}
18. system_os_post
Function: send message to task
Prototype:
bool system_os_post (
uint8 prio,
os_signal_t sig,
os_param_t par
)
Parameters:
uint8 prio
: message parameters
Return:
true:
succeed
false: fail
29/183
Espressif Systems
19. system_get_time
Function:
Get system time (us).
Prototype:
uint32 system_get_time(void)
Parameter:
null
Return:
System time in microsecond.
20. system_get_rtc_time
Function: Get RTC time, as denoted by the number of RTC clock periods.
Example:
If system_get_rtc_time returns 10 (it means 10 RTC cycles), and
system_rtc_clock_cali_proc returns 5.75 (means 5.75us per RTC cycle), then
the real time is 10 x 5.75 = 57.5 us.
Note:
System time will return to zero because of system_restart, but RTC still
goes on.
reset by pin EXT_RST : RTC memory wont change, RTC timer returns to zero
watchdog reset : RTC memory wont change, RTC timer wont change
power on : RTC memory is random value, RTC timer starts from zero
reset by pin CHIP_EN : RTC memory is random value, RTC timer starts from
zero
Prototype:
uint32 system_get_rtc_time(void)
30/183
Espressif Systems
Parameter:
null
Return:
RTC time
21. system_rtc_clock_cali_proc
Function:
Get RTC clock period.
Note:
RTC clock period has decimal part.
RTC clock period will change according to temperature, so RTC timer is not
very precise.
Prototype:
uint32 system_rtc_clock_cali_proc(void)
Parameter:
null
Return:
RTC clock period (in us), bit11~ bit0 are decimal.
Note:
see RTC demo in Appendix.
Example:
os_printf(clk cal : %d \r\n,system_rtc_clock_cali_proc()>>12);
22. system_rtc_mem_write
Function:
During deep sleep, only RTC still working, so maybe we need to save some
user data in RTC memory. Only user data area can be used by user.
|<--------system data--------->|<-----------------user data--------------->|
|
256 bytes
512 bytes
Note:
RTC memory is 4 bytes aligned for read and write operations. Parameter
des_addr means block number(4 bytes per block). So, if we want to save some
data at the beginning of user data area, des_addr will be 256/4 = 64,
save_size will be data length.
31/183
Espressif Systems
Prototype:
bool system_rtc_mem_write (
uint32 des_addr,
void * src_addr,
uint32 save_size
)
Parameter:
uint32 des_addr
data pointer.
des_addr >=64
void * src_addr
uint32 save_size :
Return:
true:
succeed
false: fail
23. system_rtc_mem_read
Function:
Read user data from RTC memory. Only user data area should be accessed by
the user.
|<--------system data--------->|<-----------------user data--------------->|
|
256 bytes
512 bytes
Note:
RTC memory is 4 bytes aligned for read and write operations. Parameter
src_addr means block number(4 bytes per block). So, to read data from the
beginning of user data area, src_addr will be 256/4=64, save_size will be
data length.
Prototype:
bool system_rtc_mem_read (
uint32 src_addr,
void * des_addr,
uint32 save_size
)
Parameter:
uint32 src_addr
data pointer
64
void * des_addr
uint32 save_size :
32/183
Espressif Systems
Return:
true:
succeed
false: fail
24. system_uart_swap
Function:
UART0 swap. Use MTCK as UART0 RX, MTDO as UART0 TX, so ROM log will not
output from this new UART0. We also need to use MTDO (U0CTS) and MTCK
(U0RTS) as UART0 in hardware.
Prototype:
void system_uart_swap (void)
Parameter:
null
Return:
null
25. system_uart_de_swap
Function:
Disable UART0 swap. Use original UART0, not MTCK and MTDO.
Prototype:
void system_uart_de_swap (void)
Parameter:
null
Return:
null
26. system_get_boot_version
Function:
Get version info of boot
Prototype:
uint8 system_get_boot_version (void)
Parameter:
null
Return:
Version info of boot.
33/183
Espressif Systems
Note:
If boot version >= 3 , you could enable boot enhance mode (refer to
system_restart_enhance)
27. system_get_userbin_addr
Function: Get address of the current running user bin (user1.bin or user2.bin).
Prototype:
uint32 system_get_userbin_addr (void)
Parameter:
null
Return:
Start address info of the current running user bin.
28. system_get_boot_mode
Function: Get boot mode.
Prototype:
uint8 system_get_boot_mode (void)
Parameter:
null
Return:
#define SYS_BOOT_ENHANCE_MODE 0
#define SYS_BOOT_NORMAL_MODE
Note:
Enhance boot mode: can load and run FW at any address;
Normal boot mode: can only load and run normal user1.bin (or user2.bin).
29. system_restart_enhance
Function:
Restarts system, and enters enhance boot mode.
Prototype:
bool system_restart_enhance(
uint8 bin_type,
uint32 bin_addr
)
34/183
Espressif Systems
Parameter:
uint8 bin_type : type of bin
#define SYS_BOOT_NORMAL_BIN
#define SYS_BOOT_TEST_BIN
0
1
// user1.bin or user2.bin
// can only be Espressif test bin
succeed
false: Fail
Note:
SYS_BOOT_TEST_BIN is for factory test during production; you can apply for
the test bin from Espressif Systems.
30. system_update_cpu_freq
Function:
Set CPU frequency. Default is 80MHz.
Note:
System bus frequency is 80MHz, will not be affected by CPU frequency. The
frequency of UART, SPI, or other peripheral devices, are divided from system
bus frequency, so they will not be affected by CPU frequency either.
Prototype:
bool system_update_cpu_freq(uint8 freq)
Parameter:
uint8 freq : CPU frequency
#define SYS_CPU_80MHz
80
succeed
false: fail
31. system_get_cpu_freq
Function:
Get CPU frequency.
Prototype:
uint8 system_get_cpu_freq(void)
Parameter:
null
35/183
Espressif Systems
Return:
CPU frequency, unit : MHz.
32. system_get_flash_size_map
Function:
Parameter:
none
Return:
flash map
33. system_get_rst_info
Function:
= 0,
REANSON_WDT_RST
= 1,
= 2,
36/183
Espressif Systems
= 3,
= 4,
REANSON_DEEP_SLEEP_AWAKE
= 5,
REANSON_EXT_SYS_RST = 6,
};
struct rst_info {
uint32 reason;
// enum rst_reason
uint32 exccause;
uint32 epc1; // the address that error occurred
uint32 epc2;
uint32 epc3;
uint32 excvaddr;
uint32 depc;
};
Prototype:
struct rst_info* system_get_rst_info(void)
Parameter:
none
Return:
34. system_soft_wdt_stop
Function:
none
Return:
none
37/183
Espressif Systems
35. system_soft_wdt_restart
Function:
Parameter:
none
Return:
none
36. system_soft_wdt_feed
Function:
Parameter:
none
Return:
none
37. os_memset
Function:
38/183
Espressif Systems
Return:
none
Example
uint8 buffer[32];
os_memset(buffer, 0, sizeof(buffer));
38. system_show_malloc
Function:
The memory usage which cause memory leak issue may be in the logs, not
ensure, just for reference.
This API is only for debugging. After calling this API, the program may
go wrong, so please do not call it in normal usage.
Prototype:
void system_show_malloc(void)
Parameter:
null
Return:
null
39. os_memcpy
Function:
copy memory
Prototype:
os_memcpy(void *des, void *src, size_t n)
Parameter:
none
39/183
Espressif Systems
Example
uint8 buffer[4] = {0};
os_memcpy(buffer, abcd, 4);
40. os_strlen
Function:
Parameter:
char *s - string
Return:
string length
Example
char *ssid = ESP8266;
os_memcpy(softAP_config.ssid, ssid, os_strlen(ssid));
41. os_printf
Function:
print format
Note
Do not print more than 125 bytes or continuously call this API to print
data, otherwise may cause the data lose.
Prototype:
os_printf(const char *s)
Parameter:
none
Example
os_printf(SDK version: %s \n, system_get_sdk_version());
40/183
Espressif Systems
42. os_bzero
Function:
Parameter:
Return:
none
43. os_delay_us
Function:
Parameter:
Return:
none
44. os_install_putc1
Function:
none
Example:
os_install_putc1((void *)uart1_write_char) in uart_init will set os_printf
to be output from UART 1, otherwise, os_printf default output from UART 0.
41/183
Espressif Systems
3.4.
More details about flash read/write operation in documentation 99A-SDK-Espressif IOT Flash RW
Operation http://bbs.espressif.com/viewtopic.php?f=21&t=413
1.
spi_flash_get_id
Function:
Get ID info of spi flash
Prototype:
uint32 spi_flash_get_id (void)
Parameters:
null
Return:
SPI flash ID
2.
spi_flash_erase_sector
Function:
Erase sector in flash
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.
spi_flash_write
Function:
Write data to flash. Flash read/write has to be 4-bytes aligned.
Prototype:
SpiFlashOpResult spi_flash_write (
uint32 des_addr,
uint32 *src_addr,
uint32 size
)
42/183
Espressif Systems
Parameters:
uint32 des_addr
:length of data
Return:
typedef enum{
SPI_FLASH_RESULT_OK,
SPI_FLASH_RESULT_ERR,
SPI_FLASH_RESULT_TIMEOUT
} SpiFlashOpResult;
4.
spi_flash_read
Function:
size:
length of data
Return:
typedef enum {
SPI_FLASH_RESULT_OK,
SPI_FLASH_RESULT_ERR,
SPI_FLASH_RESULT_TIMEOUT
} SpiFlashOpResult;
Example
uint32 value;
uint8 *addr = (uint8 *)&value;
spi_flash_read(0x3E * SPI_FLASH_SEC_SIZE, (uint32 *)addr, 4);
os_printf("0x3E sec:%02x%02x%02x%02x\r\n", addr[0], addr[1], addr[2],
addr[3]);
43/183
Espressif Systems
5.
system_param_save_with_protect
Function:
Write data into flash with protection. Flash read/write has to be 4-bytes
aligned.
Protection of flash read/write : use 3 sectors (4KBytes per sector) to save
4KB data with protect, sector 0 and sector 1 are data sectors, back up each
other, save data alternately, sector 2 is flag sector, point out which
sector is keeping the latest data, sector 0 or sector 1.
Note:
More details about protection of flash read/write in documentation 99A-SDKEspressif IOT Flash RW Operation http://bbs.espressif.com/viewtopic.php?
f=21&t=413
Prototype:
bool system_param_save_with_protect (
uint16 start_sec,
void *param,
uint16 len
)
Parameter:
uint16 start_sec
truesucceed
falsefail
6.
system_param_load
Function:
Read data which saved into flash with protection. Flash read/write has to be
4-bytes aligned.
Protection of flash read/write : use 3 sectors (4KBytes per sector) to save
4KB data with protect, sector 0 and sector 1 are data sectors, back up each
other, save data alternately, sector 2 is flag sector, point out which
sector is keeping the latest data, sector 0 or sector 1.
44/183
Espressif Systems
Note:
More details about protection of flash read/write in documentation 99A-SDKEspressif IOT Flash RW Operation http://bbs.espressif.com/viewtopic.php?
f=21&t=413
Prototype:
bool system_param_load (
uint16 start_sec,
uint16 offset,
void *param,
)
uint16 len
Parameter:
uint16 start_sec
void *param
: data pointer
uint16 len
Return:
truesucceed
falsefail
7.
spi_flash_set_read_func
Function:
Prototype:
45/183
Espressif Systems
SpiFlashChip *spi,
uint32 src_addr,
uint32 * des_addr,
uint32 size
)
Return:
none
46/183
Espressif Systems
3.5.
wifi_station APIs
and other APIs which set/get configurations of the ESP8266 station can
only be called if the ESP8266 station is enabled.
APIs and other APIs which set/get configurations of the ESP8266 soft-AP can
only be called if the ESP8266 soft-AP is enabled.
wifi_softap
wifi_get_opmode
Function:
get WiFi current operating mode
Prototype:
uint8 wifi_get_opmode (void)
Parameters:
null
Return:
WiFi working modes:
0x01: station mode
0x02: soft-AP mode
0x03: station+soft-AP
2.
wifi_get_opmode_default
Function:
get WiFi operating mode that saved in flash
Prototype:
uint8 wifi_get_opmode_default (void)
Parameters:
null
Return:
WiFi working modes:
0x01: station mode
0x02: soft-AP mode
0x03: station+soft-AP
47/183
Espressif Systems
3.
wifi_set_opmode
Function:
Sets WiFi working mode as station, soft-AP or station+soft-AP, and save it
to flash. Default is soft-AP mode.
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.
This configuration will be saved in flash system parameter area if changed.
Prototype:
bool wifi_set_opmode (uint8 opmode)
Parameters:
uint8 opmode: WiFi operating modes:
0x01: station mode
0x02: soft-AP mode
0x03: station+soft-AP
Return:
true:
succeed
false: fail
4.
wifi_set_opmode_current
Function:
Sets WiFi working mode as station, soft-AP or station+soft-AP, and wont
save to flash
Prototype:
bool wifi_set_opmode_current (uint8 opmode)
Parameters:
uint8 opmode: WiFi operating modes:
0x01: station mode
0x02: soft-AP mode
0x03: station+soft-AP
Return:
true:
succeed
false: fail
5.
wifi_station_get_config
Function:
Get WiFi station current configuration
48/183
Espressif Systems
Prototype:
bool wifi_station_get_config (struct station_config *config)
Parameters:
struct station_config *config : WiFi station configuration pointer
Return:
true:
succeed
false: fail
6.
wifi_station_get_config_default
Function:
Get WiFi station configuration that saved in flash
Prototype:
bool wifi_station_get_config_default (struct station_config *config)
Parameters:
struct station_config *config : WiFi station configuration pointer
Return:
true:
succeed
false: fail
7.
wifi_station_set_config
Function:
Set WiFi station configuration, and save it to flash
Note:
Prototype:
bool wifi_station_set_config (struct station_config *config)
49/183
Espressif Systems
Parameters:
struct station_config *config: WiFi station configuration pointer
Return:
true:
succeed
false: fail
Example
void ICACHE_FLASH_ATTR
user_set_station_config(void)
{
stationConf.bssid_set = 0;
wifi_station_set_config(&stationConf);
}
void user_init(void)
{
user_set_station_config();
}
8.
wifi_station_set_config_current
Function:
Set WiFi station configuration, wont save to flash
Note:
Prototype:
bool wifi_station_set_config_current (struct station_config *config)
50/183
Espressif Systems
Parameters:
struct station_config *config: WiFi station configuration pointer
Return:
true:
succeed
false: fail
9.
wifi_station_connect
Function:
To connect WiFi station to AP
Note:
Do not call this API in user_init. This API need to be called after
system initializes and the ESP8266 station enabled.
Prototype:
bool wifi_station_connect (void)
Parameters:
null
Return:
true:
succeed
false: fail
10. wifi_station_disconnect
Function:
Disconnects WiFi station from AP
Note
Do not call this API in user_init. This API need to be called after system
initializes and the ESP8266 station enabled.
Prototype:
bool wifi_station_disconnect (void)
Parameters:
null
Return:
true:
succeed
false: fail
51/183
Espressif Systems
11. wifi_station_get_connect_status
Function:
Get WiFi connection status of ESP8266 station to AP.
Notice:
If in a special case, called wifi_station_set_reconnect_policy to disable
reconnect, and did not call wifi_set_event_handler_cb to register WiFi event
handler, wifi_station_get_connect_status becomes invalid and can not get the
right status.
Prototype:
uint8 wifi_station_get_connect_status (void)
Parameters:
null
Return:
enum{
STATION_IDLE = 0,
STATION_CONNECTING,
STATION_WRONG_PASSWORD,
STATION_NO_AP_FOUND,
STATION_CONNECT_FAIL,
STATION_GOT_IP
};
12. wifi_station_scan
Function:
Scan all available APs
Note:
Do not call this API in user_init. This API need to be called after system
initializes and the ESP8266 station enabled.
Prototype:
bool wifi_station_scan (struct scan_config *config, scan_done_cb_t cb);
Structure:
struct scan_config {
uint8 *ssid;
// APs ssid
uint8 *bssid;
// APs bssid
uint8 channel;
52/183
Espressif Systems
Parameters:
struct scan_config *config: AP config for scan
if config==null: scan all APs
if config.ssid==null && config.bssid==null && config.channel!=null:
ESP8266 will scan the specific channel.
scan_done_cb_t cb: callback function after scan
Return:
true:
succeed
false: fail
13. scan_done_cb_t
Function:
Callback function for wifi_station_scan
Prototype:
void scan_done_cb_t (void *arg, STATUS status)
Parameters:
void *arg: information of APs that be found, refer to struct bss_info
STATUS status: get status
Return:
null
Example:
wifi_station_scan(&config, scan_done);
static void ICACHE_FLASH_ATTR scan_done(void *arg, STATUS status) {
if (status == OK) {
struct bss_info *bss_link = (struct bss_info *)arg;
bss_link = bss_link->next.stqe_next; //ignore first
...
}
}
14. wifi_station_ap_number_set
Function:
Sets the number of APs that will be cached for ESP8266 station mode.
Whenever ESP8266 station connects to an AP, it keeps caches a record of this
AP's SSID and password. The cached ID index starts from 0.
Note
This configuration will be saved in flash system parameter area if changed.
53/183
Espressif Systems
Prototype:
bool wifi_station_ap_number_set (uint8 ap_number)
Parameters:
uint8 ap_number: the number of APs can be recorded (MAX: 5)
Return:
true:
succeed
false: fail
15. wifi_station_get_ap_info
Function:
Get information of APs recorded by ESP8266 station.
Prototype:
uint8 wifi_station_get_ap_info(struct station_config config[])
Parameters:
struct station_config config[]: information of APs, array size has to be 5.
Return:
The number of APs recorded.
Example:
struct station_config config[5];
int i = wifi_station_get_ap_info(config);
16. wifi_station_ap_change
Function:
Switch ESP8266 station connection to AP as specified
Prototype:
bool wifi_station_ap_change (uint8 new_ap_id)
Parameters:
uint8 new_ap_id : APs record id, start counting from 0.
Return:
true:
succeed
false: fail
17. wifi_station_get_current_ap_id
Function:
Get the current record id of AP.
54/183
Espressif Systems
Prototype:
uint8 wifi_station_get_current_ap_id ();
Parameter:
null
Return:
The index of the AP, which ESP8266 is currently connected to, in the cached
AP list.
18. wifi_station_get_auto_connect
Function:
Checks if ESP8266 station mode will connect to AP (which is cached)
automatically or not when it is powered on.
Prototype:
uint8 wifi_station_get_auto_connect(void)
Parameter:
null
Return:
0:
19. wifi_station_set_auto_connect
Function:
Setting the ESP8266 station to connect to the AP (which is recorded)
automatically or not when powered on. Enable auto-connect by default.
Note:
Call this API in user_init, it is effective in this current power on; call
it in other place, it will be effective in next power on.
This configuration will be saved in flash system parameter area if changed.
Prototype:
bool wifi_station_set_auto_connect(uint8 set)
Parameter:
uint8 set: Automatically connect or not:
0: will not connect automatically
1: to connect automatically
55/183
Espressif Systems
Return:
true:
succeed
false: fail
20. wifi_station_dhcpc_start
Function:
Enable ESP8266 station DHCP client.
Note:
DHCP is enabled by default.
This configuration interacts with static IP API (wifi_set_ip_info):
If DHCP is enabled,, static IP will be disabled;
If static IP is enabled,, DHCP will be disabled;
These settings depend on the last configuration.
Prototype:
bool wifi_station_dhcpc_start(void)
Parameter:
null
Return:
true:
succeed
false: fail
21. wifi_station_dhcpc_stop
Function:
Disable ESP8266 station DHCP client.
Note:
DHCP default enable.
Prototype:
bool wifi_station_dhcpc_stop(void)
Parameter:
null
Return:
true:
succeed
false: fail
56/183
Espressif Systems
22. wifi_station_dhcpc_status
Function: Get ESP8266 station DHCP client status.
Prototype:
enum dhcp_status wifi_station_dhcpc_status(void)
Parameter:
null
Return:
enum dhcp_status {
DHCP_STOPPED,
DHCP_STARTED
};
23. wifi_station_dhcpc_set_maxtry
Function:
Set the maximum number that ESP8266 station DHCP client will try to
reconnect to the AP.
Prototype:
Parameter:
uint8 num -
Return:
truesucceed
falsefail
24. wifi_station_set_reconnect_policy
Function:
Set whether reconnect or not when the ESP8266 station is disconnected from
AP.
Note
We suggest to call this API in user_init
This API can only be called when the ESP8266 station is enabled.
Prototype:
57/183
Espressif Systems
Parameter:
bool set -
Return:
truesucceed
falsefail
25. wifi_station_get_rssi
Function:
Get rssi of the AP which ESP8266 station connected to
Prototype:
sint8 wifi_station_get_rssi(void)
Parameter:
none
Return:
31
failinvalid value.
26. wifi_station_set_hostname
Function:
Set ESP8266 station DHCP hostname
Prototype:
27. wifi_station_get_hostname
Function:
Get ESP8266 station DHCP hostname
58/183
Espressif Systems
Prototype:
char* wifi_station_get_hostname(void)
Parameter:
none
Return:
hostname
28. wifi_softap_get_config
Function:
Get WiFi soft-AP current configuration
Prototype:
bool wifi_softap_get_config(struct softap_config *config)
Parameter:
struct softap_config *config : ESP8266 soft-AP config
Return:
true:
succeed
false: fail
29. wifi_softap_get_config_default
Function:
Get WiFi soft-AP configurations saved in flash
Prototype:
bool wifi_softap_get_config_default(struct softap_config *config)
Parameter:
struct softap_config *config : ESP8266 soft-AP config
Return:
true:
succeed
false: fail
30. wifi_softap_set_config
Function:
Set WiFi soft-AP configuration and save it to flash
Note
59/183
Espressif Systems
In soft-AP + station mode, the ESP8266 soft-AP will adjust its channel
configuration to be the as same as the ESP8266. More details in
appendix or BBS http://bbs.espressif.com/viewtopic.php?f=10&t=324
Prototype:
bool wifi_softap_set_config (struct softap_config *config)
Parameter:
struct softap_config *config :
Return:
true:
succeed
false: fail
31. wifi_softap_set_config_current
Function:
Set WiFi soft-AP configuration, wont save it to flash
Note:
In the soft-AP + station mode, ESP8266 soft-AP will adjust its channel
configuration to be as same as the ESP8266. More details in appendix
or BBS http://bbs.espressif.com/viewtopic.php?f=10&t=324
Prototype:
bool wifi_softap_set_config_current (struct softap_config *config)
Parameter:
struct softap_config *config :
Return:
true:
succeed
false: fail
32. wifi_softap_get_station_num
Function:
uint8 wifi_softap_get_station_num(void)
Parameter:
none
60/183
Espressif Systems
Return:
how many stations connected to ESP8266 soft-AP
33. wifi_softap_get_station_info
Function:
Get connected station devices under soft-AP mode, including MAC and IP
Note:
This API can not get static IP. It can only be used when DHCP enabled.
Prototype:
struct station_info * wifi_softap_get_station_info(void)
Input Parameters:
null
Return:
struct station_info* : station information structure
34. wifi_softap_free_station_info
Function:
Frees the struct station_info by calling the wifi_softap_get_station_info
function
Prototype:
void wifi_softap_free_station_info(void)
Input Parameters:
null
Return:
null
Examples 1 (Getting MAC and IP information):
struct station_info * station = wifi_softap_get_station_info();
struct station_info * next_station;
while(station) {
os_printf(bssid : MACSTR, ip : IPSTR/n,
MAC2STR(station->bssid), IP2STR(&station->ip));
next_station = STAILQ_NEXT(station, next);
os_free(station);
// Free it directly
station = next_station;
}
61/183
Espressif Systems
35. wifi_softap_dhcps_start
Function: Enable ESP8266 soft-AP DHCP server.
Note:
DHCP default enable.
This configuration interacts with static IP API (wifi_set_ip_info):
If enable DHCP, static IP will be disabled;
If enable static IP, DHCP will be disabled;
This will depend on the last configuration.
Prototype:
bool wifi_softap_dhcps_start(void)
Parameter:
null
Return:
true:
succeed
false: fail
36. wifi_softap_dhcps_stop
Function: Disable ESP8266 soft-AP DHCP server.
Note: DHCP default enable.
Prototype:
bool wifi_softap_dhcps_stop(void)
Parameter:
null
Return:
true:
succeed
false: fail
62/183
Espressif Systems
37. wifi_softap_set_dhcps_lease
Function:
Set the IP range that can be got from the ESP8266 soft-AP DHCP server.
Note:
Prototype:
bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please)
Parameter:
struct dhcps_lease {
struct ip_addr start_ip;
struct ip_addr end_ip;
};
Return:
true:
succeed
false: fail
Example:
void dhcps_lease_test(void)
{
struct dhcps_lease dhcp_lease;
const char* start_ip = 192.168.5.100;
const char* end_ip = 192.168.5.105;
dhcp_lease.start_ip.addr = ipaddr_addr(start_ip);
dhcp_lease.end_ip.addr = ipaddr_addr(end_ip);
wifi_softap_set_dhcps_lease(&dhcp_lease);
}
or
void dhcps_lease_test(void)
{
struct dhcps_lease dhcp_lease;
63/183
Espressif Systems
38. wifi_softap_get_dhcps_lease
Function:
Query the IP range that can be got from the ESP8266 soft-AP DHCP server.
Note:
This API can only be called during ESP8266 soft-AP DHCP server enabled.
Prototype:
bool wifi_softap_get_dhcps_lease(struct dhcps_lease *please)
Return:
true:
succeed
false: fail
39. wifi_softap_set_dhcps_lease_time
Function:
Set ESP8266 soft-AP DHCP server lease time, default is 120 minutes.
Note:
This API can only be called during ESP8266 soft-AP DHCP server enabled.
Prototype:
64/183
Espressif Systems
Return:
true:
succeed;
false: fail
40. wifi_softap_get_dhcps_lease_time
Function:
Prototype:
uint32 wifi_softap_get_dhcps_lease_time(void)
Return:
lease time, uint: minute.
41. wifi_softap_reset_dhcps_lease_time
Function:
Reset ESP8266 soft-AP DHCP server lease time which is 120 minutes by
default.
Note:
This API can only be called during ESP8266 soft-AP DHCP server enabled.
Prototype:
bool wifi_softap_reset_dhcps_lease_time(void)
Return:
true:
succeed;
false: faiil
42. wifi_softap_dhcps_status
Function: Get ESP8266 soft-AP DHCP server status.
Prototype:
enum dhcp_status wifi_softap_dhcps_status(void)
Parameter:
null
65/183
Espressif Systems
Return:
enum dhcp_status {
DHCP_STOPPED,
DHCP_STARTED
};
43. wifi_softap_set_dhcps_offer_option
Function:
Set ESP8266 soft-AP DHCP server option.
Structure
enum dhcps_offer_option{
OFFER_START = 0x00,
OFFER_ROUTER = 0x01,
OFFER_END
};
Prototype:
uint8 level -
void* optarg -
default to be enable
: succeed
false : fail
Example:
uint8 mode = 0
wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode);
44. wifi_set_phy_mode
Function:
Set ESP8266 physical mode (802.11b/g/n).
66/183
Espressif Systems
Note:
Prototype:
bool wifi_set_phy_mode(enum phy_mode mode)
Parameter:
enum phy_mode mode : physical mode
enum phy_mode {
PHY_MODE_11B = 1,
PHY_MODE_11G = 2,
PHY_MODE_11N = 3
};
Return:
true
: succeed
false : fail
45. wifi_get_phy_mode
Function:
Get ESP8266 physical mode (802.11b/g/n)
Prototype:
enum phy_mode wifi_get_phy_mode(void)
Parameter:
null
Return:
enum phy_mode{
PHY_MODE_11B = 1,
PHY_MODE_11G = 2,
PHY_MODE_11N = 3
};
46. wifi_get_ip_info
Function:
Get IP info of WiFi station or soft-AP interface
67/183
Espressif Systems
Prototype:
bool wifi_get_ip_info(
uint8 if_index,
struct ip_info *info
)
Parameters:
uint8 if_index : the interface to get IP info: 0x00 for STATION_IF, 0x01 for
SOFTAP_IF.
struct ip_info *info : pointer to get IP info of a certain interface
Return:
true:
succeed
false: fail
47. wifi_set_ip_info
Function:
Set IP address of ESP8266 station or soft-AP
Note:
To set static IP, please disable DHCP first (wifi_station_dhcpc_stop or
wifi_softap_dhcps_stop):
If enable static IP, DHCP will be disabled;
If enable DHCP, static IP will be disabled;
Prototype:
bool wifi_set_ip_info(
uint8 if_index,
struct ip_info *info
)
Prototype:
uint8 if_index
#define STATION_IF
0x00
#define SOFTAP_IF
struct ip_info *info
0x01
:
IP information
Example:
wifi_set_opmode(STATIONAP_MODE); //Set softAP + station mode
struct ip_info info;
wifi_station_dhcpc_stop();
wifi_softap_dhcps_stop();
68/183
Espressif Systems
succeed
false: fail
48. wifi_set_macaddr
Function:
Sets MAC address
Note:
ESP8266 soft-AP and station have different MAC addresses, please do not
set them to be the same.
The bit 0 of the first byte of ESP8266 MAC address can not be 1. For
example, MAC address can be "1a:XX:XX:XX:XX:XX", but can not be
"15:XX:XX:XX:XX:XX".
Prototype:
bool wifi_set_macaddr(
uint8 if_index,
uint8 *macaddr
)
Parameter:
uint8 if_index
#define STATION_IF
0x00
#define SOFTAP_IF
0x01
uint8 *macaddr
MAC address
Example:
wifi_set_opmode(STATIONAP_MODE);
69/183
Espressif Systems
succeed
false: fail
49. wifi_get_macaddr
Function: get MAC address
Prototype:
bool wifi_get_macaddr(
uint8 if_index,
uint8 *macaddr
)
Parameter:
uint8 if_index
#define STATION_IF
0x00
#define SOFTAP_IF
0x01
uint8 *macaddr :
MAC address
Return:
true:
succeed
false: fail
50. wifi_set_sleep_type
Function:
Sets sleep type for power saving. Set NONE_SLEEP_T to disable power saving.
Note: Default to be Modem sleep.
Prototype:
bool wifi_set_sleep_type(enum sleep_type type)
Parameters:
enum sleep_type type
sleep type
Return:
true:
succeed
false: fail
70/183
Espressif Systems
51. wifi_get_sleep_type
Function:
Gets sleep type.
Prototype:
enum sleep_type wifi_get_sleep_type(void)
Parameters:
null
Return:
enum sleep_type {
NONE_SLEEP_T = 0;
LIGHT_SLEEP_T,
MODEM_SLEEP_T
};
52. wifi_status_led_install
Function:
Installs WiFi status LED
Prototype:
void wifi_status_led_install (
uint8 gpio_id,
uint32 gpio_name,
uint8 gpio_func
)
Parameter:
uint8 gpio_id
: GPIO number
PERIPHS_IO_MUX_GPIO0_U
#define HUMITURE_WIFI_LED_IO_NUM
#define HUMITURE_WIFI_LED_IO_FUNC
FUNC_GPIO0
wifi_status_led_install(HUMITURE_WIFI_LED_IO_NUM,
HUMITURE_WIFI_LED_IO_MUX, HUMITURE_WIFI_LED_IO_FUNC)
71/183
Espressif Systems
53. wifi_status_led_uninstall
Function: Uninstall WiFi status LED
Prototype:
void wifi_status_led_uninstall ()
Parameter:
null
Return:
null
54. wifi_set_broadcast_if
Function:
Set ESP8266 send UDP broadcast from station interface or soft-AP interface,
or both station and soft-AP interfaces. Default to be soft-AP.
Note
If set broadcast interface to be station onlyESP8266 softAP DHCP server
will be disable.
Prototype:
bool wifi_set_broadcast_if (uint8 interface)
Parameter:
uint8 interface : 1:station; 2:soft-AP, 3:station+soft-AP
Return:
true:
succeed
false: fail
72/183
Espressif Systems
56. wifi_set_event_handler_cb
Function:
wifi_event_handler_cb_t cb : callback
Return:
none
Example:
void wifi_handle_event_cb(System_Event_t *evt)
{
os_printf("event %x\n", evt->event);
switch (evt->event) {
case EVENT_STAMODE_CONNECTED:
os_printf("connect to ssid %s, channel %d\n",
evt->event_info.connected.ssid,
evt->event_info.connected.channel);
break;
case EVENT_STAMODE_DISCONNECTED:
os_printf("disconnect from ssid %s, reason %d\n",
evt->event_info.disconnected.ssid,
evt->event_info.disconnected.reason);
break;
case EVENT_STAMODE_AUTHMODE_CHANGE:
os_printf("mode: %d -> %d\n",
evt->event_info.auth_change.old_mode,
evt->event_info.auth_change.new_mode);
break;
case EVENT_STAMODE_GOT_IP:
os_printf("ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR,
IP2STR(&evt->event_info.got_ip.ip),
IP2STR(&evt->event_info.got_ip.mask),
IP2STR(&evt->event_info.got_ip.gw));
os_printf("\n");
break;
case EVENT_SOFTAPMODE_STACONNECTED:
os_printf("station: " MACSTR "join, AID = %d\n",
73/183
Espressif Systems
MAC2STR(evt->event_info.sta_connected.mac),
evt->event_info.sta_connected.aid);
break;
case EVENT_SOFTAPMODE_STADISCONNECTED:
os_printf("station: " MACSTR "leave, AID = %d\n",
MAC2STR(evt->event_info.sta_disconnected.mac),
evt->event_info.sta_disconnected.aid);
break;
default:
break;
}
}
void user_init(void)
{
// TODO: add your own code here....
wifi_set_event_hander_cb(wifi_handle_event_cb);
}
57. wifi_wps_enable
Function:
74/183
Espressif Systems
Return:
true:
succeed
false: fail
58. wifi_wps_disable
Function:
Parameter:
none
Return:
true:
succeed
false: fail
59. wifi_wps_start
Function:
Parameter:
none
Return:
true: means that WPS starts to work successfully, does not mean WPS succeed.
false: fail
60. wifi_set_wps_cb
Function:
75/183
Espressif Systems
WPS_CB_ST_FAILED,
WPS_CB_ST_TIMEOUT,
WPS_CB_ST_WEP,
Otherwise, it means that WPS fail, user can create a timer to retry WPS
by wifi_wps_start after a while, or call wifi_wps_disable to disable
WPS and release resource.
Prototype:
bool wifi_set_wps_cb(wps_st_cb_t cb)
Parameter:
wps_st_cb_t cb : callback
Return:
true:
succeed
false: fail
61. wifi_register_send_pkt_freedom_cb
Function:
0, succeed;
76/183
Espressif Systems
-1, fail.
62. wifi_unregister_send_pkt_freedom_cb
Function:
Parameter:
null
Return:
null
63. wifi_send_pkt_freedom
Function:
Packet has to be the whole 802.11 packet, does not include the FCS. The
length of the packet has to be longer than the minimum length of the
header of 802.11 packet which is 24 bytes, and less than 1400 bytes.
The rate of sending packet is same as the management packet which is the
same as the system rate of sending packets.
Do not support encrypted packet, the encrypt bit in the packet has to be
0.
Only after the previous packet was sent, entered the sent callback, the
next packet is allowed to send. Otherwise, wifi_send_pkt_freedom will
return fail.
Prototype:
int wifi_send_pkt_freedom(uint8 *buf, int lenbool sys_seq)
Parameter:
uint8 *buf
int len
: pointer of packet
: packet length
bool sys_seq
: follow the systems 802.11 packets sequence number or
not, if it is true, the sequence number will be increased 1 every time a
packet sent.
77/183
Espressif Systems
Return:
0, succeed;
-1, fail.
64. wifi_rfid_locp_recv_open
Function:
Parameter:
null
Return:
0, succeed;
otherwise, fail.
65. wifi_rfid_locp_recv_close
Function:
Parameter:
null
Return:
null
66. wifi_register_rfid_locp_recv_cb
Function:
Register a callback of receiving WDS packets. Only if the first MAC address
of the WDS packet is a multicast address.
Callback Definition:
typedef void (*rfid_locp_cb_t)(uint8 *frm, int len, int rssi);
Parameter:
uint8 *frm
int len
: packet length
int rssi
: signal strength
78/183
Espressif Systems
Prototype:
int wifi_register_rfid_locp_recv_cb(rfid_locp_cb_t cb)
Parameter:
rfid_locp_cb_t cb : callback
Return:
0, succeed;
otherwise, fail.
67. wifi_unregister_rfid_locp_recv_cb
Function:
null
79/183
Espressif Systems
3.6.
1.
wifi_set_user_fixed_rate
Function:
Set the fixed rate and mask of sending data from ESP8266.
Structure and Definition
enum FIXED_RATE {
PHY_RATE_48
PHY_RATE_24
PHY_RATE_12
PHY_RATE_6
PHY_RATE_54
PHY_RATE_36
PHY_RATE_18
PHY_RATE_9
=
=
=
=
=
=
=
=
0x8,
0x9,
0xA,
0xB,
0xC,
0xD,
0xE,
0xF,
}
#define
#define
#define
#define
FIXED_RATE_MASK_NONE
FIXED_RATE_MASK_STA
FIXED_RATE_MASK_AP
FIXED_RATE_MASK_ALL
(0x00)
(0x01)
(0x02)
(0x03)
Note:
If the enable_mask is 0, both ESP8266 station and soft-AP will not send
data in the fixed rate.
ESP8266 station and soft-AP share the same rate, they can not be set into
the different rate.
Prototype:
int wifi_set_user_fixed_rate(uint8 enable_mask, uint8 rate)
Parameter:
uint8 enable_mask:0x00 - disable the fixed rate
0x01 - use the fixed rate on ESP8266 station
0x02 - use the fixed rate on ESP8266 soft-AP
0x03 - use the fixed rate on ESP8266 station and soft-AP
uint8 rate
Return:
0, succeed;
otherwise, fail.
80/183
Espressif Systems
2.
wifi_get_user_fixed_rate
Function:
Parameter:
uint8 *enable_mask
uint8 *rate
Return:
0, succeed;
otherwise, fail.
3.
wifi_set_user_sup_rate
Function:
Set the rate range in the IE of support rate in ESP8266s beacon, probe req/
resp and other packets. Tell other devices about the rate range supported by
ESP8266 to limit the rate of sending packets from other devices.
Note
This API can only support 802.11g now, but it will support 802.11b in next
version.
Parameter Definition:
enum
};
support_rate {
RATE_11B5M
RATE_11B11M
RATE_11B1M
RATE_11B2M
RATE_11G6M
RATE_11G12M
RATE_11G24M
RATE_11G48M
RATE_11G54M
RATE_11G9M
RATE_11G18M
RATE_11G36M
=
=
=
=
=
=
=
=
=
=
=
=
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
Prototype:
int wifi_set_user_sup_rate(uint8 min, uint8 max)
Parameter:
uint8 min
support_rate.
81/183
Espressif Systems
uint8 max
support_rate.
Return:
0, succeed;
otherwise, fail.
Example:
wifi_set_user_sup_rate(RATE_11G6M, RATE_11G24M);
4.
wifi_set_user_rate_limit
Function:
Limit the initial rate of sending data from ESP8266. The rate of
retransmission is not limited by this API.
Parameter Definition:
enum RATE_11B_ID {
RATE_11B_B11M
RATE_11B_B5M
RATE_11B_B2M
RATE_11B_B1M
}
enum RATE_11G_ID {
RATE_11G_G54M
RATE_11G_G48M
RATE_11G_G36M
RATE_11G_G24M
RATE_11G_G18M
RATE_11G_G12M
RATE_11G_G9M
RATE_11G_G6M
RATE_11G_B5M
RATE_11G_B2M
RATE_11G_B1M
}
=
=
=
=
0,
1,
2,
3,
=
=
=
=
=
=
=
=
=
=
=
0,
1,
2,
3,
4,
5,
6,
7
8,
9,
10
82/183
Espressif Systems
enum RATE_11N_ID {
RATE_11N_MCS7S
RATE_11N_MCS7
RATE_11N_MCS6
RATE_11N_MCS5
RATE_11N_MCS4
RATE_11N_MCS3
RATE_11N_MCS2
RATE_11N_MCS1
RATE_11N_MCS0
RATE_11N_B5M
RATE_11N_B2M
RATE_11N_B1M
}
=
=
=
=
=
=
=
=
=
=
=
=
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11
Prototype:
bool wifi_set_user_rate_limit(uint8 mode, uint8 ifidx, uint8 max, uint8 min)
Parameter:
uint8 mode
WiFi mode
#define RC_LIMIT_11B
#define RC_LIMIT_11G
#define RC_LIMIT_11N
uint8 ifidx
: interface of ESP8266
0x00 - ESP8266 station
0x01 - ESP8266 soft-AP
uint8 max : the maximum value of the rate, according to the enum rate
corresponding to the first parameter mode.
uint8 min : the minimum value of the rate, according to the enum rate
corresponding to the first parameter mode.
Return:
truesucceed
falsefail
Example:
// Set the rate limitation of ESP8266 station in 11G mode, 6M ~ 18M.
wifi_set_user_rate_limit(RC_LIMIT_11G, 0, RATE_11G_G18M, RATE_11G_G6M);
5.
wifi_set_user_limit_rate_mask
Function:
83/183
Espressif Systems
Definition
#define
#define
#define
#define
LIMIT_RATE_MASK_NONE
LIMIT_RATE_MASK_STA
LIMIT_RATE_MASK_AP
LIMIT_RATE_MASK_ALL
(0x00
(0x01)
(0x02)
(0x03)
Prototype:
bool wifi_set_user_limit_rate_mask(uint8 enable_mask)
Parameter:
uint8 enable_mask :
0x00
0x01
0x02
0x03
Return:
truesucceed
falsefail
6.
wifi_get_user_limit_rate_mask
Function:
84/183
Espressif Systems
3.7.
wifi_set_opmode has to be set to NULL_MODE before enter force sleep mode. Then users need to
wake up from sleep, disable the force sleep function by wifi_fpm_close before set Wi-Fi mode back
to normal mode. More details in Example below.
1.
wifi_fpm_open
Function:
2.
wifi_fpm_close
Function:
3.
wifi_fpm_do_wakeup
Function:
85/183
Espressif Systems
Parameter:
null
Return:
null
4.
wifi_fpm_do_sleep
Function:
Force ESP8266 enter sleep mode, and it will wake up automatically when time
out.
Note
This API can only be called when force sleep function is enabled, after
calling wifi_fpm_open. This API can not be called after calling
wifi_fpm_close.
Prototype:
int8 wifi_fpm_do_sleep (uint32 sleep_time_in_us)
Parameter:
uint32 sleep_time_in_us : sleep time, ESP8266 will wake up automatically
when time out. Unit: us. Range: 10000 ~ 268435455(0xFFFFFFF)
If sleep_time_in_us is 0xFFFFFFF, the ESP8266 will sleep till
Return:
0, succeed;
-1, fail to sleep, sleep status error;
-2, fail to sleep, force sleep function is not enabled.
5.
wifi_fpm_set_sleep_type
Function:
86/183
Espressif Systems
Parameter:
enum sleep_type{
NONE_SLEEP_T =
0,
LIGHT_SLEEP_T,
MODEM_SLEEP_T,
};
Return:
null
6.
wifi_fpm_get_sleep_type
Function:
0,
LIGHT_SLEEP_T,
MODEM_SLEEP_T,
};
7.
Example
#define FPM_SLEEP_MAX_TIME
0xFFFFFFF
wifi_station_disconnect();
wifi_set_opmode(NULL_MODE);
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_open();
wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
87/183
Espressif Systems
3.8.
ESP-NOW APIs
ESP-NOW can not wake ESP8266 up from sleep, so if the target ESP8266 station is in sleep,
ESP-NOW communication will fail.
In station mode, ESP8266 supports 10 encrypt ESP-NOW peers at most, with the unencrypted
peers, it can be 20 peers in total at most.
In the soft-AP mode or soft-AP + station mode, the ESP8266 supports 6 encrypt ESP-NOW
peers at most, with the unencrypted peers, it can be 20 peers in total at most.
1.
esp_now_init
Function:
ESP-NOW initialization
Prototype:
init esp_now_init(void)
Parameter:
none
Return:
0, succeed
otherwise, fail
2.
esp_now_deinit
Function:
Deinitialize ESP-NOW
Prototype:
int esp_now_deinit(void)
Parameter:
none
Return:
0, succeed
otherwise, fail
88/183
Espressif Systems
3.
esp_now_register_recv_cb
Function:
: data received
u8 len
: data length
Prototype:
Parameter:
4.
esp_now_unregister_recv_cb
Function:
Prototype:
int esp_now_unregister_recv_cb(void)
Parameter:
none
Return:
0, succeed
otherwise, fail
5.
esp_now_register_send_cb
Function:
89/183
Espressif Systems
mt_tx_status {
T_TX_STATUS_OK = 0,
MT_TX_STATUS_FAILED,
}
The status will be T_TX_STATUS_OK, if ESP-NOW send packet successfully.
Users should make sure by themselves that key of communication is correct.
Prototype:
u8 esp_now_register_send_cb(esp_now_send_cb_t cb)
Parameter:
esp_now_send_cb_t cb
: callback
Return:
0, succeed
otherwise, fail
6.
esp_now_unregister_send_cb
Function:
Prototype:
int esp_now_unregister_send_cb(void)
Parameter:
null
Return:
0, succeed
otherwise, fail
7.
esp_now_send
Function:
90/183
Espressif Systems
Prototype:
Parameter:
u8 *da
u8 len
: data length
Return:
0, succeed
otherwise, fail
8.
esp_now_add_peer
Function:
Add an ESP-NOW peer, store MAC address of target device into ESP-NOW MAC
list.
Structure
typedef enum mt_role {
MT_ROLE_IDLE = 0,
MT_ROLE_CONTROLLER,
MT_ROLE_SLAVE,
MT_ROLE_MAX,
}
Prototype:
u8 *mac_addr
u8 role
u8 channel
: channel of device
u8 *key
u8 key_len
Return:
0, succeed
otherwise, fail
91/183
Espressif Systems
9.
esp_now_del_peer
Function:
Delete an ESP-NOW peer, delete MAC address of the device from ESP-NOW MAC
list.
Prototype:
Parameter:
u8 *mac_addr
Return:
0, succeed
otherwise, fail
10. esp_now_set_self_role
Function:
Prototype:
: role type
Return:
0, succeed
otherwise, fail
11. esp_now_get_self_role
Function:
u8 esp_now_get_self_role(void)
92/183
Espressif Systems
Parameter:
none
Return:
role type
12. esp_now_set_peer_role
Function:
Set ESP-NOW role for a target device. If it is set multiple times, new role
will cover the old one.
Structure
typedef enum mt_role {
MT_ROLE_IDLE = 0,
MT_ROLE_CONTROLLER,
MT_ROLE_SLAVE,
MT_ROLE_MAX,
}
Prototype:
u8 role
: role type
Return:
0, succeed
otherwise, fail
13. esp_now_get_peer_role
Function:
Return:
MT_ROLE_CONTROLLER, role type is controller
93/183
Espressif Systems
14. esp_now_set_peer_key
Function:
Set ESP-NOW key for a target device. If it is set multiple times, new role
will cover the old one.
Prototype:
u8 *key
u8 key_len
Return:
0, succeed
otherwise, fail
15. esp_now_get_peer_key
Function:
u8 *key
u8 *key_len
: key length
Return:
0, succeed
> 0, find target device but cant get key
< 0, fail
94/183
Espressif Systems
16. esp_now_set_peer_channel
Function:
Prototype:
17. esp_now_get_peer_channel
Function:
Prototype:
Return:
1 ~ 13 (some area may get 14), succeed
otherwise, fail
18. esp_now_is_peer_exist
Function:
95/183
Espressif Systems
u8 *mac_addr
Return:
0, device does not exist
< 0, error occur, check fail
> 0, device exists
19. esp_now_fetch_peer
Function:
Get MAC address of ESP-NOW device which is pointed now, and move the pointer
to next one in ESP-NOW MAC list or move the pointer to the first one in ESPNOW MAC list
Note
Prototype:
u8 *esp_now_fetch_peer(bool restart)
Parameter:
bool restart
Return:
NULL, no ESP-NOW devices exist
Otherwise, MAC address of ESP-NOW device which is pointed now
20. esp_now_get_cnt_info
Function:
Get the total number of ESP-NOW devices which are associated, and the number
count of encrypted devices.
Prototype:
u8 *encryp_cnt
Return:
0, succeed
96/183
Espressif Systems
otherwise, fail
21. esp_now_set_kok
Function:
Set the encrypt key of communication key. All ESP-NOW devices share the same
encrypt key. If users do not set the encrypt key, ESP-NOW communication key
will be encrypted by a default key.
Prototype:
u8 len
Return:
0, succeed
otherwise, fail
97/183
Espressif Systems
3.9.
1.
Function:
Checks user bin
Prototype:
uint8 system_upgrade_userbin_check()
Parameter:
none
Return:
0x00 : UPGRADE_FW_BIN1, i.e. user1.bin
0x01 : UPGRADE_FW_BIN2, i.e. user2.bin
2.
system_upgrade_flag_set
Function:
Sets upgrade status flag.
Note:
If you using system_upgrade_start to upgrade, this API need not be called.
If you using spi_flash_write to upgrade firmware yourself, this flag need to
be set to UPGRADE_FLAG_FINISH, then call system_upgrade_reboot to reboot to
run new firmware.
Prototype:
void system_upgrade_flag_set(uint8 flag)
Parameter:
uint8 flag:
#define UPGRADE_FLAG_IDLE
0x00
#define UPGRADE_FLAG_START
0x01
#define UPGRADE_FLAG_FINISH
0x02
Return:
null
3.
system_upgrade_flag_check
Function:
Gets upgrade status flag.
Prototype:
uint8 system_upgrade_flag_check()
98/183
Espressif Systems
Parameter:
null
Return:
4.
#define UPGRADE_FLAG_IDLE
0x00
#define UPGRADE_FLAG_START
0x01
#define UPGRADE_FLAG_FINISH
0x02
system_upgrade_start
Function:
Configures parameters and start upgrade
Prototype:
bool system_upgrade_start (struct upgrade_server_info *server)
Parameters:
struct upgrade_server_info *server : server related parameters
Return:
true: start upgrade
false: upgrade cant be started.
5.
system_upgrade_reboot
99/183
Espressif Systems
3.10.
1.
Function:
Enable promiscuous mode for sniffer
Note
(1)promiscuous mode can only be enabled in station mode.
(2)During promiscuous modesnifferESP8266 station and soft-AP are disabled.
(3)Before enable promiscuous modeplease call wifi_station_disconnect first
(4)Dont call any other APIs during snifferplease call
wifi_promiscuous_enable(0) first.
Prototype:
void wifi_promiscuous_enable(uint8 promiscuous)
Parameter:
uint8 promiscuous :
0: disable promiscuous;
1: enable promiscuous
Return:
null
2.
wifi_promiscuous_set_mac
Function:
Set MAC address filter for sniffer.
Note:
This filter only be available in the current sniffer phase, if you disable
sniffer and then enable sniffer, you need to set filter again if you need
it.
Prototype:
void wifi_promiscuous_set_mac(const uint8_t *address)
Parameter:
const uint8_t *address :
MAC address
Return:
null
Example:
char ap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab};
100/183
Espressif Systems
wifi_promiscuous_set_mac(ap_mac);
3.
wifi_set_promiscuous_rx_cb
Function:
Registers an RX callback function in promiscuous mode, which will be called
when data packet is received.
Prototype:
void wifi_set_promiscuous_rx_cb(wifi_promiscuous_cb_t cb)
Parameter:
wifi_promiscuous_cb_t cb : callback
Return:
null
4.
wifi_get_channel
Function:
Get Wi-Fi channel
Prototype:
uint8 wifi_get_channel(void)
Parameters:
null
Return:
Channel number
5.
wifi_set_channel
Function:
Set Wi-Fi channel, for sniffer mode
Prototype:
bool wifi_set_channel (uint8 channel)
Parameters:
uint8 channel :
channel number
Return:
true:
succeed
false: fail
101/183
Espressif Systems
3.11.
Herein we only introduce smart-config APIsusers can inquire Espressif Systems for smart-config
documentation which will contain more details. Please make sure the target AP is enabled before
enable smart-config.
1.
smartconfig_start
Function:
Start smart configuration mode, to connect ESP8266 station to AP, by
sniffing for special packets from the air, containing SSID and password of
desired AP. You need to broadcast the SSID and password (e.g. from mobile
device or computer) with the SSID and password encoded.
Note:
(1)This api can only be called in station mode.
(2)During smart-configESP8266 station and soft-AP are disabled.
(3)Can not call smartconfig_start twice before it finishplease call
smartconfig_stop first.
(4)Dont call any other APIs during smart-config, please call smartconfig_stop
first.
Structure:
typedef enum
SC_STATUS_WAIT = 0,
SC_STATUS_FIND_CHANNEL,
SC_STATUS_GETTING_SSID_PSWD,
SC_STATUS_LINK,
SC_STATUS_LINK_OVER,
} sc_status;
typedef enum
SC_TYPE_ESPTOUCH = 0,
SC_TYPE_AIRKISS,
} sc_type;
Prototype:
bool smartconfig_start(
sc_callback_t cb,
uint8 log
)
102/183
Espressif Systems
Parameter:
sc_callback_t cb :
changed;
parameter status of this callback shows the status of smart-config:
if status ==
if status ==
struct station_config;
uint8 log : 1: UART output logs; otherwise: UART only outputs the result.
Return:
true:
succeed
false: fail
Example:
void ICACHE_FLASH_ATTR
smartconfig_done(sc_status status, void *pdata)
{
switch(status) {
case SC_STATUS_WAIT:
os_printf("SC_STATUS_WAIT\n");
break;
case SC_STATUS_FIND_CHANNEL:
os_printf("SC_STATUS_FIND_CHANNEL\n");
break;
case SC_STATUS_GETTING_SSID_PSWD:
os_printf("SC_STATUS_GETTING_SSID_PSWD\n");
sc_type *type = pdata;
if (*type == SC_TYPE_ESPTOUCH) {
os_printf("SC_TYPE:SC_TYPE_ESPTOUCH\n");
} else {
os_printf("SC_TYPE:SC_TYPE_AIRKISS\n");
}
break;
case SC_STATUS_LINK:
103/183
Espressif Systems
os_printf("SC_STATUS_LINK\n");
struct station_config *sta_conf = pdata;
wifi_station_set_config(sta_conf);
wifi_station_disconnect();
wifi_station_connect();
break;
case SC_STATUS_LINK_OVER:
os_printf("SC_STATUS_LINK_OVER\n");
if (pdata != NULL) {
uint8 phone_ip[4] = {0};
memcpy(phone_ip, (uint8*)pdata, 4);
os_printf("Phone ip: %d.%d.%d.%d
\n",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]);
}
smartconfig_stop();
break;
}
}
smartconfig_start(smartconfig_done);
2.
smartconfig_stop
Function:
stop smart config, free the buffer taken by smartconfig_start.
Note:
Whether connect to AP succeed or not, this API should be called to free
memory taken by smartconfig_start.
Prototype:
bool smartconfig_stop(void)
Parameter:
null
Return:
true:
succeed
false: fail
3.
smartconfig_set_type
Function:
104/183
Espressif Systems
Note:
typedef enum
SC_TYPE_ESPTOUCH = 0,
SC_TYPE_AIRKISS,
SC_TYPE_ESPTOUCH_AIRKISS,
} sc_type;
Return:
true:
succeed;
false: fail
105/183
Espressif Systems
3.12.
1.
SNTP APIs
sntp_setserver
Function:
Prototype:
Parameter:
Return:
none
2.
sntp_getserver
Function:
Prototype:
Parameter:
unsigned char idx : SNTP server index, support 3 SNTP server at most0 ~ 2
Return:
IP address
3.
sntp_setservername
Function:
Prototype:
Return:
none
106/183
Espressif Systems
4.
sntp_getservername
Function:
Prototype:
Parameter:
unsigned char idx : SNTP server index support 3 SNTP server at most0 ~ 2)
Return:
domain name
5.
sntp_init
Function:
SNTP initialize
Prototype:
void sntp_init(void)
Parameter:
none
Return:
none
6.
sntp_stop
Function:
Stop SNTP
Prototype:
void sntp_stop(void)
Parameter:
none
Return:
none
7.
sntp_get_current_timestamp
Function:
107/183
Espressif Systems
Prototype:
uint32 sntp_get_current_timestamp()
Parameter:
none
Return:
time stamp
8.
sntp_get_real_time
Function:
Prototype:
char* sntp_get_real_time(long t)
Parameter:
9.
sntp_set_timezone
Function:
Prototype:
108/183
Espressif Systems
sntp_init();
}
10. sntp_get_timezone
Function:
Prototype:
none
Return
time zonerange-11 ~ 13
109/183
Espressif Systems
uint32 current_stamp;
current_stamp = sntp_get_current_timestamp();
if(current_stamp == 0){
os_timer_arm(&sntp_timer, 100, 0);
} else{
os_timer_disarm(&sntp_timer);
os_printf(sntp: %d, %s \n,current_stamp,
sntp_get_real_time(current_stamp));
}
}
110/183
4.
Espressif Systems
TCP/UDP APIs
Found in esp_iot_sdk/include/espconn.h. The network APIs can be grouped into the following
types:
General APIs: APIs can be used for both TCP and UDP .
4.1.
1.
Function:
Delete a transmission.
Note:
Corresponding creation API :
TCP: espconn_accept,
UDP: espconn_create
Prototype:
sint8 espconn_delete(struct espconn *espconn)
Parameter:
struct espconn *espconn : corresponding connected control block structure
Return:
0
: succeed
Non-0
2.
espconn_gethostbyname
Function:
DNS
Prototype:
err_t espconn_gethostbyname(
struct espconn *pespconn,
const char *hostname,
ip_addr_t *addr,
dns_found_callback found
)
111/183
Espressif Systems
Parameters:
struct espconn *espconn
ip_addr_t *addr
: IP address
3.
espconn_port
112/183
Espressif Systems
4.
espconn_regist_sentcb
Function:
Register data sent function which will be called back when data are
successfully sent.
Prototype:
sint8 espconn_regist_sentcb(
struct espconn *espconn,
espconn_sent_callback sent_cb
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
espconn_sent_callback sent_cb : registered callback function
Return:
0
: succeed
Non-0
5.
espconn_regist_recvcb
Function:
register data receive function which will be called back when data are
received
Prototype:
sint8 espconn_regist_recvcb(
struct espconn *espconn,
espconn_recv_callback recv_cb
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
espconn_connect_callback connect_cb : registered callback function
Return:
0
: succeed
Non-0
6.
espconn_sent_callback
Function:
Callback after the data are sent
113/183
Espressif Systems
Prototype:
void espconn_sent_callback (void *arg)
Parameters:
void *arg : pointer corresponding structure espconn. This pointer may be
different in different callbacks, please dont use this pointer directly to
distinguish one from another in multiple connections, use remote_ip and
remote_port in espconn instead.
Return:
null
7.
espconn_recv_callback
Function:
callback after data are received
Prototype:
void espconn_recv_callback (
void *arg,
char *pdata,
unsigned short len
)
Parameters:
void *arg : pointer corresponding structure espconn. This pointer may be
different in different callbacks, please dont use this pointer directly to
distinguish one from another in multiple connections, use remote_ip and
remote_port in espconn instead.
char *pdata : received data entry parameters
unsigned short len : received data length
Return:
null
8.
espconn_send
Function:
Send data through network
Note:
114/183
Espressif Systems
Prototype:
sint8 espconn_send(
struct espconn *espconn,
uint8 *psent,
uint16 length
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
uint8 *psent
: pointer of data
uint16 length
: data length
Return:
0
: succeed
Non-0
: error code
9.
espconn_sent
Prototype:
sint8 espconn_sent(
struct espconn *espconn,
uint8 *psent,
uint16 length
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
uint8 *psent
uint16 length
115/183
Espressif Systems
Return:
0
: succeed
Non-0
: error code
4.2.
TCP APIs
TCP APIs act only on TCP connections and do not affect nor apply to UDP connections.
1.
espconn_accept
Function:
Creates a TCP server (i.e. accepts connections.)
Prototype:
sint8 espconn_accept(struct espconn *espconn)
Parameter:
struct espconn *espconn : corresponding connected control block structure
Return:
0
: succeed
Non-0
: error code
2.
espconn_regist_time
Function:
Register timeout interval of ESP8266 TCP server.
Note:
Call this API after espconn_accept, before listened a TCP connection.
This timeout interval is not very precise, only as reference.
If timeout is set to 0, timeout will be disable and ESP8266 TCP server will
not disconnect TCP clients has stopped communication. This usage of
timeout=0, is deprecated.
116/183
Espressif Systems
Prototype:
sint8 espconn_regist_time(
struct espconn *espconn,
uint32 interval,
uint8 type_flag
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
uint32 interval : timeout interval, unit: second, maximum: 7200 seconds
uint8 type_flag : 0, set all connections; 1, set a single connection
Return:
0
: succeed
Non-0
3.
espconn_get_connection_info
Function:
Get the information about a TCP connection or UDP transmission.
Prototype:
sint8 espconn_get_connection_info(
struct espconn *espconn,
remot_info **pcon_info,
uint8 typeflags
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
remot_info **pcon_info
uint8 typeflags
Return:
0
: succeed
Non-0
4.
espconn_connect
Function:
Connect to a TCP server (ESP8266 acting as TCP client).
Note
117/183
Espressif Systems
: succeed
Non-0
: error code
5.
espconn_connect_callback
6.
espconn_regist_connectcb
Function:
Register connection function which will be called back under successful TCP
connection
118/183
Espressif Systems
Prototype:
sint8 espconn_regist_connectcb(
struct espconn *espconn,
espconn_connect_callback connect_cb
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
espconn_connect_callback connect_cb : registered callback function
Return:
0
: succeed
Non-0
7.
espconn_set_opt
119/183
Espressif Systems
Return:
0
: succeed
Non-0
8.
espconn_clear_opt
Function:
Clear option of TCP connection.
Prototype:
sint8 espconn_clear_opt(
struct espconn *espconn,
uint8 opt
)
Structure
enum espconn_option{
ESPCONN_START = 0x00
ESPCONN_REUSEADDR = 0x01
ESPCONN_NODELAY = 0x02
ESPCONN_COPY = 0x04
ESPCONN_KEEPALIVE = 0x08
ESPCONN_END
}
Parameters:
: succeed
Non-0
9.
espconn_set_keepalive
Function:
120/183
Espressif Systems
Prototype:
sint8 espconn_set_keepalive(struct espconn *espconn, uint8 level, void*
optarg)
Structure
enum espconn_level{
ESPCONN_KEEPIDLE,
ESPCONN_KEEPINTVL,
ESPCONN_KEEPCNT
}
Parameters:
struct espconn *espconn : corresponding connected control block structure
uint8 level : Default to do TCP keep-alive detection every ESPCONN_KEEPIDLE,
if there in no response, retry ESPCONN_KEEPCNT times every
ESPCONN_KEEPINTVL. If still no response, considers it as TCP connection
broke, goes into espconn_reconnect_callback.
Notice, keep alive interval is not precise, only for reference, it depends
on priority.
Description:
ESPCONN_KEEPIDLE - TCP keep-alive interval, unitsecond
ESPCONN_KEEPINTVL - packet interval during TCP keep-alive, unitsecond
ESPCONN_KEEPCNT - maximum packet count of TCP keep-alive
void* optarg value of parameter
Return:
0
: succeed
Non-0
121/183
Espressif Systems
10. espconn_get_keepalive
Function:
: succeed
Non-0
11. espconn_reconnect_callback
Function:
Enter this callback when error occurred, TCP connection broke. This callback
is registered by espconn_regist_reconcb
Prototype:
void espconn_reconnect_callback (void *arg, sint8 err)
122/183
Espressif Systems
Parameter:
void *arg : pointer corresponding structure espconn. This pointer may be
different in different callbacks, please do not use this pointer directly to
distinguish one from another in multiple connections, use remote_ip and
remote_port in espconn instead.
sint8 err : error code
ESCONN_TIMEOUT - Timeout
ESPCONN_ABRT - TCP connection aborted
ESPCONN_RST - TCP connection abort
ESPCONN_CLSD - TCP connection closed
ESPCONN_CONN - TCP connection
ESPCONN_HANDSHAKE - TCP SSL handshake fail
ESPCONN_PROTO_MSG - SSL application invalid
Return:
none
12. espconn_regist_reconcb
Function:
Register reconnect callback
Note:
espconn_reconnect_callback is more like a network-broken error handler; it
handles errors that occurs in any phase of the connection. For instance, if
espconn_send fails, espconn_reconnect_callback will be called because the
network is broken.
Prototype:
sint8 espconn_regist_reconcb(
struct espconn *espconn,
espconn_reconnect_callback recon_cb
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
espconn_reconnect_callback recon_cb : registered callback function
Return:
0
: succeed
Non-0
123/183
Espressif Systems
13. espconn_disconnect
Function:
Disconnect a TCP connection
Note:
Dont call this API in any espconn callback. If needed, please use
system_os_task and system_os_post to trigger espconn_disconnect
Prototype:
sint8 espconn_disconnect(struct espconn *espconn)
Parameters:
struct espconn *espconn : corresponding connected control structure
Return:
0
: succeed
Non-0
14. espconn_regist_disconcb
Function:
Register disconnection function which will be called back under successful
TCP disconnection
Prototype:
sint8 espconn_regist_disconcb(
struct espconn *espconn,
espconn_connect_callback discon_cb
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
espconn_connect_callback connect_cb : registered callback function
Return:
0
: succeed
Non-0
15. espconn_regist_write_finish
Function:
Register a callback which will be called when all sending data is completely
write into write buffer or sent. Need to call espconn_set_opt to enable
write-buffer first.
124/183
Espressif Systems
Note:
Prototype:
sint8 espconn_regist_write_finish (
struct espconn *espconn,
espconn_connect_callback write_finish_fn
)
Parameters:
struct espconn *espconn
espconn_connect_callback write_finish_fn :
Return:
0
: succeed
Non-0
16.
espconn_tcp_get_max_con
Function:
Get maximum number of how many TCP connections are allowed.
Prototype:
uint8 espconn_tcp_get_max_con(void)
Parameter:
null
Return:
Maximum number of how many TCP connections are allowed.
17. espconn_tcp_set_max_con
Function:
Set the maximum number of how many TCP connection is allowed.
Prototype:
sint8 espconn_tcp_set_max_con(uint8 num)
125/183
Espressif Systems
Parameter:
uint8 num :
Return:
0
: succeed
Non-0
18. espconn_tcp_get_max_con_allow
Function:
Get the maximum number of TCP clients which are allowed to connect to
ESP8266 TCP server.
Prototype:
sint8 espconn_tcp_get_max_con_allow(struct espconn *espconn)
Parameter:
struct espconn *espconn : corresponding connected control structure
Return:
> 0 : Maximum number of TCP clients which are allowed.
< 0 : error code ESPCONN_ARG - illegal argumentcant find TCP connection
according to structure espconn
19. espconn_tcp_set_max_con_allow
Function:
Set the maximum number of TCP clients allowed to connect to ESP8266 TCP
server.
Prototype:
sint8 espconn_tcp_set_max_con_allow(struct espconn *espconn, uint8 num)
Parameter:
struct espconn *espconn : corresponding connected control structure
uint8 num : Maximum number of TCP clients which are allowed.
Return:
0
: succeed
Non-0
20. espconn_recv_hold
Function:
Puts in a request to block the TCP receive function.
126/183
Espressif Systems
Note:
The function does not act immediately; we recommend calling it while
reserving 5*1460 bytes of memory.
This API can be called more than once.
Prototype:
sint8 espconn_recv_hold(struct espconn *espconn)
Parameter:
struct espconn *espconn : corresponding connected control structure
Return:
0
: succeed
Non-0
21. espconn_recv_unhold
Function:
Unblock TCP receiving data (i.e. undo espconn_recv_hold).
Note:
This API takes effect immediately.
Prototype:
sint8 espconn_recv_unhold(struct espconn *espconn)
Parameter:
struct espconn *espconn : corresponding connected control structure
Return:
0
: succeed
Non-0
22. espconn_secure_accept
Function:
Creates an SSL TCP server.
Note:
This API can be called only once, only one SSL server is allowed to be
created, and only one SSL client can be connected.
If SSL encrypted packet size is larger than ESP8266 SSL buffer size
(default 2KB, set by espconn_secure_set_size), SSL connection will
fail, will enter espconn_reconnect_callback
127/183
Espressif Systems
be used.
and
Non-0
: error code
succeed
23. espconn_secure_set_size
Function:
Set buffer size of encrypted data (SSL)
Note:
Buffer size default to be 2Kbytes. If need to change, please call this API
before espconn_secure_accept (ESP8266 as TCP SSL server) or
espconn_secure_connect (ESP8266 as TCP SSL client)
Prototype:
bool espconn_secure_set_size (uint8 level, uint16 size)
Parameters:
uint8 level : set buffer for ESP8266 SSL server/client
0x01
SSL client
0x02
SSL server
0x03
128/183
Espressif Systems
Return:
true
: succeed
false
: fail
24. espconn_secure_get_size
Function:
Get buffer size of encrypted data (SSL)
Prototype:
sint16 espconn_secure_get_size (uint8 level)
Parameters:
uint8 level : buffer for ESP8266 SSL server/client
0x01
SSL client
0x02
SSL server
0x03
Return:
buffer size
25. espconn_secure_connect
Function:
Secure connect (SSL) to a TCP server (ESP8266 is acting as TCP client.)
Note:
Only one connection is allowed when the ESP8266 acts as a SSL client,
this API can be called only once, or call espconn_secure_disconnect to
disconnect first, then call this API to create another SSL connection.
If SSL encrypted packet size is larger than the ESP8266 SSL buffer size
(default 2KB, set by espconn_secure_set_size), the SSL connection will
fail, will enter espconn_reconnect_callback
be used.
Prototype:
sint8 espconn_secure_connect (struct espconn *espconn)
129/183
Espressif Systems
Parameters:
struct espconn *espconn : corresponding connected control block structure
Return:
0
: succeed
Non-0
: error code
ESPCONN_MEM - Out of memory
ESPCONN_ISCONN - Already connected
ESPCONN_ARG - illegal argumentcant find TCP connection
26. espconn_secure_send
Function: send encrypted data (SSL)
Note:
Please call espconn_secure_send after espconn_sent_callback of the pre-packet.
Prototype:
sint8 espconn_secure_send (
struct espconn *espconn,
uint8 *psent,
uint16 length
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
uint8 *psent : sent data pointer
uint16 length : sent data length
Return:
0
: succeed
Non-0
27. espconn_secure_sent
[@deprecated] This API is deprecated, please use espconn_secure_send instead.
Function: send encrypted data (SSL)
Note:
Please call espconn_secure_sent after espconn_sent_callback of the pre-packet.
130/183
Espressif Systems
Prototype:
sint8 espconn_secure_sent (
struct espconn *espconn,
uint8 *psent,
uint16 length
)
Parameters:
struct espconn *espconn : corresponding connected control block structure
uint8 *psent : sent data pointer
uint16 length : sent data length
Return:
0
: succeed
Non-0
28. espconn_secure_disconnect
Function: secure TCP disconnection(SSL)
Note:
Do not call this API in any espconn callback. If needed, please use
system_os_task and system_os_post to trigger espconn_secure_disconnect
Prototype:
sint8 espconn_secure_disconnect(struct espconn *espconn)
Parameters:
struct espconn *espconn : corresponding connected control block structure
Return:
0
: succeed
Non-0
29. espconn_secure_ca_disable
Function:
131/183
Espressif Systems
Prototype:
bool espconn_secure_ca_disable (uint8 level)
Parameter:
uint8 level : set configuration for ESP8266 SSL server/client
0x01
SSL client
0x02
SSL server
0x03
Return:
true
: succeed
false
: fail
30. espconn_secure_ca_enable
Function:
Prototype:
bool espconn_secure_ca_enable (uint8 level, uint16 flash_sector)
Parameter:
uint8 level : set configuration for ESP8266 SSL server/client
0x01
SSL client
0x02
SSL server
0x03
132/183
Espressif Systems
Return:
true
: succeed
false
: fail
31. espconn_secure_cert_req_enable
Function:
Prototype:
bool espconn_secure_cert_req_enable (uint8 level, uint8 flash_sector)
Parameter:
uint8 level : can only be set as 0x01
: succeed
false
: fail
32. espconn_secure_cert_req_disable
Function:
Prototype:
bool espconn_secure_ca_disable (uint8 level)
Parameter:
uint8 level : can only be set as 0x01
Return:
true
false
: succeed
: fail
133/183
Espressif Systems
33. espconn_secure_set_default_certificate
Function:
Prototype:
bool espconn_secure_set_default_certificate (const uint8_t* certificate,
uint16_t length)
Parameter:
const uint8_t* certificate : pointer of the certificate
uint16_t length : length of the certificate
Return:
true
false
: succeed
: fail
34. espconn_secure_set_default_private_key
Function:
Prototype:
bool espconn_secure_set_default_private_key (const uint8_t* key, uint16_t
length)
Parameter:
const uint8_t* key : pointer of the secure key
uint16_t length : length of the secure key
Return:
true
: succeed
false
: fail
134/183
Espressif Systems
4.3.
1.
UDP APIs
espconn_create
Function:
Create UDP transmission.
Note:
Parameter remote_ip and remote_port need to be set, do not set to be 0.
Prototype:
sin8 espconn_create(struct espconn *espconn)
Parameter:
struct espconn *espconn : corresponding UDP control block structure
Return:
0
: succeed
Non-0
: error code
ESPCONN_ISCONN - Already connected
ESPCONN_MEM - Out of memory
ESPCONN_ARG - illegal argumentcant find UDP transmission
2.
espconn_sendto
Function:
Send UDP data
Prototype:
: pointer of data
uint16 length
: data length
Return:
0
Non-0
: succeed
: error code
ESPCONN_ISCONN - Already connected
ESPCONN_MEM - Out of memory
ESPCONN_IF - send UDP data fail
135/183
Espressif Systems
3.
espconn_igmp_join
Function:
Join a multicast group
Note:
This API can only be called after the ESP8266 station connects to a router.
Prototype:
sint8 espconn_igmp_join(ip_addr_t *host_ip, ip_addr_t *multicast_ip)
Parameters:
ip_addr_t *host_ip
IP of host
ip_addr_t *multicast_ip :
IP of multicast group
Return:
3.
: succeed
Non-0
espconn_igmp_leave
Function:
Quit a multicast group
Prototype:
sint8 espconn_igmp_leave(ip_addr_t *host_ip, ip_addr_t *multicast_ip)
Parameters:
ip_addr_t *host_ip
: IP of host
4.
: succeed
Non-0
espconn_dns_setserver
Function:
136/183
Espressif Systems
Parameter:
char numdns
none
4.4.
1.
mDNS APIs
espconn_mdns_init
Function:
mDNS initialization
Note
Structure:
struct mdns_info{
char *host_name;
char *server_name;
uint16 server_port;
unsigned long ipAddr;
char *txt_data[10];
};
Prototype:
mDNS information
Return:
none
Example
struct mdns_info *info = (struct mdns_info *)os_zalloc(sizeof(struct
mdns_info));
137/183
Espressif Systems
info->host_name = espressif;
info->ipAddr = station_ipconfig.ip.addr; //ESP8266 station IP
info->server_name = iot;
info->server_port = 8080;
info->txt_data[0] = version = now;
info->txt_data[1] = user1 = data1;
info->txt_data[2] = user2 = data2;
espconn_mdns_init(info);
2.
espconn_mdns_close
Function:
Prototype:
void espconn_mdns_close(void)
Parameter:
none
Return:
none
3.
espconn_mdns_server_register
Function:
Prototype:
void espconn_mdns_server_register(void)
Parameter:
none
Return:
none
4.
espconn_mdns_server_unregister
Function:
void espconn_mdns_server_unregister(void)
138/183
Espressif Systems
Parameter:
none
Return:
none
5.
espconn_mdns_get_servername
Function:
Prototype:
char* espconn_mdns_get_servername(void)
Parameter:
none
Return:
server name
6.
espconn_mdns_set_servername
Function:
Prototype:
Parameter:
7.
espconn_mdns_set_hostname
Function:
Prototype:
139/183
Espressif Systems
8.
espconn_mdns_get_hostname
Function:
Prototype:
char* espconn_mdns_get_hostname(void)
Parameter:
none
Return:
host name
9.
espconn_mdns_disable
Function:
Prototype:
void espconn_mdns_disable(void)
Parameter:
none
Return:
none
10. espconn_mdns_enable
Function:
Enable mDNS
Prototype:
void espconn_mdns_enable(void)
Parameter:
none
Return:
none
140/183
5.
Espressif Systems
Application Related
5.1.
AT APIs
at_response_ok
Function:
Output OK to AT Port (UART0)
Prototype:
void at_response_ok(void)
Parameter:
null
Return:
null
2.
at_response_error
Function:
output ERROR to AT Port (UART0)
Prototype:
void at_response_error(void)
Parameter:
null
Return:
null
3.
at_cmd_array_regist
Function:
register user-define AT commands.
Can be called only once to register all user-define AT commands.
Prototype:
void at_cmd_array_regist (
at_function * custom_at_cmd_arrar,
uint32 cmd_num
)
141/183
Espressif Systems
Parameter:
at_function * custom_at_cmd_arrar : Array of user-define AT commands
uint32 cmd_num : Number counts of user-define AT commands
Return:
null
Example:
refer to esp_iot_sdk/examples/at/user/user_main.c
4.
at_get_next_int_dec
Function:
parse int from AT command
Prototype:
bool at_get_next_int_dec (char **p_src,int* result,int* err)
Parameter:
char **p_src : *p_src is the AT command that need to be parsed
int* result
int* err
Return:
true:
5.
at_data_str_copy
142/183
Espressif Systems
Return:
length of string:
>=0: succeed and returns the length of the string
<0 : fail and returns -1
Example:
refer to esp_iot_sdk/examples/at/user/user_main.c
6.
at_init
Function:
AT initialize
Prototype:
void at_init (void)
Parameter:
null
Return:
null
Example:
refer to esp_iot_sdk/examples/at/user/user_main.c
7.
at_port_print
Function:
output string to AT PORT(UART0)
Prototype:
void at_port_print(const char *str)
Parameter:
const char *str : string that need to output
Return:
null
Example:
refer to esp_iot_sdk/examples/at/user/user_main.c
8.
at_set_custom_info
Function:
User-define version info of AT which can be got by AT+GMR.
Prototype:
void at_set_custom_info (char *info)
143/183
Espressif Systems
Parameter:
char *info : version info
Return:
null
9.
at_enter_special_state
Function:
Enter processing state. In processing state, AT core will return busy for
any further AT commands.
Prototype:
void at_enter_special_state (void)
Parameter:
null
Return:
null
10. at_leave_special_state
Function:
Exit from AT processing state.
Prototype:
void at_leave_special_state (void)
Parameter:
null
Return:
null
11. at_get_version
Function:
Get Espressif AT lib version.
Prototype:
uint32 at_get_version (void)
Parameter:
null
Return:
Espressif AT lib version
144/183
Espressif Systems
12. at_register_uart_rx_intr
Function:
Prototype:
Parameter:
null
Example:
void user_uart_rx_intr(uint8* data, int32 len)
{
// UART0 rx for user
os_printf(len=%d \r\n,len);
os_printf(data);
// change UART0 for AT
at_register_uart_rx_intr(NULL);
}
void user_init(void){ at_register_uart_rx_intr(user_uart_rx_intr); }
13. at_response
Function:
Set AT response
Note
at_response outputs from UART0 TX by default which is same as at_port_print.
But if called at_register_response_func, the string of at_response will be
the parameter of response_func, users can define their own behavior.
Prototype:
145/183
Espressif Systems
Parameter:
Return:
none
14. at_register_response_func
Function:
none
146/183
Espressif Systems
5.2.
jsonparse_setup
Function:
json initialize parsing
Prototype:
void jsonparse_setup(
struct jsonparse_state *state,
const char *json,
int len
)
Parameters:
struct jsonparse_state *state : json parsing pointer
const char *json : json parsing character string
int len : character string length
Return:
null
2.
jsonparse_next
Function:
Returns jsonparse next object
Prototype:
int jsonparse_next(struct jsonparse_state *state)
Parameters:
struct jsonparse_state *state : json parsing pointer
Return:
int : parsing result
3.
jsonparse_copy_value
Function:
Copies current parsing character string to a certain buffer
147/183
Espressif Systems
Prototype:
int jsonparse_copy_value(
struct jsonparse_state *state,
char *str,
int size
)
Parameters:
struct jsonparse_state *state : json parsing pointer
char *str : buffer pointer
int size : buffer size
Return:
int : copy result
4.
jsonparse_get_value_as_int
Function:
Parses json to get integer
Prototype:
int jsonparse_get_value_as_int(struct jsonparse_state *state)
Parameters:
struct jsonparse_state *state : json parsing pointer
Return:
int : parsing result
5.
jsonparse_get_value_as_long
Function:
Parses json to get long integer
Prototype:
long jsonparse_get_value_as_long(struct jsonparse_state *state)
Parameters:
struct jsonparse_state *state : json parsing pointer
Return:
long : parsing result
6.
jsonparse_get_len
Function:
Gets parsed json length
148/183
Espressif Systems
Prototype:
int jsonparse_get_value_len(struct jsonparse_state *state)
Parameters:
struct jsonparse_state *state : json parsing pointer
Return:
int : parsed jason length
7.
jsonparse_get_value_as_type
Function:
Parses json data type
Prototype:
int jsonparse_get_value_as_type(struct jsonparse_state *state)
Parameters:
struct jsonparse_state *state : json parsing pointer
Return:
int : parsed json data type
8.
jsonparse_strcmp_value
Function:
Compares parsed json and certain character string
Prototype:
int jsonparse_strcmp_value(struct jsonparse_state *state, const char *str)
Parameters:
struct jsonparse_state *state : json parsing pointer
const char *str : character buffer
Return:
int : comparison result
9.
jsontree_set_up
Function:
Creates json data tree
149/183
Espressif Systems
Prototype:
void jsontree_setup(
struct jsontree_context *js_ctx,
struct jsontree_value *root,
int (* putchar)(int)
)
Parameters:
struct jsontree_context *js_ctx : json tree element pointer
struct jsontree_value *root : root element pointer
int (* putchar)(int) : input function
Return:
null
10. jsontree_reset
Function:
Resets json tree
Prototype:
void jsontree_reset(struct jsontree_context *js_ctx)
Parameters:
struct jsontree_context *js_ctx : json data tree pointer
Return:
null
11. jsontree_path_name
Function:
get json tree parameters
Prototype:
const char *jsontree_path_name(
const struct jsontree_cotext *js_ctx,
int depth
)
Parameters:
struct jsontree_context *js_ctx : json tree pointer
int depth : json tree depth
Return:
char* : parameter pointer
150/183
Espressif Systems
12. jsontree_write_int
Function:
write integer to json tree
Prototype:
void jsontree_write_int(
const struct jsontree_context *js_ctx,
int value
)
Parameters:
struct jsontree_context *js_ctx : json tree pointer
int value : integer value
Return:
null
13. jsontree_write_int_array
Function:
Writes integer array to json tree
Prototype:
void jsontree_write_int_array(
const struct jsontree_context *js_ctx,
const int *text,
uint32 length
)
Parameters:
struct jsontree_context *js_ctx : json tree pointer
int *text : array entry address
uint32 length : array length
Return:
null
14. jsontree_write_string
Function:
Writes string to json tree
151/183
Espressif Systems
Prototype:
void jsontree_write_string(
const struct jsontree_context *js_ctx,
const char *text
)
Parameters:
struct jsontree_context *js_ctx : json tree pointer
const char* text : character string pointer
Return:
null
15. jsontree_print_next
Function:
json tree depth
Prototype:
int jsontree_print_next(struct jsontree_context *js_ctx)
Parameters:
struct jsontree_context *js_ctx : json tree pointer
Return:
int : json tree depth
16. jsontree_find_next
Function:
find json tree element
Prototype:
struct jsontree_value *jsontree_find_next(
struct jsontree_context *js_ctx,
int type
)
Parameters:
struct jsontree_context *js_ctx : json tree pointer
int : type
Return:
struct jsontree_value * : json tree element pointer
152/183
6.
Espressif Systems
Timer
*timer_next;
uint32_t
timer_expire;
uint32_t
timer_period;
ETSTimerFunc
*timer_func;
void
*timer_arg;
} ETSTimer;
6.2.
1.
struct station_config {
uint8 ssid[32];
uint8 password[64];
uint8 bssid_set;
uint8 bssid[6];
};
Note:
BSSID as MAC address of AP, will be used when several APs have the same
SSID.
If station_config.bssid_set==1 , station_config.bssid has to be set,
otherwise, the connection will fail.
In general, station_config.bssid_set need to be 0.
2.
soft-AP related
153/183
Espressif Systems
uint8 ssid[32];
uint8 password[64];
uint8 ssid_len;
uint8 channel;
// support 1 ~ 13
uint8 authmode;
uint8 ssid_hidden;
// default 0
uint8 max_connection;
// default 4, max 4
uint16 beacon_interval;
};
Note:
If softap_config.ssid_len==0, check ssid till a termination character is
found; otherwise, it will depend on softap_config.ssid_len.
3.
scan related
struct scan_config {
uint8 *ssid;
uint8 *bssid;
uint8 channel;
uint8 show_hidden; // Scan APs which are hiding their SSID or not.
};
struct bss_info {
STAILQ_ENTRY(bss_info) next;
u8 bssid[6];
u8 ssid[32];
u8 channel;
s8 rssi;
u8 authmode;
uint8 is_hidden; // SSID of current AP is hidden or not.
sint16 freq_offset; // APs frequency offset
};
typedef void (* scan_done_cb_t)(void *arg, STATUS status);
4.
enum {
EVENT_STAMODE_CONNECTED = 0,
EVENT_STAMODE_DISCONNECTED,
EVENT_STAMODE_AUTHMODE_CHANGE,
EVENT_STAMODE_GOT_IP,
154/183
Espressif Systems
EVENT_STAMODE_DHCP_TIMEOUT,
EVENT_SOFTAPMODE_STACONNECTED,
EVENT_SOFTAPMODE_STADISCONNECTED,
EVENT_SOFTAPMODE_PROBEREQRECVED,
EVENT_MAX
};
enum {
REASON_UNSPECIFIED
= 1,
REASON_AUTH_EXPIRE
= 2,
REASON_AUTH_LEAVE
= 3,
REASON_ASSOC_EXPIRE
= 4,
REASON_ASSOC_TOOMANY
= 5,
REASON_NOT_AUTHED
= 6,
REASON_NOT_ASSOCED
= 7,
REASON_ASSOC_LEAVE
= 8,
REASON_ASSOC_NOT_AUTHED
= 9,
REASON_DISASSOC_PWRCAP_BAD
= 10,
/* 11h */
REASON_DISASSOC_SUPCHAN_BAD
= 11,
/* 11h */
REASON_IE_INVALID
= 13,
/* 11i */
REASON_MIC_FAILURE
= 14,
/* 11i */
REASON_4WAY_HANDSHAKE_TIMEOUT
= 15,
/* 11i */
REASON_GROUP_KEY_UPDATE_TIMEOUT = 16,
/* 11i */
REASON_IE_IN_4WAY_DIFFERS
= 17,
/* 11i */
REASON_GROUP_CIPHER_INVALID
= 18,
/* 11i */
REASON_PAIRWISE_CIPHER_INVALID
= 19,
/* 11i */
REASON_AKMP_INVALID
= 20,
/* 11i */
REASON_UNSUPP_RSN_IE_VERSION
= 21,
/* 11i */
REASON_INVALID_RSN_IE_CAP
= 22,
/* 11i */
REASON_802_1X_AUTH_FAILED
= 23,
/* 11i */
REASON_CIPHER_SUITE_REJECTED
= 24,
/* 11i */
REASON_BEACON_TIMEOUT
= 200,
REASON_NO_AP_FOUND
= 201,
REASON_AUTH_FAIL
= 202,
REASON_ASSOC_FAIL
= 203,
REASON_HANDSHAKE_TIMEOUT
= 204,
};
155/183
Espressif Systems
typedef struct {
uint8 ssid[32];
uint8 ssid_len;
uint8 bssid[6];
uint8 channel;
} Event_StaMode_Connected_t;
typedef struct {
uint8 ssid[32];
uint8 ssid_len;
uint8 bssid[6];
uint8 reason;
} Event_StaMode_Disconnected_t;
typedef struct {
uint8 old_mode;
uint8 new_mode;
} Event_StaMode_AuthMode_Change_t;
typedef struct {
struct ip_addr ip;
struct ip_addr mask;
struct ip_addr gw;
} Event_StaMode_Got_IP_t;
typedef struct {
uint8 mac[6];
uint8 aid;
} Event_SoftAPMode_StaConnected_t;
typedef struct {
uint8 mac[6];
uint8 aid;
} Event_SoftAPMode_StaDisconnected_t;
typedef struct {
int rssi;
uint8 mac[6];
} Event_SoftAPMode_ProbeReqRecved_t;
156/183
Espressif Systems
typedef union {
Event_StaMode_Connected_t
connected;
Event_StaMode_Disconnected_t
disconnected;
Event_StaMode_AuthMode_Change_t
auth_change;
Event_StaMode_Got_IP_t
got_ip;
Event_SoftAPMode_StaConnected_t
sta_connected;
Event_SoftAPMode_StaDisconnected_t
sta_disconnected;
Event_SoftAPMode_ProbeReqRecved_t
ap_probereqrecved;
} Event_Info_u;
typedef struct _esp_event {
uint32 event;
Event_Info_u event_info;
} System_Event_t;
5.
typedef enum
SC_STATUS_WAIT = 0,
SC_STATUS_FIND_CHANNEL,
SC_STATUS_GETTING_SSID_PSWD,
SC_STATUS_LINK,
SC_STATUS_LINK_OVER,
} sc_status;
typedef enum
SC_TYPE_ESPTOUCH = 0,
SC_TYPE_AIRKISS,
SC_TYPE_ESPTOUCH_AIRKISS,
} sc_type;
6.3.
1.
struct jsontree_value {
uint8_t type;
};
struct jsontree_pair {
157/183
Espressif Systems
158/183
Espressif Systems
char vtype;
char error;
char stack[JSONPARSE_MAX_DEPTH];
};
2.
JSON_TYPE_OBJECT,
sizeof(jsontree_pair_##name)/sizeof(struct jsontree_pair),
jsontree_pair_##name }
#define JSONTREE_PAIR_ARRAY(value) (struct jsontree_value *)(value)
#define JSONTREE_ARRAY(name, ...)
JSON_TYPE_ARRAY,
sizeof(jsontree_value_##name)/sizeof(struct jsontree_value*),
jsontree_value_##name }
6.4.
1.
espconn parameters
callback function
2.
espconn
159/183
Espressif Systems
espconn_reconnect_callback reconnect_callback;
espconn_connect_callback disconnect_callback;
espconn_connect_callback write_finish_fn;
} esp_tcp;
typedef struct _esp_udp {
int remote_port;
int local_port;
uint8 local_ip[4];
uint8 remote_ip[4];
} esp_udp;
/** Protocol family and type of the espconn */
enum espconn_type {
ESPCONN_INVALID
= 0,
/* ESPCONN_TCP Group */
ESPCONN_TCP
= 0x10,
/* ESPCONN_UDP Group */
ESPCONN_UDP
= 0x20,
};
/** Current state of the espconn. Non-TCP espconn are always in state
ESPCONN_NONE! */
enum espconn_state {
ESPCONN_NONE,
ESPCONN_WAIT,
ESPCONN_LISTEN,
ESPCONN_CONNECT,
ESPCONN_WRITE,
ESPCONN_READ,
ESPCONN_CLOSE
};
enum espconn_option{
ESPCONN_START = 0x00
ESPCONN_REUSEADDR = 0x01
ESPCONN_NODELAY = 0x02
ESPCONN_COPY = 0x04
ESPCONN_KEEPALIVE = 0x08
160/183
Espressif Systems
ESPCONN_END
}
enum espconn_level{
ESPCONN_KEEPIDLE,
ESPCONN_KEEPINTVL,
ESPCONN_KEEPCNT
}
/** A espconn descriptor */
struct espconn {
/** type of the espconn (TCP, UDP) */
enum espconn_type type;
/** current state of the espconn */
enum espconn_state state;
union {
esp_tcp *tcp;
esp_udp *udp;
} proto;
/** A callback function that is informed about events for this espconn */
espconn_recv_callback recv_callback;
espconn_sent_callback sent_callback;
uint8 link_cnt;
void *reverse; // reversed for customer use
};
6.5.
/* interrupt related */
#define ETS_SPI_INUM
#define ETS_GPIO_INUM
#define ETS_UART_INUM
#define ETS_UART1_INUM
#define ETS_FRC_TIMER1_INUM
ets_intr_lock()
161/183
Espressif Systems
ets_intr_unlock()
/* enable a interrupt */
#define ETS_INTR_ENABLE(inum)
ets_isr_unmask((1<<inum))
/* disable a interrupt */
#define ETS_INTR_DISABLE(inum)
ets_isr_mask((1<<inum))
ETS_INTR_ENABLE(ETS_SPI_INUM)
ETS_INTR_ENABLE(ETS_UART_INUM)
ETS_INTR_DISABLE(ETS_UART_INUM)
162/183
Espressif Systems
ETS_INTR_ENABLE(ETS_FRC_TIMER1_INUM)
ETS_INTR_DISABLE(ETS_FRC_TIMER1_INUM)
ETS_INTR_ENABLE(ETS_GPIO_INUM)
ETS_INTR_DISABLE(ETS_GPIO_INUM)
163/183
7.
Espressif Systems
The following macros are used to control the GPIO pins' status.
PIN_PULLUP_DIS(PIN_NAME)
Disable pin pull up
PIN_PULLUP_EN(PIN_NAME)
Enable pin pull up
PIN_FUNC_SELECT(PIN_NAME, FUNC)
Select pin function
Example:
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); // Use MTDI pin as
GPIO12.
2.
gpio_output_set
164/183
Espressif Systems
Example:
gpio_output_set(BIT12, 0, BIT12, 0):
Set GPIO12 as high-level output;
gpio_output_set(0, BIT12, BIT12, 0):
Set GPIO12 as low-level output
gpio_output_set(BIT12, BIT13, BIT12|BIT13, 0):
Set GPIO12 as high-level output, GPIO13 as low-level output.
gpio_output_set(0, 0, 0, BIT12):
Set GPIO12 as input
3.
GPIO_OUTPUT_SET(gpio_no, bit_value)
Set gpio_no as output bit_value, the same as the output example in 5.1.2
GPIO_DIS_OUTPUT(gpio_no)
Set gpio_no as input, the same as the input example in 5.1.2.
GPIO_INPUT_GET(gpio_no)
Get the level status of gpio_no.
4.
GPIO interrupt
ETS_GPIO_INTR_ATTACH(func, arg)
Register GPIO interrupt control function
ETS_GPIO_INTR_DISABLE()
Disable GPIO interrupt
ETS_GPIO_INTR_ENABLE()
Enable GPIO interrupt
5.
gpio_pin_intr_state_set
Function:
set GPIO interrupt state
Prototype:
void gpio_pin_intr_state_set(
uint32 i,
GPIO_INT_TYPE intr_state
)
165/183
Espressif Systems
Input Parameters:
uint32 i : GPIO pin ID, if you want to set GPIO14, pls use GPIO_ID_PIN(14);
GPIO_INT_TYPE intr_state : interrupt type as the following:
typedef enum {
GPIO_PIN_INTR_DISABLE = 0,
GPIO_PIN_INTR_POSEDGE = 1,
GPIO_PIN_INTR_NEGEDGE = 2,
GPIO_PIN_INTR_ANYEGDE = 3,
GPIO_PIN_INTR_LOLEVEL = 4,
GPIO_PIN_INTR_HILEVEL = 5
} GPIO_INT_TYPE;
Return:
null
6.
Follow the steps below to clear interrupt status in GPIO interrupt processing function:
uint32 gpio_status;
gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
//clear interrupt status
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status);
166/183
Espressif Systems
7.2.
By default, UART0 is a debug output interface. In the case of a dual UART, UART0 works as data
receive and transmit interface, while UART1 debug output interface. Please make sure all hardware
are correctly connected.
Users can inquire Espressif Systems for UART documentation which will contain more details.
1.
uart_init
Function:
Initializes baud rates of the two UARTs
Prototype:
void uart_init(
UartBautRate uart0_br,
UartBautRate uart1_br
)
Parameters:
UartBautRate uart0_br : uart0 baud rate
UartBautRate uart1_br : uart1 baud rate
Baud Rates:
typedef enum {
BIT_RATE_9600
= 9600,
BIT_RATE_19200
= 19200,
BIT_RATE_38400
= 38400,
BIT_RATE_57600
= 57600,
BIT_RATE_74880
= 74880,
BIT_RATE_115200 = 115200,
BIT_RATE_230400 = 230400,
BIT_RATE_460800 = 460800,
BIT_RATE_921600 = 921600
} UartBautRate;
Return:
null
2.
uart0_tx_buffer
Function:
Sends user-defined data through UART0
Prototype:
void uart0_tx_buffer(uint8 *buf, uint16 len)
167/183
Espressif Systems
Parameter:
uint8 *buf : data to send later
uint16 len : the length of data to send later
Return:
null
3.
uart0_rx_intr_handler
Function:
UART0 interrupt processing function. Users can add the processing of
received data in this function.
Prototype:
void uart0_rx_intr_handler(void *para)
Parameter:
void *para : the pointer pointing to RcvMsgBuff structure
Return:
null
168/183
Espressif Systems
7.3.
Users can inquire apply to Espressif Systems for I2C documentation which will contain more details.
1.
i2c_master_gpio_init
Function:
Set GPIO in I2C master mode
Prototype:
void i2c_master_gpio_init (void)
Input Parameters:
null
Return:
null
2.
i2c_master_init
Function:
Initialize I2C
Prototype:
void i2c_master_init(void)
Input Parameters:
null
Return:
null
3.
i2c_master_start
4.
i2c_master_stop
Function:
configures I2C to stop sending data
169/183
Espressif Systems
Prototype:
void i2c_master_stop(void)
Input Parameters:
null
Return:
null
5.
i2c_master_send_ack
Function:
Sends I2C ACK
Prototype:
void i2c_master_send_ack (void)
Input Parameters:
null
Return:
null
6.
i2c_master_send_nack
Function:
Sends I2C NACK
Prototype:
void i2c_master_send_nack (void)
Input Parameters:
null
Return:
null
7.
i2c_master_checkAck
Function:
Checks ACK from slave
Prototype:
bool i2c_master_checkAck (void)
Input Parameters:
null
170/183
Espressif Systems
Return:
true:
8.
i2c_master_readByte
Function:
Read one byte from I2C slave
Prototype:
uint8 i2c_master_readByte (void)
Input Parameters:
null
Return:
uint8 : the value that was read
9.
i2c_master_writeByte
Function:
Write one byte to slave
Prototype:
void i2c_master_writeByte (uint8 wrdata)
Input Parameters:
uint8 wrdata : data to write
Return:
null
171/183
Espressif Systems
7.4.
PWM Related
Herein only introduces the PWM related APIs in pwm.h. Users can inquire Espressif Systems for PWM
documentation which will contain more details.
PWM APIs can not be called when APIs in hw_timer.c are in use, because they use the same
hardware timer.
1.
pwm_init
Function:
Initialize PWM function, including GPIO selection, period and duty cycle.
Note:
This API can be called only once.
Prototype:
void pwm_init(
uint32 period,
uint8 *duty,
uint32 pwm_channel_num,
uint32 (*pin_info_list)[3])
Parameter:
uint32 period : PWM period
uint8 *duty : duty cycle of each output
uint32 pwm_channel_num: PWM channel number
uint32 (*pin_info_list)[3]: GPIO parameter of PWM channel, it is a pointer
of n * 3 array which defines GPIO register, IO reuse of corresponding PIN
and GPIO number.
Return:
null
Example:
uint32 io_info[][3] =
{{PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM},
{PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM},
{PWM_2_OUT_IO_MUX,PWM_2_OUT_IO_FUNC,PWM_2_OUT_IO_NUM}};
pwm_init(light_param.pwm_period, light_param.pwm_duty, 3, io_info);
172/183
Espressif Systems
2.
pwm_start
Function:
Starts PWM. This function needs to be called after PWM config is changed.
Prototype:
void pwm_start (void)
Parameter:
null
Return:
null
3.
pwm_set_duty
Function:
Sets duty cycle of a PWM output. Set the time that high-level single will
last, duty depends on period, the maximum value can be Period * 1000 /45.
For example, 1KHz PWM, duty range is 0 ~ 22222
Note:
After set configurationpwm_start need to be called to take effect.
Prototype:
void pwm_set_duty(uint32 duty, uint8 channel)
Input Parameters:
uint32 duty : the time that high-level single will last, duty cycle will be
(duty*45)/ (period*1000)
uint8 channel : PWM channel, depends on how many PWM channels is used, in
IOT_Demo it depends on #define PWM_CHANNEL
Return:
null
4.
pwm_get_duty
Function:
Gets duty cycle of PWM output, duty cycle will be (duty*45)/ (period*1000)
Prototype:
uint8 pwm_get_duty(uint8 channel)
Input Parameters:
uint8 channel : PWM channel, depends on how many PWM channels is used, in
IOT_Demo it depends on #define PWM_CHANNEL
173/183
Espressif Systems
Return:
uint8 : duty cycle of PWM output
5.
pwm_set_period
Function:
Sets PWM period, unit: us. For example, for 1KHz PWM, period is 1000 us
Note:
After set configurationpwm_start need to be called to take effect.
Prototype:
void pwm_set_period(uint32 period)
Input Parameters:
uint32 period : PWM period, unit: us
Return:
null
6.
pwm_get_period
Function:
Gets PWM period.
Prototype:
uint32 pwm_get_period(void)
Parameter:
null
Return:
PWM period, unit: us.
7.
get_pwm_version
Function:
174/183
Espressif Systems
!
8.
Appendix
8.1.
1.
ESPCONN Programming
TCP Client Mode
Notes
ESP8266, working in Station mode, will start client connections when given an IP address.
ESP8266, working in soft-AP mode, will start client connections when the devices connected
to the ESP8266 are given IP addresses.
Steps
Call espconn_connect function and set up the connection with TCP Server.
Registered connected callback functions will be called after successful connection, which will
register corresponding callback function. We recommend registering a disconnect callback
function.
When using receive callback function or sent callback function to run disconnect, it is
recommended to set a time delay to make sure that the all firmware functions are completed.
2.
Notes
If the ESP8266 is in Station mode, it will start server listening when given an IP address.
Steps
175/183
Espressif Systems
!
Registered connect function will be called after a successful connection, which will register a
corresponding callback function.
3.
Register Function
Callback
Description
espconn_regist_connectcb
espconn_connect_callback
espconn_regist_reconcb
espconn_reconnect_callback
espconn_regist_sentcb
espconn_sent_callback
espconn_regist_recvcb
espconn_recv_callback
espconn_regist_write_finish
espconn_write_finish_callback
espconn_regist_disconcb
espconn_disconnect_callback
Notice:
Parameter arg of callback is the pointer corresponding structure espconn. This pointer may
be different in different callbacks, please do not use this pointer directly to distinguish one
from another in multiple connections, use remote_ip and remote_port in espconn instead.
8.2.
Demo code below shows how to get RTC time and to read and write to RTC memory.
#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
os_timer_t rtc_test_t;
#define
RTC_MAGIC
0x55aaaa55
176/183
Espressif Systems
typedef struct {
uint64 time_acc;
uint32 magic ;
uint32 time_base;
}RTC_TIMER_DEMO;
void rtc_count()
{
RTC_TIMER_DEMO rtc_time;
static uint8 cnt = 0;
system_rtc_mem_read(64, &rtc_time, sizeof(rtc_time));
if(rtc_time.magic!=RTC_MAGIC){
os_printf("rtc time init...\r\n");
rtc_time.magic = RTC_MAGIC;
rtc_time.time_acc= 0;
rtc_time.time_base = system_get_rtc_time();
os_printf("time base : %d \r\n",rtc_time.time_base);
}
os_printf("==================\r\n");
os_printf("RTC time test : \r\n");
uint32 rtc_t1,rtc_t2;
uint32 st1,st2;
uint32 cal1, cal2;
rtc_t1 = system_get_rtc_time();
st1 = system_get_time();
cal1 = system_rtc_clock_cali_proc();
os_delay_us(300);
st2 = system_get_time();
rtc_t2 = system_get_rtc_time();
cal2 = system_rtc_clock_cali_proc();
os_printf(" rtc_t2-t1 : %d \r\n",rtc_t2-rtc_t1);
os_printf(" st2-t2 :
%d
\r\n",st2-st1);
177/183
Espressif Systems
os_printf("cal 1
: %d.%d
\r\n", ((cal1*1000)>>12)/1000,
((cal1*1000)>>12)%1000 );
os_printf("cal 2
: %d.%d \r\n",((cal2*1000)>>12)/1000,
((cal2*1000)>>12)%1000 );
os_printf("==================\r\n\r\n");
rtc_time.time_acc += (
((uint64)(rtc_t2 - rtc_time.time_base))
( (uint64)((cal2*1000)>>12))
os_printf("rtc time acc
) ;
: %lld \r\n",rtc_time.time_acc);
os_printf("power on time :
%lld
us\r\n", rtc_time.time_acc/1000);
os_printf("power on time :
%lld.%02lld
S\r\n", (rtc_time.time_acc/
10000000)/100, (rtc_time.time_acc/10000000)%100);
rtc_time.time_base = rtc_t2;
system_rtc_mem_write(64, &rtc_time, sizeof(rtc_time));
os_printf("------------------------\r\n");
if(5== (cnt++)){
os_printf("system restart\r\n");
system_restart();
}else{
os_printf("continue ...\r\n");
}
}
void user_init(void)
{
rtc_count();
os_printf("SDK version:%s\n", system_get_sdk_version());
os_timer_disarm(&rtc_test_t);
os_timer_setfn(&rtc_test_t,rtc_count,NULL);
os_timer_arm(&rtc_test_t,10000,1);
}
8.3.
The ESP8266 can enter the promiscuous mode (sniffer) and capture IEEE 802.11 packets in the air.
The following HT20 packet types are supported:
178/183
Espressif Systems
802.11b
802.11g
AMPDU
HT40
LDPC
Although the ESP8266 can not decipher some IEEE80211 packets completely, it can Get the length
of these packets.
Therefore, when in the sniffer mode, the ESP8266 can either (1) completely capture the packets or
(2) Get the length of the packets.
For packets that ESP8266 can decipher completely, the ESP8266 returns with the
For packets that ESP8266 cannot completely decipher, the ESP8266 returns with
Structure RxControl and sniffer_buf are used to represent these two kinds of packets. Structure
sniffer_buf contains structure RxControl.
struct RxControl {
signed rssi:8;
unsigned rate:4;
unsigned is_group:1;
unsigned:1;
unsigned sig_mode:2;
179/183
Espressif Systems
unsigned:1;
unsigned Aggregation:1;
unsigned STBC:2;
unsigned FEC_CODING:1; // if is 11n packet, shows if is LDPC packet or not.
unsigned SGI:1;
unsigned rxend_state:8;
unsigned ampdu_cnt:8;
unsigned channel:4; //which channel this packet in.
unsigned:12;
};
struct LenSeq{
u16 len; // length of packet
u16 seq; // serial number of packet, the high 12bits are serial number,
//
//length of packet
};
struct sniffer_buf2{
struct RxControl rx_ctrl;
u8 buf[112];
u16 cnt;
u16 len;
//length of packet
};
The callback function wifi_promiscuous_rx contains two parameters ( buf and len). len shows the
length of buf, it can be: len = 128, len = X * 10, len = 12.
LEN == 128
buf contains structure sniffer_buf2: it is the management packet, it has 112 bytes of data.
sniffer_buf2.cnt is 1.
180/183
Espressif Systems
LEN == X * 10
buf contains structure sniffer_buf: this structure is reliable, data packets represented by it
sniffer_buf.cnt shows the number of packets in buf. The value of len is decided by
sniffer_buf.cnt.
If sniffer_buf.cnt > 1, it is a AMPDU packet. Because headers of each MPDU packets are
similar, we only provide the length of each packet (from the header of MAC packet to FCS)
This structure contains: length of packet, MAC address of both communication sides, length
of the packet header.
LEN == 12
buf contains structure RxControl; but this structure is not reliable. It cannot show the MAC
It does not show the number or the length of the sub-packets of AMPDU packets.
RSSI and FEC_CODING are used to judge whether the packets are from the same device.
Summary
It is recommended that users speed up the processing of individual packets, otherwise, some followup packets may be lost.
Format of an entire IEEE802.11 packet is shown as below.
The first 24 bytes of MAC header of the data packet are needed:
181/183
Espressif Systems
For WEP encrypted packets, the MAC header is followed by an 4-byte IV, and there is a 4byte ICV before the FCS.
For TKIP encrypted packets, the MAC header is followed by a 4-byte IV and a 4-byte EIV, and
there are an 8-byte MIC and a 4-byte ICV before the FCS.
For CCMP encrypted packets, the MAC header is followed by an 8-byte CCMP header, and
there is an 8-byte MIC before the FCS.
8.4.
Even though ESP8266 supports the softAP+station mode, it is limited to only one hardware channel.
In the softAP+station mode, the ESP8266 soft-AP will adjust its channel configuration to be same as
the ESP8266 station.
This limitation may cause some inconveniences in the softAP+station mode that users need to pay
special attention to, for example:
Case 1:
(1) When the user connects the ESP8266 to a router (for example, channel 6),
(2) and sets the ESP8266 soft-AP through wifi_softap_set_config,
(3) If the value is effective, the API will return to true. However, the channel will be automatically
adjusted to channel 6 in order to be in line with the ESP8266 station interface. This is because
there is only one hardware channel in this mode.
Case 2:
(1) If the user sets the channel of the ESP8266 soft-AP through wifi_softap_set_config (for
example, channel 5),
(2) other stations will connect to the ESP8266 soft-AP,
(3) then the user connects the ESP8266 station to a router (for example, channel 6),
(4) the ESP8266 softAP will adjust its channel to be as same as the ESP8266 station (which is
channel 6 in this case).
(5) As a result of the change of channel, the station Wi-Fi connected to the ESP8266 soft-AP in
step two will be disconnected.
Case 3:
(1) Other stations are connected to the ESP8266 softAP .
182/183
Espressif Systems
(2) If the ESP8266s station interface has been scanning or trying to connect to a target router, the
ESP8266 softAP-end connection may break.
This is because the ESP8266 station will try to find its target router in different channels, which means
it will keep changing channels, and as a result, the ESP8266 channel is changing, too. Therefore, the
ESP8266 softAP-end connection may break.
In cases like this, users can set a timer to call wifi_station_disconnect to stop the ESP8266 station
from continuously trying to connect to the router. Or use wifi_station_set_reconnect_policy or
wifi_station_set_auto_connect to disable the ESP8266 station from reconnecting to the router.
8.5.
ESP8266 outputs boot messages through UART0 with baud rate 74880:
Messages
Description
1: power on
rst cause
2: external reset
4: hardware watchdog-reset
boot mode
(first parameter)
chksum
183/183