polistirolo78‎ > ‎

webserver

costycnc.it/led? arg1=bla&test=1

webServer.args()  return how many args  (in this case =2 ..... arg1 and test)
webServer.arg(0) return >> bla ..... webServer.arg(1) return >> 1
webServer.argName(0) return >> arg1  ....  webServer.argName(1)  return >> test
webServer.hasArg("costel")  return >> false ...... webServer.hasArg("test") return >>true
webServer.uri()  return     >> /led


ftp.onstatic-it.setupdns.net c t1

Casella di testo

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <FS.h>

const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 1, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80);
File fsUploadFile;
//*******************************************

void handleFileDelete(){
  if(webServer.args() == 0) return webServer.send(500, "text/plain", "BAD ARGS");
  String path = webServer.arg(0);
  //DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
  if(path == "/")
    return webServer.send(500, "text/plain", "BAD PATH");
  if(!SPIFFS.exists(path))
    return webServer.send(404, "text/plain", "FileNotFound");
  SPIFFS.remove(path);
  webServer.send(200, "text/plain", "");
  path = String();
}

String getContentType(String filename){
  if(webServer.hasArg("download")) return "application/octet-stream";
  else if(filename.endsWith(".htm")) return "text/html";
  else if(filename.endsWith(".html")) return "text/html";
  else if(filename.endsWith(".css")) return "text/css";
  else if(filename.endsWith(".js")) return "application/javascript";
  else if(filename.endsWith(".png")) return "image/png";
  else if(filename.endsWith(".gif")) return "image/gif";
  else if(filename.endsWith(".jpg")) return "image/jpeg";
  else if(filename.endsWith(".ico")) return "image/x-icon";
  else if(filename.endsWith(".xml")) return "text/xml";
  else if(filename.endsWith(".pdf")) return "application/x-pdf";
  else if(filename.endsWith(".zip")) return "application/x-zip";
  else if(filename.endsWith(".gz")) return "application/x-gzip";
  return "text/plain";
}



bool handleFileRead(String path){
  //DBG_OUTPUT_PORT.println("handleFileRead: " + path);
  if(path.endsWith("/")) path += "index.htm";
  String contentType = getContentType(path);
  String pathWithGz = path + ".gz";
  if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
    if(SPIFFS.exists(pathWithGz))
      path += ".gz";
    File file = SPIFFS.open(path, "r");
    size_t sent = webServer.streamFile(file, contentType);
    file.close();
    return true;
  }
  return false;
}



void handleFileUpload(){
  if(webServer.uri() != "/edit") return;
  HTTPUpload& upload = webServer.upload();
  if(upload.status == UPLOAD_FILE_START){
    String filename = upload.filename;
    if(!filename.startsWith("/")) filename = "/"+filename;
    //DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
    fsUploadFile = SPIFFS.open(filename, "w");
    filename = String();
  } else if(upload.status == UPLOAD_FILE_WRITE){
    //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
    if(fsUploadFile)
      fsUploadFile.write(upload.buf, upload.currentSize);
  } else if(upload.status == UPLOAD_FILE_END){
    if(fsUploadFile)
      fsUploadFile.close();
    //DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize);
  }
}

void handleFileList() {
  //if(!webServer.hasArg("dir")) {webServer.send(500, "text/plain", "BAD ARGS"); return;}
  
  String path = webServer.arg("dir");
  //DBG_OUTPUT_PORT.println("handleFileList: " + path);
  Dir dir = SPIFFS.openDir(path);
  path = String();

  String output = "[";
  while(dir.next()){
    File entry = dir.openFile("r");
    if (output != "[") output += ',';
    bool isDir = false;
    output += "{\"type\":\"";
    output += (isDir)?"dir":"file";
    output += "\",\"name\":\"";
    output += String(entry.name()).substring(1);
    output += "\"}";
    entry.close();
  }
  
  output += "]";
  webServer.send(200, "text/json", output);
}

//**********************************************
void setup() {
  delay(1000);
  Serial.begin(9600);
  //***********
  SPIFFS.begin();
  {
    Dir dir = SPIFFS.openDir("/");
    while (dir.next()) {    
      String fileName = dir.fileName();
      size_t fileSize = dir.fileSize();
     // DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
    }
    //DBG_OUTPUT_PORT.printf("\n");
  }
  //**********************
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP("Costycnc machine");

  // modify TTL associated  with the domain name (in seconds)
  // default is 60 seconds
  dnsServer.setTTL(300);
  // set which return code will be used for all other domains (e.g. sending
  // ServerFailure instead of NonExistentDomain will reduce number of queries
  // sent by clients)
  // default is DNSReplyCode::NonExistentDomain
  dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure);

  // start DNS server for a specific domain name
  dnsServer.start(DNS_PORT, "costycnc.it", apIP);

  // simple HTTP server to see that DNS server is working
   webServer.on("/inline", [](){
    webServer.send(200, "text/html", "<html>this works as well</br><a href='/'>acasa</a></html>");
  });
  //**********************************************
   webServer.on("/list", HTTP_GET, handleFileList);
   webServer.on("/edit", HTTP_POST, [](){ webServer.send(200, "text/plain", ""); }, handleFileUpload);
   webServer.on("/edit", HTTP_DELETE, handleFileDelete);
   
     webServer.onNotFound([](){
    if(!handleFileRead(webServer.uri()))
      webServer.send(404, "text/plain", "FileNotFound");
  });
  
  webServer.on("/all", HTTP_GET, [](){
    String json = "{";
    json += "\"heap\":"+String(ESP.getFreeHeap());
    json += ", \"analog\":"+String(analogRead(A0));
    json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
    json += "}";
    webServer.send(200, "text/json", json);
    json = String();
  });
   //************************************************
  /*webServer.onNotFound([]() {
    String message="<html><body>";
    message+="<textarea  id='mytext' rows='20' cols='50'></textarea>";
    message += "<button onclick='select()' style='font-size:300%;'>Save gcode</button></b><br><br>";
    message += "<a href='/inline'>aici</a><br><br>";
    message += "<form action='/action_page.php'>";
    message += "First name: <input type='text' name='FirstName' value='Mickey' style='font-size:300%;'><br>";
    message += "Last name: <input type='text' name='LastName' value='Mouse' style='font-size:300%;'><br>";
    message += "<input type='submit' value='Submit' style='font-size:300%;'>";
    message += "</form>";
    message += "<script>function select(){alert('e bine');}</script>";
    message +="</body><html>";
    

  Serial.print("Configuring access point...");
    webServer.send(200, "text/html", message);
  });*/
  webServer.begin();
}

void loop() {
  dnsServer.processNextRequest();
  webServer.handleClient();
}

Casella di testo

<form action="http://costycnc.it/edit" method="post" enctype="multipart/form-data">
  CAMPO TESTO <input type="text" name="testo1">
  FILE <input type="file" name="file1">
  <input type="submit" value="Upload">
</form>

<form action="http://costycnc.it/edit" method="get" >
  CAMPO TESTO <input type="text" name="testo1">
  <input type="submit" value="Upload">
</form>

26.07  10.46 am
**********************
   webServer.on("/testi", handle_test);
**********************

void handle_test(){
  //webServer.send(200, "text/plain", "hello");
  //handleFileRead("/tested.nc");
  File f = SPIFFS.open("/tested.nc", "r");
  if (!f) {
    return webServer.send(404, "text/plain", "FileNotFound=");
    }

    String s=f.readStringUntil('\n');
    s +=f.readStringUntil('\n');
    s +=f.readStringUntil('\n');
     
      webServer.send(200, "text/plain", s);
      f.close();
  

}


ċ
index.htm
(3k)
Boboaca Costel,
24 iul. 2017, 02:02
ą
p1.jpg
(26k)
Boboaca Costel,
24 iul. 2017, 02:02
ċ
test.html
(0k)
Boboaca Costel,
22 iul. 2017, 06:08
ċ
webserver26.07_compl_mih.zip
(2k)
Boboaca Costel,
26 iul. 2017, 00:53
Comments