ESP_AsyncFSBrowser.ino 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include <ArduinoOTA.h>
  2. #ifdef ESP32
  3. #include <FS.h>
  4. #include <SPIFFS.h>
  5. #include <ESPmDNS.h>
  6. #include <WiFi.h>
  7. #include <AsyncTCP.h>
  8. #elif defined(ESP8266)
  9. #include <ESP8266WiFi.h>
  10. #include <ESPAsyncTCP.h>
  11. #include <ESP8266mDNS.h>
  12. #endif
  13. #include <ESPAsyncWebServer.h>
  14. #include <SPIFFSEditor.h>
  15. // SKETCH BEGIN
  16. AsyncWebServer server(80);
  17. AsyncWebSocket ws("/ws");
  18. AsyncEventSource events("/events");
  19. void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
  20. if(type == WS_EVT_CONNECT){
  21. Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
  22. client->printf("Hello Client %u :)", client->id());
  23. client->ping();
  24. } else if(type == WS_EVT_DISCONNECT){
  25. Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id());
  26. } else if(type == WS_EVT_ERROR){
  27. Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
  28. } else if(type == WS_EVT_PONG){
  29. Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
  30. } else if(type == WS_EVT_DATA){
  31. AwsFrameInfo * info = (AwsFrameInfo*)arg;
  32. String msg = "";
  33. if(info->final && info->index == 0 && info->len == len){
  34. //the whole message is in a single frame and we got all of it's data
  35. Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len);
  36. if(info->opcode == WS_TEXT){
  37. for(size_t i=0; i < info->len; i++) {
  38. msg += (char) data[i];
  39. }
  40. } else {
  41. char buff[3];
  42. for(size_t i=0; i < info->len; i++) {
  43. sprintf(buff, "%02x ", (uint8_t) data[i]);
  44. msg += buff ;
  45. }
  46. }
  47. Serial.printf("%s\n",msg.c_str());
  48. if(info->opcode == WS_TEXT)
  49. client->text("I got your text message");
  50. else
  51. client->binary("I got your binary message");
  52. } else {
  53. //message is comprised of multiple frames or the frame is split into multiple packets
  54. if(info->index == 0){
  55. if(info->num == 0)
  56. Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
  57. Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len);
  58. }
  59. Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len);
  60. if(info->opcode == WS_TEXT){
  61. for(size_t i=0; i < len; i++) {
  62. msg += (char) data[i];
  63. }
  64. } else {
  65. char buff[3];
  66. for(size_t i=0; i < len; i++) {
  67. sprintf(buff, "%02x ", (uint8_t) data[i]);
  68. msg += buff ;
  69. }
  70. }
  71. Serial.printf("%s\n",msg.c_str());
  72. if((info->index + len) == info->len){
  73. Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len);
  74. if(info->final){
  75. Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
  76. if(info->message_opcode == WS_TEXT)
  77. client->text("I got your text message");
  78. else
  79. client->binary("I got your binary message");
  80. }
  81. }
  82. }
  83. }
  84. }
  85. const char* ssid = "*******";
  86. const char* password = "*******";
  87. const char * hostName = "esp-async";
  88. const char* http_username = "admin";
  89. const char* http_password = "admin";
  90. void setup(){
  91. Serial.begin(115200);
  92. Serial.setDebugOutput(true);
  93. WiFi.mode(WIFI_AP_STA);
  94. WiFi.softAP(hostName);
  95. WiFi.begin(ssid, password);
  96. if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  97. Serial.printf("STA: Failed!\n");
  98. WiFi.disconnect(false);
  99. delay(1000);
  100. WiFi.begin(ssid, password);
  101. }
  102. //Send OTA events to the browser
  103. ArduinoOTA.onStart([]() { events.send("Update Start", "ota"); });
  104. ArduinoOTA.onEnd([]() { events.send("Update End", "ota"); });
  105. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  106. char p[32];
  107. sprintf(p, "Progress: %u%%\n", (progress/(total/100)));
  108. events.send(p, "ota");
  109. });
  110. ArduinoOTA.onError([](ota_error_t error) {
  111. if(error == OTA_AUTH_ERROR) events.send("Auth Failed", "ota");
  112. else if(error == OTA_BEGIN_ERROR) events.send("Begin Failed", "ota");
  113. else if(error == OTA_CONNECT_ERROR) events.send("Connect Failed", "ota");
  114. else if(error == OTA_RECEIVE_ERROR) events.send("Recieve Failed", "ota");
  115. else if(error == OTA_END_ERROR) events.send("End Failed", "ota");
  116. });
  117. ArduinoOTA.setHostname(hostName);
  118. ArduinoOTA.begin();
  119. MDNS.addService("http","tcp",80);
  120. SPIFFS.begin();
  121. ws.onEvent(onWsEvent);
  122. server.addHandler(&ws);
  123. events.onConnect([](AsyncEventSourceClient *client){
  124. client->send("hello!",NULL,millis(),1000);
  125. });
  126. server.addHandler(&events);
  127. #ifdef ESP32
  128. server.addHandler(new SPIFFSEditor(SPIFFS, http_username,http_password));
  129. #elif defined(ESP8266)
  130. server.addHandler(new SPIFFSEditor(http_username,http_password));
  131. #endif
  132. server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
  133. request->send(200, "text/plain", String(ESP.getFreeHeap()));
  134. });
  135. server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
  136. server.onNotFound([](AsyncWebServerRequest *request){
  137. Serial.printf("NOT_FOUND: ");
  138. if(request->method() == HTTP_GET)
  139. Serial.printf("GET");
  140. else if(request->method() == HTTP_POST)
  141. Serial.printf("POST");
  142. else if(request->method() == HTTP_DELETE)
  143. Serial.printf("DELETE");
  144. else if(request->method() == HTTP_PUT)
  145. Serial.printf("PUT");
  146. else if(request->method() == HTTP_PATCH)
  147. Serial.printf("PATCH");
  148. else if(request->method() == HTTP_HEAD)
  149. Serial.printf("HEAD");
  150. else if(request->method() == HTTP_OPTIONS)
  151. Serial.printf("OPTIONS");
  152. else
  153. Serial.printf("UNKNOWN");
  154. Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
  155. if(request->contentLength()){
  156. Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str());
  157. Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength());
  158. }
  159. int headers = request->headers();
  160. int i;
  161. for(i=0;i<headers;i++){
  162. AsyncWebHeader* h = request->getHeader(i);
  163. Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str());
  164. }
  165. int params = request->params();
  166. for(i=0;i<params;i++){
  167. AsyncWebParameter* p = request->getParam(i);
  168. if(p->isFile()){
  169. Serial.printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
  170. } else if(p->isPost()){
  171. Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
  172. } else {
  173. Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
  174. }
  175. }
  176. request->send(404);
  177. });
  178. server.onFileUpload([](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final){
  179. if(!index)
  180. Serial.printf("UploadStart: %s\n", filename.c_str());
  181. Serial.printf("%s", (const char*)data);
  182. if(final)
  183. Serial.printf("UploadEnd: %s (%u)\n", filename.c_str(), index+len);
  184. });
  185. server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){
  186. if(!index)
  187. Serial.printf("BodyStart: %u\n", total);
  188. Serial.printf("%s", (const char*)data);
  189. if(index + len == total)
  190. Serial.printf("BodyEnd: %u\n", total);
  191. });
  192. server.begin();
  193. }
  194. void loop(){
  195. ArduinoOTA.handle();
  196. ws.cleanupClients();
  197. }