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
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 HLW8012_SUPPORT
  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(WIFI_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. wifiConfigure();
  97. // Message callbacks
  98. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  99. #if DEBUG_SERIAL_SUPPORT || DEBUG_UDP_SUPPORT
  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 MDNS_SUPPORT
  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", getSetting("webPort", WEB_PORT).toInt());
  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 HLW8012_SUPPORT
  157. if (code == MESSAGE_CONNECTED) {
  158. hlw8012Enable(true);
  159. }
  160. if (code == MESSAGE_DISCONNECTED) {
  161. hlw8012Enable(false);
  162. }
  163. #endif
  164. });
  165. }
  166. void wifiLoop() {
  167. jw.loop();
  168. }