最終更新: llysfactoryll 2020年09月22日(火) 15:15:31履歴
(ESP32のWiFi機能で別のファイルシステムの状態を監視する-2)
ESP32側の構成は概ねつぎのとおりである。
SimpleWiFiServerをベースにしている。
FWServerはクラス化してみた。HttpServerもそうすればいいのだが・・
FileWatcher
#include <dummy.h> #include <WiFi.h> #include "FWServer.h" const char* ssid = "内緒"; const char* password = "内緒"; WiFiServer server_80(80); FWServer fw_server(49999); void setup() { Serial.begin(115200); delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server_80.begin(); fw_server.setup(); } void loop(){ loop_Http(server_80); fw_server.loop(); }
HttpServer
void loop_Http(WiFiServer& server){ WiFiClient client = server.available(); // listen for incoming clients if (client) { // if you get a client, Serial.println("HTTP: New Client."); // print a message out the serial port String requestLines = ""; // make a String to hold incoming data from the client String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { HttpOnRequest(client, requestLines); // break out of the while loop: break; } else { // if you got a newline, then clear currentLine: requestLines += currentLine; requestLines += '\n'; currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // close the connection: client.stop(); Serial.println("Client Disconnected."); } } void HttpOnRequest(WiFiClient& client, String request) { if(request.startsWith("GET")) { HttpOnGet(client, request); } } void HttpOnGet(WiFiClient& client, String request) { String param = request.substring(3); param.trim(); if(param.startsWith("/ ")) { //Serial.println("On Get /"); // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); // the content of the HTTP response follows the header: HtmlHome(client); // The HTTP response ends with another blank line: client.println(); } else { client.println("HTTP/1.1 404 Page Not Found"); client.println(); } }
HtmlPages
void HtmlHome(WiFiClient client) { const char* top = "<!doctype html>" "<html>" "<head>" "<title>File Watcher</title>" "<meta charset=\"UTF-8\">" "<meta name=\"author\" content=\"2020.09.20 - 2020.09.20 ysfactory\">" "<meta http-equiv=\"refresh\" content=\"5\" >" "</head>" "<body>" "<div id=\"header\">" "<h1>File Watcher</h1>" "</div>" "<hr>" "<div id=\"list\">"; const char* bottom = "</div>" "<hr>" "<div id=\"footer\">" "わいずいふぁくとりい" "</div>" "</body>" "</html>"; client.println(top); HtmlList(client); client.println(bottom); } void HtmlList(WiFiClient client) { fw_server.List(); for(int i=0; i<fw_server.m_response.NofToken; ++i) { char line[128]; sprintf(line, "<div>%s</div>", fw_server.m_response.Token[i]); client.println(line); } }
コメントをかく