spiffs_rest_api_nonblocking.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <WiFiClient.h>
  17. //#include <ESP8266WebServer.h>
  18. #include <ESP8266mDNS.h>
  19. #include <ESP8266HTTPUpdateServer.h>
  20. //#include <PubSubClient.h>
  21. //#include <EasySSDP.h> // http://ryandowning.net/EasySSDP/
  22. //extension of ESP8266WebServer with SPIFFS handlers built in
  23. #include <SPIFFSReadServer.h> // http://ryandowning.net/SPIFFSReadServer/
  24. // upload data folder to chip with Arduino ESP8266 filesystem uploader
  25. // https://github.com/esp8266/arduino-esp8266fs-plugin
  26. #include <DNSServer.h>
  27. #include <FS.h>
  28. #define DEVICE_NAME "ESP8266 DEVICE"
  29. //server objects
  30. SPIFFSReadServer server(80);
  31. DNSServer dnsServer;
  32. PersWiFiManager persWM(server, dnsServer);
  33. ESP8266HTTPUpdateServer httpUpdater;
  34. ////// Sample program data
  35. int x;
  36. String y;
  37. float z;
  38. void setup() {
  39. DEBUG_BEGIN; //for terminal debugging
  40. DEBUG_PRINT();
  41. //optional code handlers to run everytime wifi is connected...
  42. persWM.onConnect([]() {
  43. DEBUG_PRINT("wifi connected");
  44. DEBUG_PRINT(WiFi.SSID());
  45. DEBUG_PRINT(WiFi.localIP());
  46. //EasySSDP::begin(server);
  47. });
  48. //...or AP mode is started
  49. persWM.onAp([](){
  50. DEBUG_PRINT("AP MODE");
  51. DEBUG_PRINT(persWM.getApSsid());
  52. });
  53. //allows serving of files from SPIFFS
  54. SPIFFS.begin();
  55. //sets network name for AP mode
  56. persWM.setApCredentials(DEVICE_NAME);
  57. //persWM.setApCredentials(DEVICE_NAME, "password"); optional password
  58. //make connecting/disconnecting non-blocking
  59. persWM.setConnectNonBlock(true);
  60. //in non-blocking mode, program will continue past this point without waiting
  61. persWM.begin();
  62. //handles commands from webpage, sends live data in JSON format
  63. server.on("/api", []() {
  64. DEBUG_PRINT("server.on /api");
  65. if (server.hasArg("x")) {
  66. x = server.arg("x").toInt();
  67. DEBUG_PRINT(String("x: ") + x);
  68. } //if
  69. if (server.hasArg("y")) {
  70. y = server.arg("y");
  71. DEBUG_PRINT("y: " + y);
  72. } //if
  73. if (server.hasArg("z")) {
  74. z = server.arg("z").toFloat();;
  75. //DEBUG_PRINT("z: " + z);
  76. } //if
  77. //build json object of program data
  78. StaticJsonBuffer<200> jsonBuffer;
  79. JsonObject &json = jsonBuffer.createObject();
  80. json["x"] = x;
  81. json["y"] = y;
  82. json["z"] = z;
  83. json["ssid"] = WiFi.SSID();
  84. char jsonchar[200];
  85. json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
  86. server.send(200, "application/json", jsonchar);
  87. }); //server.on api
  88. // HTTP Updater at /update
  89. httpUpdater.setup(&server);
  90. server.begin();
  91. DEBUG_PRINT("setup complete.");
  92. DEBUG_PRINT("Hodensack!");
  93. } //void setup
  94. void loop() {
  95. //in non-blocking mode, handleWiFi must be called in the main loop
  96. persWM.handleWiFi();
  97. dnsServer.processNextRequest();
  98. server.handleClient();
  99. //DEBUG_PRINT(millis());
  100. // do stuff with x and y
  101. } //void loop