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.

267 lines
7.6 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
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 wifiConfigure() {
  37. jw.setHostname(getSetting("hostname").c_str());
  38. jw.setSoftAP(getSetting("hostname").c_str(), getSetting("adminPass", ADMIN_PASS).c_str());
  39. jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT);
  40. jw.setReconnectTimeout(WIFI_RECONNECT_INTERVAL);
  41. jw.setAPMode(WIFI_AP_MODE);
  42. jw.cleanNetworks();
  43. // If system is flagged unstable we do not init wifi networks
  44. if (!systemCheck()) return;
  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. // Inject hardcoded networks
  96. void wifiInject() {
  97. #ifdef WIFI1_SSID
  98. if (getSetting("ssid", 0, "").length() == 0) setSetting("ssid", 0, WIFI1_SSID);
  99. #endif
  100. #ifdef WIFI1_PASS
  101. if (getSetting("pass", 0, "").length() == 0) setSetting("pass", 0, WIFI1_PASS);
  102. #endif
  103. #ifdef WIFI1_IP
  104. if (getSetting("ip", 0, "").length() == 0) setSetting("ip", 0, WIFI1_IP);
  105. #endif
  106. #ifdef WIFI1_GW
  107. if (getSetting("gw", 0, "").length() == 0) setSetting("gw", 0, WIFI1_GW);
  108. #endif
  109. #ifdef WIFI1_MASK
  110. if (getSetting("mask", 0, "").length() == 0) setSetting("mask", 0, WIFI1_MASK);
  111. #endif
  112. #ifdef WIFI1_DNS
  113. if (getSetting("dns", 0, "").length() == 0) setSetting("dns", 0, WIFI1_DNS);
  114. #endif
  115. #ifdef WIFI2_SSID
  116. if (getSetting("ssid", 1, "").length() == 0) setSetting("ssid", 1, WIFI2_SSID);
  117. #endif
  118. #ifdef WIFI2_PASS
  119. if (getSetting("pass", 1, "").length() == 0) setSetting("pass", 1, WIFI2_PASS);
  120. #endif
  121. #ifdef WIFI2_IP
  122. if (getSetting("ip", 1, "").length() == 0) setSetting("ip", 1, WIFI2_IP);
  123. #endif
  124. #ifdef WIFI2_GW
  125. if (getSetting("gw", 1, "").length() == 0) setSetting("gw", 1, WIFI2_GW);
  126. #endif
  127. #ifdef WIFI2_MASK
  128. if (getSetting("mask", 1, "").length() == 0) setSetting("mask", 1, WIFI2_MASK);
  129. #endif
  130. #ifdef WIFI2_DNS
  131. if (getSetting("dns", 1, "").length() == 0) setSetting("dns", 1, WIFI2_DNS);
  132. #endif
  133. }
  134. void wifiSetup() {
  135. wifiInject();
  136. wifiConfigure();
  137. // Message callbacks
  138. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  139. #if DEBUG_SUPPORT
  140. if (code == MESSAGE_SCANNING) {
  141. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  142. }
  143. if (code == MESSAGE_SCAN_FAILED) {
  144. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  145. }
  146. if (code == MESSAGE_NO_NETWORKS) {
  147. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  148. }
  149. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  150. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  151. }
  152. if (code == MESSAGE_FOUND_NETWORK) {
  153. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  154. }
  155. if (code == MESSAGE_CONNECTING) {
  156. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  157. }
  158. if (code == MESSAGE_CONNECT_WAITING) {
  159. // too much noise
  160. }
  161. if (code == MESSAGE_CONNECT_FAILED) {
  162. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  163. }
  164. if (code == MESSAGE_CONNECTED) {
  165. wifiStatus();
  166. }
  167. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  168. wifiStatus();
  169. }
  170. if (code == MESSAGE_DISCONNECTED) {
  171. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  172. }
  173. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  174. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  175. }
  176. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  177. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  178. }
  179. #endif // DEBUG_SUPPORT
  180. // Configure mDNS
  181. #if MDNS_SUPPORT
  182. if (code == MESSAGE_CONNECTED || code == MESSAGE_ACCESSPOINT_CREATED) {
  183. if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) {
  184. DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
  185. #if WEB_SUPPORT
  186. MDNS.addService("http", "tcp", getSetting("webPort", WEB_PORT).toInt());
  187. #endif
  188. #if TELNET_SUPPORT
  189. MDNS.addService("telnet", "tcp", TELNET_PORT);
  190. #endif
  191. if (code == MESSAGE_CONNECTED) mqttDiscover();
  192. } else {
  193. DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
  194. }
  195. }
  196. #endif
  197. // NTP connection reset
  198. #if NTP_SUPPORT
  199. if (code == MESSAGE_CONNECTED) {
  200. ntpConnect();
  201. }
  202. #endif
  203. });
  204. }
  205. void wifiLoop() {
  206. jw.loop();
  207. }