堕落不振功业废,勤耕不辍日月新

ESP8266–web服务器基础

综合 hailen 6℃

ESP8266–web服务器基础

/*
* ESPRSSIF MIT License
*
* Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the “Software”), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES or OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT or OTHERWISE, ARISING FROM, OUT OF or IN
* CONNECTION WITH THE SOFTWARE or THE USE or OTHER DEALINGS IN THE SOFTWARE.
*
*/

#include “osapi.h”
#include “at_custom.h”
#include “user_interface.h”
#include “espconn.h”
#include “ets_sys.h”
#include “mem.h”

uint8_t http_head[] = “HTTP/1.1 200 OK

HTTP测试成功”;
struct espconn tcpserver;

void ICACHE_FLASH_ATTR
tcpserver_recon_cb(void *arg, sint8 errType)//异常断开回调
{
struct espconn *pespconn = (struct espconn *)arg;
os_printf(”
异常断开”);
}

void ICACHE_FLASH_ATTR
tcpserver_discon_cb(void *arg)//正常断开回调
{
struct espconn *pespconn = (struct espconn *)arg;
os_printf(”
正常断开”);
}

void ICACHE_FLASH_ATTR
tcpclient_sent_cb(void *arg)//发送回调
{
struct espconn *pespconn = (struct espconn *)arg;
espconn_disconnect(pespconn);//断开连接
}

void ICACHE_FLASH_ATTR
tcpserver_recv(void *arg, char *pdata, unsigned short len)//接收函数
{
char * http_flg = NULL;

unsigned short i,web_len = 0;
struct espconn *pespconn = (struct espconn *)arg;

http_flg = strstr(pdata,”HTTP”);
if(http_flg != NULL){
espconn_send(pespconn,http_head,sizeof(http_head));
}
}

void ICACHE_FLASH_ATTR
tcpserver_listen(void *arg)//服务器被链接回调
{
struct espconn *pespconn = (struct espconn *)arg;

espconn_regist_reconcb(pespconn, tcpserver_recon_cb);//开启异常断开回调
espconn_regist_disconcb(pespconn, tcpserver_discon_cb);//开启正常断开回调
espconn_regist_recvcb(pespconn, tcpserver_recv);//开启接收回调
espconn_regist_sentcb(pespconn, tcpclient_sent_cb);//开启发送成功回调
}

void ICACHE_FLASH_ATTR
tcp_server(void)//开启tcp服务器
{
tcpserver.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
tcpserver.proto.tcp->local_port = 80;//监听本地端口号

tcpserver.type = ESPCONN_TCP;
tcpserver.state = ESPCONN_NONE;

espconn_regist_connectcb(&tcpserver, tcpserver_listen);//链接成功回调
espconn_accept(&tcpserver);//开启TCP服务器
espconn_regist_time(&tcpserver, 1, 0);//设置服务器超时时间为1秒
}

// test :AT+TEST=1,”abc”<,3>
void ICACHE_FLASH_ATTR
at_setupCmdTest(uint8_t id, char *pPara)
{
int result = 0, err = 0, flag = 0;
uint8 buffer[32] = {0};
pPara++; // skip ‘=’

//get the first parameter
// digit
flag = at_get_next_int_dec(&pPara, &result, &err);

// flag must be ture because there are more parameter
if (flag == FALSE) {
at_response_error();
return;
}

if (*pPara++ != ‘,’) { // skip ‘,’
at_response_error();
return;
}

os_sprintf(buffer, “the first parameter:%d
“, result);
at_port_print(buffer);

//get the second parameter
// string
at_data_str_copy(buffer, &pPara, 10);
at_port_print(“the second parameter:”);
at_port_print(buffer);
at_port_print(”
“);

if (*pPara == ‘,’) {
pPara++; // skip ‘,’
result = 0;
//there is the third parameter
// digit
flag = at_get_next_int_dec(&pPara, &result, &err);
// we donot care of flag
os_sprintf(buffer, “the third parameter:%d
“, result);
at_port_print(buffer);
}

if (*pPara != ‘
‘) {
at_response_error();
return;
}

at_response_ok();
}

void ICACHE_FLASH_ATTR
at_testCmdTest(uint8_t id)
{
uint8 buffer[32] = {0};

os_sprintf(buffer, “%s
“, “at_testCmdTest”);
at_port_print(buffer);
at_response_ok();
}

void ICACHE_FLASH_ATTR
at_queryCmdTest(uint8_t id)
{
uint8 buffer[32] = {0};

os_sprintf(buffer, “%s
“, “at_queryCmdTest”);
at_port_print(buffer);
at_response_ok();
}

void ICACHE_FLASH_ATTR
at_exeCmdTest(uint8_t id)
{
uint8 buffer[32] = {0};

os_sprintf(buffer, “%s
“, “at_exeCmdTest”);
at_port_print(buffer);
at_response_ok();
}

extern void at_exeCmdCiupdate(uint8_t id);
at_funcationType at_custom_cmd[] = {
{“+TEST”, 5, at_testCmdTest, at_queryCmdTest, at_setupCmdTest, at_exeCmdTest},
#ifdef AT_UPGRADE_SUPPORT
{“+CIUPDATE”, 9, NULL, NULL, NULL, at_exeCmdCiupdate}
#endif
};

/******************************************************************************
* FunctionName : user_rf_cal_sector_set
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
* We add this function to force users to set rf cal sector, since
* we don’t know which sector is free in user’s application.
* sector map for last several sectors : ABBBCDDD
* A : rf cal
* B : at parameters
* C : rf init data
* D : sdk parameters
* Parameters : none
* Returns : rf cal sector
*******************************************************************************/
uint32 ICACHE_FLASH_ATTR
user_rf_cal_sector_set(void)
{
enum flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;

switch (size_map) {
case FLASH_SIZE_4M_MAP_256_256:
rf_cal_sec = 128 – 8;
break;

case FLASH_SIZE_8M_MAP_512_512:
rf_cal_sec = 256 – 5;
break;

case FLASH_SIZE_16M_MAP_512_512:
case FLASH_SIZE_16M_MAP_1024_1024:
rf_cal_sec = 512 – 5;
break;

case FLASH_SIZE_32M_MAP_512_512:
case FLASH_SIZE_32M_MAP_1024_1024:
rf_cal_sec = 1024 – 5;
break;

default:
rf_cal_sec = 0;
break;
}

return rf_cal_sec;
}

void ICACHE_FLASH_ATTR
user_rf_pre_init(void)
{
}

void ICACHE_FLASH_ATTR
sys_init_cb(void)//初始化完成后回调函数
{
os_printf(”
HTTP测试”);
tcp_server();//开启tcp服务器
}

void ICACHE_FLASH_ATTR
user_init(void)
{
uart_init(115200,115200);
wifi_set_opmode_current(STATIONAP_MODE);//设置WiFi工作模式
system_init_done_cb(sys_init_cb);//创建初始化完成后回调函数
}

/*
void ICACHE_FLASH_ATTR
user_init(void)
{
char buf[64] = {0};
at_customLinkMax = 5;
at_init();
os_sprintf(buf,”compile time:%s %s”,__DATE__,__TIME__);
at_set_custom_info(buf);
at_port_print(”
ready
“);
at_cmd_array_regist(&at_custom_cmd[0], sizeof(at_custom_cmd)/sizeof(at_custom_cmd[0]));
}
*/
注意使用IE浏览器 ,使用其他浏览器会下载一个文件,用记事本打开是同样的内容

 

 

 

 
———————
作者:疯仔嵌入式
来源:CSDN
原文:https://blog.csdn.net/yichu5074/article/details/81105580
版权声明:本文为博主原创文章,转载请附上博文链接!

转载请注明:我是IT » ESP8266–web服务器基础

喜欢 (0)or分享 (0)