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.

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