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.

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