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.

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