Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.2 KiB

  1. /*
  2. JustWifi
  3. Wifi Manager for ESP8266
  4. Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "JustWifi.h"
  17. #include <functional>
  18. bool JustWifi::connected() {
  19. return (WiFi.status() == WL_CONNECTED);
  20. }
  21. bool JustWifi::autoConnect() {
  22. unsigned long timeout;
  23. // Return if already connected
  24. if (connected()) return true;
  25. // Try to connect to last successful network
  26. if (WiFi.SSID().length() > 0) {
  27. ETS_UART_INTR_DISABLE();
  28. wifi_station_disconnect();
  29. ETS_UART_INTR_ENABLE();
  30. _doCallback(MESSAGE_AUTO_CONNECTING, (char *) WiFi.SSID().c_str());
  31. WiFi.mode(WIFI_STA);
  32. WiFi.begin();
  33. // Check connection with timeout
  34. timeout = millis();
  35. while (millis() - timeout < _connect_timeout) {
  36. _doCallback(MESSAGE_CONNECT_WAITING);
  37. if (WiFi.status() == WL_CONNECTED) break;
  38. delay(100);
  39. }
  40. if (WiFi.status() == WL_CONNECTED) {
  41. _mode = MODE_STATION;
  42. _doCallback(MESSAGE_CONNECTED);
  43. return true;
  44. }
  45. _doCallback(MESSAGE_AUTO_FAILED);
  46. } else {
  47. _doCallback(MESSAGE_AUTO_NOSSID);
  48. }
  49. _mode = MODE_NONE;
  50. return false;
  51. }
  52. bool JustWifi::connect() {
  53. unsigned long timeout;
  54. // Return if already connected
  55. if (connected()) return true;
  56. // Loop through configured networks
  57. for (unsigned char i=0; i<_network_count; i++) {
  58. // Skip if no SSID defined
  59. if (_network[i].ssid.length() == 0) continue;
  60. // TODO: Static IP options here
  61. // Connect
  62. _doCallback(MESSAGE_CONNECTING, (char *) _network[i].ssid.c_str());
  63. WiFi.begin(_network[i].ssid.c_str(), _network[i].pass.c_str());
  64. // Check connection with timeout
  65. timeout = millis();
  66. while (millis() - timeout < _connect_timeout) {
  67. _doCallback(MESSAGE_CONNECT_WAITING);
  68. if (WiFi.status() == WL_CONNECTED) break;
  69. delay(100);
  70. }
  71. // Get out of the i loop if connected
  72. if (WiFi.status() == WL_CONNECTED) {
  73. break;
  74. } else {
  75. _mode = MODE_NONE;
  76. _doCallback(MESSAGE_CONNECT_FAILED, (char *) _network[i].ssid.c_str());
  77. }
  78. }
  79. if (WiFi.status() == WL_CONNECTED) {
  80. //WiFi.setAutoConnect(true);
  81. //WiFi.setAutoReconnect(true);
  82. _mode = MODE_STATION;
  83. _doCallback(MESSAGE_CONNECTED);
  84. return true;
  85. }
  86. return false;
  87. }
  88. bool JustWifi::startAP(char * ssid, char * pass) {
  89. // Return if already connected
  90. // if (connected()) return true;
  91. // TODO: Static IP options here
  92. _doCallback(MESSAGE_ACCESSPOINT_CREATING);
  93. WiFi.mode(WIFI_AP);
  94. if (strlen(pass) > 0) {
  95. if (strlen(pass) < 8 || strlen(pass) > 63) {
  96. _mode = MODE_NONE;
  97. _doCallback(MESSAGE_ACCESSPOINT_FAILED);
  98. return false;
  99. }
  100. WiFi.softAP(ssid, pass);
  101. } else {
  102. WiFi.softAP(ssid);
  103. }
  104. // TODO: Setup the DNS server redirecting all the queries to this IP
  105. _ssid = String(ssid);
  106. _mode = MODE_ACCESS_POINT;
  107. _doCallback(MESSAGE_ACCESSPOINT_CREATED);
  108. return true;
  109. }
  110. bool JustWifi::disconnect() {
  111. WiFi.disconnect(true);
  112. _mode = MODE_NONE;
  113. _doCallback(MESSAGE_DISCONNECTED);
  114. }
  115. bool JustWifi::cleanNetworks() {
  116. _network_count = 0;
  117. }
  118. bool JustWifi::addNetwork(char * ssid, char * pass) {
  119. if (_network_count == MAX_NETWORKS) return false;
  120. _network[_network_count].ssid = String(ssid);
  121. _network[_network_count].pass = String(pass);
  122. _network_count++;
  123. return true;
  124. }
  125. justwifi_mode_t JustWifi::getMode() {
  126. return _mode;
  127. }
  128. String JustWifi::getIP() {
  129. if (_mode == MODE_STATION) {
  130. return WiFi.localIP().toString();
  131. } else if (_mode == MODE_ACCESS_POINT) {
  132. return WiFi.softAPIP().toString();
  133. }
  134. return String("");
  135. }
  136. String JustWifi::getNetwork() {
  137. if (_mode == MODE_STATION) {
  138. return WiFi.SSID();
  139. }
  140. return _ssid;
  141. }
  142. void JustWifi::setConnectTimeout(unsigned long ms) {
  143. _connect_timeout = ms;
  144. }
  145. void JustWifi::onMessage(TMessageFunction fn) {
  146. _callback = fn;
  147. }
  148. void JustWifi::loop() {
  149. if ((WiFi.status() != WL_CONNECTED) && (_mode == MODE_STATION)) {
  150. _mode = MODE_NONE;
  151. _doCallback(MESSAGE_DISCONNECTED);
  152. }
  153. if ((WiFi.status() == WL_CONNECTED) && (_mode != MODE_STATION)) {
  154. _mode = MODE_STATION;
  155. _doCallback(MESSAGE_CONNECTED);
  156. }
  157. }
  158. void JustWifi::_doCallback(justwifi_messages_t message, char * parameter) {
  159. if (_callback != NULL) _callback(message, parameter);
  160. }