HelloWorld.ino 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. HelloWorld.ino, Example for the AutoConnect library.
  3. Copyright (c) 2020, Hieromon Ikasamo
  4. https://github.com/Hieromon/AutoConnect
  5. This software is released under the MIT License.
  6. https://opensource.org/licenses/MIT
  7. */
  8. /*
  9. To experience this example, upload the JSON file which is style.json
  10. from the data folder. Its file contains the attributes for the Caption
  11. of AutoConnectText.
  12. Let compile the Sketch and upload it to the ESP module. At that time,
  13. don't forget to upload green.json and tomato.json placed in the data
  14. folder.
  15. Now let's run the sketch. You can see the Hello, world screen by
  16. accessing the IP address of the ESP module from a web browser. If you
  17. apply green.json or tomato.json displayed on the same screen, the text
  18. color will change.
  19. There, you will find the Update appearing in the AutoConnect menu.
  20. Then prepare a new JSON document with the text editor you're used to.
  21. You can easily create a new style definition by using the green.json
  22. included in this example as a template.
  23. Next, select the Update menu in your browser to navigate to the upload
  24. screen. What you upload is the new style definition JSON file you just
  25. created. Let's display the Hello screen again. You will see the new
  26. file just uploaded will be added. It is the convenience of OTA updates
  27. enhanced with AutoCOnnect v1.2.0.
  28. */
  29. #if defined(ARDUINO_ARCH_ESP8266)
  30. #include <ESP8266WiFi.h>
  31. #include <ESP8266WebServer.h>
  32. typedef ESP8266WebServer WEBServer;
  33. #elif defined(ARDUINO_ARCH_ESP32)
  34. #include <WiFi.h>
  35. #include <WebServer.h>
  36. typedef WebServer WEBServer;
  37. #endif
  38. #include <AutoConnect.h>
  39. /*
  40. AC_USE_SPIFFS indicates SPIFFS or LittleFS as available file systems that
  41. will become the AUTOCONNECT_USE_SPIFFS identifier and is exported as showing
  42. the valid file system. After including AutoConnect.h, the Sketch can determine
  43. whether to use FS.h or LittleFS.h by AUTOCONNECT_USE_SPIFFS definition.
  44. */
  45. #include <FS.h>
  46. #if defined(ARDUINO_ARCH_ESP8266)
  47. #ifdef AUTOCONNECT_USE_SPIFFS
  48. FS& FlashFS = SPIFFS;
  49. #else
  50. #include <LittleFS.h>
  51. FS& FlashFS = LittleFS;
  52. #endif
  53. #elif defined(ARDUINO_ARCH_ESP32)
  54. #include <SPIFFS.h>
  55. fs::SPIFFSFS& FlashFS = SPIFFS;
  56. #endif
  57. #define HELLO_URI "/hello"
  58. // Declare AutoConnectText with only a value.
  59. // Qualify the Caption by reading style attributes from the style.json file.
  60. ACText(Caption, "Hello, world");
  61. ACRadio(Styles, {}, "");
  62. ACSubmit(Apply, "Apply", HELLO_URI);
  63. //AutoConnectAux for the custom Web page.
  64. AutoConnectAux helloPage(HELLO_URI, "Hello", true, { Caption, Styles, Apply });
  65. AutoConnectConfig config;
  66. AutoConnect portal;
  67. // JSON document loading buffer
  68. String ElementJson;
  69. // Load the element from specified file in the flash on board.
  70. void loadParam(const char* fileName) {
  71. Serial.printf("Style %s ", fileName);
  72. File param = FlashFS.open(fileName, "r");
  73. if (param) {
  74. ElementJson = param.readString();
  75. param.close();
  76. Serial.printf("loaded:\n%s", ElementJson.c_str());
  77. }
  78. else
  79. Serial.println("open failed");
  80. }
  81. // Redirects from root to the hello page.
  82. void onRoot() {
  83. WEBServer& webServer = portal.host();
  84. webServer.sendHeader("Location", String("http://") + webServer.client().localIP().toString() + String(HELLO_URI));
  85. webServer.send(302, "text/plain", "");
  86. webServer.client().flush();
  87. webServer.client().stop();
  88. }
  89. // Load the attribute of the element to modify at runtime from external.
  90. String onHello(AutoConnectAux& aux, PageArgument& args) {
  91. // Select the style parameter file and load it into the text element.
  92. AutoConnectRadio& styles = helloPage["Styles"].as<AutoConnectRadio>();
  93. String styleName = styles.value();
  94. if (styleName.length())
  95. loadParam(styleName.c_str());
  96. // List parameter files stored on the flash.
  97. // Those files need to be uploaded to the filesystem in advance.
  98. styles.empty();
  99. #if defined(ARDUINO_ARCH_ESP32)
  100. File dir = FlashFS.open("/", "r");
  101. if (dir) {
  102. File parmFile = dir.openNextFile();
  103. while (parmFile) {
  104. if (!parmFile.isDirectory())
  105. styles.add(String(parmFile.name()));
  106. parmFile = dir.openNextFile();
  107. }
  108. }
  109. #elif defined(ARDUINO_ARCH_ESP8266)
  110. Dir dir = FlashFS.openDir("/");
  111. while (dir.next()) {
  112. if (!dir.isDirectory())
  113. styles.add(dir.fileName());
  114. }
  115. #endif
  116. // Apply picked style
  117. helloPage.loadElement(ElementJson);
  118. return String();
  119. }
  120. void setup() {
  121. delay(1000);
  122. Serial.begin(115200);
  123. Serial.println();
  124. #if defined(ARDUINO_ARCH_ESP8266)
  125. FlashFS.begin();
  126. #elif defined(ARDUINO_ARCH_ESP32)
  127. FlashFS.begin(true);
  128. #endif
  129. helloPage.on(onHello); // Register the attribute overwrite handler.
  130. portal.join(helloPage); // Join the hello page.
  131. config.ota = AC_OTA_BUILTIN;
  132. portal.config(config);
  133. portal.begin();
  134. WEBServer& webServer = portal.host();
  135. webServer.on("/", onRoot); // Register the root page redirector.
  136. }
  137. void loop() {
  138. portal.handleClient();
  139. }