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.

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