Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
#include <FS.h>
2
#include <LittleFS.h>
3
#include <AsyncFsWebServer.h>   // https://github.com/cotestatnt/async-esp-fs-webserver/
4
 
5
#define FILESYSTEM LittleFS
6
AsyncFsWebServer server(FILESYSTEM, 80, "esphost");
7
 
8
////////////////////////////  HTTP Request Handlers  ////////////////////////////////////
9
void getDefaultValue (AsyncWebServerRequest *request) {
10
  // Send to client default values as JSON string because it's very easy to parse JSON in Javascript
11
  String defaultVal = "{\"car\":\"Ferrari\", \"firstname\":\"Enzo\", \"lastname\":\"Ferrari\",\"age\":90}";
12
  request->send(200, "text/json", defaultVal);
13
}
14
 
15
void handleForm1(AsyncWebServerRequest *request) {
16
  String reply;
17
  if(request->hasArg("cars")) {
18
    reply += "You have submitted with Form1: ";
19
    reply += request->arg("cars");
20
  }
21
  Serial.println(reply);
22
  request->send(200, "text/plain", reply);
23
}
24
 
25
void handleForm2(AsyncWebServerRequest *request) {
26
  String reply;
27
  if(request->hasArg("firstname")) {
28
    reply += "You have submitted with Form2: ";
29
    reply += request->arg("firstname");
30
  }
31
  if(request->hasArg("lastname")) {
32
    reply += " ";
33
    reply += request->arg("lastname");
34
  }
35
  if(request->hasArg("age")) {
36
    reply += ", age: ";
37
    reply += request->arg("age");
38
  }
39
  Serial.println(reply);
40
  request->send(200, "text/plain", reply);
41
}
42
 
43
////////////////////////////////  Filesystem  /////////////////////////////////////////
44
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
45
  Serial.printf("\nListing directory: %s\n", dirname);
46
  File root = fs.open(dirname, "r");
47
  if (!root) {
48
    Serial.println("- failed to open directory");
49
    return;
50
  }
51
  if (!root.isDirectory()) {
52
    Serial.println(" - not a directory");
53
    return;
54
  }
55
  File file = root.openNextFile();
56
  while (file) {
57
    if (file.isDirectory()) {
58
      if (levels) {
59
        #ifdef ESP32
60
          listDir(fs, file.path(), levels - 1);
61
        #elif defined(ESP8266)
62
          listDir(fs, file.fullName(), levels - 1);
63
        #endif
64
      }
65
    } else {
66
      Serial.printf("|__ FILE: %s (%d bytes)\n",file.name(), file.size());
67
    }
68
    file = root.openNextFile();
69
  }
70
}
71
 
72
bool startFilesystem() {
73
  if (FILESYSTEM.begin()){
74
    listDir(FILESYSTEM, "/", 1);
75
    return true;
76
  }
77
  else {
78
      Serial.println("ERROR on mounting filesystem. It will be reformatted!");
79
      FILESYSTEM.format();
80
      ESP.restart();
81
  }
82
  return false;
83
}
84
 
85
 
86
void setup(){
87
  Serial.begin(115200);
88
 
89
  // FILESYSTEM INIT
90
  startFilesystem();
91
 
92
  // Try to connect to WiFi (will start AP if not connected after timeout)
93
  if (!server.startWiFi(10000)) {
94
    Serial.println("\nWiFi not connected! Starting AP mode...");
95
    server.startCaptivePortal("ESP_AP", "123456789", "/setup");
96
  }
97
 
98
  // Add custom page handlers to webserver
99
  server.on("/getDefault", HTTP_GET, getDefaultValue);
100
  server.on("/setForm1", HTTP_POST, handleForm1);
101
  server.on("/setForm2", HTTP_POST, handleForm2);
102
 
103
  // Enable ACE FS file web editor and add FS info callback function
104
  server.enableFsCodeEditor();
105
 
106
  // Start server
107
  server.init();
108
  Serial.print(F("ESP Web Server started on IP Address: "));
109
  Serial.println(server.getServerIP());
110
  Serial.println(F(
111
    "This is \"handleFormData.ino\" example.\n"
112
    "Open /setup page to configure optional parameters.\n"
113
    "Open /edit page to view, edit or upload example or your custom webserver source files."
114
  ));
115
}
116
 
117
 
118
void loop() {
119
 
120
  // This delay is required in order to avoid loopTask() WDT reset on ESP32
121
  delay(1);  
122
}