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.

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