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.

231 lines
6.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. WIFI MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "JustWifi.h"
  6. #if ENABLE_CAPTIVE_PORTAL
  7. #include <DNSServer.h>
  8. DNSServer dnsServer;
  9. #endif
  10. // -----------------------------------------------------------------------------
  11. // WIFI
  12. // -----------------------------------------------------------------------------
  13. String getIP() {
  14. if (WiFi.getMode() == WIFI_AP) {
  15. return WiFi.softAPIP().toString();
  16. }
  17. return WiFi.localIP().toString();
  18. }
  19. String getNetwork() {
  20. if (WiFi.getMode() == WIFI_AP) {
  21. return jw.getAPSSID();
  22. }
  23. return WiFi.SSID();
  24. }
  25. void wifiDisconnect() {
  26. #if ENABLE_POW
  27. powEnable(false);
  28. #endif
  29. jw.disconnect();
  30. }
  31. void resetConnectionTimeout() {
  32. jw.resetReconnectTimeout();
  33. }
  34. bool wifiConnected() {
  35. return jw.connected();
  36. }
  37. bool createAP() {
  38. jw.disconnect();
  39. jw.resetReconnectTimeout();
  40. return jw.createAP();
  41. }
  42. void wifiConfigure() {
  43. jw.setHostname(getSetting("hostname", HOSTNAME).c_str());
  44. jw.setSoftAP(getSetting("hostname", HOSTNAME).c_str(), getSetting("adminPass", ADMIN_PASS).c_str(), AP_MODE_IP, AP_MODE_GW, AP_MODE_MASK);
  45. jw.setAPMode(AP_MODE);
  46. jw.cleanNetworks();
  47. int i;
  48. for (i = 0; i< WIFI_MAX_NETWORKS; i++) {
  49. if (getSetting("ssid" + String(i)).length() == 0) break;
  50. if (getSetting("ip" + String(i)).length() == 0) {
  51. jw.addNetwork(
  52. getSetting("ssid" + String(i)).c_str(),
  53. getSetting("pass" + String(i)).c_str()
  54. );
  55. } else {
  56. jw.addNetwork(
  57. getSetting("ssid" + String(i)).c_str(),
  58. getSetting("pass" + String(i)).c_str(),
  59. getSetting("ip" + String(i)).c_str(),
  60. getSetting("gw" + String(i)).c_str(),
  61. getSetting("mask" + String(i)).c_str(),
  62. getSetting("dns" + String(i)).c_str()
  63. );
  64. }
  65. }
  66. // Scan for best network only if we have more than 1 defined
  67. jw.scanNetworks(i>1);
  68. }
  69. void wifiStatus() {
  70. if (WiFi.getMode() == WIFI_AP_STA) {
  71. DEBUG_MSG("[WIFI] MODE AP + STA --------------------------------\n");
  72. } else if (WiFi.getMode() == WIFI_AP) {
  73. DEBUG_MSG("[WIFI] MODE AP --------------------------------------\n");
  74. } else if (WiFi.getMode() == WIFI_STA) {
  75. DEBUG_MSG("[WIFI] MODE STA -------------------------------------\n");
  76. } else {
  77. DEBUG_MSG("[WIFI] MODE OFF -------------------------------------\n");
  78. DEBUG_MSG("[WIFI] No connection\n");
  79. }
  80. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  81. DEBUG_MSG("[WIFI] SSID %s\n", jw.getAPSSID().c_str());
  82. DEBUG_MSG("[WIFI] PASS %s\n", getSetting("adminPass", ADMIN_PASS).c_str());
  83. DEBUG_MSG("[WIFI] IP %s\n", WiFi.softAPIP().toString().c_str());
  84. DEBUG_MSG("[WIFI] MAC %s\n", WiFi.softAPmacAddress().c_str());
  85. }
  86. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  87. DEBUG_MSG("[WIFI] SSID %s\n", WiFi.SSID().c_str());
  88. DEBUG_MSG("[WIFI] IP %s\n", WiFi.localIP().toString().c_str());
  89. DEBUG_MSG("[WIFI] MAC %s\n", WiFi.macAddress().c_str());
  90. DEBUG_MSG("[WIFI] GW %s\n", WiFi.gatewayIP().toString().c_str());
  91. DEBUG_MSG("[WIFI] MASK %s\n", WiFi.subnetMask().toString().c_str());
  92. DEBUG_MSG("[WIFI] DNS %s\n", WiFi.dnsIP().toString().c_str());
  93. DEBUG_MSG("[WIFI] HOST %s\n", WiFi.hostname().c_str());
  94. }
  95. DEBUG_MSG("[WIFI] ----------------------------------------------\n");
  96. }
  97. void wifiSetup() {
  98. wifiConfigure();
  99. // Message callbacks
  100. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  101. #ifdef DEBUG_PORT
  102. if (code == MESSAGE_SCANNING) {
  103. DEBUG_MSG("[WIFI] Scanning\n");
  104. }
  105. if (code == MESSAGE_SCAN_FAILED) {
  106. DEBUG_MSG("[WIFI] Scan failed\n");
  107. }
  108. if (code == MESSAGE_NO_NETWORKS) {
  109. DEBUG_MSG("[WIFI] No networks found\n");
  110. }
  111. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  112. DEBUG_MSG("[WIFI] No known networks found\n");
  113. }
  114. if (code == MESSAGE_FOUND_NETWORK) {
  115. DEBUG_MSG("[WIFI] %s\n", parameter);
  116. }
  117. if (code == MESSAGE_CONNECTING) {
  118. DEBUG_MSG("[WIFI] Connecting to %s\n", parameter);
  119. }
  120. if (code == MESSAGE_CONNECT_WAITING) {
  121. // too much noise
  122. }
  123. if (code == MESSAGE_CONNECT_FAILED) {
  124. DEBUG_MSG("[WIFI] Could not connect to %s\n", parameter);
  125. }
  126. if (code == MESSAGE_CONNECTED) {
  127. wifiStatus();
  128. }
  129. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  130. wifiStatus();
  131. }
  132. if (code == MESSAGE_DISCONNECTED) {
  133. DEBUG_MSG("[WIFI] Disconnected\n");
  134. }
  135. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  136. DEBUG_MSG("[WIFI] Creating access point\n");
  137. }
  138. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  139. DEBUG_MSG("[WIFI] Could not create access point\n");
  140. }
  141. #endif
  142. // Configure mDNS
  143. if (code == MESSAGE_CONNECTED) {
  144. if (MDNS.begin((char *) WiFi.hostname().c_str())) {
  145. MDNS.addService("http", "tcp", 80);
  146. DEBUG_MSG("[MDNS] OK\n");
  147. } else {
  148. DEBUG_MSG("[MDNS] FAIL\n");
  149. }
  150. }
  151. // Configure captive portal
  152. #if ENABLE_CAPTIVE_PORTAL
  153. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  154. dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
  155. }
  156. if (code == MESSAGE_DISCONNECTED) {
  157. dnsServer.stop();
  158. }
  159. #endif
  160. // NTP connection reset
  161. if (code == MESSAGE_CONNECTED) {
  162. ntpConnect();
  163. }
  164. // Manage POW
  165. #if ENABLE_POW
  166. if (code == MESSAGE_CONNECTED) {
  167. powEnable(true);
  168. }
  169. if (code == MESSAGE_DISCONNECTED) {
  170. powEnable(false);
  171. }
  172. #endif
  173. });
  174. }
  175. void wifiLoop() {
  176. jw.loop();
  177. #if ENABLE_CAPTIVE_PORTAL
  178. if (WiFi.getMode() == WIFI_AP) {
  179. dnsServer.processNextRequest();
  180. }
  181. #endif
  182. }