ElegantOTA.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef ElegantOTA_h
  2. #define ElegantOTA_h
  3. #if defined(ESP8266)
  4. #include "Arduino.h"
  5. #include "stdlib_noniso.h"
  6. #include "elegantWebpage.h"
  7. #endif
  8. #if defined(ESP8266)
  9. #define HARDWARE "ESP8266"
  10. #include "ESP8266WiFi.h"
  11. #include "WiFiClient.h"
  12. #include "ESP8266WebServer.h"
  13. #include "ESP8266HTTPUpdateServer.h"
  14. #endif
  15. class ElegantOtaClass{
  16. public:
  17. #if defined(ESP8266)
  18. void begin(ESP8266WebServer *server, const char * username = "", const char * password = ""){
  19. _server = server;
  20. // If a username is actually given use authentication
  21. if(0 < strlen(username)){
  22. _username = username;
  23. _password = password;
  24. _server->on("/update", HTTP_GET, [&](){
  25. if (!_server->authenticate(_username, _password)) {
  26. return _server->requestAuthentication();
  27. }
  28. _server->sendHeader("Content-Encoding", "gzip");
  29. _server->send_P(200, "text/html", (const char*)ELEGANT_HTML, ELEGANT_HTML_SIZE);
  30. });
  31. } else {
  32. _server->on("/update", HTTP_GET, [&](){
  33. _server->sendHeader("Content-Encoding", "gzip");
  34. _server->send_P(200, "text/html", (const char*)ELEGANT_HTML, ELEGANT_HTML_SIZE);
  35. });
  36. }
  37. _httpUpdater.setup(server);
  38. }
  39. #endif
  40. private:
  41. #if defined(ESP8266)
  42. ESP8266WebServer *_server;
  43. ESP8266HTTPUpdateServer _httpUpdater;
  44. const char * _username;
  45. const char * _password;
  46. #endif
  47. };
  48. ElegantOtaClass ElegantOTA;
  49. #endif