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.

200 lines
4.8 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. // Return if already connected
  23. if (connected()) return true;
  24. // Try to connect to last successful network
  25. WiFi.mode(WIFI_STA);
  26. if (WiFi.SSID()) {
  27. _doCallback(MESSAGE_AUTO_CONNECTING, (char *) WiFi.SSID().c_str());
  28. ETS_UART_INTR_DISABLE();
  29. wifi_station_disconnect();
  30. ETS_UART_INTR_ENABLE();
  31. WiFi.begin();
  32. if (WiFi.status() == WL_CONNECTED) {
  33. _mode = MODE_STATION;
  34. _doCallback(MESSAGE_CONNECTED);
  35. return true;
  36. }
  37. _doCallback(MESSAGE_AUTO_FAILED);
  38. } else {
  39. _doCallback(MESSAGE_AUTO_NOSSID);
  40. }
  41. _mode = MODE_NONE;
  42. return false;
  43. }
  44. bool JustWifi::connect() {
  45. unsigned long timeout;
  46. // Return if already connected
  47. if (connected()) return true;
  48. // Loop through configured networks
  49. for (unsigned char i=0; i<_network_count; i++) {
  50. // TODO: Static IP options here
  51. // Connect
  52. _doCallback(MESSAGE_CONNECTING, (char *) _network[i].ssid.c_str());
  53. WiFi.begin(_network[i].ssid.c_str(), _network[i].pass.c_str());
  54. // Check connection with timeout
  55. timeout = millis();
  56. while (millis() - timeout < _connect_timeout) {
  57. _doCallback(MESSAGE_CONNECT_WAITING);
  58. if (WiFi.status() == WL_CONNECTED) break;
  59. delay(100);
  60. }
  61. // Get out of the i loop if connected
  62. if (WiFi.status() == WL_CONNECTED) {
  63. break;
  64. } else {
  65. _mode = MODE_NONE;
  66. _doCallback(MESSAGE_CONNECT_FAILED, (char *) _network[i].ssid.c_str());
  67. }
  68. }
  69. if (WiFi.status() == WL_CONNECTED) {
  70. //WiFi.setAutoConnect(true);
  71. //WiFi.setAutoReconnect(true);
  72. _mode = MODE_STATION;
  73. _doCallback(MESSAGE_CONNECTED);
  74. return true;
  75. }
  76. return false;
  77. }
  78. bool JustWifi::startAP(char * ssid, char * pass) {
  79. // Return if already connected
  80. // if (connected()) return true;
  81. // TODO: Static IP options here
  82. _doCallback(MESSAGE_ACCESSPOINT_CREATING);
  83. WiFi.mode(WIFI_AP);
  84. if (strlen(pass) > 0) {
  85. if (strlen(pass) < 8 || strlen(pass) > 63) {
  86. _mode = MODE_NONE;
  87. _doCallback(MESSAGE_ACCESSPOINT_FAILED);
  88. return false;
  89. }
  90. WiFi.softAP(ssid, pass);
  91. } else {
  92. WiFi.softAP(ssid);
  93. }
  94. // TODO: Setup the DNS server redirecting all the queries to this IP
  95. _ssid = String(ssid);
  96. _mode = MODE_ACCESS_POINT;
  97. _doCallback(MESSAGE_ACCESSPOINT_CREATED);
  98. return true;
  99. }
  100. bool JustWifi::disconnect() {
  101. WiFi.disconnect(true);
  102. _mode = MODE_NONE;
  103. _doCallback(MESSAGE_DISCONNECTED);
  104. }
  105. bool JustWifi::cleanNetworks() {
  106. _network_count = 0;
  107. }
  108. bool JustWifi::addNetwork(char * ssid, char * pass) {
  109. if (_network_count == MAX_NETWORKS) return false;
  110. _network[_network_count].ssid = String(ssid);
  111. _network[_network_count].pass = String(pass);
  112. _network_count++;
  113. return true;
  114. }
  115. justwifi_mode_t JustWifi::getMode() {
  116. return _mode;
  117. }
  118. String JustWifi::getIP() {
  119. if (_mode == MODE_STATION) {
  120. return WiFi.localIP().toString();
  121. } else if (_mode == MODE_ACCESS_POINT) {
  122. return WiFi.softAPIP().toString();
  123. }
  124. return String("");
  125. }
  126. String JustWifi::getNetwork() {
  127. if (_mode == MODE_STATION) {
  128. return WiFi.SSID();
  129. }
  130. return _ssid;
  131. }
  132. void JustWifi::setConnectTimeout(unsigned long ms) {
  133. _connect_timeout = ms;
  134. }
  135. void JustWifi::onMessage(TMessageFunction fn) {
  136. _callback = fn;
  137. }
  138. void JustWifi::loop() {
  139. if ((WiFi.status() != WL_CONNECTED) && (_mode == MODE_STATION)) {
  140. _mode = MODE_NONE;
  141. _doCallback(MESSAGE_DISCONNECTED);
  142. }
  143. if ((WiFi.status() == WL_CONNECTED) && (_mode != MODE_STATION)) {
  144. _mode = MODE_STATION;
  145. _doCallback(MESSAGE_CONNECTED);
  146. }
  147. }
  148. void JustWifi::_doCallback(justwifi_messages_t message, char * parameter) {
  149. if (_callback != NULL) _callback(message, parameter);
  150. }