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.

252 lines
7.2 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
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
7 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. jw.disconnect();
  23. }
  24. void resetConnectionTimeout() {
  25. jw.resetReconnectTimeout();
  26. }
  27. bool wifiConnected() {
  28. return jw.connected();
  29. }
  30. bool createAP() {
  31. jw.disconnect();
  32. jw.resetReconnectTimeout();
  33. return jw.createAP();
  34. }
  35. void wifiReconnectCheck() {
  36. bool connected = false;
  37. #if WEB_SUPPORT
  38. if (wsConnected()) connected = true;
  39. #endif
  40. #if TELNET_SUPPORT
  41. if (telnetConnected()) connected = true;
  42. #endif
  43. jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL);
  44. }
  45. void wifiConfigure() {
  46. jw.setHostname(getSetting("hostname").c_str());
  47. jw.setSoftAP(getSetting("hostname").c_str(), getSetting("adminPass", ADMIN_PASS).c_str());
  48. jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT);
  49. wifiReconnectCheck();
  50. jw.setAPMode(WIFI_AP_MODE);
  51. jw.cleanNetworks();
  52. // If system is flagged unstable we do not init wifi networks
  53. #if SYSTEM_CHECK_ENABLED
  54. if (!systemCheck()) return;
  55. #endif
  56. int i;
  57. for (i = 0; i< WIFI_MAX_NETWORKS; i++) {
  58. if (getSetting("ssid" + String(i)).length() == 0) break;
  59. if (getSetting("ip" + String(i)).length() == 0) {
  60. jw.addNetwork(
  61. getSetting("ssid" + String(i)).c_str(),
  62. getSetting("pass" + String(i)).c_str()
  63. );
  64. } else {
  65. jw.addNetwork(
  66. getSetting("ssid" + String(i)).c_str(),
  67. getSetting("pass" + String(i)).c_str(),
  68. getSetting("ip" + String(i)).c_str(),
  69. getSetting("gw" + String(i)).c_str(),
  70. getSetting("mask" + String(i)).c_str(),
  71. getSetting("dns" + String(i)).c_str()
  72. );
  73. }
  74. }
  75. // Scan for best network only if we have more than 1 defined
  76. jw.scanNetworks(i>1);
  77. }
  78. void wifiStatus() {
  79. if (WiFi.getMode() == WIFI_AP_STA) {
  80. DEBUG_MSG_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));
  81. } else if (WiFi.getMode() == WIFI_AP) {
  82. DEBUG_MSG_P(PSTR("[WIFI] MODE AP --------------------------------------\n"));
  83. } else if (WiFi.getMode() == WIFI_STA) {
  84. DEBUG_MSG_P(PSTR("[WIFI] MODE STA -------------------------------------\n"));
  85. } else {
  86. DEBUG_MSG_P(PSTR("[WIFI] MODE OFF -------------------------------------\n"));
  87. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  88. }
  89. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  90. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), jw.getAPSSID().c_str());
  91. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  92. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  93. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  94. }
  95. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  96. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  97. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  98. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  99. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  100. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  101. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  102. DEBUG_MSG_P(PSTR("[WIFI] HOST %s\n"), WiFi.hostname().c_str());
  103. }
  104. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  105. }
  106. // Inject hardcoded networks
  107. void wifiInject() {
  108. #ifdef WIFI1_SSID
  109. if (getSetting("ssid", 0, "").length() == 0) setSetting("ssid", 0, WIFI1_SSID);
  110. #endif
  111. #ifdef WIFI1_PASS
  112. if (getSetting("pass", 0, "").length() == 0) setSetting("pass", 0, WIFI1_PASS);
  113. #endif
  114. #ifdef WIFI1_IP
  115. if (getSetting("ip", 0, "").length() == 0) setSetting("ip", 0, WIFI1_IP);
  116. #endif
  117. #ifdef WIFI1_GW
  118. if (getSetting("gw", 0, "").length() == 0) setSetting("gw", 0, WIFI1_GW);
  119. #endif
  120. #ifdef WIFI1_MASK
  121. if (getSetting("mask", 0, "").length() == 0) setSetting("mask", 0, WIFI1_MASK);
  122. #endif
  123. #ifdef WIFI1_DNS
  124. if (getSetting("dns", 0, "").length() == 0) setSetting("dns", 0, WIFI1_DNS);
  125. #endif
  126. #ifdef WIFI2_SSID
  127. if (getSetting("ssid", 1, "").length() == 0) setSetting("ssid", 1, WIFI2_SSID);
  128. #endif
  129. #ifdef WIFI2_PASS
  130. if (getSetting("pass", 1, "").length() == 0) setSetting("pass", 1, WIFI2_PASS);
  131. #endif
  132. #ifdef WIFI2_IP
  133. if (getSetting("ip", 1, "").length() == 0) setSetting("ip", 1, WIFI2_IP);
  134. #endif
  135. #ifdef WIFI2_GW
  136. if (getSetting("gw", 1, "").length() == 0) setSetting("gw", 1, WIFI2_GW);
  137. #endif
  138. #ifdef WIFI2_MASK
  139. if (getSetting("mask", 1, "").length() == 0) setSetting("mask", 1, WIFI2_MASK);
  140. #endif
  141. #ifdef WIFI2_DNS
  142. if (getSetting("dns", 1, "").length() == 0) setSetting("dns", 1, WIFI2_DNS);
  143. #endif
  144. }
  145. void wifiSetup() {
  146. #if WIFI_SLEEP_ENABLED
  147. wifi_set_sleep_type(LIGHT_SLEEP_T);
  148. #endif
  149. wifiInject();
  150. wifiConfigure();
  151. // Message callbacks
  152. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  153. #if DEBUG_SUPPORT
  154. if (code == MESSAGE_SCANNING) {
  155. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  156. }
  157. if (code == MESSAGE_SCAN_FAILED) {
  158. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  159. }
  160. if (code == MESSAGE_NO_NETWORKS) {
  161. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  162. }
  163. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  164. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  165. }
  166. if (code == MESSAGE_FOUND_NETWORK) {
  167. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  168. }
  169. if (code == MESSAGE_CONNECTING) {
  170. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  171. }
  172. if (code == MESSAGE_CONNECT_WAITING) {
  173. // too much noise
  174. }
  175. if (code == MESSAGE_CONNECT_FAILED) {
  176. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  177. }
  178. if (code == MESSAGE_CONNECTED) {
  179. wifiStatus();
  180. }
  181. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  182. wifiStatus();
  183. }
  184. if (code == MESSAGE_DISCONNECTED) {
  185. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  186. }
  187. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  188. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  189. }
  190. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  191. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  192. }
  193. #endif // DEBUG_SUPPORT
  194. #if MDNS_SUPPORT
  195. if (code == MESSAGE_CONNECTED) mdnsFindMQTT();
  196. #endif // MDNS_SUPPORT
  197. });
  198. }
  199. void wifiLoop() {
  200. jw.loop();
  201. }