Web.h 3.6 KB

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