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.

443 lines
12 KiB

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