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.

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