Sample code below is based on ESP8266_NONOS_SDK.
Note:for ESP8266_RTOS_SDK, please call Espconn_init () on the User_init to initialize Espconn first.
We suggest to call the next espconn_send in Espconn_sent_callback which means that the previous packet is sent.
Or users could enable the espconn_write_finish_callback which means the data sending by Espconn_send is written into the Write-buffer or already sent, and call the next espconn_send in Espconn_write_finish_callback.
The Write-buffer can store 8 packets at most, waiting for the TCP ACK. The size of Write-buffer is 2920 bytes.
Introduce an API here:
Function:set option of TCP connection
Prototype:
Sint8 espconn_set_opt (
struct Espconn *espconn,
Uint8 opt
)
Parameter:
struct Espconn *espconn:corresponding connected control structure
Uint8 Opt:option of TCP connection
Bit 0:1: Free memory after TCP disconnection happen need not wait 2 minutes;
Bit 1:1: Disable NALGO algorithm during TCP data transmission, quiken the data transmission.
Bit 2:1: Enable Espconn_write_finish_callback.
Bit 3:1: Enable TCP keep-alive function
Return:
0:succeed
Non-0: Error (refer to Espconn.h for details.)
e.g. Espconn_arg:illegal Argument,can ' t find TCP connection according to structure Espconn
Note:
In general, we need is not the call of this API;
If call espconn_set_opt, you call the IT in TCP connected callback.
Sample Code of Send TCP data continually is as Bellow
1. Call espconn_set_opt to enable write buffer;
2. Register a write finish callback;
3. Send next packet in write finish callback.
* FUNCTIONNAME:USER_TCP_RECV_CB * description:receive callback. * Parameters:arg--Additional argument to pass to the callback function * Returns:none ********************** /LOCAL void icache_flash_attr user_tcp_recv_cb (void *arg, Char *pusrdata, unsigned short length) {//received Some data from TCP connection os_printf ("TCP recv!!!
%s \ r \ n ", pusrdata);
}/****************************************************************************** * FUNCTIONNAME:USER_TCP_SENT_CB
* Description:data sent callback. * Parameters:arg--Additional argument to pass to the callback function * Returns:none **********************
/LOCAL void icache_flash_attr user_tcp_sent_cb (void *arg) {//data sent successfully}/****************************************************************************** * Functi ONNAME:USER_TCP_DISCON_CB * descriptIon:disconnect callback. * Parameters:arg--Additional argument to pass to the callback function * Returns:none ********************** /LOCAL void icache_flash_attr user_tcp_discon_cb (void * ARG) {//tcp Disconnect successfully os_printf ("TCP Disconnect SUCCEED!!!
\ r \ n "); }/****************************************************************************** * Functionname:user_tcp_write_
Finish * Description:data need to being sent by Espconn_sent have been written into write buffer successfully,
Call Espconn_sent to send next packet is allowed. * Parameters:pespconn--the espconn used to connetion with the host * Returns:none ************************** /LOCAL void icache_flash_attr user_tcp_write_finish (void *arg
{struct Espconn *pespconn = arg;
Espconn_sent (Pespconn, "Hello world!", 12); }
/************ Functionname:user_sent_data * DESCRIPTION:PR Ocessing the application data and sending it to the host * Parameters:pespconn – the espconn used to connetion with The host * Returns:none *******************************************************************************/LOCAL voi
D icache_flash_attr user_sent_data (struct espconn *pespconn) {espconn_sent (Pespconn, "Hello world!", 12); }/****************************************************************************** * Functionname:user_tcp_connect
_CB * DESCRIPTION:A New incoming TCP connection has been connected. * Parameters:arg--Additional argument to pass to the callback function * Returns:none ********************** /LOCAL void icache_flash_attr user_tcp_connect_cb (void *
Arg) {struct Espconn *pespconn = arg; os_printf ("Connect SUCCEED!!!
\ r \ n "); EspcoNN_REGIST_RECVCB (Pespconn, USER_TCP_RECV_CB);
ESPCONN_REGIST_SENTCB (Pespconn, USER_TCP_SENT_CB);
ESPCONN_REGIST_DISCONCB (Pespconn, USER_TCP_DISCON_CB); Espconn_set_opt (Pespconn, 0x04); Enable Write buffer Espconn_regist_write_finish (pespconn, user_tcp_write_finish);
Register Write finish callback User_sent_data (Pespconn); }/****************************************************************************** * Functionname:user_tcp_recon_
CB * Description:reconnect Callback, error occured in TCP connection. * Parameters:arg--Additional argument to pass to the callback function * Returns:none ********************** /LOCAL void icache_flash_attr user_tcp_recon_cb (void *arg
, Sint8 err) {//error occured, TCP connection broke. User can try to reconnect here. os_printf ("Reconnect callback, error code%d!!!
\ r \ n ", err); }
/********************************************************** FUNCTIONNAME:USER_CHECK_IP * description:check whether get IP addr or not * Parameters: None * Returns:none *******************************************************************************/void ICACHE_
Flash_attr user_check_ip (void) {struct ip_info ipconfig;
Disarm timer first os_timer_disarm (&test_timer);
Get IP info of ESP8266 station wifi_get_ip_info (station_if, &ipconfig); if (wifi_station_get_connect_status () = = Station_got_ip && ipconfig.ip.addr! = 0) {os_printf ("GOT IP!! !
\ r \ n ");
Connect to TCP server as Net_domain user_tcp_conn.proto.tcp = &user_tcp;
User_tcp_conn.type = espconn_tcp;
User_tcp_conn.state = Espconn_none; const char Esp_server_ip[4] = {x, x, x, x}; Remote IP of TCP server os_memcpy (USER_TCP_CONN.PROTO.TCP->REMOTE_IP, ESP_SERVER_IP, 4); Remote IP of TCP server User_tcp_conn.proto.tcp->remote_port = TCP_server_port; Remote port of TCP Server User_tcp_conn.proto.tcp->local_port = Espconn_port (); Local Port of ESP8266 ESPCONN_REGIST_CONNECTCB (&user_tcp_conn, USER_TCP_CONNECT_CB); Register Connect callback ESPCONN_REGIST_RECONCB (&user_tcp_conn, USER_TCP_RECON_CB); Register reconnect callback as error handler Espconn_connect (&user_tcp_conn);
TCP Connect} else {if (wifi_station_get_connect_status () = = Station_wrong_password | |
Wifi_station_get_connect_status () = = Station_no_ap_found | | Wifi_station_get_connect_status () = = Station_connect_fail)) {os_printf ("Connect FAIL!!!
\ r \ n "); } else {//re-arm timer to check IP os_timer_setfn (&test_timer, (os_timer_func_t *
) user_check_ip, NULL);
Os_timer_arm (&test_timer, 100, 0); }
}
}
/*********************************************************** Functionname:user_set_station_config * Description:set the router info which ESP8266 station Would connect to * Parameters:none * Returns:none ********************************************************** /void Icache_flash_attr user_set_station_config (void) {//Wifi configuration char ssid[32] =
"SSID";
Char password[64] = "password";
struct Station_config stationconf;
Need not MAC address Stationconf.bssid_set = 0;
Set AP Settings os_memcpy (&stationconf.ssid, SSID, 32);
os_memcpy (&stationconf.password, password, 64);
Wifi_station_set_config (&stationconf);
Set a timer to check the whether got IP from router succeed or not.
Os_timer_disarm (&test_timer);
OS_TIMER_SETFN (&test_timer, (os_timer_func_t *) user_check_ip, NULL);
Os_timer_arm (&test_timer, 100, 0); }/****************************************************************************** * FuNctionname:user_init * Description:entry of user application, init user function here * parameters:none * Retu
Rns:none *******************************************************************************/void User_init (void) {
os_printf ("SDK version:%s\n", system_get_sdk_version ());
Set SoftAP + Station mode Wifi_set_opmode (Stationap_mode);
ESP8266 Connect to Router user_set_station_config ();
}