ESP8266学习笔记(10)——官⽅WebServer ⼀、背景
WebServer作为HTTP服务器,接收⼿机APP请求,配置⽹关接⼊⽹络。
Post请求和Get请求:
⼆、流程
① 初始化WebServer,绑定本地端⼝
② 开启监听
③ 解析请求数据包
④ 回发响应数据包
三、初始化WebServer
⽂件所在 ESP8266_NONOS_SDK-2.1.0\IoT_Demo\user/user_webserver.c。
创建TCP服务端,参考ESP8266学习笔记(5)——TCP/UDP接⼝使⽤。
传⼊参数,本地端⼝80。
response_send(ptrespconn, false); // ------------------下述 6.1
}
os_printf("pusrdata:%s\n", pusrdata);
/* 为URL框架申请空间 */
pURL_Frame = (URL_Frame *)os_zalloc(sizeof(URL_Frame)); // ------------------下述 5.3
/* 解析数据,加⼊URL框架 */
parse_url(precvbuffer, pURL_Frame); // ------------------下述 5.4
os_printf("Type:%d, Select:%s, Command:%s, Filename:%s ",
pURL_Frame->Type,pURL_Frame->pSelect,pURL_Frame->pCommand,pURL_Frame->pFilename); switch (pURL_Frame->Type)
{
case GET:
os_printf("We have a GET request.\n");
if (os_strcmp(pURL_Frame->pSelect, "client") == 0 &&
os_strcmp(pURL_Frame->pCommand, "command") == 0)
{
if (os_strcmp(pURL_Frame->pFilename, "info") == 0)
{
json_send(ptrespconn, INFOMATION);
}
if (os_strcmp(pURL_Frame->pFilename, "status") == 0)
{
json_send(ptrespconn, CONNECT_STATUS);
}
else if (os_strcmp(pURL_Frame->pFilename, "scan") == 0)
{
char *strstr = NULL;
strstr = (char *)os_strstr(pusrdata, "&");
if (strstr == NULL)
{
if (pscaninfo == NULL)
{
pscaninfo = (scaninfo *)os_zalloc(sizeof(scaninfo));
}
pscaninfo->pespconn = ptrespconn;
pscaninfo->pagenum = 0;
pscaninfo->page_sn = 0;
pscaninfo->data_cnt = 0;
wifi_station_scan(NULL, json_scan_cb);
} else {
strstr ++;
if (os_strncmp(strstr, "page", 4) == 0) {
if (pscaninfo != NULL) {
pscaninfo->pagenum = *(strstr + 5);
pscaninfo->pagenum -= 0x30;
if (pscaninfo->pagenum > pscaninfo->totalpage || pscaninfo->pagenum == 0) {
response_send(ptrespconn, false);
} else {
json_send(ptrespconn, SCAN);
}
} else {
response_send(ptrespconn, false);
}
} else if(os_strncmp(strstr, "finish", 6) == 0){
bss_temp = bss_head;
while(bss_temp != NULL) {
bss_head = bss_temp->next.stqe_next;
os_free(bss_temp);
bss_temp = bss_head;
}
bss_head = NULL;
bss_temp = NULL;
response_send(ptrespconn, true);
} else {
response_send(ptrespconn, false);
}
}
}
} else {
response_send(ptrespconn, false);
}
} else if (os_strcmp(pURL_Frame->pSelect, "config") == 0 &&
os_strcmp(pURL_Frame->pCommand, "command") == 0) {
if (os_strcmp(pURL_Frame->pFilename, "wifi") == 0) {
ap_conf = (struct softap_config *)os_zalloc(sizeof(struct softap_config));
sta_conf = (struct station_config *)os_zalloc(sizeof(struct station_config));
json_send(ptrespconn, WIFI);
os_free(sta_conf);
os_free(ap_conf);
sta_conf = NULL;
ap_conf = NULL;
}
#if PLUG_DEVICE
else if (os_strcmp(pURL_Frame->pFilename, "switch") == 0) {
json_send(ptrespconn, SWITCH_STATUS);
}
#endif
else if (os_strcmp(pURL_Frame->pFilename, "reboot") == 0) {
json_send(ptrespconn, REBOOT);
} else {
response_send(ptrespconn, false);
}
} else if (os_strcmp(pURL_Frame->pSelect, "upgrade") == 0 &&
os_strcmp(pURL_Frame->pCommand, "command") == 0) {
if (os_strcmp(pURL_Frame->pFilename, "getuser") == 0) {
json_send(ptrespconn , USER_BIN);
}
} else {
response_send(ptrespconn, false);
}
break;
case POST:
os_printf("We have a POST request.\n");
pParseBuffer = (char *)os_strstr(precvbuffer, "\r\n\r\n");
if (pParseBuffer == NULL) {
break;
}
pParseBuffer += 4;
if (os_strcmp(pURL_Frame->pSelect, "config") == 0 &&
os_strcmp(pURL_Frame->pCommand, "command") == 0) {
if (os_strcmp(pURL_Frame->pFilename, "reboot") == 0) {
if (pParseBuffer != NULL) {
if (restart_10ms != NULL) {
os_timer_disarm(restart_10ms);
}
if (rstparm == NULL) {
rstparm = (rst_parm *)os_zalloc(sizeof(rst_parm));
}
rstparm->pespconn = ptrespconn;
rstparm->parmtype = REBOOT;
if (restart_10ms == NULL) {
restart_10ms = (os_timer_t *)os_malloc(sizeof(os_timer_t));
}
os_timer_setfn(restart_10ms, (os_timer_func_t *)restart_10ms_cb, NULL); os_timer_arm(restart_10ms, 10, 0); // delay 10ms, then do
response_send(ptrespconn, true);
} else {
} else {
response_send(ptrespconn, false);
}
} else if (os_strcmp(pURL_Frame->pFilename, "wifi") == 0) {
if (pParseBuffer != NULL) {
struct jsontree_context js;
user_esp_platform_set_connect_status(DEVICE_CONNECTING);
if (restart_10ms != NULL) {
os_timer_disarm(restart_10ms);
}
if (ap_conf == NULL) {
ap_conf = (struct softap_config *)os_zalloc(sizeof(struct softap_config));
}
if (sta_conf == NULL) {
sta_conf = (struct station_config *)os_zalloc(sizeof(struct station_config)); }
jsontree_setup(&js, (struct jsontree_value *)&wifi_req_tree, json_putchar);
json_parse(&js, pParseBuffer);
if (rstparm == NULL) {
rstparm = (rst_parm *)os_zalloc(sizeof(rst_parm));
}
rstparm->pespconn = ptrespconn;
rstparm->parmtype = WIFI;
if (sta_conf->ssid[0] != 0x00 || ap_conf->ssid[0] != 0x00) {
ap_conf->ssid_hidden = 0;
ap_conf->max_connection = 4;
if (restart_10ms == NULL) {
restart_10ms = (os_timer_t *)os_malloc(sizeof(os_timer_t));
}
os_timer_disarm(restart_10ms);
webserver接口开发os_timer_setfn(restart_10ms, (os_timer_func_t *)restart_10ms_cb, NULL); os_timer_arm(restart_10ms, 10, 0); // delay 10ms, then do
} else {
os_free(ap_conf);
os_free(sta_conf);
os_free(rstparm);
sta_conf = NULL;
ap_conf = NULL;
rstparm =NULL;
}
response_send(ptrespconn, true);
} else {
response_send(ptrespconn, false);
}
}
else if (os_strcmp(pURL_Frame->pFilename, "switch") == 0) {
if (pParseBuffer != NULL) {
struct jsontree_context js;
jsontree_setup(&js, (struct jsontree_value *)&StatusTree, json_putchar);
json_parse(&js, pParseBuffer);
// ----------Altered by Leung 2018.11.12-------START
if (user_plug_get_status() == 1) // 获取继电器状态
{
data_send(ptrespconn, true, "{\"status\": 1}"); // 返回APP继电器状态——打开 }
else
{
data_send(ptrespconn, true, "{\"status\": 0}"); // 返回APP继电器状态——关闭 }
// ----------Altered by Leung 2018.11.12-------END
} else {
response_send(ptrespconn, false);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论