add
This commit is contained in:
758
Arduino/ESP_Switch/ESP_Switch.ino
Normal file
758
Arduino/ESP_Switch/ESP_Switch.ino
Normal file
@@ -0,0 +1,758 @@
|
||||
/*
|
||||
To upload through terminal you can use: curl -u admin:admin -F "image=@firmware.bin" esp8266-webupdate.local/firmware
|
||||
*/
|
||||
|
||||
//链接LED,没有链接亮
|
||||
#define LEDLINK 4
|
||||
//运行led,正常亮
|
||||
#define LEDRUN 0
|
||||
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
#include <ESP8266HTTPUpdateServer.h>
|
||||
#include <FS.h>
|
||||
#include <LittleFS.h>
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "Jiang-2.4G"
|
||||
#define STAPSK "1446935823"
|
||||
#endif
|
||||
|
||||
//login
|
||||
String login_;
|
||||
static const char AUTHORIZATION_HEADER[] PROGMEM = "Authorization";
|
||||
|
||||
|
||||
#define DBG_OUTPUT_PORT Serial1
|
||||
const char* fsName = "LittleFS";
|
||||
static bool fsOK;
|
||||
FS* fileSystem = &LittleFS;
|
||||
String unsupportedFiles = String();
|
||||
static const char TEXT_PLAIN[] PROGMEM = "text/plain";
|
||||
static const char FS_INIT_ERROR[] PROGMEM = "FS INIT ERROR";
|
||||
static const char FILE_NOT_FOUND[] PROGMEM = "FileNotFound";
|
||||
File uploadFile;
|
||||
#define USE_LITTLEFS
|
||||
//const char* fsName = "LittleFS";
|
||||
//FS* fileSystem = &LittleFS;
|
||||
LittleFSConfig fileSystemConfig = LittleFSConfig();
|
||||
|
||||
const char* host = "esp8266-webupdate";
|
||||
const char* update_path = "/firmware";
|
||||
const char* update_username = "admin";
|
||||
const char* update_password = "admin";
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
ESP8266WebServer httpServer(80);
|
||||
ESP8266HTTPUpdateServer httpUpdater;
|
||||
|
||||
String bin_Sersion = "V1.0";
|
||||
//名称
|
||||
String name;
|
||||
// 存放 MAC 位址的阵列
|
||||
uint8_t mac[6];
|
||||
//存放开关状态
|
||||
char J;
|
||||
|
||||
/*
|
||||
1.初始化led
|
||||
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
char mac_ap[18];
|
||||
bool ret;
|
||||
pinMode(LEDLINK, OUTPUT);
|
||||
pinMode(LEDRUN, OUTPUT);
|
||||
digitalWrite(LEDLINK, HIGH);
|
||||
digitalWrite(LEDRUN, LOW);
|
||||
|
||||
Serial1.begin(115200);
|
||||
Serial1.println();
|
||||
Serial1.println("Booting Sketch...");
|
||||
|
||||
strcpy(mac_ap, WiFi.softAPmacAddress().c_str());
|
||||
Serial1.println();
|
||||
|
||||
Serial1.print("FS_inti... ");
|
||||
//LittleFS.format();
|
||||
if (!LittleFS.begin()) {
|
||||
Serial1.println("[ERROR]");
|
||||
return;
|
||||
}else{
|
||||
Serial1.println("[ OK ]");
|
||||
}
|
||||
|
||||
|
||||
Serial1.print("WiFi_init ");
|
||||
//Serial1.println(mac_ap);
|
||||
|
||||
name = "ESP8266IO_To_COM_" + String(char(mac_ap[9])) + String(char(mac_ap[10])) + String(char(mac_ap[12])) + String(char(mac_ap[13])) + String(char(mac_ap[15])) + String(char(mac_ap[16]));
|
||||
|
||||
IPAddress softLocal(192, 168, 4, 1);
|
||||
IPAddress softGateway(192, 168, 4, 1);
|
||||
IPAddress softSubnet(255, 255, 255, 0);
|
||||
|
||||
WiFi.softAP(name);
|
||||
WiFi.softAPConfig(softLocal, softGateway, softSubnet);
|
||||
WiFi.hostname(name);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
||||
WiFi.begin(ssid, password);
|
||||
Serial1.println("WiFi failed, retrying.");
|
||||
}
|
||||
Serial1.println("[ OK ]");
|
||||
fils_init();
|
||||
//Serial1.printf("Flash Actual size: %u KBytes\n", ESP.getFlashChipRealSize() / 1024);
|
||||
// temp=ESP.getFlashChipRealSize() / 1024;
|
||||
char buf[12];
|
||||
sprintf(buf,"%u KBytes",ESP.getFlashChipRealSize() / 1024);
|
||||
char getFlash[12];
|
||||
sprintf(getFlash,"%u KBytes",ESP.getFlashChipSize() / 1024);
|
||||
char Speed[12];
|
||||
sprintf(Speed,"%d MHz",ESP.getFlashChipSpeed() / 1000000);
|
||||
FlashMode_t ideMode = ESP.getFlashChipMode();
|
||||
String IDEMode;
|
||||
//Serial1.println(Speed);
|
||||
if(ideMode == FM_QIO){IDEMode="QIO";}
|
||||
else if(ideMode == FM_QOUT){IDEMode="QOUT";}
|
||||
else if(ideMode == FM_DIO){IDEMode="DIO";}
|
||||
else if(ideMode == FM_DOUT){IDEMode="DOUT";}
|
||||
Serial1.print("FSData_init");
|
||||
if(!FSData_init("/etc/hostname",name)) {Serial1.println("[ERROR]");}
|
||||
else if(!FSData_init("/etc/sysname","ESP8266 Switch 4Pin")) {Serial1.println("[ERROR]");}
|
||||
else if(!FSData_init("/etc/bin_Sersion",bin_Sersion)) {Serial1.println("[ERROR]");}
|
||||
else if(!FSData_init("/etc/CoreVersion",ESP.getCoreVersion())) {Serial1.println("[ERROR]");}
|
||||
else if(!FSData_init("/etc/Flash_ID",String(ESP.getFlashChipId()))) {Serial1.println("[ERROR]");}
|
||||
else if(!FSData_init("/etc/Flash_size",buf)) {Serial1.println("[ERROR]");}
|
||||
else if(!FSData_init("/etc/getFlash",getFlash)) {Serial1.println("[ERROR]");}
|
||||
else if(!FSData_init("/etc/Speed",Speed)) {Serial1.println("[ERROR]");}
|
||||
else if(!FSData_init("/etc/IDEMode",IDEMode)) {Serial1.println("[ERROR]");}
|
||||
else{Serial1.println("[ OK ]");}
|
||||
if(!LittleFS.exists("/login/user/root")){
|
||||
File file = LittleFS.open("/login/user/root", "w");
|
||||
if (!file) {
|
||||
Serial1.println("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
if (file.print("root")) {
|
||||
//Serial1.println("File written");
|
||||
} else {
|
||||
Serial1.println("Write failed");
|
||||
}
|
||||
delay(2000); // Make sure the CREATE and LASTWRITE times are different
|
||||
file.close();
|
||||
}
|
||||
|
||||
Serial1.print("WebSwrver_init... ");
|
||||
httpServer.on("/login/login",HTTP_GET,login);
|
||||
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
|
||||
httpServer.onNotFound(http_server);
|
||||
httpServer.begin();
|
||||
Serial1.println("[ OK ]");
|
||||
|
||||
//MDNS.addService("http", "tcp", 80);
|
||||
//Serial1.printf("HTTPUpdateServer ready! Open http://%s.local%s in your browser and login with username '%s' and password '%s'\n", host, update_path, update_username, update_password);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
httpServer.handleClient();
|
||||
|
||||
}
|
||||
|
||||
bool FSData_init(String path,String data)
|
||||
{
|
||||
bool ret = false;
|
||||
Serial1.print(".");
|
||||
if(LittleFS.exists(path)){
|
||||
File file = LittleFS.open(path, "r");
|
||||
if(data != file.readString())
|
||||
{
|
||||
file.close();
|
||||
LittleFS.remove(path);
|
||||
}else
|
||||
file.close();
|
||||
ret = true;
|
||||
}
|
||||
if(!LittleFS.exists(path)){
|
||||
File file = LittleFS.open(path, "w");
|
||||
if (!file) {
|
||||
Serial1.println("Failed to open file for writing");
|
||||
return false;
|
||||
}
|
||||
if (file.print(data)) {
|
||||
//Serial1.println("File written");
|
||||
ret = true;
|
||||
} else {
|
||||
Serial1.println("Write failed");
|
||||
}
|
||||
delay(2000); // Make sure the CREATE and LASTWRITE times are different
|
||||
file.close();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void login()
|
||||
{
|
||||
String message;
|
||||
String ret = "error";
|
||||
if(httpServer.args() == 2)
|
||||
{
|
||||
if(httpServer.argName(0) == "user"){
|
||||
if(httpServer.argName(1) == "password"){
|
||||
if(LittleFS.exists("/login/user/" + httpServer.arg(0))){
|
||||
File file = LittleFS.open("/login/user/" + httpServer.arg(0),"r");
|
||||
while (file.available()) {
|
||||
message += file.readString();
|
||||
}
|
||||
file.close();
|
||||
if(httpServer.arg(1) == message)
|
||||
{
|
||||
ret = "ok";
|
||||
login_ = httpServer.header(FPSTR(AUTHORIZATION_HEADER));
|
||||
//Serial1.println("login_");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
httpServer.send(200, u8"text/plain", ret);
|
||||
|
||||
//Serial1.println(httpServer.args());
|
||||
for (uint8_t i = 0; i < httpServer.args(); i++) {
|
||||
message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
|
||||
}
|
||||
//Serial1.println(message);
|
||||
}
|
||||
void http_server()
|
||||
{
|
||||
String message;
|
||||
String path;
|
||||
String run_html;
|
||||
|
||||
path = "/www/" + httpServer.uri();
|
||||
path = httpServer.uri();
|
||||
|
||||
Serial1.println(path);
|
||||
if(httpServer.hasHeader(FPSTR(AUTHORIZATION_HEADER))){
|
||||
String authReq = httpServer.header(FPSTR(AUTHORIZATION_HEADER));
|
||||
// Serial1.println(authReq);
|
||||
|
||||
if(authReq != login_){
|
||||
if(path == "/login/login.css"){path = "/login/login.css";}
|
||||
else if(path == "/etc/hostname"){path = "/etc/hostname";}
|
||||
else{path = "/login/";}
|
||||
}
|
||||
}
|
||||
|
||||
if (path.endsWith("/")) path += "index.html";
|
||||
if (!LittleFS.exists(path)) {if (LittleFS.exists(path += ".run")) path += ".run";}
|
||||
|
||||
if (LittleFS.exists(path)) {
|
||||
|
||||
File file = LittleFS.open(path,"r");
|
||||
|
||||
message = getContentType(path);
|
||||
// bool _run = true;
|
||||
// if (message == "run")
|
||||
// {
|
||||
// while ((file.available())||(_run)) {
|
||||
/* message = file.read();
|
||||
if(message.endsWith("\r\n")){
|
||||
switch (message){
|
||||
case "\r\n":
|
||||
break;
|
||||
case "analogWrite\r\n":
|
||||
break;
|
||||
default:
|
||||
_run = false;
|
||||
run_html = "Run Error:" + message " file:" + path;
|
||||
httpServer.send(404, "text/plain", run_html);
|
||||
}
|
||||
}*/
|
||||
// }
|
||||
// }else{
|
||||
size_t sent = httpServer.streamFile(file,message);
|
||||
// }
|
||||
file.close();
|
||||
}else{
|
||||
message = "404" + path;
|
||||
|
||||
httpServer.send(404, "text/plain", message);
|
||||
}
|
||||
//handleNotFound();
|
||||
}
|
||||
|
||||
String getContentType(String file)
|
||||
{
|
||||
if (file.endsWith(".html")) return "text/html";
|
||||
else if (file.endsWith(".htm")) return "text/html";
|
||||
else if (file.endsWith(".css")) return "text/css";
|
||||
else if (file.endsWith(".html.run")) return "run";
|
||||
return "text/plain";
|
||||
}
|
||||
|
||||
void fils_init()
|
||||
{
|
||||
|
||||
////////////////////////////////
|
||||
// WEB SERVER INIT
|
||||
|
||||
// Filesystem status
|
||||
httpServer.on("/status", HTTP_GET, handleStatus);
|
||||
|
||||
// List directory
|
||||
httpServer.on("/list", HTTP_GET, handleFileList);
|
||||
|
||||
// Load editor
|
||||
httpServer.on("/edit", HTTP_GET, handleGetEdit);
|
||||
|
||||
// Create file
|
||||
httpServer.on("/edit", HTTP_PUT, handleFileCreate);
|
||||
|
||||
// Delete file
|
||||
httpServer.on("/edit", HTTP_DELETE, handleFileDelete);
|
||||
|
||||
// Upload file
|
||||
// - first callback is called after the request has ended with all parsed arguments
|
||||
// - second callback handles file upload at that location
|
||||
httpServer.on("/edit", HTTP_POST, replyOK, handleFileUpload);
|
||||
|
||||
fsOK = fileSystem->begin();
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
The "Not Found" handler catches all URI not explicitely declared in code
|
||||
First try to find and return the requested file from the filesystem,
|
||||
and if it fails, return a 404 page with debug information
|
||||
*/
|
||||
void handleNotFound() {
|
||||
if (!fsOK) {
|
||||
return replyServerError(FPSTR(FS_INIT_ERROR));
|
||||
}
|
||||
|
||||
String uri = ESP8266WebServer::urlDecode(httpServer.uri()); // required to read paths with blanks
|
||||
|
||||
if (handleFileRead(uri)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Dump debug data
|
||||
String message;
|
||||
message.reserve(100);
|
||||
message = F("Error: File not found\n\nURI: ");
|
||||
message += uri;
|
||||
message += F("\nMethod: ");
|
||||
message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
|
||||
message += F("\nArguments: ");
|
||||
message += httpServer.args();
|
||||
message += '\n';
|
||||
for (uint8_t i = 0; i < httpServer.args(); i++) {
|
||||
message += F(" NAME:");
|
||||
message += httpServer.argName(i);
|
||||
message += F("\n VALUE:");
|
||||
message += httpServer.arg(i);
|
||||
message += '\n';
|
||||
}
|
||||
message += "path=";
|
||||
message += httpServer.arg("path");
|
||||
message += '\n';
|
||||
DBG_OUTPUT_PORT.print(message);
|
||||
|
||||
return replyNotFound(message);
|
||||
}
|
||||
|
||||
/*
|
||||
Return the FS type, status and size info
|
||||
*/
|
||||
void handleStatus() {
|
||||
DBG_OUTPUT_PORT.println("handleStatus");
|
||||
FSInfo fs_info;
|
||||
String json;
|
||||
json.reserve(128);
|
||||
|
||||
json = "{\"type\":\"";
|
||||
json += fsName;
|
||||
json += "\", \"isOk\":";
|
||||
if (fsOK) {
|
||||
fileSystem->info(fs_info);
|
||||
json += F("\"true\", \"totalBytes\":\"");
|
||||
json += fs_info.totalBytes;
|
||||
json += F("\", \"usedBytes\":\"");
|
||||
json += fs_info.usedBytes;
|
||||
json += "\"";
|
||||
} else {
|
||||
json += "\"false\"";
|
||||
}
|
||||
json += F(",\"unsupportedFiles\":\"");
|
||||
json += unsupportedFiles;
|
||||
json += "\"}";
|
||||
|
||||
httpServer.send(200, "application/json", json);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////
|
||||
// Utils to return HTTP codes, and determine content-type
|
||||
|
||||
void replyOK() {
|
||||
httpServer.send(200, FPSTR(TEXT_PLAIN), "");
|
||||
}
|
||||
|
||||
void replyOKWithMsg(String msg) {
|
||||
httpServer.send(200, FPSTR(TEXT_PLAIN), msg);
|
||||
}
|
||||
|
||||
void replyNotFound(String msg) {
|
||||
httpServer.send(404, FPSTR(TEXT_PLAIN), msg);
|
||||
}
|
||||
|
||||
void replyBadRequest(String msg) {
|
||||
DBG_OUTPUT_PORT.println(msg);
|
||||
httpServer.send(400, FPSTR(TEXT_PLAIN), msg + "\r\n");
|
||||
}
|
||||
|
||||
void replyServerError(String msg) {
|
||||
DBG_OUTPUT_PORT.println(msg);
|
||||
httpServer.send(500, FPSTR(TEXT_PLAIN), msg + "\r\n");
|
||||
}
|
||||
|
||||
/*
|
||||
Return the list of files in the directory specified by the "dir" query string parameter.
|
||||
Also demonstrates the use of chuncked responses.
|
||||
*/
|
||||
void handleFileList() {
|
||||
if (!fsOK) {
|
||||
return replyServerError(FPSTR(FS_INIT_ERROR));
|
||||
}
|
||||
|
||||
if (!httpServer.hasArg("dir")) {
|
||||
return replyBadRequest(F("DIR ARG MISSING"));
|
||||
}
|
||||
|
||||
String path = httpServer.arg("dir");
|
||||
if (path != "/" && !fileSystem->exists(path)) {
|
||||
return replyBadRequest("BAD PATH");
|
||||
}
|
||||
|
||||
DBG_OUTPUT_PORT.println(String("handleFileList: ") + path);
|
||||
Dir dir = fileSystem->openDir(path);
|
||||
path.clear();
|
||||
|
||||
// use HTTP/1.1 Chunked response to avoid building a huge temporary string
|
||||
if (!httpServer.chunkedResponseModeStart(200, "text/json")) {
|
||||
httpServer.send(505, F("text/html"), F("HTTP1.1 required"));
|
||||
return;
|
||||
}
|
||||
|
||||
// use the same string for every line
|
||||
String output;
|
||||
output.reserve(64);
|
||||
while (dir.next()) {
|
||||
#ifdef USE_SPIFFS
|
||||
String error = checkForUnsupportedPath(dir.fileName());
|
||||
if (error.length() > 0) {
|
||||
DBG_OUTPUT_PORT.println(String("Ignoring ") + error + dir.fileName());
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
if (output.length()) {
|
||||
// send string from previous iteration
|
||||
// as an HTTP chunk
|
||||
httpServer.sendContent(output);
|
||||
output = ',';
|
||||
} else {
|
||||
output = '[';
|
||||
}
|
||||
|
||||
output += "{\"type\":\"";
|
||||
if (dir.isDirectory()) {
|
||||
output += "dir";
|
||||
} else {
|
||||
output += F("file\",\"size\":\"");
|
||||
output += dir.fileSize();
|
||||
}
|
||||
|
||||
output += F("\",\"name\":\"");
|
||||
// Always return names without leading "/"
|
||||
if (dir.fileName()[0] == '/') {
|
||||
output += &(dir.fileName()[1]);
|
||||
} else {
|
||||
output += dir.fileName();
|
||||
}
|
||||
|
||||
output += "\"}";
|
||||
}
|
||||
|
||||
// send last string
|
||||
output += "]";
|
||||
httpServer.sendContent(output);
|
||||
httpServer.chunkedResponseFinalize();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Read the given file from the filesystem and stream it back to the client
|
||||
*/
|
||||
bool handleFileRead(String path) {
|
||||
DBG_OUTPUT_PORT.println(String("handleFileRead: ") + path);
|
||||
if (!fsOK) {
|
||||
replyServerError(FPSTR(FS_INIT_ERROR));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (path.endsWith("/")) {
|
||||
path += "index.htm";
|
||||
}
|
||||
|
||||
String contentType;
|
||||
if (httpServer.hasArg("download")) {
|
||||
contentType = F("application/octet-stream");
|
||||
} else {
|
||||
contentType = mime::getContentType(path);
|
||||
}
|
||||
|
||||
if (!fileSystem->exists(path)) {
|
||||
// File not found, try gzip version
|
||||
path = path + ".gz";
|
||||
}
|
||||
if (fileSystem->exists(path)) {
|
||||
File file = fileSystem->open(path, "r");
|
||||
if (httpServer.streamFile(file, contentType) != file.size()) {
|
||||
DBG_OUTPUT_PORT.println("Sent less data than expected!");
|
||||
}
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
This specific handler returns the index.htm (or a gzipped version) from the /edit folder.
|
||||
If the file is not present but the flag INCLUDE_FALLBACK_INDEX_HTM has been set, falls back to the version
|
||||
embedded in the program code.
|
||||
Otherwise, fails with a 404 page with debug information
|
||||
*/
|
||||
void handleGetEdit() {
|
||||
if (handleFileRead(F("/edit/index.htm"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef INCLUDE_FALLBACK_INDEX_HTM
|
||||
httpServer.sendHeader(F("Content-Encoding"), "gzip");
|
||||
httpServer.send(200, "text/html", index_htm_gz, index_htm_gz_len);
|
||||
#else
|
||||
replyNotFound(FPSTR(FILE_NOT_FOUND));
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
As some FS (e.g. LittleFS) delete the parent folder when the last child has been removed,
|
||||
return the path of the closest parent still existing
|
||||
*/
|
||||
String lastExistingParent(String path) {
|
||||
while (!path.isEmpty() && !fileSystem->exists(path)) {
|
||||
if (path.lastIndexOf('/') > 0) {
|
||||
path = path.substring(0, path.lastIndexOf('/'));
|
||||
} else {
|
||||
path = String(); // No slash => the top folder does not exist
|
||||
}
|
||||
}
|
||||
DBG_OUTPUT_PORT.println(String("Last existing parent: ") + path);
|
||||
return path;
|
||||
}
|
||||
|
||||
/*
|
||||
Handle the creation/rename of a new file
|
||||
Operation | req.responseText
|
||||
---------------+--------------------------------------------------------------
|
||||
Create file | parent of created file
|
||||
Create folder | parent of created folder
|
||||
Rename file | parent of source file
|
||||
Move file | parent of source file, or remaining ancestor
|
||||
Rename folder | parent of source folder
|
||||
Move folder | parent of source folder, or remaining ancestor
|
||||
*/
|
||||
void handleFileCreate() {
|
||||
if (!fsOK) {
|
||||
return replyServerError(FPSTR(FS_INIT_ERROR));
|
||||
}
|
||||
|
||||
String path = httpServer.arg("path");
|
||||
if (path.isEmpty()) {
|
||||
return replyBadRequest(F("PATH ARG MISSING"));
|
||||
}
|
||||
|
||||
#ifdef USE_SPIFFS
|
||||
if (checkForUnsupportedPath(path).length() > 0) {
|
||||
return replyServerError(F("INVALID FILENAME"));
|
||||
}
|
||||
#endif
|
||||
|
||||
if (path == "/") {
|
||||
return replyBadRequest("BAD PATH");
|
||||
}
|
||||
if (fileSystem->exists(path)) {
|
||||
return replyBadRequest(F("PATH FILE EXISTS"));
|
||||
}
|
||||
|
||||
String src = httpServer.arg("src");
|
||||
if (src.isEmpty()) {
|
||||
// No source specified: creation
|
||||
DBG_OUTPUT_PORT.println(String("handleFileCreate: ") + path);
|
||||
if (path.endsWith("/")) {
|
||||
// Create a folder
|
||||
path.remove(path.length() - 1);
|
||||
if (!fileSystem->mkdir(path)) {
|
||||
return replyServerError(F("MKDIR FAILED"));
|
||||
}
|
||||
} else {
|
||||
// Create a file
|
||||
File file = fileSystem->open(path, "w");
|
||||
if (file) {
|
||||
file.write((const char *)0);
|
||||
file.close();
|
||||
} else {
|
||||
return replyServerError(F("CREATE FAILED"));
|
||||
}
|
||||
}
|
||||
if (path.lastIndexOf('/') > -1) {
|
||||
path = path.substring(0, path.lastIndexOf('/'));
|
||||
}
|
||||
replyOKWithMsg(path);
|
||||
} else {
|
||||
// Source specified: rename
|
||||
if (src == "/") {
|
||||
return replyBadRequest("BAD SRC");
|
||||
}
|
||||
if (!fileSystem->exists(src)) {
|
||||
return replyBadRequest(F("SRC FILE NOT FOUND"));
|
||||
}
|
||||
|
||||
DBG_OUTPUT_PORT.println(String("handleFileCreate: ") + path + " from " + src);
|
||||
|
||||
if (path.endsWith("/")) {
|
||||
path.remove(path.length() - 1);
|
||||
}
|
||||
if (src.endsWith("/")) {
|
||||
src.remove(src.length() - 1);
|
||||
}
|
||||
if (!fileSystem->rename(src, path)) {
|
||||
return replyServerError(F("RENAME FAILED"));
|
||||
}
|
||||
replyOKWithMsg(lastExistingParent(src));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Delete the file or folder designed by the given path.
|
||||
If it's a file, delete it.
|
||||
If it's a folder, delete all nested contents first then the folder itself
|
||||
|
||||
IMPORTANT NOTE: using recursion is generally not recommended on embedded devices and can lead to crashes (stack overflow errors).
|
||||
This use is just for demonstration purpose, and FSBrowser might crash in case of deeply nested filesystems.
|
||||
Please don't do this on a production system.
|
||||
*/
|
||||
void deleteRecursive(String path) {
|
||||
File file = fileSystem->open(path, "r");
|
||||
bool isDir = file.isDirectory();
|
||||
file.close();
|
||||
|
||||
// If it's a plain file, delete it
|
||||
if (!isDir) {
|
||||
fileSystem->remove(path);
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise delete its contents first
|
||||
Dir dir = fileSystem->openDir(path);
|
||||
|
||||
while (dir.next()) {
|
||||
deleteRecursive(path + '/' + dir.fileName());
|
||||
}
|
||||
|
||||
// Then delete the folder itself
|
||||
fileSystem->rmdir(path);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Handle a file deletion request
|
||||
Operation | req.responseText
|
||||
---------------+--------------------------------------------------------------
|
||||
Delete file | parent of deleted file, or remaining ancestor
|
||||
Delete folder | parent of deleted folder, or remaining ancestor
|
||||
*/
|
||||
void handleFileDelete() {
|
||||
if (!fsOK) {
|
||||
return replyServerError(FPSTR(FS_INIT_ERROR));
|
||||
}
|
||||
|
||||
String path = httpServer.arg(0);
|
||||
if (path.isEmpty() || path == "/") {
|
||||
return replyBadRequest("BAD PATH");
|
||||
}
|
||||
|
||||
DBG_OUTPUT_PORT.println(String("handleFileDelete: ") + path);
|
||||
if (!fileSystem->exists(path)) {
|
||||
return replyNotFound(FPSTR(FILE_NOT_FOUND));
|
||||
}
|
||||
deleteRecursive(path);
|
||||
|
||||
replyOKWithMsg(lastExistingParent(path));
|
||||
}
|
||||
|
||||
/*
|
||||
Handle a file upload request
|
||||
*/
|
||||
void handleFileUpload() {
|
||||
if (!fsOK) {
|
||||
return replyServerError(FPSTR(FS_INIT_ERROR));
|
||||
}
|
||||
if (httpServer.uri() != "/edit") {
|
||||
return;
|
||||
}
|
||||
HTTPUpload& upload = httpServer.upload();
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
String filename = upload.filename;
|
||||
// Make sure paths always start with "/"
|
||||
if (!filename.startsWith("/")) {
|
||||
filename = "/" + filename;
|
||||
}
|
||||
DBG_OUTPUT_PORT.println(String("handleFileUpload Name: ") + filename);
|
||||
uploadFile = fileSystem->open(filename, "w");
|
||||
if (!uploadFile) {
|
||||
return replyServerError(F("CREATE FAILED"));
|
||||
}
|
||||
DBG_OUTPUT_PORT.println(String("Upload: START, filename: ") + filename);
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
if (uploadFile) {
|
||||
size_t bytesWritten = uploadFile.write(upload.buf, upload.currentSize);
|
||||
if (bytesWritten != upload.currentSize) {
|
||||
return replyServerError(F("WRITE FAILED"));
|
||||
}
|
||||
}
|
||||
DBG_OUTPUT_PORT.println(String("Upload: WRITE, Bytes: ") + upload.currentSize);
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
if (uploadFile) {
|
||||
uploadFile.close();
|
||||
}
|
||||
DBG_OUTPUT_PORT.println(String("Upload: END, Size: ") + upload.totalSize);
|
||||
}
|
||||
}
|
||||
BIN
Arduino/ESP_Switch/data/.index.html.un~
Normal file
BIN
Arduino/ESP_Switch/data/.index.html.un~
Normal file
Binary file not shown.
193
Arduino/ESP_Switch/data/css/main.css
Normal file
193
Arduino/ESP_Switch/data/css/main.css
Normal file
@@ -0,0 +1,193 @@
|
||||
body {
|
||||
font-size: 0.8rem;
|
||||
background-color: #EEE;
|
||||
margin: 0px;
|
||||
padding: 0px;
|
||||
height: 100%;
|
||||
font-family: Microsoft Yahei, WenQuanYi Micro Hei, sans-serif, "Helvetica Neue", Helvetica, Hiragino Sans GB;
|
||||
}
|
||||
|
||||
header {
|
||||
width: 100%;
|
||||
height: 4rem;
|
||||
box-shadow: 0 2px 5px rgba(0, 0, 0, .26);
|
||||
background-color:aqua;
|
||||
transition: box-shadow .2s;
|
||||
float: left;
|
||||
position: fixed;
|
||||
z-index: 2000;
|
||||
}
|
||||
|
||||
.darkMask {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
position: fixed;
|
||||
content: "";
|
||||
z-index: 99;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.h1, .h2, .h3, .h4, .h5, .h6, h1, h2, h3, h4, h5, h6 {
|
||||
font-family: inherit;
|
||||
font-weight: 400;
|
||||
line-height: 1.1;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin: 2rem 0 0 0;
|
||||
font-size: 1.8rem;
|
||||
padding-bottom: 10px;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
|
||||
.headercss {
|
||||
margin-top: 0.5rem;
|
||||
padding: 0.5rem 1rem 0 1rem;
|
||||
font-size: 1.4rem;
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
cursor: default;
|
||||
vertical-align: text-bottom;
|
||||
}
|
||||
|
||||
|
||||
a {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
|
||||
|
||||
.main-left {
|
||||
float: left;
|
||||
top: 4rem;
|
||||
width: 15%;
|
||||
width: calc(0% + 15rem);
|
||||
height: 100%;
|
||||
height: calc(100% - 4rem);
|
||||
background-color: white;
|
||||
overflow-x: auto;
|
||||
position: fixed;
|
||||
|
||||
}
|
||||
.main{
|
||||
speak: none;
|
||||
line-height: 1;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
width: 100%;
|
||||
top: 4rem;
|
||||
bottom: 0rem;
|
||||
height: 100%;
|
||||
height: calc(100% - 4rem);
|
||||
}
|
||||
.table[width="33%"], .th[width="33%"], .td[width="33%"] { width: 33%; }
|
||||
|
||||
table > tbody > tr > td, table > tbody > tr > th, table > tfoot > tr > td, table > tfoot > tr > th, table > thead > tr > td, table > thead > tr > th,
|
||||
.table > .tbody > .tr > .td, .table > .tbody > .tr > .th, .table > .tfoot > .tr > .td, .table > .tfoot > .tr > .th, .table > .thead > .tr > .td, .table > .thead > .tr > .th {
|
||||
padding: .5rem;
|
||||
border-top: 1px solid #ddd;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.th, .td {
|
||||
flex: 2 2 25%;
|
||||
align-self: flex-start;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
word-wrap: break-word;
|
||||
display: inline-block;
|
||||
font-size: 1.0rem;
|
||||
}
|
||||
|
||||
.tr {
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.th, .td {
|
||||
flex-basis: 50%;
|
||||
}
|
||||
|
||||
.cbi-section {
|
||||
margin: 1rem 0 0 0;
|
||||
padding: 2rem;
|
||||
border: 0;
|
||||
font-weight: normal;
|
||||
font-style: normal;
|
||||
line-height: 1;
|
||||
font-family: inherit;
|
||||
min-width: inherit;
|
||||
border-radius: 0;
|
||||
background-color: #FFF;
|
||||
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .16), 0 0 2px 0 rgba(0, 0, 0, .12);
|
||||
|
||||
-webkit-overflow-scrolling: touch;
|
||||
}
|
||||
|
||||
.container{
|
||||
margin-top: 0.5rem;
|
||||
padding: 0.5rem 1rem 0 1rem;
|
||||
font-size: 1.4rem;
|
||||
|
||||
}
|
||||
.main-right {
|
||||
top: 4rem;
|
||||
height: 10%;
|
||||
height: calc(100% + 15rem);
|
||||
width: calc(100% - 16rem);
|
||||
float: right;
|
||||
|
||||
}
|
||||
|
||||
.current_nav {
|
||||
background-color: #348AF3;
|
||||
color: white;
|
||||
font-weight: bold;
|
||||
padding: 20px 20px 20px 20px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#biao ul {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#biao ul li a, a:visited {
|
||||
padding: 20px;
|
||||
width: 200px;
|
||||
display: block;
|
||||
background-color: transparent;
|
||||
color: #7F7F7F;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#biao ul li a:hover {
|
||||
background-color:darkgray;
|
||||
color: #348AF3;
|
||||
font-weight: normal;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
table, .table {
|
||||
width: 100%;
|
||||
border: 1px solid #eee;
|
||||
}
|
||||
.tr {
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
font-size: 1.2rem;
|
||||
height: 1em;
|
||||
}
|
||||
|
||||
.biao {
|
||||
margin-top: 0.5rem;
|
||||
color: #404040;
|
||||
display: block;
|
||||
margin-top: 2rem;
|
||||
font-size: 1.2rem;
|
||||
padding: 0.5rem 1rem;
|
||||
cursor: pointer;
|
||||
padding: 0;
|
||||
display: none;
|
||||
display: block;
|
||||
}
|
||||
15
Arduino/ESP_Switch/data/css/theme_default.css
Normal file
15
Arduino/ESP_Switch/data/css/theme_default.css
Normal file
@@ -0,0 +1,15 @@
|
||||
body {
|
||||
|
||||
}
|
||||
|
||||
|
||||
.to {
|
||||
box-shadow: 0 2px 5px rgba(0, 0, 0, .26);
|
||||
background-color: #66ccff;
|
||||
}
|
||||
|
||||
.to_text {
|
||||
color: white; /*文本颜色*/
|
||||
}
|
||||
|
||||
|
||||
1128
Arduino/ESP_Switch/data/edit/index.htm
Normal file
1128
Arduino/ESP_Switch/data/edit/index.htm
Normal file
File diff suppressed because it is too large
Load Diff
1128
Arduino/ESP_Switch/data/edit/index.html
Normal file
1128
Arduino/ESP_Switch/data/edit/index.html
Normal file
File diff suppressed because it is too large
Load Diff
BIN
Arduino/ESP_Switch/data/favicon.ico
Normal file
BIN
Arduino/ESP_Switch/data/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
22
Arduino/ESP_Switch/data/index.htm
Normal file
22
Arduino/ESP_Switch/data/index.htm
Normal file
@@ -0,0 +1,22 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
||||
<title>ESP Index</title>
|
||||
<style>
|
||||
body {
|
||||
background-color:black;
|
||||
color:white;
|
||||
}
|
||||
</style>
|
||||
<script type="text/javascript">
|
||||
function onBodyLoad(){
|
||||
console.log("we are loaded!!");
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body id="index" onload="onBodyLoad()">
|
||||
<h1>ESP8266 Pin Functions</h1>
|
||||
<img src="pins.png" />
|
||||
</body>
|
||||
</html>
|
||||
52
Arduino/ESP_Switch/data/index.html
Normal file
52
Arduino/ESP_Switch/data/index.html
Normal file
@@ -0,0 +1,52 @@
|
||||
<html lang="zh-cn">
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<!--meta http-equiv="refresh" content="0;url=/index.htm"--> <!--跳转登录-->
|
||||
<title>状态</title>
|
||||
<link href="/css/main.css" rel="stylesheet" />
|
||||
<link href="/css/theme_default.css" rel="stylesheet" />
|
||||
<script src="/js/main.js"></script>
|
||||
</head>
|
||||
<body class="background" onload="run_()">
|
||||
<div>
|
||||
<header class="to">
|
||||
<dir class="headercss to_text">
|
||||
<a class="brand">ESP8266</a>
|
||||
</dir>
|
||||
</header>
|
||||
<dev class="main">
|
||||
<div class="main-left" style="width:15rem;">
|
||||
<div id="biao">
|
||||
<ul>
|
||||
<li class="current_nav">状态</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="main-right" style="overflow-y: auto;">
|
||||
<div id="maincontent">
|
||||
<div class="darkMask" style="display: none;"></div>
|
||||
<div class="container">
|
||||
<h2 name="content">状态</h2>
|
||||
<div class="cbi-section">
|
||||
<h3>系统</h3>
|
||||
<div class="table" width="100%">
|
||||
<div class="tr"><div class="td left" width="33%">主机名</div><div id="Host_name" class="td left">NO DATA</div></div>
|
||||
<div class="tr"><div class="td left" width="33%">主机型号</div><div id="Host_model" class="td left">NO DATA</div></div>
|
||||
<div class="tr"><div class="td left" width="33%">架构</div><div class="td left">ESP8266</div></div>
|
||||
<div class="tr"><div class="td left" width="33%">固件版本</div><div class="td left">我不知道</div></div>
|
||||
<div class="tr"><div class="td left" width="33%">内核版本</div><div class="td left">不知道</div></div>
|
||||
<div class="tr"><div class="td left" width="33%">本地时间</div><div class="td left" id="localtime">不知道</div></div>
|
||||
<div class="tr"><div class="td left" width="33%">运行时间</div><div class="td left" id="uptime">不知道</div></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</dev>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
138
Arduino/ESP_Switch/data/index.html~
Normal file
138
Arduino/ESP_Switch/data/index.html~
Normal file
@@ -0,0 +1,138 @@
|
||||
<html lang="zh-cn">
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>登录</title>
|
||||
<link href="/login/login.css" rel="stylesheet" />
|
||||
<script type="text/javascript">
|
||||
function run(){
|
||||
var xmlhttp;
|
||||
if (window.XMLHttpRequest)
|
||||
{// code for IE7+, Firefox, Chrome, Opera, Safari
|
||||
xmlhttp=new XMLHttpRequest();
|
||||
}else{
|
||||
// code for IE6, IE5
|
||||
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
|
||||
}
|
||||
xmlhttp.onreadystatechange=function()
|
||||
{
|
||||
if (xmlhttp.readyState==4 && xmlhttp.status==200)
|
||||
{
|
||||
document.getElementById("name").innerText = xmlhttp.responseText;
|
||||
document.getElementById("error").innerText = '';
|
||||
}
|
||||
}
|
||||
xmlhttp.open("GET","/etc/hostname",true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
function button1_Click() {
|
||||
if(document.getElementById("username").value == '')
|
||||
{
|
||||
document.getElementById("error").innerText = '请输入用户名';
|
||||
}else
|
||||
{
|
||||
var xmlhttp;
|
||||
if (window.XMLHttpRequest)
|
||||
{// code for IE7+, Firefox, Chrome, Opera, Safari
|
||||
xmlhttp=new XMLHttpRequest();
|
||||
}else{
|
||||
// code for IE6, IE5
|
||||
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
|
||||
}
|
||||
|
||||
xmlhttp.onreadystatechange=function()
|
||||
{
|
||||
if (xmlhttp.readyState==4 && xmlhttp.status==200)
|
||||
{
|
||||
if(xmlhttp.responseText == "ok")
|
||||
{
|
||||
if("http://" + window.location.host + "/login/" == document.URL)
|
||||
{
|
||||
window.location.href="/";
|
||||
}else
|
||||
location.reload(true);
|
||||
}else
|
||||
{
|
||||
document.getElementById("error").innerText = '用户名或者密码错误';
|
||||
}
|
||||
}
|
||||
}
|
||||
xmlhttp.open("GET","/login/login?user=" + document.getElementById("username").value + "&password=" + document.getElementById("password").value,true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
if(document.getElementById("username").value == '')
|
||||
{
|
||||
document.getElementById("error").innerText = '请输入用户名';
|
||||
}else
|
||||
{
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET','/login/login?user=' + document.getElementById("username").value + '&password=' + document.getElementById("password").value);
|
||||
|
||||
|
||||
//document.getElementById("username").value = document.getElementById("password").value;
|
||||
|
||||
//document.getElementById("error").innerText = xhr.responeText;
|
||||
//var p =document.getElementById("ll");
|
||||
//p.innerHTML = xhr.status;
|
||||
//var out = xhr.responeText;
|
||||
//document.getElementById("username").value = out;
|
||||
xhr.onload = function(){
|
||||
if(xhr.status == 200){
|
||||
//document.getElementById("error").innerText = xhr.responeText;
|
||||
var p =document.getElementById("ll");
|
||||
//p.innerHTML = xhr.responeText;
|
||||
var out = xhr.responeText;
|
||||
p.innerHTML = out;
|
||||
}
|
||||
}
|
||||
xhr.onreadyStechange = function(){
|
||||
if(xhr.status == 200){
|
||||
|
||||
document.getElementById("username").value = xhr.responeText;
|
||||
console.log(xhr.responeText);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
xhr.send(null);
|
||||
}
|
||||
};*/
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload="run()">
|
||||
<div id="login" class="loginweb">
|
||||
<ul class="load_ul">
|
||||
<li class="text_center">
|
||||
<div>
|
||||
<span name="name" id="name" class="white login_text">需要 JavaScript!</span>
|
||||
</div><p></p>
|
||||
</li>
|
||||
<li class="text_center">
|
||||
<div class="div_info">
|
||||
<!--asp:TextBox runat="server" ID="username" class="inptText" placeholder="用户名"></asp:TextBox-->
|
||||
<input placeholder="用户名" type="text" id="username" class="inptText" />
|
||||
</div>
|
||||
</li>
|
||||
<li class="text_center">
|
||||
<div class="div_info">
|
||||
<!--asp:TextBox runat="server" ID="username" class="inptText" placeholder="用户名"></asp:TextBox-->
|
||||
<input placeholder="密码" type="password" id="password" class="inptText" />
|
||||
</div>
|
||||
<!--asp:HyperLink ID="error" runat="server"></asp:HyperLink-->
|
||||
<a id="error" style="color:Red;">必须开启浏览器的 JavaScript 支持,否则 Web 无法正常工作。</a>
|
||||
<p id="ll"></p>
|
||||
</li>
|
||||
<li class="text_center">
|
||||
<!--asp:Button runat="server" ID="button1" Text="登录" class="button login_button" OnClick="button1_Click" /-->
|
||||
<button type="submit" name="button" class="button" OnClick="button1_Click()">登录</button>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
48
Arduino/ESP_Switch/data/js/main.js
Normal file
48
Arduino/ESP_Switch/data/js/main.js
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
|
||||
function run_(){
|
||||
Host_name_();
|
||||
Host_model_();
|
||||
|
||||
}
|
||||
|
||||
function Host_name_(){
|
||||
var xmlhttp;
|
||||
if (window.XMLHttpRequest)
|
||||
{// code for IE7+, Firefox, Chrome, Opera, Safari
|
||||
xmlhttp=new XMLHttpRequest();
|
||||
}else{
|
||||
// code for IE6, IE5
|
||||
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
|
||||
}
|
||||
xmlhttp.onreadystatechange=function()
|
||||
{
|
||||
if (xmlhttp.readyState==4 && xmlhttp.status==200)
|
||||
{
|
||||
document.getElementById("Host_name").innerText = xmlhttp.responseText;
|
||||
}
|
||||
}
|
||||
xmlhttp.open("GET","/etc/hostname",true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
|
||||
function Host_model_(){
|
||||
var xmlhttp;
|
||||
if (window.XMLHttpRequest)
|
||||
{// code for IE7+, Firefox, Chrome, Opera, Safari
|
||||
xmlhttp=new XMLHttpRequest();
|
||||
}else{
|
||||
// code for IE6, IE5
|
||||
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
|
||||
}
|
||||
xmlhttp.onreadystatechange=function()
|
||||
{
|
||||
if (xmlhttp.readyState==4 && xmlhttp.status==200)
|
||||
{
|
||||
document.getElementById("Host_model").innerText = xmlhttp.responseText;
|
||||
}
|
||||
}
|
||||
xmlhttp.open("GET","/etc/sysname",true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
|
||||
138
Arduino/ESP_Switch/data/login/index.html
Normal file
138
Arduino/ESP_Switch/data/login/index.html
Normal file
@@ -0,0 +1,138 @@
|
||||
<html lang="zh-cn">
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>登录</title>
|
||||
<link href="/login/login.css" rel="stylesheet" />
|
||||
<script type="text/javascript">
|
||||
function run(){
|
||||
var xmlhttp;
|
||||
if (window.XMLHttpRequest)
|
||||
{// code for IE7+, Firefox, Chrome, Opera, Safari
|
||||
xmlhttp=new XMLHttpRequest();
|
||||
}else{
|
||||
// code for IE6, IE5
|
||||
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
|
||||
}
|
||||
xmlhttp.onreadystatechange=function()
|
||||
{
|
||||
if (xmlhttp.readyState==4 && xmlhttp.status==200)
|
||||
{
|
||||
document.getElementById("name").innerText = xmlhttp.responseText;
|
||||
document.getElementById("error").innerText = '';
|
||||
}
|
||||
}
|
||||
xmlhttp.open("GET","/etc/hostname",true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
function button1_Click() {
|
||||
if(document.getElementById("username").value == '')
|
||||
{
|
||||
document.getElementById("error").innerText = '请输入用户名';
|
||||
}else
|
||||
{
|
||||
var xmlhttp;
|
||||
if (window.XMLHttpRequest)
|
||||
{// code for IE7+, Firefox, Chrome, Opera, Safari
|
||||
xmlhttp=new XMLHttpRequest();
|
||||
}else{
|
||||
// code for IE6, IE5
|
||||
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
|
||||
}
|
||||
|
||||
xmlhttp.onreadystatechange=function()
|
||||
{
|
||||
if (xmlhttp.readyState==4 && xmlhttp.status==200)
|
||||
{
|
||||
if(xmlhttp.responseText == "ok")
|
||||
{
|
||||
if("http://" + window.location.host + "/login/" == document.URL)
|
||||
{
|
||||
window.location.href="/";
|
||||
}else
|
||||
location.reload(true);
|
||||
}else
|
||||
{
|
||||
document.getElementById("error").innerText = '用户名或者密码错误';
|
||||
}
|
||||
}
|
||||
}
|
||||
xmlhttp.open("GET","/login/login?user=" + document.getElementById("username").value + "&password=" + document.getElementById("password").value,true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
if(document.getElementById("username").value == '')
|
||||
{
|
||||
document.getElementById("error").innerText = '请输入用户名';
|
||||
}else
|
||||
{
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET','/login/login?user=' + document.getElementById("username").value + '&password=' + document.getElementById("password").value);
|
||||
|
||||
|
||||
//document.getElementById("username").value = document.getElementById("password").value;
|
||||
|
||||
//document.getElementById("error").innerText = xhr.responeText;
|
||||
//var p =document.getElementById("ll");
|
||||
//p.innerHTML = xhr.status;
|
||||
//var out = xhr.responeText;
|
||||
//document.getElementById("username").value = out;
|
||||
xhr.onload = function(){
|
||||
if(xhr.status == 200){
|
||||
//document.getElementById("error").innerText = xhr.responeText;
|
||||
var p =document.getElementById("ll");
|
||||
//p.innerHTML = xhr.responeText;
|
||||
var out = xhr.responeText;
|
||||
p.innerHTML = out;
|
||||
}
|
||||
}
|
||||
xhr.onreadyStechange = function(){
|
||||
if(xhr.status == 200){
|
||||
|
||||
document.getElementById("username").value = xhr.responeText;
|
||||
console.log(xhr.responeText);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
xhr.send(null);
|
||||
}
|
||||
};*/
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body onload="run()">
|
||||
<div id="login" class="loginweb">
|
||||
<ul class="load_ul">
|
||||
<li class="text_center">
|
||||
<div>
|
||||
<span name="name" id="name" class="white login_text">需要 JavaScript!</span>
|
||||
</div><p></p>
|
||||
</li>
|
||||
<li class="text_center">
|
||||
<div class="div_info">
|
||||
<!--asp:TextBox runat="server" ID="username" class="inptText" placeholder="用户名"></asp:TextBox-->
|
||||
<input placeholder="用户名" type="text" id="username" class="inptText" />
|
||||
</div>
|
||||
</li>
|
||||
<li class="text_center">
|
||||
<div class="div_info">
|
||||
<!--asp:TextBox runat="server" ID="username" class="inptText" placeholder="用户名"></asp:TextBox-->
|
||||
<input placeholder="密码" type="password" id="password" class="inptText" />
|
||||
</div>
|
||||
<!--asp:HyperLink ID="error" runat="server"></asp:HyperLink-->
|
||||
<a id="error" style="color:Red;">必须开启浏览器的 JavaScript 支持,否则 Web 无法正常工作。</a>
|
||||
<p id="ll"></p>
|
||||
</li>
|
||||
<li class="text_center">
|
||||
<!--asp:Button runat="server" ID="button1" Text="登录" class="button login_button" OnClick="button1_Click" /-->
|
||||
<button type="submit" name="button" class="button" OnClick="button1_Click()">登录</button>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
54
Arduino/ESP_Switch/data/login/login.css
Normal file
54
Arduino/ESP_Switch/data/login/login.css
Normal file
@@ -0,0 +1,54 @@
|
||||
/*登录样式文件,确保可以正常运行,细节请写到主题样式里面(theme_)*/
|
||||
body {
|
||||
background-color: #66ccff;
|
||||
}
|
||||
|
||||
.loginWeb {
|
||||
background-color: #66ccff;
|
||||
background: #66ccff;
|
||||
position: absolute;
|
||||
top: 20%;
|
||||
left: 0;
|
||||
right: 0;
|
||||
width: 360px;
|
||||
height: 310px;
|
||||
margin: auto;
|
||||
color: #66ccff;
|
||||
}
|
||||
|
||||
.white {
|
||||
color: #FFF;
|
||||
font-size: 1.4em;
|
||||
text-align:center;
|
||||
}
|
||||
|
||||
.text_center {
|
||||
-webkit-animation: centerShow 1.2s ease;
|
||||
-moz-animation: centerShow 1.2s ease;
|
||||
-o-animation: centerShow 1.2s ease;
|
||||
animation: centerShow 1.2s ease
|
||||
}
|
||||
|
||||
.div_info {
|
||||
position: relative;
|
||||
margin-bottom: 15px
|
||||
}
|
||||
|
||||
.inptText {
|
||||
color: #000000;
|
||||
display: inline-block;
|
||||
height: 40px;
|
||||
width: 100%;
|
||||
border-radius: 2px;
|
||||
border-width: 0;
|
||||
outline: none
|
||||
}
|
||||
|
||||
.button {
|
||||
height: 40px;
|
||||
width: 100%;
|
||||
border-width: 0;
|
||||
background-color: chartreuse;
|
||||
}
|
||||
|
||||
|
||||
BIN
Arduino/ESP_Switch/data/pins.png
Normal file
BIN
Arduino/ESP_Switch/data/pins.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
224
Arduino/testArduino/testArduino.ino
Normal file
224
Arduino/testArduino/testArduino.ino
Normal file
@@ -0,0 +1,224 @@
|
||||
volatile int item;
|
||||
volatile int mills;
|
||||
volatile int wd;
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
pinMode(2, OUTPUT);
|
||||
pinMode(3, OUTPUT);
|
||||
pinMode(4, OUTPUT);
|
||||
pinMode(5, OUTPUT);
|
||||
pinMode(6, OUTPUT);
|
||||
pinMode(7, OUTPUT);
|
||||
pinMode(8, OUTPUT);
|
||||
pinMode(9, OUTPUT);
|
||||
//2,1cs,3=,4|上,52cs,63cs
|
||||
digitalWrite(2,HIGH);
|
||||
digitalWrite(3,LOW);
|
||||
digitalWrite(4,LOW);
|
||||
digitalWrite(5,HIGH);
|
||||
digitalWrite(6,HIGH);
|
||||
pinMode(A0, INPUT);
|
||||
pinMode(A1, OUTPUT);
|
||||
digitalWrite(7,LOW);
|
||||
pinMode(A2, OUTPUT);
|
||||
digitalWrite(A2,HIGH);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,LOW);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
pinMode(A5, OUTPUT);
|
||||
digitalWrite(A5,HIGH);
|
||||
digitalWrite(8,LOW);
|
||||
digitalWrite(9,LOW);
|
||||
smg(2);
|
||||
pinMode(13, OUTPUT);
|
||||
wd = 0;
|
||||
mills = 0;
|
||||
item = 0;
|
||||
}
|
||||
|
||||
void loop(){
|
||||
Serial.println(analogRead(A0));
|
||||
Serial.print("");
|
||||
|
||||
if (analogRead(A0) < wd) {
|
||||
digitalWrite(13,HIGH);
|
||||
analogWrite(11,100);
|
||||
|
||||
} else {
|
||||
digitalWrite(13,LOW);
|
||||
analogWrite(11,255);
|
||||
|
||||
}
|
||||
|
||||
smg(10);
|
||||
pinMode(A2, OUTPUT);
|
||||
digitalWrite(A2,HIGH);
|
||||
smg(item % 10);
|
||||
delay(1);
|
||||
pinMode(A2, OUTPUT);
|
||||
digitalWrite(A2,LOW);
|
||||
smg(10);
|
||||
digitalWrite(6,HIGH);
|
||||
smg(item % 100 / 10);
|
||||
delay(1);
|
||||
digitalWrite(6,LOW);
|
||||
smg(10);
|
||||
digitalWrite(5,HIGH);
|
||||
smg(item % 1000 / 100);
|
||||
delay(1);
|
||||
digitalWrite(5,LOW);
|
||||
digitalWrite(2,LOW);
|
||||
|
||||
if (mills == 0) {
|
||||
item = analogRead(A0);
|
||||
//item=item+30;
|
||||
mills = 50;
|
||||
|
||||
} else {
|
||||
mills = mills - 1 ;
|
||||
|
||||
}
|
||||
if (Serial.available() > 0 == true) {
|
||||
wd = String(Serial.readString()).toInt();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
void smg(int data)
|
||||
{
|
||||
if (data == 0) {
|
||||
digitalWrite(3,LOW);
|
||||
digitalWrite(4,LOW);
|
||||
digitalWrite(7,LOW);
|
||||
digitalWrite(8,LOW);
|
||||
digitalWrite(9,LOW);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,HIGH);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
|
||||
}
|
||||
if (data == 1) {
|
||||
digitalWrite(3,HIGH);
|
||||
digitalWrite(4,HIGH);
|
||||
digitalWrite(7,LOW);
|
||||
digitalWrite(8,HIGH);
|
||||
digitalWrite(9,HIGH);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,HIGH);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
|
||||
}
|
||||
if (data == 2) {
|
||||
digitalWrite(3,LOW);
|
||||
digitalWrite(4,HIGH);
|
||||
digitalWrite(7,LOW);
|
||||
digitalWrite(8,LOW);
|
||||
digitalWrite(9,LOW);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,LOW);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,HIGH);
|
||||
|
||||
}
|
||||
if (data == 3) {
|
||||
digitalWrite(3,LOW);
|
||||
digitalWrite(4,HIGH);
|
||||
digitalWrite(7,LOW);
|
||||
digitalWrite(8,HIGH);
|
||||
digitalWrite(9,LOW);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,LOW);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
|
||||
}
|
||||
if (data == 4) {
|
||||
digitalWrite(3,HIGH);
|
||||
digitalWrite(4,LOW);
|
||||
digitalWrite(7,LOW);
|
||||
digitalWrite(8,HIGH);
|
||||
digitalWrite(9,HIGH);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,LOW);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
|
||||
}
|
||||
if (data == 5) {
|
||||
digitalWrite(3,LOW);
|
||||
digitalWrite(4,LOW);
|
||||
digitalWrite(7,HIGH);
|
||||
digitalWrite(8,HIGH);
|
||||
digitalWrite(9,LOW);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,LOW);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
|
||||
}
|
||||
if (data == 6) {
|
||||
digitalWrite(3,LOW);
|
||||
digitalWrite(4,LOW);
|
||||
digitalWrite(7,HIGH);
|
||||
digitalWrite(8,LOW);
|
||||
digitalWrite(9,LOW);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,LOW);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
|
||||
}
|
||||
if (data == 7) {
|
||||
digitalWrite(3,LOW);
|
||||
digitalWrite(4,HIGH);
|
||||
digitalWrite(7,LOW);
|
||||
digitalWrite(8,HIGH);
|
||||
digitalWrite(9,HIGH);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,HIGH);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
|
||||
}
|
||||
if (data == 8) {
|
||||
digitalWrite(3,LOW);
|
||||
digitalWrite(4,LOW);
|
||||
digitalWrite(7,LOW);
|
||||
digitalWrite(8,LOW);
|
||||
digitalWrite(9,LOW);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,LOW);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
|
||||
}
|
||||
if (data == 9) {
|
||||
digitalWrite(3,LOW);
|
||||
digitalWrite(4,LOW);
|
||||
digitalWrite(7,LOW);
|
||||
digitalWrite(8,HIGH);
|
||||
digitalWrite(9,LOW);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,LOW);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,LOW);
|
||||
|
||||
}
|
||||
if (data == 10) {
|
||||
digitalWrite(3,HIGH);
|
||||
digitalWrite(4,HIGH);
|
||||
digitalWrite(7,HIGH);
|
||||
digitalWrite(8,HIGH);
|
||||
digitalWrite(9,HIGH);
|
||||
pinMode(A3, OUTPUT);
|
||||
digitalWrite(A3,HIGH);
|
||||
pinMode(A4, OUTPUT);
|
||||
digitalWrite(A4,HIGH);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user