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.

233 lines
6.4 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
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_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));
  72. } else if (WiFi.getMode() == WIFI_AP) {
  73. DEBUG_MSG_P(PSTR("[WIFI] MODE AP --------------------------------------\n"));
  74. } else if (WiFi.getMode() == WIFI_STA) {
  75. DEBUG_MSG_P(PSTR("[WIFI] MODE STA -------------------------------------\n"));
  76. } else {
  77. DEBUG_MSG_P(PSTR("[WIFI] MODE OFF -------------------------------------\n"));
  78. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  79. }
  80. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  81. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), jw.getAPSSID().c_str());
  82. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  83. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  84. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  85. }
  86. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  87. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  88. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  89. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  90. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  91. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  92. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  93. DEBUG_MSG_P(PSTR("[WIFI] HOST %s\n"), WiFi.hostname().c_str());
  94. }
  95. DEBUG_MSG_P(PSTR("[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_P(PSTR("[WIFI] Scanning\n"));
  104. }
  105. if (code == MESSAGE_SCAN_FAILED) {
  106. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  107. }
  108. if (code == MESSAGE_NO_NETWORKS) {
  109. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  110. }
  111. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  112. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  113. }
  114. if (code == MESSAGE_FOUND_NETWORK) {
  115. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  116. }
  117. if (code == MESSAGE_CONNECTING) {
  118. DEBUG_MSG_P(PSTR("[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_P(PSTR("[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_P(PSTR("[WIFI] Disconnected\n"));
  134. }
  135. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  136. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  137. }
  138. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  139. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  140. }
  141. #endif
  142. // Configure mDNS
  143. #if ENABLE_MDNS
  144. if (code == MESSAGE_CONNECTED) {
  145. if (MDNS.begin((char *) WiFi.hostname().c_str())) {
  146. MDNS.addService("http", "tcp", 80);
  147. DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
  148. } else {
  149. DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
  150. }
  151. }
  152. #endif
  153. // Configure captive portal
  154. #if ENABLE_CAPTIVE_PORTAL
  155. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  156. dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
  157. }
  158. if (code == MESSAGE_DISCONNECTED) {
  159. dnsServer.stop();
  160. }
  161. #endif
  162. // NTP connection reset
  163. if (code == MESSAGE_CONNECTED) {
  164. ntpConnect();
  165. }
  166. // Manage POW
  167. #if ENABLE_POW
  168. if (code == MESSAGE_CONNECTED) {
  169. powEnable(true);
  170. }
  171. if (code == MESSAGE_DISCONNECTED) {
  172. powEnable(false);
  173. }
  174. #endif
  175. });
  176. }
  177. void wifiLoop() {
  178. jw.loop();
  179. #if ENABLE_CAPTIVE_PORTAL
  180. if (WiFi.getMode() == WIFI_AP) {
  181. dnsServer.processNextRequest();
  182. }
  183. #endif
  184. }