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.

455 lines
13 KiB

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
  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. #include <Ticker.h>
  7. uint32_t _wifi_scan_client_id = 0;
  8. // -----------------------------------------------------------------------------
  9. // PRIVATE
  10. // -----------------------------------------------------------------------------
  11. void _wifiConfigure() {
  12. jw.setHostname(getSetting("hostname").c_str());
  13. #if USE_PASSWORD
  14. jw.setSoftAP(getSetting("hostname").c_str(), getSetting("adminPass", ADMIN_PASS).c_str());
  15. #else
  16. jw.setSoftAP(getSetting("hostname").c_str());
  17. #endif
  18. jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT);
  19. wifiReconnectCheck();
  20. jw.setAPMode(WIFI_AP_MODE);
  21. jw.cleanNetworks();
  22. // If system is flagged unstable we do not init wifi networks
  23. #if SYSTEM_CHECK_ENABLED
  24. if (!systemCheck()) return;
  25. #endif
  26. // Clean settings
  27. _wifiClean(WIFI_MAX_NETWORKS);
  28. int i;
  29. for (i = 0; i< WIFI_MAX_NETWORKS; i++) {
  30. if (getSetting("ssid" + String(i)).length() == 0) break;
  31. if (getSetting("ip" + String(i)).length() == 0) {
  32. jw.addNetwork(
  33. getSetting("ssid" + String(i)).c_str(),
  34. getSetting("pass" + String(i)).c_str()
  35. );
  36. } else {
  37. jw.addNetwork(
  38. getSetting("ssid" + String(i)).c_str(),
  39. getSetting("pass" + String(i)).c_str(),
  40. getSetting("ip" + String(i)).c_str(),
  41. getSetting("gw" + String(i)).c_str(),
  42. getSetting("mask" + String(i)).c_str(),
  43. getSetting("dns" + String(i)).c_str()
  44. );
  45. }
  46. }
  47. jw.scanNetworks(getSetting("wifiScan", WIFI_SCAN_NETWORKS).toInt() == 1);
  48. }
  49. void _wifiScan(uint32_t client_id = 0) {
  50. DEBUG_MSG_P(PSTR("[WIFI] Start scanning\n"));
  51. #if WEB_SUPPORT
  52. String output;
  53. #endif
  54. unsigned char result = WiFi.scanNetworks();
  55. if (result == WIFI_SCAN_FAILED) {
  56. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  57. #if WEB_SUPPORT
  58. output = String("Failed scan");
  59. #endif
  60. } else if (result == 0) {
  61. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  62. #if WEB_SUPPORT
  63. output = String("No networks found");
  64. #endif
  65. } else {
  66. DEBUG_MSG_P(PSTR("[WIFI] %d networks found:\n"), result);
  67. // Populate defined networks with scan data
  68. for (int8_t i = 0; i < result; ++i) {
  69. String ssid_scan;
  70. int32_t rssi_scan;
  71. uint8_t sec_scan;
  72. uint8_t* BSSID_scan;
  73. int32_t chan_scan;
  74. bool hidden_scan;
  75. char buffer[128];
  76. WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan, hidden_scan);
  77. snprintf_P(buffer, sizeof(buffer),
  78. PSTR("BSSID: %02X:%02X:%02X:%02X:%02X:%02X SEC: %s RSSI: %3d CH: %2d SSID: %s"),
  79. BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], BSSID_scan[6],
  80. (sec_scan != ENC_TYPE_NONE ? "YES" : "NO "),
  81. rssi_scan,
  82. chan_scan,
  83. (char *) ssid_scan.c_str()
  84. );
  85. DEBUG_MSG_P(PSTR("[WIFI] > %s\n"), buffer);
  86. #if WEB_SUPPORT
  87. if (client_id > 0) output = output + String(buffer) + String("<br />");
  88. #endif
  89. }
  90. }
  91. #if WEB_SUPPORT
  92. if (client_id > 0) {
  93. output = String("{\"scanResult\": \"") + output + String("\"}");
  94. wsSend(client_id, output.c_str());
  95. }
  96. #endif
  97. WiFi.scanDelete();
  98. }
  99. bool _wifiClean(unsigned char num) {
  100. bool changed = false;
  101. int i = 0;
  102. // Clean defined settings
  103. while (i < num) {
  104. // Skip on first non-defined setting
  105. if (!hasSetting("ssid", i)) {
  106. delSetting("ssid", i);
  107. break;
  108. }
  109. // Delete empty values
  110. if (!hasSetting("pass", i)) delSetting("pass", i);
  111. if (!hasSetting("ip", i)) delSetting("ip", i);
  112. if (!hasSetting("gw", i)) delSetting("gw", i);
  113. if (!hasSetting("mask", i)) delSetting("mask", i);
  114. if (!hasSetting("dns", i)) delSetting("dns", i);
  115. ++i;
  116. }
  117. // Delete all other settings
  118. while (i < WIFI_MAX_NETWORKS) {
  119. changed = hasSetting("ssid", i);
  120. delSetting("ssid", i);
  121. delSetting("pass", i);
  122. delSetting("ip", i);
  123. delSetting("gw", i);
  124. delSetting("mask", i);
  125. delSetting("dns", i);
  126. ++i;
  127. }
  128. return changed;
  129. }
  130. // Inject hardcoded networks
  131. void _wifiInject() {
  132. #ifdef WIFI1_SSID
  133. if (getSetting("ssid", 0, "").length() == 0) setSetting("ssid", 0, WIFI1_SSID);
  134. #endif
  135. #ifdef WIFI1_PASS
  136. if (getSetting("pass", 0, "").length() == 0) setSetting("pass", 0, WIFI1_PASS);
  137. #endif
  138. #ifdef WIFI1_IP
  139. if (getSetting("ip", 0, "").length() == 0) setSetting("ip", 0, WIFI1_IP);
  140. #endif
  141. #ifdef WIFI1_GW
  142. if (getSetting("gw", 0, "").length() == 0) setSetting("gw", 0, WIFI1_GW);
  143. #endif
  144. #ifdef WIFI1_MASK
  145. if (getSetting("mask", 0, "").length() == 0) setSetting("mask", 0, WIFI1_MASK);
  146. #endif
  147. #ifdef WIFI1_DNS
  148. if (getSetting("dns", 0, "").length() == 0) setSetting("dns", 0, WIFI1_DNS);
  149. #endif
  150. #ifdef WIFI2_SSID
  151. if (getSetting("ssid", 1, "").length() == 0) setSetting("ssid", 1, WIFI2_SSID);
  152. #endif
  153. #ifdef WIFI2_PASS
  154. if (getSetting("pass", 1, "").length() == 0) setSetting("pass", 1, WIFI2_PASS);
  155. #endif
  156. #ifdef WIFI2_IP
  157. if (getSetting("ip", 1, "").length() == 0) setSetting("ip", 1, WIFI2_IP);
  158. #endif
  159. #ifdef WIFI2_GW
  160. if (getSetting("gw", 1, "").length() == 0) setSetting("gw", 1, WIFI2_GW);
  161. #endif
  162. #ifdef WIFI2_MASK
  163. if (getSetting("mask", 1, "").length() == 0) setSetting("mask", 1, WIFI2_MASK);
  164. #endif
  165. #ifdef WIFI2_DNS
  166. if (getSetting("dns", 1, "").length() == 0) setSetting("dns", 1, WIFI2_DNS);
  167. #endif
  168. }
  169. #if DEBUG_SUPPORT
  170. void _wifiDebug(justwifi_messages_t code, char * parameter) {
  171. if (code == MESSAGE_SCANNING) {
  172. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  173. }
  174. if (code == MESSAGE_SCAN_FAILED) {
  175. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  176. }
  177. if (code == MESSAGE_NO_NETWORKS) {
  178. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  179. }
  180. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  181. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  182. }
  183. if (code == MESSAGE_FOUND_NETWORK) {
  184. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  185. }
  186. if (code == MESSAGE_CONNECTING) {
  187. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  188. }
  189. if (code == MESSAGE_CONNECT_WAITING) {
  190. // too much noise
  191. }
  192. if (code == MESSAGE_CONNECT_FAILED) {
  193. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  194. }
  195. if (code == MESSAGE_CONNECTED) {
  196. wifiStatus();
  197. }
  198. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  199. wifiStatus();
  200. }
  201. if (code == MESSAGE_DISCONNECTED) {
  202. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  203. }
  204. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  205. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  206. }
  207. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  208. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  209. }
  210. }
  211. #endif // DEBUG_SUPPORT
  212. // -----------------------------------------------------------------------------
  213. // SETTINGS
  214. // -----------------------------------------------------------------------------
  215. #if TERMINAL_SUPPORT
  216. void _wifiInitCommands() {
  217. settingsRegisterCommand(F("WIFI.RESET"), [](Embedis* e) {
  218. _wifiConfigure();
  219. wifiDisconnect();
  220. DEBUG_MSG_P(PSTR("+OK\n"));
  221. });
  222. settingsRegisterCommand(F("WIFI.SCAN"), [](Embedis* e) {
  223. _wifiScan();
  224. DEBUG_MSG_P(PSTR("+OK\n"));
  225. });
  226. }
  227. #endif
  228. // -----------------------------------------------------------------------------
  229. // WEB
  230. // -----------------------------------------------------------------------------
  231. #if WEB_SUPPORT
  232. void _wifiWebSocketOnSend(JsonObject& root) {
  233. root["maxNetworks"] = WIFI_MAX_NETWORKS;
  234. JsonArray& wifi = root.createNestedArray("wifi");
  235. for (byte i=0; i<WIFI_MAX_NETWORKS; i++) {
  236. if (!hasSetting("ssid", i)) break;
  237. JsonObject& network = wifi.createNestedObject();
  238. network["ssid"] = getSetting("ssid", i, "");
  239. network["pass"] = getSetting("pass", i, "");
  240. network["ip"] = getSetting("ip", i, "");
  241. network["gw"] = getSetting("gw", i, "");
  242. network["mask"] = getSetting("mask", i, "");
  243. network["dns"] = getSetting("dns", i, "");
  244. }
  245. }
  246. void _wifiWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  247. if (strcmp(action, "scan") == 0) _wifi_scan_client_id = client_id;
  248. }
  249. #endif
  250. // -----------------------------------------------------------------------------
  251. // API
  252. // -----------------------------------------------------------------------------
  253. String getIP() {
  254. if (WiFi.getMode() == WIFI_AP) {
  255. return WiFi.softAPIP().toString();
  256. }
  257. return WiFi.localIP().toString();
  258. }
  259. String getNetwork() {
  260. if (WiFi.getMode() == WIFI_AP) {
  261. return jw.getAPSSID();
  262. }
  263. return WiFi.SSID();
  264. }
  265. double wifiDistance(int rssi) {
  266. double exponent = (double) (WIFI_RSSI_1M - rssi) / WIFI_PROPAGATION_CONST / 10.0;
  267. return round(pow(10, exponent));
  268. }
  269. bool wifiConnected() {
  270. return jw.connected();
  271. }
  272. void wifiDisconnect() {
  273. jw.disconnect();
  274. }
  275. bool createAP() {
  276. jw.disconnect();
  277. jw.resetReconnectTimeout();
  278. return jw.createAP();
  279. }
  280. void wifiReconnectCheck() {
  281. bool connected = false;
  282. #if WEB_SUPPORT
  283. if (wsConnected()) connected = true;
  284. #endif
  285. #if TELNET_SUPPORT
  286. if (telnetConnected()) connected = true;
  287. #endif
  288. jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL);
  289. }
  290. void wifiStatus() {
  291. if (WiFi.getMode() == WIFI_AP_STA) {
  292. DEBUG_MSG_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));
  293. } else if (WiFi.getMode() == WIFI_AP) {
  294. DEBUG_MSG_P(PSTR("[WIFI] MODE AP --------------------------------------\n"));
  295. } else if (WiFi.getMode() == WIFI_STA) {
  296. DEBUG_MSG_P(PSTR("[WIFI] MODE STA -------------------------------------\n"));
  297. } else {
  298. DEBUG_MSG_P(PSTR("[WIFI] MODE OFF -------------------------------------\n"));
  299. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  300. }
  301. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  302. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), jw.getAPSSID().c_str());
  303. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  304. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  305. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  306. }
  307. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  308. uint8_t * bssid = WiFi.BSSID();
  309. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  310. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  311. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  312. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  313. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  314. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  315. DEBUG_MSG_P(PSTR("[WIFI] HOST %s\n"), WiFi.hostname().c_str());
  316. DEBUG_MSG_P(PSTR("[WIFI] BSSID %02X:%02X:%02X:%02X:%02X:%02X\n"),
  317. bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], bssid[6]
  318. );
  319. DEBUG_MSG_P(PSTR("[WIFI] CH %d\n"), WiFi.channel());
  320. DEBUG_MSG_P(PSTR("[WIFI] RSSI %d\n"), WiFi.RSSI());
  321. }
  322. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  323. }
  324. void wifiRegister(wifi_callback_f callback) {
  325. jw.subscribe(callback);
  326. }
  327. // -----------------------------------------------------------------------------
  328. // INITIALIZATION
  329. // -----------------------------------------------------------------------------
  330. void wifiSetup() {
  331. #if WIFI_SLEEP_ENABLED
  332. wifi_set_sleep_type(LIGHT_SLEEP_T);
  333. #endif
  334. _wifiInject();
  335. _wifiConfigure();
  336. // Message callbacks
  337. #if DEBUG_SUPPORT
  338. wifiRegister(_wifiDebug);
  339. #endif
  340. #if WEB_SUPPORT
  341. wsOnSendRegister(_wifiWebSocketOnSend);
  342. wsOnAfterParseRegister(_wifiConfigure);
  343. wsOnActionRegister(_wifiWebSocketOnAction);
  344. #endif
  345. #if TERMINAL_SUPPORT
  346. _wifiInitCommands();
  347. #endif
  348. }
  349. void wifiLoop() {
  350. jw.loop();
  351. if (_wifi_scan_client_id > 0) {
  352. _wifiScan(_wifi_scan_client_id);
  353. _wifi_scan_client_id = 0;
  354. }
  355. }