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.

322 lines
9.0 KiB

6 years ago
6 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
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
  1. /*
  2. WIFI MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "JustWifi.h"
  6. // -----------------------------------------------------------------------------
  7. // WIFI
  8. // -----------------------------------------------------------------------------
  9. void _wifiWebSocketOnSend(JsonObject& root) {
  10. root["maxNetworks"] = WIFI_MAX_NETWORKS;
  11. JsonArray& wifi = root.createNestedArray("wifi");
  12. for (byte i=0; i<WIFI_MAX_NETWORKS; i++) {
  13. if (!hasSetting("ssid", i)) break;
  14. JsonObject& network = wifi.createNestedObject();
  15. network["ssid"] = getSetting("ssid", i, "");
  16. network["pass"] = getSetting("pass", i, "");
  17. network["ip"] = getSetting("ip", i, "");
  18. network["gw"] = getSetting("gw", i, "");
  19. network["mask"] = getSetting("mask", i, "");
  20. network["dns"] = getSetting("dns", i, "");
  21. }
  22. }
  23. String getIP() {
  24. if (WiFi.getMode() == WIFI_AP) {
  25. return WiFi.softAPIP().toString();
  26. }
  27. return WiFi.localIP().toString();
  28. }
  29. String getNetwork() {
  30. if (WiFi.getMode() == WIFI_AP) {
  31. return jw.getAPSSID();
  32. }
  33. return WiFi.SSID();
  34. }
  35. void wifiDisconnect() {
  36. jw.disconnect();
  37. }
  38. void resetConnectionTimeout() {
  39. jw.resetReconnectTimeout();
  40. }
  41. bool wifiConnected() {
  42. return jw.connected();
  43. }
  44. bool createAP() {
  45. jw.disconnect();
  46. jw.resetReconnectTimeout();
  47. return jw.createAP();
  48. }
  49. void wifiReconnectCheck() {
  50. bool connected = false;
  51. #if WEB_SUPPORT
  52. if (wsConnected()) connected = true;
  53. #endif
  54. #if TELNET_SUPPORT
  55. if (telnetConnected()) connected = true;
  56. #endif
  57. jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL);
  58. }
  59. void wifiConfigure() {
  60. jw.setHostname(getSetting("hostname").c_str());
  61. #if USE_PASSWORD
  62. jw.setSoftAP(getSetting("hostname").c_str(), getSetting("adminPass", ADMIN_PASS).c_str());
  63. #else
  64. jw.setSoftAP(getSetting("hostname").c_str());
  65. #endif
  66. jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT);
  67. wifiReconnectCheck();
  68. jw.setAPMode(WIFI_AP_MODE);
  69. jw.cleanNetworks();
  70. // If system is flagged unstable we do not init wifi networks
  71. #if SYSTEM_CHECK_ENABLED
  72. if (!systemCheck()) return;
  73. #endif
  74. // Clean settings
  75. wifiClean(WIFI_MAX_NETWORKS);
  76. int i;
  77. for (i = 0; i< WIFI_MAX_NETWORKS; i++) {
  78. if (getSetting("ssid" + String(i)).length() == 0) break;
  79. if (getSetting("ip" + String(i)).length() == 0) {
  80. jw.addNetwork(
  81. getSetting("ssid" + String(i)).c_str(),
  82. getSetting("pass" + String(i)).c_str()
  83. );
  84. } else {
  85. jw.addNetwork(
  86. getSetting("ssid" + String(i)).c_str(),
  87. getSetting("pass" + String(i)).c_str(),
  88. getSetting("ip" + String(i)).c_str(),
  89. getSetting("gw" + String(i)).c_str(),
  90. getSetting("mask" + String(i)).c_str(),
  91. getSetting("dns" + String(i)).c_str()
  92. );
  93. }
  94. }
  95. // Scan for best network only if we have more than 1 defined
  96. jw.scanNetworks(i>1);
  97. }
  98. void wifiStatus() {
  99. if (WiFi.getMode() == WIFI_AP_STA) {
  100. DEBUG_MSG_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));
  101. } else if (WiFi.getMode() == WIFI_AP) {
  102. DEBUG_MSG_P(PSTR("[WIFI] MODE AP --------------------------------------\n"));
  103. } else if (WiFi.getMode() == WIFI_STA) {
  104. DEBUG_MSG_P(PSTR("[WIFI] MODE STA -------------------------------------\n"));
  105. } else {
  106. DEBUG_MSG_P(PSTR("[WIFI] MODE OFF -------------------------------------\n"));
  107. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  108. }
  109. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  110. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), jw.getAPSSID().c_str());
  111. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  112. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  113. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  114. }
  115. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  116. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  117. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  118. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  119. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  120. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  121. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  122. DEBUG_MSG_P(PSTR("[WIFI] HOST %s\n"), WiFi.hostname().c_str());
  123. }
  124. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  125. }
  126. bool wifiClean(unsigned char num) {
  127. bool changed = false;
  128. int i = 0;
  129. // Clean defined settings
  130. while (i < num) {
  131. // Skip on first non-defined setting
  132. if (!hasSetting("ssid", i)) {
  133. delSetting("ssid", i);
  134. break;
  135. }
  136. // Delete empty values
  137. if (!hasSetting("pass", i)) delSetting("pass", i);
  138. if (!hasSetting("ip", i)) delSetting("ip", i);
  139. if (!hasSetting("gw", i)) delSetting("gw", i);
  140. if (!hasSetting("mask", i)) delSetting("mask", i);
  141. if (!hasSetting("dns", i)) delSetting("dns", i);
  142. ++i;
  143. }
  144. // Delete all other settings
  145. while (i < WIFI_MAX_NETWORKS) {
  146. changed = hasSetting("ssid", i);
  147. delSetting("ssid", i);
  148. delSetting("pass", i);
  149. delSetting("ip", i);
  150. delSetting("gw", i);
  151. delSetting("mask", i);
  152. delSetting("dns", i);
  153. ++i;
  154. }
  155. return changed;
  156. }
  157. // Inject hardcoded networks
  158. void wifiInject() {
  159. #ifdef WIFI1_SSID
  160. if (getSetting("ssid", 0, "").length() == 0) setSetting("ssid", 0, WIFI1_SSID);
  161. #endif
  162. #ifdef WIFI1_PASS
  163. if (getSetting("pass", 0, "").length() == 0) setSetting("pass", 0, WIFI1_PASS);
  164. #endif
  165. #ifdef WIFI1_IP
  166. if (getSetting("ip", 0, "").length() == 0) setSetting("ip", 0, WIFI1_IP);
  167. #endif
  168. #ifdef WIFI1_GW
  169. if (getSetting("gw", 0, "").length() == 0) setSetting("gw", 0, WIFI1_GW);
  170. #endif
  171. #ifdef WIFI1_MASK
  172. if (getSetting("mask", 0, "").length() == 0) setSetting("mask", 0, WIFI1_MASK);
  173. #endif
  174. #ifdef WIFI1_DNS
  175. if (getSetting("dns", 0, "").length() == 0) setSetting("dns", 0, WIFI1_DNS);
  176. #endif
  177. #ifdef WIFI2_SSID
  178. if (getSetting("ssid", 1, "").length() == 0) setSetting("ssid", 1, WIFI2_SSID);
  179. #endif
  180. #ifdef WIFI2_PASS
  181. if (getSetting("pass", 1, "").length() == 0) setSetting("pass", 1, WIFI2_PASS);
  182. #endif
  183. #ifdef WIFI2_IP
  184. if (getSetting("ip", 1, "").length() == 0) setSetting("ip", 1, WIFI2_IP);
  185. #endif
  186. #ifdef WIFI2_GW
  187. if (getSetting("gw", 1, "").length() == 0) setSetting("gw", 1, WIFI2_GW);
  188. #endif
  189. #ifdef WIFI2_MASK
  190. if (getSetting("mask", 1, "").length() == 0) setSetting("mask", 1, WIFI2_MASK);
  191. #endif
  192. #ifdef WIFI2_DNS
  193. if (getSetting("dns", 1, "").length() == 0) setSetting("dns", 1, WIFI2_DNS);
  194. #endif
  195. }
  196. void wifiSetup() {
  197. #if WIFI_SLEEP_ENABLED
  198. wifi_set_sleep_type(LIGHT_SLEEP_T);
  199. #endif
  200. wifiInject();
  201. wifiConfigure();
  202. // Message callbacks
  203. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  204. #if DEBUG_SUPPORT
  205. if (code == MESSAGE_SCANNING) {
  206. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  207. }
  208. if (code == MESSAGE_SCAN_FAILED) {
  209. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  210. }
  211. if (code == MESSAGE_NO_NETWORKS) {
  212. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  213. }
  214. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  215. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  216. }
  217. if (code == MESSAGE_FOUND_NETWORK) {
  218. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  219. }
  220. if (code == MESSAGE_CONNECTING) {
  221. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  222. }
  223. if (code == MESSAGE_CONNECT_WAITING) {
  224. // too much noise
  225. }
  226. if (code == MESSAGE_CONNECT_FAILED) {
  227. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  228. }
  229. if (code == MESSAGE_CONNECTED) {
  230. wifiStatus();
  231. }
  232. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  233. wifiStatus();
  234. }
  235. if (code == MESSAGE_DISCONNECTED) {
  236. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  237. }
  238. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  239. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  240. }
  241. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  242. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  243. }
  244. #endif // DEBUG_SUPPORT
  245. #if MQTT_SUPPORT
  246. #if MDNS_SUPPORT
  247. if (code == MESSAGE_CONNECTED) mdnsFindMQTT();
  248. #endif // MDNS_SUPPORT
  249. #endif // MQTT_SUPPORT
  250. });
  251. #if WEB_SUPPORT
  252. wsOnSendRegister(_wifiWebSocketOnSend);
  253. wsOnAfterParseRegister(wifiConfigure);
  254. #endif
  255. }
  256. void wifiLoop() {
  257. jw.loop();
  258. }