Web.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef Web_h
  2. #define Web_h
  3. /*
  4. Code taken from espressif ESP32 OTA Update example
  5. */
  6. #include "configs.h"
  7. #include <WiFi.h>
  8. #include <WiFiClient.h>
  9. #include <WebServer.h>
  10. #include <ESPmDNS.h>
  11. #include <Update.h>
  12. #include "Assets.h"
  13. #ifdef HAS_SCREEN
  14. #include "Display.h"
  15. #endif
  16. #include "WiFiScan.h"
  17. #ifdef HAS_SCREEN
  18. extern Display display_obj;
  19. #endif
  20. extern WiFiScan wifi_scan_obj;
  21. class Web
  22. {
  23. private:
  24. PROGMEM const char* host = "esp32marauder";
  25. PROGMEM const char* ssid = "MarauderOTA";
  26. PROGMEM const char* password = "justcallmekoko";
  27. bool serving = false;
  28. int num_sta = 0;
  29. PROGMEM const char* loginIndex =
  30. "<form name='loginForm'>"
  31. "<table width='20%' bgcolor='A09F9F' align='center'>"
  32. "<tr>"
  33. "<td colspan=2>"
  34. "<center><font size=4><b>ESP32 Login Page</b></font></center>"
  35. "<br>"
  36. "</td>"
  37. "<br>"
  38. "<br>"
  39. "</tr>"
  40. "<td>Username:</td>"
  41. "<td><input type='text' size=25 name='userid'><br></td>"
  42. "</tr>"
  43. "<br>"
  44. "<br>"
  45. "<tr>"
  46. "<td>Password:</td>"
  47. "<td><input type='Password' size=25 name='pwd'><br></td>"
  48. "<br>"
  49. "<br>"
  50. "</tr>"
  51. "<tr>"
  52. "<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
  53. "</tr>"
  54. "</table>"
  55. "</form>"
  56. "<script>"
  57. "function check(form)"
  58. "{"
  59. "if(form.userid.value=='admin' && form.pwd.value=='admin')"
  60. "{"
  61. "window.open('/serverIndex')"
  62. "}"
  63. "else"
  64. "{"
  65. " alert('Error Password or Username')/*displays error message*/"
  66. "}"
  67. "}"
  68. "</script>";
  69. /*
  70. * Server Index Page
  71. */
  72. PROGMEM const char* serverIndex =
  73. "<script src='/jquery.min.js'></script>"
  74. "Because the lack of an asynchronous webserver in this Arduino sketch like 'ESPAsyncWebServer', <br/>"
  75. "both file 'serverIndex' and 'jquery.min.js' can't be read from the webserver at the same time. <br/><br/>"
  76. "Your web browser probably requests those two files simultaneously and therefore <br/>"
  77. "the javascript file failed to load. By a refresh of this page, the browser cash has already <br/>"
  78. "load 'serverIndex' file, the web browser will do a second attempt to only read the javascript file. <br/>"
  79. "This second attempt, with an idle webserver, will be processed.<br/><br/>"
  80. "Long story short, press F5 (refresh web browser) before uploading your firmware. <br/><br/>"
  81. "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
  82. "<input type='file' name='update'>"
  83. "<input type='submit' value='Update'>"
  84. "</form>"
  85. "<div id='prg'>progress: 0%</div>"
  86. "<script>"
  87. "$('form').submit(function(e){"
  88. "e.preventDefault();"
  89. "var form = $('#upload_form')[0];"
  90. "var data = new FormData(form);"
  91. " $.ajax({"
  92. "url: '/update',"
  93. "type: 'POST',"
  94. "data: data,"
  95. "contentType: false,"
  96. "processData:false,"
  97. "xhr: function() {"
  98. "var xhr = new window.XMLHttpRequest();"
  99. "xhr.upload.addEventListener('progress', function(evt) {"
  100. "if (evt.lengthComputable) {"
  101. "var per = evt.loaded / evt.total;"
  102. "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
  103. "}"
  104. "}, false);"
  105. "return xhr;"
  106. "},"
  107. "success:function(d, s) {"
  108. "console.log('success!')"
  109. "},"
  110. "error: function (a, b, c) {"
  111. "}"
  112. "});"
  113. "});"
  114. "</script>";
  115. public:
  116. Web();
  117. void main();
  118. PROGMEM static void onJavaScript();
  119. void setupOTAupdate();
  120. void shutdownServer();
  121. };
  122. #endif