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.

278 lines
7.9 KiB

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