20a-Esp8266 Rtos SDK Programming Guide en
20a-Esp8266 Rtos SDK Programming Guide en
Programming Guide
Version 1.5
Copyright 2017
About This Guide
This document provides sample codes based on ESP8266_RTOS_SDK.
The document is structured as follows:
Release Notes
2. Overview ................................................................................................................................ 2
2.1. RTOS SDK Introduction .............................................................................................................2
3. Sample Codes........................................................................................................................4
3.1. Directory Structure of RTOS SDK ..............................................................................................4
3.2.11. How to Port Apps from Non-OS SDK to RTOS SDK .................................................15
A. Appendix .............................................................................................................................. 35
A.1. Snier Introduction ...................................................................................................................35
1. Introduction
The ESP8266EX offers a complete and self-contained Wi-Fi SoC solution. With low power
consumption, a compact design and high stability, it caters to the needs of its users.
It can be used to host applications or to offload Wi-Fi network functions from other
application processors. When the ESP8266 hosts an application, as the only processor in
the device, it boots up directly from an external flash. It has an built-in, high-speed cache
which improves the performance of the system and optimizes the memory. Alternatively,
when the ESP8266EX is used as a Wi-Fi adapter, wireless internet access can be added to
any microcontroller-based device through the SPI/SDIO interface or the I2C/UART
interface, and thus provide the users with a simple Wi-Fi solution.
Furthermore, the ESP8266EX offers a high level of on-chip integration. It integrates the
antenna switch, RF balun, power amplifier, low-noise receive amplifier, filters, and power
management modules. It requires minimal external circuitry, and the entire solution,
including the front-end module, is designed to occupy minimal PCB space.
The ESP8266EX also integrates an enhanced version of the 32-bit processor of Tensilicas
L106 Diamond series, with on-chip SRAM. The ESP8266EX is often integrated with
external sensors and other application-specific devices through its GPIOs. The SDK files
provide examples of the software of the related applications.
The ESCPEspressif Systems Smart Connectivity Platform has many cutting-edge
advantages, including energy-efficient VoIP that can switch rapidly between sleep and
wake modes, adaptive radio bias for low-power operations, front-end signal processing
capacity, problem-shooting capacity, and the radio system co-existence feature to remove
cellular, bluetooth, DDR, LVDS and LCD interference.
The SDK based on the ESP8266EX IoT platform offers users a simple, high-speed and
efficient software platform for IoT device development. This programming guide provides an
overview of the SDK, as well as details of the APIs. The present document should be
particularly helpful to embedded software developers who use the ESP8266EX IoT platform
for software development.
Espressif ! /37
1 2017.02
2. Overview
!
2. Overview
2.1. RTOS SDK Introduction
The SDK provides its users with a set of interfaces for data reception and transmission.
Users do not need to worry about the set-up of the network, including Wi-Fi and TCP/IP
stack. Instead, they can focus on the IoT application development. They can do so by
receiving and transmitting data through the interfaces.
All network functions on the ESP8266 IoT platform are realized in the library, and are not
transparent to the users. Users can initialize the interface in user_main.c.
void user_init(void) is the entry-point function of the application. It provides users with
an initialization interface, and users can add more functions to the interface, including
hardware initialization, network parameters setting, and timer initialization.
Espressif ! /37
2 2017.02
2. Overview
!
Espressif ! /37
3 2017.02
3. Sample Codes
!
3. Sample Codes
3.1. Directory Structure of RTOS SDK
ESP8266 RTOS SDK can be downloaded via the following link:
ESP8266 RTOS SDK.
Below is the directory structure of the ESP8266 RTOS SDK.
bin: boot and initialization firmware.
documents: ESP8266_RTOS_SDK files.
driver_lib: sample codes for drivers.
examples: sample codes for Espressifs application programs.
- openssl_demo: OpenSSL-API-related sample codes.
- project_template: sample codes for a project template.
- smart_config: SmartConfig-related sample codes.
- spiffs_test: SPIFFS-related sample codes.
- websocket_demo: WebSocket-related sample codes.
include: header files of ESP8266_RTOS_SDK, including software interfaces and
macro functions for users to use.
ld: linker scripts used for compiling; users do not need to modify them.
lib: library files provided in ESP8266_RTOS_SDK.
third_party: third-party library of Espressifs open-source codes, currently including
freeRTOS, JSON, lwIP, mbedTLS, noPoll, OpenSSL, SPIFFS, and SSL.
tools: tools; users do not need to modify them.
Espressif ! /!37
4 2017.02
3. Sample Codes
!
Examples of RTC
3.2.1. Initialization
1. The initialization of application programs can be implemented in user_main.c. The
function void user_init(void), which is the entry-point function, can be used by users
to implement the initialization process. It is suggested that the SDKs version information
be printed, and the Wi-Fi working mode be set.
void user_init(void)
Wi-Fi_set_opmode(STATIONAP_MODE);
uart_config.baud_rate = BIT_RATE_74880;
uart_config.data_bits = UART_WordLength_8b;
uart_config.parity = USART_Parity_None;
uart_config.stop_bits = USART_StopBits_1;
uart_config.flow_ctrl = USART_HardwareFlowControl_None;
uart_config.UART_RxFlowThresh = 120;
uart_config.UART_InverseMask = UART_None_Inverse;
UART_ParamConfig(UART0, &uart_config);
Register the UART interrupt function and enable the UART interrupt:
UART_IntrConfTypeDef uart_intr;
uart_intr.UART_RX_FifoFullIntrThresh = 10;
uart_intr.UART_RX_TimeOutIntrThresh = 2;
uart_intr.UART_TX_FifoEmptyIntrThresh = 20;
UART_IntrConfig(UART0, &uart_intr);
Espressif ! /!37
5 2017.02
3. Sample Codes
!
UART_SetPrintPort(UART0);
UART_intr_handler_register(uart0_rx_intr_handler);
ETS_UART_INTR_ENABLE();
Register the task and execute the function. Take the execution of task 2 as an example:
void task2(void *pvParameters)
while (1) {
vTaskDelete(NULL);
4. Compile the application program, generate firmware and download it into the ESP8266
module.
5. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
SDK version:1.0.3(601f5cd)
2. Compile the application program, generate firmware and download it into the ESP8266
module.
3. Power off the module, and change it to operation mode; then power on the module and
run the program.
Espressif ! /!37
6 2017.02
3. Sample Codes
!
Result:
ESP8266 chip ID:0x97f740
sprintf(config->ssid, DEMO_AP_SSID);
sprintf(config->password, DEMO_AP_PASSWORD);
Wi-Fi_station_set_config(config);
free(config);
Wi-Fi_station_connect();
3. Compile the application program, generate firmware and program it into ESP8266
module.
4. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
connected with DEMO_AP, channel 11
ip:192.168.1.103,mask:255.255.255.0,gw:192.168.1.1
Espressif ! /!37
7 2017.02
3. Sample Codes
!
sprintf(config->ssid, DEMO_AP_SSID);
sprintf(config->password, DEMO_AP_PASSWORD);
config->authmode = AUTH_WPA_WPA2_PSK;
config->max_connection = 4;
free(config);
while(station){
MAC2STR(station->bssid), IP2STR(&station->ip));
Wi-Fi_set_ip_info(SOFTAP_IF, &info);
5. The range of the IP address allocated by ESP8266 SoftAP can be set by developers.
For example, the IP address can range from 192.168.5.100 to 192.168.5.105. Please
enable the DHCP server when the configuration is completed.
struct dhcps_lease dhcp_lease;
Wi-Fi_softap_set_dhcps_lease(&dhcp_lease);
Espressif ! /!37
8 2017.02
3. Sample Codes
!
6. Compile the application program, generate firmware and download it into the ESP8266
module.
7. Power off the module, and change it to operation mode; then power on the module and
run the program. Connect a PC or other Stations to the ESP8266 SoftAP.
!
Result:
ESP8266 works as SoftAP, and the following information will be printed when a Station is
connected to it:
switch (evt->event_id) {
case EVENT_STAMODE_CONNECTED:
evt->event_info.connected.ssid,
evt->event_info.connected.channel);
break;
case EVENT_STAMODE_DISCONNECTED:
evt->event_info.disconnected.ssid,
evt->event_info.disconnected.reason);
break;
case EVENT_STAMODE_AUTHMODE_CHANGE:
evt->event_info.auth_change.old_mode,
evt->event_info.auth_change.new_mode);
break;
case EVENT_STAMODE_GOT_IP:
IP2STR(&evt->event_info.got_ip.ip),
Espressif ! /!37
9 2017.02
3. Sample Codes
!
IP2STR(&evt->event_info.got_ip.mask),
IP2STR(&evt->event_info.got_ip.gw));
printf("\n");
break;
case EVENT_SOFTAPMODE_STACONNECTED:
MAC2STR(evt->event_info.sta_connected.mac),
evt->event_info.sta_connected.aid);
break;
case EVENT_SOFTAPMODE_STADISCONNECTED:
MAC2STR(evt->event_info.sta_disconnected.mac),
evt->event_info.sta_disconnected.aid);
break;
default:
break;
void user_init(void)
Wi-Fi_set_event_handler_cb(Wi-Fi_handle_event_cb);
3. Compile the application program, generate firmware and download it into the ESP8266
module.
4. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
For example, when ESP8266 functions as a Station, the process of how it is connected to
a router is shown below:
Wi-Fi_handle_event_cb : event 1
Wi-Fi_handle_event_cb : event 4
IP:192.168.1.126,mask:255.255.255.0,gw:192.168.1.1
Wi-Fi_handle_event_cb : event 2
Espressif ! /! 37
10 2017.02
3. Sample Codes
!
unique and exclusive. If users want to reset the MAC address, the uniqueness of the
MAC address should be assured.
2. Set ESP8266 to Station+SoftAP mode.
Wi-Fi_set_opmode(STATIONAP_MODE);
Wi-Fi_get_macaddr(STATION_IF, sta_mac);
4. Set the MAC addresses of the Station and SoftAP interfaces. The setting of MAC
addresses is not stored in the flash, and the setting can only be done when the
corresponding interface is enabled first.
char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab};
Wi-Fi_set_macaddr(SOFTAP_IF, sofap_mac);
Wi-Fi_set_macaddr(STATION_IF, sta_mac);
5. Compile the application program, generate firmware and download it into the ESP8266
module.
6. Power off the module, and change it to operation mode, then power on the module and
run the program.
Notice:
Mac addresses are different for the two distinct modes of ESP8266 (SoftAP and Station). Please do
not set the same MAC address for ESP8266 SoftAP and ESP8266 Station.
Bit 0 of the first byte of the MAC address should not be 1. For example, the MAC address can be set
as 1a:fe:36:97:d5:7b, but not as 15:fe:36:97:d5:7b.
Result:
ESP8266 station MAC :18:fe:34:97:f7:40
Espressif ! /! 37
11 2017.02
3. Sample Codes
!
uint8 ssid[33];
char temp[128];
if (status == OK) {
memset(ssid, 0, 33);
else
printf("(%d,\"%s\",%d,\""MACSTR"\",%d)\r\n",
MAC2STR(bss_link->bssid),bss_link->channel);
bss_link = bss_link->next.stqe_next;
} else {
3. Compile the application program, generate firmware and download it into the ESP8266
module.
4. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
Hello, welcome to scan-task!
scandone
(0,"ESP_A13319",-41,"1a:fe:34:a1:33:19",1)
(4,"sscgov217",-75,"80:89:17:79:63:cc",1)
(0,"ESP_97F0B1",-46,"1a:fe:34:97:f0:b1",1)
(0,"ESP_A1327E",-36,"1a:fe:34:a1:32:7e",1)
Espressif ! /! 37
12 2017.02
3. Sample Codes
!
Scan the AP with a specified SSID. After the scanning is completed, scan_done will be
called back.
struct scan_config config;
memset(&config, 0, sizeof(config));
config.ssid = DEMO_AP_SSID;
Wi-Fi_station_scan(&config,scan_done);
2. Compile the application program, generate firmware and download it into the ESP8266
module.
3. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
scandone
(3,"DEMO_AP",-49,"aa:5b:78:30:46:0a",11)
uint32 value;
2. Similarly, when writing data into sectors on a flash memory, the data address should also
be four-byte aligned. Use spi_flash_erase_sector to erase the sector first, then call
spi_flash_write to write data into it. For example,
uint32 data[M];
spi_flash_erase_sector(N);
Espressif ! /! 37
13 2017.02
3. Sample Codes
!
3. Compile the application program, generate firmware and download it into the ESP8266
module.
4. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
read data from 0x3E000 : 05 00 04 02
cal = system_rtc_clock_cali_proc();
Read and write RTC memory. Please note that RTC memory access must be four-byte
aligned.
typedef struct {
uint64 time_acc;
uint32 magic ;
uint32 time_base;
} RTC_TIMER_DEMO;
2. Compile the application program, generate firmware and download it into the ESP8266
module.
Espressif ! /! 37
14 2017.02
3. Sample Codes
!
3. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
rtc_time: 1613921
cal: 6.406
ETSEvent test_q[Q_NUM];
switch(e->sig)
case 1:
func1(e->par);
break;
case 2:
func2();
break;
case 3:
func3();
break;
default:
break;
void func_send_Sig(void)
ETSSignal sig = 2;
system_os_post(2,sig,0);
void task_ini(void)
system_os_task(test_task, 2, test_qQ_NUM);
Espressif ! /! 37
15 2017.02
3. Sample Codes
!
xQueueHandle test_q;
xTaskHandle test_task_hdl;
int *sig;
for(;;) {
vTaskSuspendAll();
switch(*sig)
case 1:
func1();
break;
case 2:
func2();
break;
default:
break;
free(sig);
xTaskResumeAll();
void func_send_Sig(void)
*evt = 2;
if(xQueueSend(test_q,&evt,10/portTick_RATE_MS)!=pdTRUE){
os_printf("test_q is full\n");
// It is the address of parameter that stored in test_q, so int *evt and int *sig can be
other types.
void task_ini(void)
Espressif ! /! 37
16 2017.02
3. Sample Codes
!
2. Create a socket.
LOCAL int32 sock_fd;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(UDP_LOCAL_PORT);
server_addr.sin_len = sizeof(server_addr);
do{
if (sock_fd == -1) {
vTaskDelay(1000/portTICK_RATE_MS);
}while(sock_fd == -1);
Espressif ! /! 37
17 2017.02
3. Sample Codes
!
if (ret != 0) {
vTaskDelay(1000/portTICK_RATE_MS);
}while(ret != 0);
memset(udp_msg, 0, UDP_DATA_LEN);
memset(&from, 0, sizeof(from));
if (ret > 0) {
printf("ESP8266 UDP task > recv %d Bytes from %s, Port %d\n,ret,
inet_ntoa(from.sin_addr), ntohs(from.sin_port));
if(udp_msg){
free(udp_msg);
udp_msg = NULL;
close(sock_fd);
5. Compile the application program, generate firmware and download it into the ESP8266
module.
6. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
ip:192.168.1.103,mask:255.255.255.0,gw:192.168.1.1
Espressif ! /! 37
18 2017.02
3. Sample Codes
!
ESP8266 UDP task > recv data 16 Bytes from 192.168.1.112, Port 57233
UDP communication can be set up at the PC terminal by using network debugging tools;
then ESP8266 UDP test will be sent to the ESP8266 UDP port. When the UDP data is
received by ESP8266, the same message will be sent to the PC terminal, too.
Espressif ! /! 37
19 2017.02
3. Sample Codes
!
if (-1 == sta_socket) {
close(sta_socket);
vTaskDelay(1000 / portTICK_RATE_MS);
continue;
remote_ip.sin_family = AF_INET;
remote_ip.sin_addr.s_addr = inet_addr(SERVER_IP);
remote_ip.sin_port = htons(SERVER_PORT);
close(sta_socket);
vTaskDelay(1000 / portTICK_RATE_MS);
continue;
close(sta_socket);
vTaskDelay(1000 / portTICK_RATE_MS);
continue;
free(pbuf);
recv_buf[recbytes] = 0;
Espressif ! /! 37
20 2017.02
3. Sample Codes
!
printf("ESP8266 TCP client task > recv data %d bytes!\nESP8266 TCP client task > %s
\n", recbytes, recv_buf);
free(recv_buf);
if (recbytes <= 0) {
close(sta_socket);
4. Compile the application program, generate firmware and download it into the ESP8266
module.
5. Power off the module, and change it to operation mode, then power on the module and
run the program.
Result:
ESP8266 TCP client task > socket ok!
Below is an example showing that the TCP server established at the terminal of network
debugging tool communicates with the ESP8266 successfully.
Espressif ! /! 37
21 2017.02
3. Sample Codes
!
int32 listenfd;
int32 ret;
int stack_counter=0;
server_addr.sin_len = sizeof(server_addr);
do{
if (listenfd == -1) {
vTaskDelay(1000/portTICK_RATE_MS);
}while(listenfd == -1);
do{
if (ret != 0) {
vTaskDelay(1000/portTICK_RATE_MS);
}while(ret != 0);
if (ret != 0) {
vTaskDelay(1000/portTICK_RATE_MS);
Espressif ! /! 37
22 2017.02
3. Sample Codes
!
}while(ret != 0);
Wait until the TCP client is connected to the server; then start receiving data packets
when the TCP communication is established:
int32 client_sock;
for (;;) {
continue;
recv_buf[recbytes] = 0;
printf("ESP8266 TCP server task > read data success %d!\nESP8266 TCP server task
> %s\n", recbytes, recv_buf);
free(recv_buf);
if (recbytes <= 0) {
close(client_sock);
2. Compile the application program, generate firmware and download it into the ESP8266
module.
3. Power off the module, and change it to operation mode; then power on the module and
run the program.
4. Establish a TCP client using the network debugging tool, then connect the TCP client
with the ESP8266 TCP server, and start sending data.
Espressif ! /! 37
23 2017.02
3. Sample Codes
!
!
Result:
ip:192.168.1.127,mask:255.255.255.0,gw:192.168.1.1
got ip !!!
Espressif ! /! 37
24 2017.02
3. Sample Codes
!
Notice:
Erasing the flash memory is a slow process. Thus it may take longer time to erase a flash memory sector
while writing information into other sectors of the flash at the same time. Besides, the stability of the
network might also be affected. Consequently, it is suggested that users call function
spi_flash_erase_sector to erase sectors to be upgraded first; connect to the network, and download
the latest firmware from OTA server; then, call function spi_flash_write to write information into the
flash.
1. Users can establish their own cloud server, or use the cloud server provided by
Espressif.
2. Upload the new firmware to the cloud server.
3. Descriptions of the codes are listed below:
Connect the ESP8266 module to the AP (for details of this process users can refer to
previous examples). Then check if the ESP8266 Station can get the IP address through
function upgrade_task.
Wi-Fi_get_ip_info(STATION_IF, &ipconfig);
while (ipconfig.ip.addr == 0) {
vTaskDelay(1000 / portTICK_RATE_MS);
Wi-Fi_get_ip_info(STATION_IF, &ipconfig);
When the IP address is obtained by ESP8266, the module will be connected to the
cloud server. (Users can refer to previous examples of socket programming).
system_upgrade_flag_set: sets a flag to indicate the upgrade status.
- UPGRADE_FLAG_IDLE: idle.
- UPGRADE_FLAG_START: starts the upgrade.
- UPGRADE_FLAG_FINISH: finishes downloading new firmware from the cloud server.
system_upgrade_userbin_check: checks the user binary file that the system is
running. If the system is running user1.bin, then user2.bin will be downloaded; if the
systems is running user2.bin, then user1.bin will be downloaded.
system_upgrade_init();
system_upgrade_flag_set(UPGRADE_FLAG_START);
Send the downloading request to the server. After the upgraded firmware data is
received successfully, program it into the flash.
if(write(sta_socket,server->url,strlen(server->url)+1) < 0) {
Espressif ! /! 37
25 2017.02
3. Sample Codes
!
Set a software timer to check the upgrade status of the firmware periodically. If the
timer indicates a time-out, and the firmware has not been updated from the cloud
server, it means the upgrade failed. The status of the firmware upgrade will become
idle and the upgrade will stop.
If the firmware has been successfully downloaded from the server, the upgrade status
will be shown as UPGRADE_FLAG_FINISH. Call function system_upgrade_reboot,
reboot ESP8266, and start running the newly updated firmware.
4. Compile the application program, generate firmware and download it into the ESP8266
module.
5. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
Establish a server at the PC terminal via the webserver, then upload user1.bin and
user2.bin to the same server. After the firmware has been programmed into
ESP8266, user1.bin will is run first by default, then user2.bin will be downloaded
from the server.
!
The module will reboot automatically when user2.bin has been downloaded, and will
start running user2.bin. Until the next FOTA upgrade, ESP8266 will not run
user1.bin. When there is a new firmware on the server and a new FOTA upgrade is
requested by the users, user1.bin will be downloaded from the server. When the
module reboots automatically, user1.bin is run in this case. This process repeats.
Espressif ! /! 37
26 2017.02
3. Sample Codes
!
!
The print information during ESP8266 upgrading process:
ip:192.168.1.127,mask:255.255.255.0,gw:192.168.1.1
socket ok!
connect ok!
Host: "192.168.1.114":80
Connection: keep-alive
Cache-Control: no-cache
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
send success
totallen = 1460
totallen = 2920
Espressif ! /! 37
27 2017.02
3. Sample Codes
!
Notice:
When forced-sleep interface is called, the chip will not enter sleep mode instantly, it will enter sleep
mode when the system is executing the idle task.
Below are the sample codes for sleep modes.
void fpm_wakup_cb_func1(void)
Wi-Fi_station_connect(); // connect to AP
void user_func()
Wi-Fi_station_disconnect();
#ifdef SLEEP_MAX
Wi-Fi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
#else
Wi-Fi_fpm_do_sleep(50*1000);
#endif
#ifdef SLEEP_MAX
void func1(void)
Wi-Fi_fpm_do_wakeup();
Espressif ! /! 37
28 2017.02
3. Sample Codes
!
Wi-Fi_station_connect(); // connect to AP
#endif
Wi-Fi_station_connect(); // connect to AP
#ifndef SLEEP_MAX
void user_func()
Wi-Fi_station_disconnect();
Wi-Fi_fpm_do_sleep(50*1000);
#else
// Or wakeup by GPIO
void user_func()
Wi-Fi_station_disconnect();
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U,3);
gpio_pin_wakeup_enable(13, GPIO_PIN_INTR_LOLEVEL);
Wi-Fi_fpm_do_sleep(0xFFFFFFF);
Espressif ! /! 37
29 2017.02
3. Sample Codes
!
#endif
config.phys_size = FS1_FLASH_SIZE;
config.phys_addr = FS1_FLASH_ADDR;
config.phys_erase_block = SECTOR_SIZE;
config.log_block_size = LOG_BLOCK;
config.log_page_size = LOG_PAGE;
config.fd_buf_size = FD_BUF_SIZE * 2;
config.cache_buf_size = CACHE_BUF_SIZE;
esp_spiffs_init(&config);
2. Open and create a new file, and write the data in it.
char *buf="hello world";
if(pfd <= 3) {
if (write_byte <= 0)
close(pfd);
close(pfd);
Espressif ! /! 37
30 2017.02
3. Sample Codes
!
pTestParamer->ip.addr = ipaddr_addr(SSL_SERVER_IP);
pTestParamer->port = server_port;
Wi-Fi_get_ip_info(STATION_IF, &ipconfig);
while (ipconfig.ip.addr == 0) {
vTaskDelay(1000 / portTICK_RATE_MS);
Wi-Fi_get_ip_info(STATION_IF, &ipconfig);
memset(&client_addr, 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(port);
client_addr.sin_addr.s_addr = sin_addr;
Espressif ! /! 37
31 2017.02
3. Sample Codes
!
printf("heap_size %d\n,system_get_free_heap_size());
If using SPIFFS file system, please run the tool spiffy (https://github.com/xlfe/spiffy.
Please note that the spiffs_config.h of this tool has to be the same as the one in the
RTOS SDK). Then generate spiffs_rom.bin, and write it into the flash; for details on
this process please refer to the example of esp_spiffs_init.
Below is an example showing how to read information about the SSL encryption key
and certificate from the flash using SPIFFS.
if (ssl_obj_load(ssl_ctx, SSL_OBJ_RSA_KEY, XX.key, password)){
if (ssl != NULL){
9. If the handshake is successful, then the certificate can be released and more memory
space will be available.
Espressif ! /! 37
32 2017.02
3. Sample Codes
!
if (common_name){
display_session_id(ssl);
display_cipher(ssl);
quiet = true;
x509_free(ssl->x509_ctx);
ssl->x509_ctx=NULL;
bzero(buf, sizeof(buf));
sprintf(buf,httphead,"/", "iot.espressif.cn",port);
os_printf("%s\n", buf);
ssl_free(ssl);
ssl_ctx_free(ssl_ctx);
close(client_fd);
vTaskDelay(1000 / portTICK_RATE_MS);
os_printf("send fail\n");
continue;
if(recbytes == 0){
vTaskDelay(500 / portTICK_RATE_MS);
continue;
os_printf("%s\n", read_buf);
free(read_buf);
if(recbytes < 0) {
ssl_free(ssl);
ssl_ctx_free(ssl_ctx);
Espressif ! /! 37
33 2017.02
3. Sample Codes
!
close(client_fd);
vTaskDelay(1000 / portTICK_RATE_MS);
Result:
ip:192.168.1.127,mask:255.255.255.0,gw:192.168.1.1
4ae116a6a0445b369f010e0ea5420971497e92179a6602c8b5968c1f35b60483
CIPHER is AES128-SHA
GET / HTTP/1.1
Host: iot.espressif.cn:443
Connection: keep-alive
Espressif ! /! 37
34 2017.02
Appendix A
!
A. Appendix
A.1. Sniffer Introduction
For more details on sniffer, please refer to ESP8266 Technical Reference.
Espressif ! /! 37
35 2017.02
Appendix A
!
4. In cases like this, users can set a timer to call Wi-Fi_station_disconnect in order to
stop the ESP8266 Station from continuously trying to connect to the router. Users can
also call Wi-Fi_station_set_reconnect_policy or Wi-Fi_station_set_auto_connect
to disable the ESP8266 Station from reconnecting to the router.
!
Below is the description about the boot messages:
1: power on
chksum If chksum == csum, it means that flash is read correctly during booting.
Espressif ! /! 37
36 2017.02
Disclaimer and Copyright Notice
Information in this document, including URL references, is subject to change without
notice.
THIS DOCUMENT IS PROVIDED AS IS WITH NO WARRANTIES WHATSOEVER,
INCLUDING ANY WARRANTY OF MERCHANTABILITY, NON-INFRINGEMENT, FITNESS
FOR ANY PARTICULAR PURPOSE, OR ANY WARRANTY OTHERWISE ARISING OUT
OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.
All liability, including liability for infringement of any proprietary rights, relating to use of
information in this document is disclaimed. No licenses express or implied, by estoppel or
otherwise, to any intellectual property rights are granted herein.
The Wi-Fi Alliance Member logo is a trademark of the Wi-Fi Alliance. The Bluetooth logo is
a registered trademark of Bluetooth SIG.
All trade names, trademarks and registered trademarks mentioned in this document are
Espressif IOT Team
property of their respective owners, and are hereby acknowledged.
www.espressif.com Copyright 2017 Espressif Inc. All rights reserved.