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.

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