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.

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