Statistics: Posted by AlexLuo — Wed Nov 22, 2017 7:42 am
Statistics: Posted by Alexis — Tue Nov 21, 2017 2:49 pm
Code:
#define HTTP_HEADER "Connection: keep-alive\r\n\
Cache-Control: no-cache\r\n\
User-Agent: rBoot-Sample/1.0\r\n\
Accept: */*\r\n\r\n"
void ICACHE_FLASH_ATTR user_tcp_recv_cb(void *arg, char *pusrdata, unsigned short length)
{
os_printf("Block received: %d\n", length);
}
void ICACHE_FLASH_ATTR user_tcp_discon_cb(void *arg)
{
os_printf("Time [%d]: ", system_get_time());
os_printf("Disconnect callback\n");
}
void ICACHE_FLASH_ATTR user_tcp_connect_cb(void *arg)
{
char request[REQUEST_SIZE] = {0};
os_printf("Connect callback\n");
espconn *conn = (espconn *) arg;
espconn_regist_recvcb(conn, user_tcp_recv_cb);
espconn_regist_disconcb(conn, user_tcp_discon_cb);
os_sprintf(request,
"GET %s HTTP/1.1\r\nHost: %s\r\n%s\r\n",
OTA_PATH, OTA_HOST, HTTP_HEADER);
os_printf("Time [%d]:", system_get_time());
os_printf("Request: %s\n", request);
espconn_sent(conn, (uint8_t *) request, os_strlen((char*)request));
}
void ICACHE_FLASH_ATTR user_tcp_recon_cb(void *arg, sint8 errType)
{
os_printf("Time [%d]: ", system_get_time());
os_printf("Reconnect: %d\n", errType);
}
/******************************************************************************
* 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");
#if 1
// 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] = OTA_IP;
uint32_t keepalive_time = 60;
os_memcpy(user_tcp_conn.proto.tcp->remote_ip, esp_server_ip, 4);
user_tcp_conn.proto.tcp->remote_port = 80;
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_set_opt(&user_tcp_conn, ESPCONN_KEEPALIVE);
espconn_set_keepalive(&user_tcp_conn, ESPCONN_KEEPIDLE, &keepalive_time);
espconn_connect(&user_tcp_conn);
#endif
}
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 will connect to
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_set_station_config(void)
{
// Wifi configuration
char ssid[32] = SSID;
char password[64] = SSID_PASSWORD;
struct station_config stationConf;
os_printf("Set station config\n");
//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 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);
}
#ifdef ADD_HEAP_PRINT_OUT
void ICACHE_FLASH_ATTR
user_heap_print_out()
{
LOG_STATEMENT(INFO, "Remaining heap:%d\n", system_get_free_heap_size());
}
void ICACHE_FLASH_ATTR
user_routine_heap_print_out()
{
os_timer_disarm(&test_timer);
os_timer_setfn(&test_timer, (os_timer_func_t *)user_heap_print_out, NULL);
os_timer_arm(&test_timer, 3000, 1);
}
#endif
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
extern "C" ICACHE_FLASH_ATTR void user_init(void)
{
// Initialize UART
uart_init(BIT_RATE_115200, BIT_RATE_115200);
// Configure the UART
os_printf("\r\nSystem init...\r\n");
do_global_ctors();
os_printf("\r\nGlobal constructors invoked\r\n");
user_set_station_config();
}
Statistics: Posted by Guest — Thu Feb 23, 2017 6:28 pm