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.

486 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
6 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. if (strlen(WIFI1_SSID)) {
  133. if (!hasSetting("ssid", 0)) {
  134. setSetting("ssid", 0, WIFI1_SSID);
  135. setSetting("pass", 0, WIFI1_PASS);
  136. setSetting("ip", 0, WIFI1_IP);
  137. setSetting("gw", 0, WIFI1_GW);
  138. setSetting("mask", 0, WIFI1_MASK);
  139. setSetting("dns", 0, WIFI1_DNS);
  140. }
  141. if (strlen(WIFI2_SSID)) {
  142. if (!hasSetting("ssid", 1)) {
  143. setSetting("ssid", 1, WIFI2_SSID);
  144. setSetting("pass", 1, WIFI2_PASS);
  145. setSetting("ip", 1, WIFI2_IP);
  146. setSetting("gw", 1, WIFI2_GW);
  147. setSetting("mask", 1, WIFI2_MASK);
  148. setSetting("dns", 1, WIFI2_DNS);
  149. }
  150. }
  151. }
  152. }
  153. #if WIFI_AP_CAPTIVE
  154. #include "DNSServer.h"
  155. DNSServer _wifi_dnsServer;
  156. void _wifiCaptivePortal(justwifi_messages_t code, char * parameter) {
  157. if (MESSAGE_ACCESSPOINT_CREATED == code) {
  158. _wifi_dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  159. _wifi_dnsServer.start(53, "*", WiFi.softAPIP());
  160. DEBUG_MSG_P(PSTR("[WIFI] Captive portal enabled\n"));
  161. }
  162. if (MESSAGE_CONNECTED == code) {
  163. _wifi_dnsServer.stop();
  164. DEBUG_MSG_P(PSTR("[WIFI] Captive portal disabled\n"));
  165. }
  166. }
  167. #endif // WIFI_AP_CAPTIVE
  168. #if DEBUG_SUPPORT
  169. void _wifiDebug(justwifi_messages_t code, char * parameter) {
  170. if (code == MESSAGE_SCANNING) {
  171. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  172. }
  173. if (code == MESSAGE_SCAN_FAILED) {
  174. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  175. }
  176. if (code == MESSAGE_NO_NETWORKS) {
  177. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  178. }
  179. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  180. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  181. }
  182. if (code == MESSAGE_FOUND_NETWORK) {
  183. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  184. }
  185. if (code == MESSAGE_CONNECTING) {
  186. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  187. }
  188. if (code == MESSAGE_CONNECT_WAITING) {
  189. // too much noise
  190. }
  191. if (code == MESSAGE_CONNECT_FAILED) {
  192. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  193. }
  194. if (code == MESSAGE_CONNECTED) {
  195. wifiStatus();
  196. }
  197. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  198. wifiStatus();
  199. }
  200. if (code == MESSAGE_DISCONNECTED) {
  201. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  202. }
  203. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  204. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  205. }
  206. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  207. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  208. }
  209. }
  210. #endif // DEBUG_SUPPORT
  211. // -----------------------------------------------------------------------------
  212. // SETTINGS
  213. // -----------------------------------------------------------------------------
  214. #if TERMINAL_SUPPORT
  215. void _wifiInitCommands() {
  216. settingsRegisterCommand(F("WIFI.RESET"), [](Embedis* e) {
  217. _wifiConfigure();
  218. wifiDisconnect();
  219. DEBUG_MSG_P(PSTR("+OK\n"));
  220. });
  221. settingsRegisterCommand(F("WIFI.AP"), [](Embedis* e) {
  222. createAP();
  223. DEBUG_MSG_P(PSTR("+OK\n"));
  224. });
  225. settingsRegisterCommand(F("WIFI.SCAN"), [](Embedis* e) {
  226. _wifiScan();
  227. DEBUG_MSG_P(PSTR("+OK\n"));
  228. });
  229. }
  230. #endif
  231. // -----------------------------------------------------------------------------
  232. // WEB
  233. // -----------------------------------------------------------------------------
  234. #if WEB_SUPPORT
  235. bool _wifiWebSocketOnReceive(const char * key, JsonVariant& value) {
  236. if (strncmp(key, "wifi", 4) == 0) return true;
  237. if (strncmp(key, "ssid", 4) == 0) return true;
  238. if (strncmp(key, "pass", 4) == 0) return true;
  239. if (strncmp(key, "ip", 2) == 0) return true;
  240. if (strncmp(key, "gw", 2) == 0) return true;
  241. if (strncmp(key, "mask", 4) == 0) return true;
  242. if (strncmp(key, "dns", 3) == 0) return true;
  243. return false;
  244. }
  245. void _wifiWebSocketOnSend(JsonObject& root) {
  246. root["maxNetworks"] = WIFI_MAX_NETWORKS;
  247. root["wifiScan"] = getSetting("wifiScan", WIFI_SCAN_NETWORKS).toInt() == 1;
  248. JsonArray& wifi = root.createNestedArray("wifi");
  249. for (byte i=0; i<WIFI_MAX_NETWORKS; i++) {
  250. if (!hasSetting("ssid", i)) break;
  251. JsonObject& network = wifi.createNestedObject();
  252. network["ssid"] = getSetting("ssid", i, "");
  253. network["pass"] = getSetting("pass", i, "");
  254. network["ip"] = getSetting("ip", i, "");
  255. network["gw"] = getSetting("gw", i, "");
  256. network["mask"] = getSetting("mask", i, "");
  257. network["dns"] = getSetting("dns", i, "");
  258. }
  259. }
  260. void _wifiWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  261. if (strcmp(action, "scan") == 0) _wifi_scan_client_id = client_id;
  262. }
  263. #endif
  264. // -----------------------------------------------------------------------------
  265. // API
  266. // -----------------------------------------------------------------------------
  267. String getIP() {
  268. if (WiFi.getMode() == WIFI_AP) {
  269. return WiFi.softAPIP().toString();
  270. }
  271. return WiFi.localIP().toString();
  272. }
  273. String getNetwork() {
  274. if (WiFi.getMode() == WIFI_AP) {
  275. return jw.getAPSSID();
  276. }
  277. return WiFi.SSID();
  278. }
  279. bool wifiConnected() {
  280. return jw.connected();
  281. }
  282. void wifiDisconnect() {
  283. jw.disconnect();
  284. }
  285. bool createAP() {
  286. jw.disconnect();
  287. jw.resetReconnectTimeout();
  288. return jw.createAP();
  289. }
  290. void wifiReconnectCheck() {
  291. bool connected = false;
  292. #if WEB_SUPPORT
  293. if (wsConnected()) connected = true;
  294. #endif
  295. #if TELNET_SUPPORT
  296. if (telnetConnected()) connected = true;
  297. #endif
  298. jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL);
  299. }
  300. void wifiStatus() {
  301. if (WiFi.getMode() == WIFI_AP_STA) {
  302. DEBUG_MSG_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));
  303. } else if (WiFi.getMode() == WIFI_AP) {
  304. DEBUG_MSG_P(PSTR("[WIFI] MODE AP --------------------------------------\n"));
  305. } else if (WiFi.getMode() == WIFI_STA) {
  306. DEBUG_MSG_P(PSTR("[WIFI] MODE STA -------------------------------------\n"));
  307. } else {
  308. DEBUG_MSG_P(PSTR("[WIFI] MODE OFF -------------------------------------\n"));
  309. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  310. }
  311. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  312. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), jw.getAPSSID().c_str());
  313. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  314. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  315. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  316. }
  317. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  318. uint8_t * bssid = WiFi.BSSID();
  319. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  320. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  321. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  322. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  323. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  324. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  325. DEBUG_MSG_P(PSTR("[WIFI] HOST http://%s.local\n"), WiFi.hostname().c_str());
  326. DEBUG_MSG_P(PSTR("[WIFI] BSSID %02X:%02X:%02X:%02X:%02X:%02X\n"),
  327. bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], bssid[6]
  328. );
  329. DEBUG_MSG_P(PSTR("[WIFI] CH %d\n"), WiFi.channel());
  330. DEBUG_MSG_P(PSTR("[WIFI] RSSI %d\n"), WiFi.RSSI());
  331. }
  332. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  333. }
  334. void wifiRegister(wifi_callback_f callback) {
  335. jw.subscribe(callback);
  336. }
  337. // -----------------------------------------------------------------------------
  338. // INITIALIZATION
  339. // -----------------------------------------------------------------------------
  340. void wifiSetup() {
  341. WiFi.setSleepMode(WIFI_SLEEP_MODE);
  342. _wifiInject();
  343. _wifiConfigure();
  344. // Message callbacks
  345. #if WIFI_AP_CAPTIVE
  346. wifiRegister(_wifiCaptivePortal);
  347. #endif
  348. #if DEBUG_SUPPORT
  349. wifiRegister(_wifiDebug);
  350. #endif
  351. #if WEB_SUPPORT
  352. wsOnSendRegister(_wifiWebSocketOnSend);
  353. wsOnReceiveRegister(_wifiWebSocketOnReceive);
  354. wsOnAfterParseRegister(_wifiConfigure);
  355. wsOnActionRegister(_wifiWebSocketOnAction);
  356. #endif
  357. #if TERMINAL_SUPPORT
  358. _wifiInitCommands();
  359. #endif
  360. // Register loop
  361. espurnaRegisterLoop(wifiLoop);
  362. }
  363. void wifiLoop() {
  364. jw.loop();
  365. #if WIFI_AP_CAPTIVE
  366. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  367. _wifi_dnsServer.processNextRequest();
  368. }
  369. #endif
  370. if (_wifi_scan_client_id > 0) {
  371. _wifiScan(_wifi_scan_client_id);
  372. _wifi_scan_client_id = 0;
  373. }
  374. }