123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #if defined(ARDUINO_ARCH_ESP8266)
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- typedef ESP8266WebServer WEBServer;
- #elif defined(ARDUINO_ARCH_ESP32)
- #include <WiFi.h>
- #include <WebServer.h>
- typedef WebServer WEBServer;
- #endif
- #include <AutoConnect.h>
- #include <FS.h>
- #if defined(ARDUINO_ARCH_ESP8266)
- #ifdef AUTOCONNECT_USE_SPIFFS
- FS& FlashFS = SPIFFS;
- #else
- #include <LittleFS.h>
- FS& FlashFS = LittleFS;
- #endif
- #elif defined(ARDUINO_ARCH_ESP32)
- #include <SPIFFS.h>
- fs::SPIFFSFS& FlashFS = SPIFFS;
- #endif
- #define HELLO_URI "/hello"
- ACText(Caption, "Hello, world");
- ACRadio(Styles, {}, "");
- ACSubmit(Apply, "Apply", HELLO_URI);
- AutoConnectAux helloPage(HELLO_URI, "Hello", true, { Caption, Styles, Apply });
- AutoConnectConfig config;
- AutoConnect portal;
- String ElementJson;
- void loadParam(const char* fileName) {
- Serial.printf("Style %s ", fileName);
- File param = FlashFS.open(fileName, "r");
- if (param) {
- ElementJson = param.readString();
- param.close();
- Serial.printf("loaded:\n%s", ElementJson.c_str());
- }
- else
- Serial.println("open failed");
- }
- void onRoot() {
- WEBServer& webServer = portal.host();
- webServer.sendHeader("Location", String("http://") + webServer.client().localIP().toString() + String(HELLO_URI));
- webServer.send(302, "text/plain", "");
- webServer.client().flush();
- webServer.client().stop();
- }
- String onHello(AutoConnectAux& aux, PageArgument& args) {
-
- AutoConnectRadio& styles = helloPage["Styles"].as<AutoConnectRadio>();
- String styleName = styles.value();
- if (styleName.length())
- loadParam(styleName.c_str());
-
-
- styles.empty();
- #if defined(ARDUINO_ARCH_ESP32)
- File dir = FlashFS.open("/", "r");
- if (dir) {
- File parmFile = dir.openNextFile();
- while (parmFile) {
- if (!parmFile.isDirectory())
- styles.add(String(parmFile.name()));
- parmFile = dir.openNextFile();
- }
- }
- #elif defined(ARDUINO_ARCH_ESP8266)
- Dir dir = FlashFS.openDir("/");
- while (dir.next()) {
- if (!dir.isDirectory())
- styles.add(dir.fileName());
- }
- #endif
-
- helloPage.loadElement(ElementJson);
- return String();
- }
- void setup() {
- delay(1000);
- Serial.begin(115200);
- Serial.println();
- #if defined(ARDUINO_ARCH_ESP8266)
- FlashFS.begin();
- #elif defined(ARDUINO_ARCH_ESP32)
- FlashFS.begin(true);
- #endif
- helloPage.on(onHello);
- portal.join(helloPage);
- config.ota = AC_OTA_BUILTIN;
- portal.config(config);
- portal.begin();
- WEBServer& webServer = portal.host();
- webServer.on("/", onRoot);
- }
- void loop() {
- portal.handleClient();
- }
|