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.

214 lines
5.9 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. // -----------------------------------------------------------------------------
  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. wifiConfigure();
  96. // Message callbacks
  97. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  98. #ifdef DEBUG_PORT
  99. if (code == MESSAGE_SCANNING) {
  100. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  101. }
  102. if (code == MESSAGE_SCAN_FAILED) {
  103. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  104. }
  105. if (code == MESSAGE_NO_NETWORKS) {
  106. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  107. }
  108. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  109. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  110. }
  111. if (code == MESSAGE_FOUND_NETWORK) {
  112. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  113. }
  114. if (code == MESSAGE_CONNECTING) {
  115. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  116. }
  117. if (code == MESSAGE_CONNECT_WAITING) {
  118. // too much noise
  119. }
  120. if (code == MESSAGE_CONNECT_FAILED) {
  121. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  122. }
  123. if (code == MESSAGE_CONNECTED) {
  124. wifiStatus();
  125. }
  126. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  127. wifiStatus();
  128. }
  129. if (code == MESSAGE_DISCONNECTED) {
  130. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  131. }
  132. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  133. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  134. }
  135. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  136. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  137. }
  138. #endif
  139. // Configure mDNS
  140. #if ENABLE_MDNS
  141. if (code == MESSAGE_CONNECTED || code == MESSAGE_ACCESSPOINT_CREATED) {
  142. if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) {
  143. MDNS.addService("http", "tcp", 80);
  144. DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
  145. } else {
  146. DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
  147. }
  148. }
  149. #endif
  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. }