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.

215 lines
6.0 KiB

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