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.

318 lines
8.9 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-2017 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. jw.setSoftAP(getSetting("hostname").c_str(), getSetting("adminPass", ADMIN_PASS).c_str());
  62. jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT);
  63. wifiReconnectCheck();
  64. jw.setAPMode(WIFI_AP_MODE);
  65. jw.cleanNetworks();
  66. // If system is flagged unstable we do not init wifi networks
  67. #if SYSTEM_CHECK_ENABLED
  68. if (!systemCheck()) return;
  69. #endif
  70. // Clean settings
  71. wifiClean(WIFI_MAX_NETWORKS);
  72. int i;
  73. for (i = 0; i< WIFI_MAX_NETWORKS; i++) {
  74. if (getSetting("ssid" + String(i)).length() == 0) break;
  75. if (getSetting("ip" + String(i)).length() == 0) {
  76. jw.addNetwork(
  77. getSetting("ssid" + String(i)).c_str(),
  78. getSetting("pass" + String(i)).c_str()
  79. );
  80. } else {
  81. jw.addNetwork(
  82. getSetting("ssid" + String(i)).c_str(),
  83. getSetting("pass" + String(i)).c_str(),
  84. getSetting("ip" + String(i)).c_str(),
  85. getSetting("gw" + String(i)).c_str(),
  86. getSetting("mask" + String(i)).c_str(),
  87. getSetting("dns" + String(i)).c_str()
  88. );
  89. }
  90. }
  91. // Scan for best network only if we have more than 1 defined
  92. jw.scanNetworks(i>1);
  93. }
  94. void wifiStatus() {
  95. if (WiFi.getMode() == WIFI_AP_STA) {
  96. DEBUG_MSG_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));
  97. } else if (WiFi.getMode() == WIFI_AP) {
  98. DEBUG_MSG_P(PSTR("[WIFI] MODE AP --------------------------------------\n"));
  99. } else if (WiFi.getMode() == WIFI_STA) {
  100. DEBUG_MSG_P(PSTR("[WIFI] MODE STA -------------------------------------\n"));
  101. } else {
  102. DEBUG_MSG_P(PSTR("[WIFI] MODE OFF -------------------------------------\n"));
  103. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  104. }
  105. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  106. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), jw.getAPSSID().c_str());
  107. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  108. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  109. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  110. }
  111. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  112. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  113. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  114. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  115. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  116. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  117. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  118. DEBUG_MSG_P(PSTR("[WIFI] HOST %s\n"), WiFi.hostname().c_str());
  119. }
  120. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  121. }
  122. bool wifiClean(unsigned char num) {
  123. bool changed = false;
  124. int i = 0;
  125. // Clean defined settings
  126. while (i < num) {
  127. // Skip on first non-defined setting
  128. if (!hasSetting("ssid", i)) {
  129. delSetting("ssid", i);
  130. break;
  131. }
  132. // Delete empty values
  133. if (!hasSetting("pass", i)) delSetting("pass", i);
  134. if (!hasSetting("ip", i)) delSetting("ip", i);
  135. if (!hasSetting("gw", i)) delSetting("gw", i);
  136. if (!hasSetting("mask", i)) delSetting("mask", i);
  137. if (!hasSetting("dns", i)) delSetting("dns", i);
  138. ++i;
  139. }
  140. // Delete all other settings
  141. while (i < WIFI_MAX_NETWORKS) {
  142. changed = hasSetting("ssid", i);
  143. delSetting("ssid", i);
  144. delSetting("pass", i);
  145. delSetting("ip", i);
  146. delSetting("gw", i);
  147. delSetting("mask", i);
  148. delSetting("dns", i);
  149. ++i;
  150. }
  151. return changed;
  152. }
  153. // Inject hardcoded networks
  154. void wifiInject() {
  155. #ifdef WIFI1_SSID
  156. if (getSetting("ssid", 0, "").length() == 0) setSetting("ssid", 0, WIFI1_SSID);
  157. #endif
  158. #ifdef WIFI1_PASS
  159. if (getSetting("pass", 0, "").length() == 0) setSetting("pass", 0, WIFI1_PASS);
  160. #endif
  161. #ifdef WIFI1_IP
  162. if (getSetting("ip", 0, "").length() == 0) setSetting("ip", 0, WIFI1_IP);
  163. #endif
  164. #ifdef WIFI1_GW
  165. if (getSetting("gw", 0, "").length() == 0) setSetting("gw", 0, WIFI1_GW);
  166. #endif
  167. #ifdef WIFI1_MASK
  168. if (getSetting("mask", 0, "").length() == 0) setSetting("mask", 0, WIFI1_MASK);
  169. #endif
  170. #ifdef WIFI1_DNS
  171. if (getSetting("dns", 0, "").length() == 0) setSetting("dns", 0, WIFI1_DNS);
  172. #endif
  173. #ifdef WIFI2_SSID
  174. if (getSetting("ssid", 1, "").length() == 0) setSetting("ssid", 1, WIFI2_SSID);
  175. #endif
  176. #ifdef WIFI2_PASS
  177. if (getSetting("pass", 1, "").length() == 0) setSetting("pass", 1, WIFI2_PASS);
  178. #endif
  179. #ifdef WIFI2_IP
  180. if (getSetting("ip", 1, "").length() == 0) setSetting("ip", 1, WIFI2_IP);
  181. #endif
  182. #ifdef WIFI2_GW
  183. if (getSetting("gw", 1, "").length() == 0) setSetting("gw", 1, WIFI2_GW);
  184. #endif
  185. #ifdef WIFI2_MASK
  186. if (getSetting("mask", 1, "").length() == 0) setSetting("mask", 1, WIFI2_MASK);
  187. #endif
  188. #ifdef WIFI2_DNS
  189. if (getSetting("dns", 1, "").length() == 0) setSetting("dns", 1, WIFI2_DNS);
  190. #endif
  191. }
  192. void wifiSetup() {
  193. #if WIFI_SLEEP_ENABLED
  194. wifi_set_sleep_type(LIGHT_SLEEP_T);
  195. #endif
  196. wifiInject();
  197. wifiConfigure();
  198. // Message callbacks
  199. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  200. #if DEBUG_SUPPORT
  201. if (code == MESSAGE_SCANNING) {
  202. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  203. }
  204. if (code == MESSAGE_SCAN_FAILED) {
  205. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  206. }
  207. if (code == MESSAGE_NO_NETWORKS) {
  208. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  209. }
  210. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  211. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  212. }
  213. if (code == MESSAGE_FOUND_NETWORK) {
  214. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  215. }
  216. if (code == MESSAGE_CONNECTING) {
  217. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  218. }
  219. if (code == MESSAGE_CONNECT_WAITING) {
  220. // too much noise
  221. }
  222. if (code == MESSAGE_CONNECT_FAILED) {
  223. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  224. }
  225. if (code == MESSAGE_CONNECTED) {
  226. wifiStatus();
  227. }
  228. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  229. wifiStatus();
  230. }
  231. if (code == MESSAGE_DISCONNECTED) {
  232. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  233. }
  234. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  235. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  236. }
  237. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  238. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  239. }
  240. #endif // DEBUG_SUPPORT
  241. #if MQTT_SUPPORT
  242. #if MDNS_SUPPORT
  243. if (code == MESSAGE_CONNECTED) mdnsFindMQTT();
  244. #endif // MDNS_SUPPORT
  245. #endif // MQTT_SUPPORT
  246. });
  247. #if WEB_SUPPORT
  248. wsOnSendRegister(_wifiWebSocketOnSend);
  249. wsOnAfterParseRegister(wifiConfigure);
  250. #endif
  251. }
  252. void wifiLoop() {
  253. jw.loop();
  254. }