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.

202 lines
5.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 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 wifiSetup() {
  66. wifiConfigure();
  67. // Message callbacks
  68. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  69. #ifdef DEBUG_PORT
  70. if (code == MESSAGE_SCANNING) {
  71. DEBUG_MSG("[WIFI] Scanning\n");
  72. }
  73. if (code == MESSAGE_SCAN_FAILED) {
  74. DEBUG_MSG("[WIFI] Scan failed\n");
  75. }
  76. if (code == MESSAGE_NO_NETWORKS) {
  77. DEBUG_MSG("[WIFI] No networks found\n");
  78. }
  79. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  80. DEBUG_MSG("[WIFI] No known networks found\n");
  81. }
  82. if (code == MESSAGE_FOUND_NETWORK) {
  83. DEBUG_MSG("[WIFI] %s\n", parameter);
  84. }
  85. if (code == MESSAGE_CONNECTING) {
  86. DEBUG_MSG("[WIFI] Connecting to %s\n", parameter);
  87. }
  88. if (code == MESSAGE_CONNECT_WAITING) {
  89. // too much noise
  90. }
  91. if (code == MESSAGE_CONNECT_FAILED) {
  92. DEBUG_MSG("[WIFI] Could not connect to %s\n", parameter);
  93. }
  94. if (code == MESSAGE_CONNECTED) {
  95. DEBUG_MSG("[WIFI] MODE STA -------------------------------------\n");
  96. DEBUG_MSG("[WIFI] SSID %s\n", WiFi.SSID().c_str());
  97. DEBUG_MSG("[WIFI] IP %s\n", WiFi.localIP().toString().c_str());
  98. DEBUG_MSG("[WIFI] MAC %s\n", WiFi.macAddress().c_str());
  99. DEBUG_MSG("[WIFI] GW %s\n", WiFi.gatewayIP().toString().c_str());
  100. DEBUG_MSG("[WIFI] MASK %s\n", WiFi.subnetMask().toString().c_str());
  101. DEBUG_MSG("[WIFI] DNS %s\n", WiFi.dnsIP().toString().c_str());
  102. DEBUG_MSG("[WIFI] HOST %s\n", WiFi.hostname().c_str());
  103. DEBUG_MSG("[WIFI] ----------------------------------------------\n");
  104. }
  105. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  106. DEBUG_MSG("[WIFI] MODE AP --------------------------------------\n");
  107. DEBUG_MSG("[WIFI] SSID %s\n", jw.getAPSSID().c_str());
  108. DEBUG_MSG("[WIFI] PASS %s\n", getSetting("adminPass", ADMIN_PASS).c_str());
  109. DEBUG_MSG("[WIFI] IP %s\n", WiFi.softAPIP().toString().c_str());
  110. DEBUG_MSG("[WIFI] MAC %s\n", WiFi.softAPmacAddress().c_str());
  111. DEBUG_MSG("[WIFI] ----------------------------------------------\n");
  112. }
  113. if (code == MESSAGE_DISCONNECTED) {
  114. DEBUG_MSG("[WIFI] Disconnected\n");
  115. }
  116. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  117. DEBUG_MSG("[WIFI] Creating access point\n");
  118. }
  119. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  120. DEBUG_MSG("[WIFI] Could not create access point\n");
  121. }
  122. #endif
  123. // Configure mDNS
  124. if (code == MESSAGE_CONNECTED) {
  125. if (MDNS.begin((char *) WiFi.hostname().c_str())) {
  126. MDNS.addService("http", "tcp", 80);
  127. DEBUG_MSG("[MDNS] OK\n");
  128. } else {
  129. DEBUG_MSG("[MDNS] FAIL\n");
  130. }
  131. }
  132. // Configure captive portal
  133. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  134. dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
  135. }
  136. if (code == MESSAGE_DISCONNECTED) {
  137. dnsServer.stop();
  138. }
  139. // NTP connection reset
  140. if (code == MESSAGE_CONNECTED) {
  141. ntpConnect();
  142. }
  143. // Manage POW
  144. #if ENABLE_POW
  145. if (code == MESSAGE_CONNECTED) {
  146. powEnable(true);
  147. }
  148. if (code == MESSAGE_DISCONNECTED) {
  149. powEnable(false);
  150. }
  151. #endif
  152. });
  153. }
  154. void wifiLoop() {
  155. jw.loop();
  156. if (WiFi.getMode() == WIFI_AP) {
  157. dnsServer.processNextRequest();
  158. }
  159. }