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.

284 lines
8.0 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 SYSTEM_CHECK_ENABLED
  55. if (!systemCheck()) return;
  56. #endif
  57. int i;
  58. for (i = 0; i< WIFI_MAX_NETWORKS; i++) {
  59. if (getSetting("ssid" + String(i)).length() == 0) break;
  60. if (getSetting("ip" + String(i)).length() == 0) {
  61. jw.addNetwork(
  62. getSetting("ssid" + String(i)).c_str(),
  63. getSetting("pass" + String(i)).c_str()
  64. );
  65. } else {
  66. jw.addNetwork(
  67. getSetting("ssid" + String(i)).c_str(),
  68. getSetting("pass" + String(i)).c_str(),
  69. getSetting("ip" + String(i)).c_str(),
  70. getSetting("gw" + String(i)).c_str(),
  71. getSetting("mask" + String(i)).c_str(),
  72. getSetting("dns" + String(i)).c_str()
  73. );
  74. }
  75. }
  76. // Scan for best network only if we have more than 1 defined
  77. jw.scanNetworks(i>1);
  78. }
  79. void wifiStatus() {
  80. if (WiFi.getMode() == WIFI_AP_STA) {
  81. DEBUG_MSG_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));
  82. } else if (WiFi.getMode() == WIFI_AP) {
  83. DEBUG_MSG_P(PSTR("[WIFI] MODE AP --------------------------------------\n"));
  84. } else if (WiFi.getMode() == WIFI_STA) {
  85. DEBUG_MSG_P(PSTR("[WIFI] MODE STA -------------------------------------\n"));
  86. } else {
  87. DEBUG_MSG_P(PSTR("[WIFI] MODE OFF -------------------------------------\n"));
  88. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  89. }
  90. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  91. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), jw.getAPSSID().c_str());
  92. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  93. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  94. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  95. }
  96. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  97. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  98. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  99. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  100. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  101. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  102. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  103. DEBUG_MSG_P(PSTR("[WIFI] HOST %s\n"), WiFi.hostname().c_str());
  104. }
  105. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  106. }
  107. // Inject hardcoded networks
  108. void wifiInject() {
  109. #ifdef WIFI1_SSID
  110. if (getSetting("ssid", 0, "").length() == 0) setSetting("ssid", 0, WIFI1_SSID);
  111. #endif
  112. #ifdef WIFI1_PASS
  113. if (getSetting("pass", 0, "").length() == 0) setSetting("pass", 0, WIFI1_PASS);
  114. #endif
  115. #ifdef WIFI1_IP
  116. if (getSetting("ip", 0, "").length() == 0) setSetting("ip", 0, WIFI1_IP);
  117. #endif
  118. #ifdef WIFI1_GW
  119. if (getSetting("gw", 0, "").length() == 0) setSetting("gw", 0, WIFI1_GW);
  120. #endif
  121. #ifdef WIFI1_MASK
  122. if (getSetting("mask", 0, "").length() == 0) setSetting("mask", 0, WIFI1_MASK);
  123. #endif
  124. #ifdef WIFI1_DNS
  125. if (getSetting("dns", 0, "").length() == 0) setSetting("dns", 0, WIFI1_DNS);
  126. #endif
  127. #ifdef WIFI2_SSID
  128. if (getSetting("ssid", 1, "").length() == 0) setSetting("ssid", 1, WIFI2_SSID);
  129. #endif
  130. #ifdef WIFI2_PASS
  131. if (getSetting("pass", 1, "").length() == 0) setSetting("pass", 1, WIFI2_PASS);
  132. #endif
  133. #ifdef WIFI2_IP
  134. if (getSetting("ip", 1, "").length() == 0) setSetting("ip", 1, WIFI2_IP);
  135. #endif
  136. #ifdef WIFI2_GW
  137. if (getSetting("gw", 1, "").length() == 0) setSetting("gw", 1, WIFI2_GW);
  138. #endif
  139. #ifdef WIFI2_MASK
  140. if (getSetting("mask", 1, "").length() == 0) setSetting("mask", 1, WIFI2_MASK);
  141. #endif
  142. #ifdef WIFI2_DNS
  143. if (getSetting("dns", 1, "").length() == 0) setSetting("dns", 1, WIFI2_DNS);
  144. #endif
  145. }
  146. void wifiSetup() {
  147. #if WIFI_SLEEP_ENABLED
  148. wifi_set_sleep_type(LIGHT_SLEEP_T);
  149. #endif
  150. wifiInject();
  151. wifiConfigure();
  152. // Message callbacks
  153. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  154. #if DEBUG_SUPPORT
  155. if (code == MESSAGE_SCANNING) {
  156. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  157. }
  158. if (code == MESSAGE_SCAN_FAILED) {
  159. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  160. }
  161. if (code == MESSAGE_NO_NETWORKS) {
  162. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  163. }
  164. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  165. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  166. }
  167. if (code == MESSAGE_FOUND_NETWORK) {
  168. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  169. }
  170. if (code == MESSAGE_CONNECTING) {
  171. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  172. }
  173. if (code == MESSAGE_CONNECT_WAITING) {
  174. // too much noise
  175. }
  176. if (code == MESSAGE_CONNECT_FAILED) {
  177. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  178. }
  179. if (code == MESSAGE_CONNECTED) {
  180. wifiStatus();
  181. }
  182. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  183. wifiStatus();
  184. }
  185. if (code == MESSAGE_DISCONNECTED) {
  186. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  187. }
  188. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  189. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  190. }
  191. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  192. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  193. }
  194. #endif // DEBUG_SUPPORT
  195. // Configure mDNS
  196. #if MDNS_SUPPORT
  197. if (code == MESSAGE_CONNECTED || code == MESSAGE_ACCESSPOINT_CREATED) {
  198. if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) {
  199. DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
  200. #if WEB_SUPPORT
  201. MDNS.addService("http", "tcp", getSetting("webPort", WEB_PORT).toInt());
  202. #endif
  203. #if TELNET_SUPPORT
  204. MDNS.addService("telnet", "tcp", TELNET_PORT);
  205. #endif
  206. if (code == MESSAGE_CONNECTED) mqttDiscover();
  207. } else {
  208. DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
  209. }
  210. }
  211. #endif
  212. // NTP connection reset
  213. #if NTP_SUPPORT
  214. if (code == MESSAGE_CONNECTED) {
  215. ntpConfigure();
  216. }
  217. #endif
  218. });
  219. }
  220. void wifiLoop() {
  221. jw.loop();
  222. }