captive_portal.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "captive_portal.h"
  2. #include "esphome/core/log.h"
  3. #include "esphome/core/application.h"
  4. #include "esphome/components/wifi/wifi_component.h"
  5. namespace esphome {
  6. namespace captive_portal {
  7. static const char *TAG = "captive_portal";
  8. void CaptivePortal::handle_index(AsyncWebServerRequest *request) {
  9. AsyncResponseStream *stream = request->beginResponseStream("text/html");
  10. stream->print(F("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" "
  11. "content=\"width=device-width,initial-scale=1,user-scalable=no\"/><title>"));
  12. stream->print(App.get_name().c_str());
  13. stream->print(F("</title><link rel=\"stylesheet\" href=\"/stylesheet.css\">"));
  14. stream->print(F("<script>function c(l){document.getElementById('ssid').value=l.innerText||l.textContent; "
  15. "document.getElementById('psk').focus();}</script>"));
  16. stream->print(F("</head>"));
  17. stream->print(F("<body><div class=\"main\"><h1>WiFi Networks</h1>"));
  18. if (request->hasArg("save")) {
  19. stream->print(F("<div class=\"info\">The ESP will now try to connect to the network...<br/>Please give it some "
  20. "time to connect.<br/>Note: Copy the changed network to your YAML file - the next OTA update will "
  21. "overwrite these settings.</div>"));
  22. }
  23. for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
  24. if (scan.get_is_hidden())
  25. continue;
  26. stream->print(F("<div class=\"network\" onclick=\"c(this)\"><a href=\"#\" class=\"network-left\">"));
  27. if (scan.get_rssi() >= -50) {
  28. stream->print(F("<img src=\"/wifi-strength-4.svg\">"));
  29. } else if (scan.get_rssi() >= -65) {
  30. stream->print(F("<img src=\"/wifi-strength-3.svg\">"));
  31. } else if (scan.get_rssi() >= -85) {
  32. stream->print(F("<img src=\"/wifi-strength-2.svg\">"));
  33. } else {
  34. stream->print(F("<img src=\"/wifi-strength-1.svg\">"));
  35. }
  36. stream->print(F("<span class=\"network-ssid\">"));
  37. stream->print(scan.get_ssid().c_str());
  38. stream->print(F("</span></a>"));
  39. if (scan.get_with_auth()) {
  40. stream->print(F("<img src=\"/lock.svg\">"));
  41. }
  42. stream->print(F("</div>"));
  43. }
  44. stream->print(F("<h3>WiFi Settings</h3><form method=\"GET\" action=\"/wifisave\"><input id=\"ssid\" name=\"ssid\" "
  45. "length=32 placeholder=\"SSID\"><br/><input id=\"psk\" name=\"psk\" length=64 type=\"password\" "
  46. "placeholder=\"Password\"><br/><br/><button type=\"submit\">Save</button></form><br><hr><br>"));
  47. stream->print(F("<h1>OTA Update</h1><form method=\"POST\" action=\"/update\" enctype=\"multipart/form-data\"><input "
  48. "type=\"file\" name=\"update\"><button type=\"submit\">Update</button></form>"));
  49. stream->print(F("</div></body></html>"));
  50. request->send(stream);
  51. }
  52. void CaptivePortal::handle_wifisave(AsyncWebServerRequest *request) {
  53. std::string ssid = request->arg("ssid").c_str();
  54. std::string psk = request->arg("psk").c_str();
  55. ESP_LOGI(TAG, "Captive Portal Requested WiFi Settings Change:");
  56. ESP_LOGI(TAG, " SSID='%s'", ssid.c_str());
  57. ESP_LOGI(TAG, " Password=" LOG_SECRET("'%s'"), psk.c_str());
  58. this->override_sta_(ssid, psk);
  59. request->redirect("/?save=true");
  60. }
  61. void CaptivePortal::override_sta_(const std::string &ssid, const std::string &password) {
  62. CaptivePortalSettings save{};
  63. strcpy(save.ssid, ssid.c_str());
  64. strcpy(save.password, password.c_str());
  65. this->pref_.save(&save);
  66. wifi::WiFiAP sta{};
  67. sta.set_ssid(ssid);
  68. sta.set_password(password);
  69. wifi::global_wifi_component->set_sta(sta);
  70. }
  71. void CaptivePortal::setup() {
  72. // Hash with compilation time
  73. // This ensures the AP override is not applied for OTA
  74. uint32_t hash = fnv1_hash(App.get_compilation_time());
  75. this->pref_ = global_preferences.make_preference<CaptivePortalSettings>(hash, true);
  76. CaptivePortalSettings save{};
  77. if (this->pref_.load(&save)) {
  78. this->override_sta_(save.ssid, save.password);
  79. }
  80. }
  81. void CaptivePortal::start() {
  82. this->base_->init();
  83. if (!this->initialized_) {
  84. this->base_->add_handler(this);
  85. this->base_->add_ota_handler();
  86. }
  87. this->dns_server_ = new DNSServer();
  88. this->dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
  89. IPAddress ip = wifi::global_wifi_component->wifi_soft_ap_ip();
  90. this->dns_server_->start(53, "*", ip);
  91. this->base_->get_server()->onNotFound([this](AsyncWebServerRequest *req) {
  92. bool not_found = false;
  93. if (!this->active_) {
  94. not_found = true;
  95. } else if (req->host() == wifi::global_wifi_component->wifi_soft_ap_ip().toString()) {
  96. not_found = true;
  97. }
  98. if (not_found) {
  99. req->send(404, "text/html", "File not found");
  100. return;
  101. }
  102. auto url = "http://" + wifi::global_wifi_component->wifi_soft_ap_ip().toString();
  103. req->redirect(url);
  104. });
  105. this->initialized_ = true;
  106. this->active_ = true;
  107. }
  108. const char STYLESHEET_CSS[] PROGMEM =
  109. R"(*{box-sizing:inherit}div,input{padding:5px;font-size:1em}input{width:95%}body{text-align:center;font-family:sans-serif}button{border:0;border-radius:.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;padding:0}.main{text-align:left;display:inline-block;min-width:260px}.network{display:flex;justify-content:space-between;align-items:center}.network-left{display:flex;align-items:center}.network-ssid{margin-bottom:-7px;margin-left:10px}.info{border:1px solid;margin:10px 0;padding:15px 10px;color:#4f8a10;background-color:#dff2bf})";
  110. const char LOCK_SVG[] PROGMEM =
  111. R"(<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"><path d="M12 17a2 2 0 0 0 2-2 2 2 0 0 0-2-2 2 2 0 0 0-2 2 2 2 0 0 0 2 2m6-9a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V10a2 2 0 0 1 2-2h1V6a5 5 0 0 1 5-5 5 5 0 0 1 5 5v2h1m-6-5a3 3 0 0 0-3 3v2h6V6a3 3 0 0 0-3-3z"/></svg>)";
  112. void CaptivePortal::handleRequest(AsyncWebServerRequest *req) {
  113. if (req->url() == "/") {
  114. this->handle_index(req);
  115. return;
  116. } else if (req->url() == "/wifisave") {
  117. this->handle_wifisave(req);
  118. return;
  119. } else if (req->url() == "/stylesheet.css") {
  120. req->send_P(200, "text/css", STYLESHEET_CSS);
  121. return;
  122. } else if (req->url() == "/lock.svg") {
  123. req->send_P(200, "image/svg+xml", LOCK_SVG);
  124. return;
  125. }
  126. AsyncResponseStream *stream = req->beginResponseStream("image/svg+xml");
  127. stream->print(F("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\"><path d=\"M12 3A18.9 18.9 0 0 "
  128. "0 .38 7C4.41 12.06 7.89 16.37 12 21.5L23.65 7C20.32 4.41 16.22 3 12 "));
  129. if (req->url() == "/wifi-strength-4.svg") {
  130. stream->print(F("3z"));
  131. } else {
  132. if (req->url() == "/wifi-strength-1.svg") {
  133. stream->print(F("3m0 2c3.07 0 6.09.86 8.71 2.45l-5.1 6.36a8.43 8.43 0 0 0-7.22-.01L3.27 7.4"));
  134. } else if (req->url() == "/wifi-strength-2.svg") {
  135. stream->print(F("3m0 2c3.07 0 6.09.86 8.71 2.45l-3.21 3.98a11.32 11.32 0 0 0-11 0L3.27 7.4"));
  136. } else if (req->url() == "/wifi-strength-3.svg") {
  137. stream->print(F("3m0 2c3.07 0 6.09.86 8.71 2.45l-1.94 2.43A13.6 13.6 0 0 0 12 8C9 8 6.68 9 5.21 9.84l-1.94-2."));
  138. }
  139. stream->print(F("4A16.94 16.94 0 0 1 12 5z"));
  140. }
  141. stream->print(F("\"/></svg>"));
  142. req->send(stream);
  143. }
  144. CaptivePortal::CaptivePortal(web_server_base::WebServerBase *base) : base_(base) { global_captive_portal = this; }
  145. float CaptivePortal::get_setup_priority() const {
  146. // Before WiFi
  147. return setup_priority::WIFI + 1.0f;
  148. }
  149. void CaptivePortal::dump_config() { ESP_LOGCONFIG(TAG, "Captive Portal:"); }
  150. CaptivePortal *global_captive_portal = nullptr;
  151. } // namespace captive_portal
  152. } // namespace esphome