spiffs_rest_api_nonblocking.ino 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. SPIFFS-served REST API example for PersWiFiManager v3.0
  3. */
  4. #define DEBUG_SERIAL //uncomment for Serial debugging statements
  5. #ifdef DEBUG_SERIAL
  6. #define DEBUG_BEGIN Serial.begin(115200)
  7. #define DEBUG_PRINT(x) Serial.println(x)
  8. #else
  9. #define DEBUG_PRINT(x)
  10. #define DEBUG_BEGIN
  11. #endif
  12. //includes
  13. #include <PersWiFiManager.h>
  14. #include <ArduinoJson.h>
  15. #include <ESP8266WiFi.h>
  16. #include <EasySSDP.h> // http://ryandowning.net/EasySSDP/
  17. //extension of ESP8266WebServer with SPIFFS handlers built in
  18. #include <SPIFFSReadServer.h> // http://ryandowning.net/SPIFFSReadServer/
  19. // upload data folder to chip with Arduino ESP8266 filesystem uploader
  20. // https://github.com/esp8266/arduino-esp8266fs-plugin
  21. #include <DNSServer.h>
  22. #include <FS.h>
  23. #define DEVICE_NAME "ESP8266 DEVICE"
  24. //server objects
  25. SPIFFSReadServer server(80);
  26. DNSServer dnsServer;
  27. PersWiFiManager persWM(server, dnsServer);
  28. ////// Sample program data
  29. int x;
  30. String y;
  31. void setup() {
  32. DEBUG_BEGIN; //for terminal debugging
  33. DEBUG_PRINT();
  34. //optional code handlers to run everytime wifi is connected...
  35. persWM.onConnect([]() {
  36. DEBUG_PRINT("wifi connected");
  37. DEBUG_PRINT(WiFi.localIP());
  38. EasySSDP::begin(server);
  39. });
  40. //...or AP mode is started
  41. persWM.onAp([](){
  42. DEBUG_PRINT("AP MODE");
  43. DEBUG_PRINT(persWM.getApSsid());
  44. });
  45. //allows serving of files from SPIFFS
  46. SPIFFS.begin();
  47. //sets network name for AP mode
  48. persWM.setApCredentials(DEVICE_NAME);
  49. //persWM.setApCredentials(DEVICE_NAME, "password"); optional password
  50. //make connecting/disconnecting non-blocking
  51. persWM.setConnectNonBlock(true);
  52. //in non-blocking mode, program will continue past this point without waiting
  53. persWM.begin();
  54. //handles commands from webpage, sends live data in JSON format
  55. server.on("/api", []() {
  56. DEBUG_PRINT("server.on /api");
  57. if (server.hasArg("x")) {
  58. x = server.arg("x").toInt();
  59. DEBUG_PRINT(String("x: ") + x);
  60. } //if
  61. if (server.hasArg("y")) {
  62. y = server.arg("y");
  63. DEBUG_PRINT("y: " + y);
  64. } //if
  65. //build json object of program data
  66. StaticJsonBuffer<200> jsonBuffer;
  67. JsonObject &json = jsonBuffer.createObject();
  68. json["x"] = x;
  69. json["y"] = y;
  70. char jsonchar[200];
  71. json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
  72. server.send(200, "application/json", jsonchar);
  73. }); //server.on api
  74. server.begin();
  75. DEBUG_PRINT("setup complete.");
  76. } //void setup
  77. void loop() {
  78. //in non-blocking mode, handleWiFi must be called in the main loop
  79. persWM.handleWiFi();
  80. dnsServer.processNextRequest();
  81. server.handleClient();
  82. //DEBUG_PRINT(millis());
  83. // do stuff with x and y
  84. } //void loop