--
WebServer
使用 ESP8266WebServer.h 而不是自己寫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> IPAddress ip(192,168,1,81); // choose IP address IPAddress subnet(255,255,255,0); ESP8266WebServer server(80); void handleRoot() { String page = "<!DOCTYPE html>\n"; page += "<html>\n<body>\n<h1>Some heading</h1><br>Generated by ESP8266\n</body>\n</html>"; server.send(200, "text/html", page); } // what to do when accessed through http://ip_address/something_undefined void handleNotFound(){ String message = "File not found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i<server.args(); i++){ message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); } void setup(void){ Serial.begin(74880); // so you can see debug messages automatically sent by ESP8266 WiFi.mode(WIFI_AP); WiFi.softAPConfig(ip, ip, subnet); // declared as: bool softAPConfig (IPAddress local_ip, IPAddress gateway, IPAddress subnet) WiFi.softAP("SOME_NAME", "password", 7); // network name, network password, wifi channel IPAddress myIP = WiFi.softAPIP(); Serial.println(); Serial.print("AP IP address: "); Serial.println(myIP); server.on("/", handleRoot); // what to do when accessed through browser using http://IP_address // what to do when accessed through http://ip_address/test server.on("/test", [](){ server.send(200, "text/plain", "This is another page"); }); server.on("/sa/1", [](){ // ... <-- put here the code for NeoPixel = on server.send(200, "text/html", "NeoPixel is now on"); }); server.on("/sa/0", [](){ // ... <-- put here the code for NeoPixel = off server.send(200, "text/html", "NeoPixel is now off"); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started..."); } void loop(void){ server.handleClient(); } |
--
URL 的 $_GET()
一般來說使用 handleSpecificArg() 是比較常用的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
void handleGenericArgs() { String message = ""; message += server.arg(0) + "\n"; String rs_message = getValue(message, ','); message += rs_message; server.send(200, "text / plain", message); //Response to the HTTP request } // void handleSpecificArg() { String message = ""; if (server.arg("Temperature") == "") { //Parameter not found message = "Temperature Argument not found"; } else { //Parameter found message = "Temperature Argument = "; message += server.arg("Temperature"); //Gets the value of the query parameter } server.send(200, "text / plain", message); //Returns the HTTP response } server.on("/rs", handleGenericArgs); |
--
應用
有了 WiFi AP 也有了取得網址變數的方法,接下來就可以設計應用場景網址的變數使用。例如 http://192.168.2.1/url?led=on&light=1 ,可以表示開啟 led 以及亮 1 秒
--
2,034 total views, 3 views today