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.

371 lines
10 KiB

7 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
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
8 years ago
8 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. jw.scanNetworks(true);
  96. }
  97. void wifiStatus() {
  98. if (WiFi.getMode() == WIFI_AP_STA) {
  99. DEBUG_MSG_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));
  100. } else if (WiFi.getMode() == WIFI_AP) {
  101. DEBUG_MSG_P(PSTR("[WIFI] MODE AP --------------------------------------\n"));
  102. } else if (WiFi.getMode() == WIFI_STA) {
  103. DEBUG_MSG_P(PSTR("[WIFI] MODE STA -------------------------------------\n"));
  104. } else {
  105. DEBUG_MSG_P(PSTR("[WIFI] MODE OFF -------------------------------------\n"));
  106. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  107. }
  108. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  109. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), jw.getAPSSID().c_str());
  110. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  111. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  112. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  113. }
  114. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  115. uint8_t * bssid = WiFi.BSSID();
  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. DEBUG_MSG_P(PSTR("[WIFI] BSSID %02X:%02X:%02X:%02X:%02X:%02X\n"),
  124. bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], bssid[6]
  125. );
  126. DEBUG_MSG_P(PSTR("[WIFI] CH %d\n"), WiFi.channel());
  127. DEBUG_MSG_P(PSTR("[WIFI] RSSI %d\n"), WiFi.RSSI());
  128. }
  129. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  130. }
  131. void wifiScan() {
  132. DEBUG_MSG_P(PSTR("[WIFI] Start scanning\n"));
  133. unsigned char result = WiFi.scanNetworks();
  134. if (result == WIFI_SCAN_FAILED) {
  135. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  136. } else if (result == 0) {
  137. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  138. } else {
  139. DEBUG_MSG_P(PSTR("[WIFI] %d networks found:\n"), result);
  140. // Populate defined networks with scan data
  141. for (int8_t i = 0; i < result; ++i) {
  142. String ssid_scan;
  143. int32_t rssi_scan;
  144. uint8_t sec_scan;
  145. uint8_t* BSSID_scan;
  146. int32_t chan_scan;
  147. bool hidden_scan;
  148. WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan, hidden_scan);
  149. DEBUG_MSG_P(PSTR("[WIFI] - BSSID: %02X:%02X:%02X:%02X:%02X:%02X SEC: %s RSSI: %3d CH: %2d SSID: %s\n"),
  150. BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], BSSID_scan[6],
  151. (sec_scan != ENC_TYPE_NONE ? "YES" : "NO "),
  152. rssi_scan,
  153. chan_scan,
  154. (char *) ssid_scan.c_str()
  155. );
  156. }
  157. }
  158. WiFi.scanDelete();
  159. }
  160. bool wifiClean(unsigned char num) {
  161. bool changed = false;
  162. int i = 0;
  163. // Clean defined settings
  164. while (i < num) {
  165. // Skip on first non-defined setting
  166. if (!hasSetting("ssid", i)) {
  167. delSetting("ssid", i);
  168. break;
  169. }
  170. // Delete empty values
  171. if (!hasSetting("pass", i)) delSetting("pass", i);
  172. if (!hasSetting("ip", i)) delSetting("ip", i);
  173. if (!hasSetting("gw", i)) delSetting("gw", i);
  174. if (!hasSetting("mask", i)) delSetting("mask", i);
  175. if (!hasSetting("dns", i)) delSetting("dns", i);
  176. ++i;
  177. }
  178. // Delete all other settings
  179. while (i < WIFI_MAX_NETWORKS) {
  180. changed = hasSetting("ssid", i);
  181. delSetting("ssid", i);
  182. delSetting("pass", i);
  183. delSetting("ip", i);
  184. delSetting("gw", i);
  185. delSetting("mask", i);
  186. delSetting("dns", i);
  187. ++i;
  188. }
  189. return changed;
  190. }
  191. // Inject hardcoded networks
  192. void wifiInject() {
  193. #ifdef WIFI1_SSID
  194. if (getSetting("ssid", 0, "").length() == 0) setSetting("ssid", 0, WIFI1_SSID);
  195. #endif
  196. #ifdef WIFI1_PASS
  197. if (getSetting("pass", 0, "").length() == 0) setSetting("pass", 0, WIFI1_PASS);
  198. #endif
  199. #ifdef WIFI1_IP
  200. if (getSetting("ip", 0, "").length() == 0) setSetting("ip", 0, WIFI1_IP);
  201. #endif
  202. #ifdef WIFI1_GW
  203. if (getSetting("gw", 0, "").length() == 0) setSetting("gw", 0, WIFI1_GW);
  204. #endif
  205. #ifdef WIFI1_MASK
  206. if (getSetting("mask", 0, "").length() == 0) setSetting("mask", 0, WIFI1_MASK);
  207. #endif
  208. #ifdef WIFI1_DNS
  209. if (getSetting("dns", 0, "").length() == 0) setSetting("dns", 0, WIFI1_DNS);
  210. #endif
  211. #ifdef WIFI2_SSID
  212. if (getSetting("ssid", 1, "").length() == 0) setSetting("ssid", 1, WIFI2_SSID);
  213. #endif
  214. #ifdef WIFI2_PASS
  215. if (getSetting("pass", 1, "").length() == 0) setSetting("pass", 1, WIFI2_PASS);
  216. #endif
  217. #ifdef WIFI2_IP
  218. if (getSetting("ip", 1, "").length() == 0) setSetting("ip", 1, WIFI2_IP);
  219. #endif
  220. #ifdef WIFI2_GW
  221. if (getSetting("gw", 1, "").length() == 0) setSetting("gw", 1, WIFI2_GW);
  222. #endif
  223. #ifdef WIFI2_MASK
  224. if (getSetting("mask", 1, "").length() == 0) setSetting("mask", 1, WIFI2_MASK);
  225. #endif
  226. #ifdef WIFI2_DNS
  227. if (getSetting("dns", 1, "").length() == 0) setSetting("dns", 1, WIFI2_DNS);
  228. #endif
  229. }
  230. #if DEBUG_SUPPORT
  231. void _wifiDebug(justwifi_messages_t code, char * parameter) {
  232. if (code == MESSAGE_SCANNING) {
  233. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  234. }
  235. if (code == MESSAGE_SCAN_FAILED) {
  236. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  237. }
  238. if (code == MESSAGE_NO_NETWORKS) {
  239. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  240. }
  241. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  242. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  243. }
  244. if (code == MESSAGE_FOUND_NETWORK) {
  245. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  246. }
  247. if (code == MESSAGE_CONNECTING) {
  248. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  249. }
  250. if (code == MESSAGE_CONNECT_WAITING) {
  251. // too much noise
  252. }
  253. if (code == MESSAGE_CONNECT_FAILED) {
  254. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  255. }
  256. if (code == MESSAGE_CONNECTED) {
  257. wifiStatus();
  258. }
  259. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  260. wifiStatus();
  261. }
  262. if (code == MESSAGE_DISCONNECTED) {
  263. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  264. }
  265. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  266. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  267. }
  268. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  269. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  270. }
  271. }
  272. #endif // DEBUG_SUPPORT
  273. void wifiRegister(wifi_callback_f callback) {
  274. jw.subscribe(callback);
  275. }
  276. void wifiSetup() {
  277. #if WIFI_SLEEP_ENABLED
  278. wifi_set_sleep_type(LIGHT_SLEEP_T);
  279. #endif
  280. wifiInject();
  281. wifiConfigure();
  282. // Message callbacks
  283. #if DEBUG_SUPPORT
  284. wifiRegister(_wifiDebug);
  285. #endif
  286. #if WEB_SUPPORT
  287. wsOnSendRegister(_wifiWebSocketOnSend);
  288. wsOnAfterParseRegister(wifiConfigure);
  289. #endif
  290. }
  291. void wifiLoop() {
  292. jw.loop();
  293. }