Esp8266 Web Server 应用实例 - Simple Web Console

2021-01-11 07:31

阅读:704

YPE html>

标签:mic   读者   ext   nec   sof   bug   doc   mail   核心   

Esp8266 Web Server 应用实例 - Simple Web Console
本文旨在向读者介绍使用Esp8266 WiFi板搭建Web Server应用的原理。示例向读者展示了如何创建Web Console,以及如何使Web Console与NodeMcu进行交互。

硬件准备:

NodeMcu

开发环境:

Arduino IDE(1.8.4),IE11

技术图片

结果预览:

IE11浏览器:
技术图片
微信浏览器:
技术图片
NodeMcu串口输出信息:
技术图片
演示:
技术图片

核心代码:

wifi_test.ino 代码:

#include "GetCmd.h"
#include 

#define DEBUG

#ifdef DEBUG
#define DBG_PRINT(x)    Serial.print(x)
#define DBG_PRINTLN(x)  Serial.println(x)
#else
#define DBG_PRINT(x)
#define DBG_PRINTLN(x)
#endif

#define REQ_HEAD "GET /_cmd_"
#define REQ_TAIL " HTTP"
#define WEB_CONSOLE_BUFFER_SIZE 10240

#ifndef STASSID
#define STASSID "ASUS_24G6"
#define STAPSK  "12345678"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;
String web_console;

const String pagePart1 = String("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n") +
""+
""+
""+
"    "+
"    "+
"    "+
"    "+
"    "+
"    CONSOLE"+
""+
""+
"
"+ " "+ " "+ " "+ " "+ " "+ " "+ " "+ "
Cmdline:
"+ "
"+ "")+ "

Create By FIRSWOF

"+ ""+ ""+ ""; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); //Web Console Output void WebPrintln() { web_console += ‘\n‘; } template void WebPrint(T msg) { web_console += msg; } template void WebPrintln(T msg) { web_console += msg; web_console += ‘\n‘; } void HtmlHexValueToChar(String &src) { String tmp; for(int i=0;i=‘0‘ && src[i+1] =‘A‘ && src[i+1] =‘a‘ && src[i+1] =‘0‘ && src[i+2] =‘A‘ && src[i+2] =‘a‘ && src[i+2] = 1) { if (strcmp(param[0], "show")==0) { if (parCnt == 1 || ( parCnt > 1 && strcmp(param[1], "all")==0)) { WebPrint("SSID:\t"); WebPrintln(STASSID); WebPrint("PWD:\t"); WebPrintln(STAPSK); bSucc = true; } } } if (!bSucc) WebPrintln("Usage: network [show [all]]"); } void CMD_Ver(char** param, uint8_t parCnt) { WebPrintln("Ver:20200812.01"); WebPrintln("Create By FIRSWOF"); } void setup() { Serial.begin(115200); Serial.println("system running..."); // prepare LED pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, 0); //Connect to WiFi network Serial.println(); Serial.println(); Serial.print(F("Connecting to ")); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } Serial.println(); Serial.println(F("WiFi connected")); // Start the server server.begin(); Serial.println(F("Server started")); // Print the IP address Serial.println(WiFi.localIP()); getCmd.AddCommand("ls", SYS_GetCmdList); getCmd.AddCommand("clear", CMD_Clear); getCmd.AddCommand("echo", CMD_Echo); getCmd.AddCommand("millis", CMD_GetMillis); getCmd.AddCommand("network", CMD_Network); getCmd.AddCommand("ver", CMD_Ver); WebPrint("\n#NodeMcu>"); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) return; client.setTimeout(3000); // default is 1000 //Wait until the client sends some data //~ DBG_PRINTLN("new client"); unsigned long timeout = millis() + 3000; do { delay(1); } while (!client.available() && millis() timeout) { Serial.println("timeout"); return; } if (web_console.length() > WEB_CONSOLE_BUFFER_SIZE) web_console = ""; // Read the first line of the request String req = client.readStringUntil(‘\r‘); DBG_PRINT(F("raw request:")); DBG_PRINTLN(req); // Match the request int from, end; from = req.indexOf(F(REQ_HEAD)); end = req.indexOf(F(REQ_TAIL)); if(req.length() == 0) { client.flush(); client.stop(); return; } else if(req.startsWith("GET / HTTP/1.1")) { DBG_PRINTLN("answer ‘GET / HTTP/1.1‘"); client.flush(); client.print(pagePart1 + web_console + pagePart2); //~ DBG_PRINTLN(web_console); } else if(req.startsWith("GET /favicon.ico HTTP/1.1")) { client.flush(); client.print("HTTP/1.1 200 OK\r\n"); from = -1; } if (from != -1 && end != -1 && end > from + strlen(REQ_HEAD)) { req = req.substring(from + strlen(REQ_HEAD), end); HtmlHexValueToChar(req); Serial.print(F("request:")); Serial.println(req); WebPrintln(req); if(!getCmd.CmdParser(req.c_str())) { WebPrint("invalid cmd"); } WebPrint("\n#NodeMcu>"); client.flush(); client.print(web_console); //~ DBG_PRINTLN(web_console); } }

Esp8266 Web Server 应用实例 - Simple Web Console

标签:mic   读者   ext   nec   sof   bug   doc   mail   核心   

原文地址:https://blog.51cto.com/firswof/2519476

上一篇:css 优先级

下一篇:API 跨域


评论


亲,登录后才可以留言!