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"
4
 
5
AsyncFsWebServer server(LittleFS, 80, "myServer");
6
uint16_t testInt = 150;
7
float testFloat = 123.456f;
8
bool testBool = true;
9
 
10
#ifndef LED_BUILTIN
11
#define LED_BUILTIN 2
12
#endif
13
const uint8_t ledPin = LED_BUILTIN;
14
 
15
 
16
////////////////////////////////  Filesystem  /////////////////////////////////////////
17
bool startFilesystem() {
18
  if (LittleFS.begin()) {
19
    server.printFileList(LittleFS, "/", 1);
20
    Serial.println();
21
    return true;
22
  } else {
23
    Serial.println("ERROR on mounting filesystem. It will be reformatted!");
24
    LittleFS.format();
25
    ESP.restart();
26
  }
27
  return false;
28
}
29
 
30
////////////////////////////  Load custom options //////////////////////////////////////
31
bool loadApplicationConfig() {
32
  if (LittleFS.exists(server.getConfiFileName())) {
33
    // Test "options" values
34
    server.getOptionValue("Test int variable", testInt);
35
    server.getOptionValue("Test float variable", testFloat);
36
    server.getOptionValue("Test bool variable", testBool);
37
    return true;
38
  }
39
  return false;
40
}
41
 
42
//---------------------------------------
43
void handleLed(AsyncWebServerRequest* request) {
44
  static int value = false;
45
  // http://xxx.xxx.xxx.xxx/led?val=1
46
  if (request->hasParam("val")) {
47
    value = request->arg("val").toInt();
48
    digitalWrite(ledPin, value);
49
  }
50
  String reply = "LED is now ";
51
  reply += value ? "ON" : "OFF";
52
  request->send(200, "text/plain", reply);
53
}
54
 
55
 
56
void setup() {
57
  pinMode(ledPin, OUTPUT);
58
  Serial.begin(115200);
59
  delay(1000);
60
  if (startFilesystem()) {
61
    // Load application config options
62
    if (loadApplicationConfig()) {
63
      Serial.printf("Stored \"testInt\" value: %d\n", testInt);
64
      Serial.printf("Stored \"testFloat\" value: %3.3f\n", testFloat);
65
      Serial.printf("Stored \"testBool\" value: %s\n", testBool?"true":"false");
66
    }
67
  } else
68
    Serial.println("LittleFS error!");
69
 
70
  // Try to connect to WiFi (will start AP if not connected after timeout)
71
  if (!server.startWiFi(10000)) {
72
    Serial.println("\nWiFi not connected! Starting AP mode...");
73
    server.startCaptivePortal("ESP_AP", "123456789", "/setup");
74
  }
75
 
76
  // Add custom application options tab and set custom title
77
  server.addOptionBox("Custom options");
78
  server.addOption("Test int variable", testInt);
79
  server.addOption("Test float variable", (double)testFloat, 0.0, 100.0, 0.001);
80
  // add a helpful comment beneath the float slider
81
  server.addComment("Test float variable", "Use this to adjust the intensity (0-100)");
82
  // add a helpful comment direclty (boolean comment appears inline by default)
83
  server.addOption("Test bool variable", testBool, "Enable/disable feature");
84
  server.setSetupPageTitle("Simple Async ESP FS WebServer");
85
 
86
  // Enable ACE FS file web editor and add FS info callback function
87
  server.enableFsCodeEditor();
88
 
89
  // Custom endpoint handler
90
  server.on("/led", HTTP_GET, handleLed);
91
 
92
  // Start server
93
  server.init();
94
  Serial.print(F("\nAsync ESP Web Server started on IP Address: "));
95
  Serial.println(server.getServerIP());
96
  Serial.println(F(
97
    "This is \"simpleServer.ino\" example.\n"
98
    "Open /setup page to configure optional parameters.\n"
99
    "Open /edit page to view, edit or upload example or your custom webserver source files."));
100
}
101
 
102
void loop() {
103
  // This delay is required in order to avoid loopTask() WDT reset on ESP32
104
  delay(10);
105
}