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.

376 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. double wifiDistance(int rssi) {
  36. double exponent = (double) (WIFI_RSSI_1M - rssi) / WIFI_PROPAGATION_CONST / 10.0;
  37. return round(pow(10, exponent));
  38. }
  39. void wifiDisconnect() {
  40. jw.disconnect();
  41. }
  42. void resetConnectionTimeout() {
  43. jw.resetReconnectTimeout();
  44. }
  45. bool wifiConnected() {
  46. return jw.connected();
  47. }
  48. bool createAP() {
  49. jw.disconnect();
  50. jw.resetReconnectTimeout();
  51. return jw.createAP();
  52. }
  53. void wifiReconnectCheck() {
  54. bool connected = false;
  55. #if WEB_SUPPORT
  56. if (wsConnected()) connected = true;
  57. #endif
  58. #if TELNET_SUPPORT
  59. if (telnetConnected()) connected = true;
  60. #endif
  61. jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL);
  62. }
  63. void wifiConfigure() {
  64. jw.setHostname(getSetting("hostname").c_str());
  65. #if USE_PASSWORD
  66. jw.setSoftAP(getSetting("hostname").c_str(), getSetting("adminPass", ADMIN_PASS).c_str());
  67. #else
  68. jw.setSoftAP(getSetting("hostname").c_str());
  69. #endif
  70. jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT);
  71. wifiReconnectCheck();
  72. jw.setAPMode(WIFI_AP_MODE);
  73. jw.cleanNetworks();
  74. // If system is flagged unstable we do not init wifi networks
  75. #if SYSTEM_CHECK_ENABLED
  76. if (!systemCheck()) return;
  77. #endif
  78. // Clean settings
  79. wifiClean(WIFI_MAX_NETWORKS);
  80. int i;
  81. for (i = 0; i< WIFI_MAX_NETWORKS; i++) {
  82. if (getSetting("ssid" + String(i)).length() == 0) break;
  83. if (getSetting("ip" + String(i)).length() == 0) {
  84. jw.addNetwork(
  85. getSetting("ssid" + String(i)).c_str(),
  86. getSetting("pass" + String(i)).c_str()
  87. );
  88. } else {
  89. jw.addNetwork(
  90. getSetting("ssid" + String(i)).c_str(),
  91. getSetting("pass" + String(i)).c_str(),
  92. getSetting("ip" + String(i)).c_str(),
  93. getSetting("gw" + String(i)).c_str(),
  94. getSetting("mask" + String(i)).c_str(),
  95. getSetting("dns" + String(i)).c_str()
  96. );
  97. }
  98. }
  99. jw.scanNetworks(true);
  100. }
  101. void wifiStatus() {
  102. if (WiFi.getMode() == WIFI_AP_STA) {
  103. DEBUG_MSG_P(PSTR("[WIFI] MODE AP + STA --------------------------------\n"));
  104. } else if (WiFi.getMode() == WIFI_AP) {
  105. DEBUG_MSG_P(PSTR("[WIFI] MODE AP --------------------------------------\n"));
  106. } else if (WiFi.getMode() == WIFI_STA) {
  107. DEBUG_MSG_P(PSTR("[WIFI] MODE STA -------------------------------------\n"));
  108. } else {
  109. DEBUG_MSG_P(PSTR("[WIFI] MODE OFF -------------------------------------\n"));
  110. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  111. }
  112. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  113. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), jw.getAPSSID().c_str());
  114. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  115. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  116. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  117. }
  118. if ((WiFi.getMode() & WIFI_STA) == WIFI_STA) {
  119. uint8_t * bssid = WiFi.BSSID();
  120. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  121. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  122. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  123. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  124. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  125. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  126. DEBUG_MSG_P(PSTR("[WIFI] HOST %s\n"), WiFi.hostname().c_str());
  127. DEBUG_MSG_P(PSTR("[WIFI] BSSID %02X:%02X:%02X:%02X:%02X:%02X\n"),
  128. bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], bssid[6]
  129. );
  130. DEBUG_MSG_P(PSTR("[WIFI] CH %d\n"), WiFi.channel());
  131. DEBUG_MSG_P(PSTR("[WIFI] RSSI %d\n"), WiFi.RSSI());
  132. }
  133. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  134. }
  135. void wifiScan() {
  136. DEBUG_MSG_P(PSTR("[WIFI] Start scanning\n"));
  137. unsigned char result = WiFi.scanNetworks();
  138. if (result == WIFI_SCAN_FAILED) {
  139. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  140. } else if (result == 0) {
  141. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  142. } else {
  143. DEBUG_MSG_P(PSTR("[WIFI] %d networks found:\n"), result);
  144. // Populate defined networks with scan data
  145. for (int8_t i = 0; i < result; ++i) {
  146. String ssid_scan;
  147. int32_t rssi_scan;
  148. uint8_t sec_scan;
  149. uint8_t* BSSID_scan;
  150. int32_t chan_scan;
  151. bool hidden_scan;
  152. WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan, hidden_scan);
  153. DEBUG_MSG_P(PSTR("[WIFI] - BSSID: %02X:%02X:%02X:%02X:%02X:%02X SEC: %s RSSI: %3d CH: %2d SSID: %s\n"),
  154. BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], BSSID_scan[6],
  155. (sec_scan != ENC_TYPE_NONE ? "YES" : "NO "),
  156. rssi_scan,
  157. chan_scan,
  158. (char *) ssid_scan.c_str()
  159. );
  160. }
  161. }
  162. WiFi.scanDelete();
  163. }
  164. bool wifiClean(unsigned char num) {
  165. bool changed = false;
  166. int i = 0;
  167. // Clean defined settings
  168. while (i < num) {
  169. // Skip on first non-defined setting
  170. if (!hasSetting("ssid", i)) {
  171. delSetting("ssid", i);
  172. break;
  173. }
  174. // Delete empty values
  175. if (!hasSetting("pass", i)) delSetting("pass", i);
  176. if (!hasSetting("ip", i)) delSetting("ip", i);
  177. if (!hasSetting("gw", i)) delSetting("gw", i);
  178. if (!hasSetting("mask", i)) delSetting("mask", i);
  179. if (!hasSetting("dns", i)) delSetting("dns", i);
  180. ++i;
  181. }
  182. // Delete all other settings
  183. while (i < WIFI_MAX_NETWORKS) {
  184. changed = hasSetting("ssid", i);
  185. delSetting("ssid", i);
  186. delSetting("pass", i);
  187. delSetting("ip", i);
  188. delSetting("gw", i);
  189. delSetting("mask", i);
  190. delSetting("dns", i);
  191. ++i;
  192. }
  193. return changed;
  194. }
  195. // Inject hardcoded networks
  196. void wifiInject() {
  197. #ifdef WIFI1_SSID
  198. if (getSetting("ssid", 0, "").length() == 0) setSetting("ssid", 0, WIFI1_SSID);
  199. #endif
  200. #ifdef WIFI1_PASS
  201. if (getSetting("pass", 0, "").length() == 0) setSetting("pass", 0, WIFI1_PASS);
  202. #endif
  203. #ifdef WIFI1_IP
  204. if (getSetting("ip", 0, "").length() == 0) setSetting("ip", 0, WIFI1_IP);
  205. #endif
  206. #ifdef WIFI1_GW
  207. if (getSetting("gw", 0, "").length() == 0) setSetting("gw", 0, WIFI1_GW);
  208. #endif
  209. #ifdef WIFI1_MASK
  210. if (getSetting("mask", 0, "").length() == 0) setSetting("mask", 0, WIFI1_MASK);
  211. #endif
  212. #ifdef WIFI1_DNS
  213. if (getSetting("dns", 0, "").length() == 0) setSetting("dns", 0, WIFI1_DNS);
  214. #endif
  215. #ifdef WIFI2_SSID
  216. if (getSetting("ssid", 1, "").length() == 0) setSetting("ssid", 1, WIFI2_SSID);
  217. #endif
  218. #ifdef WIFI2_PASS
  219. if (getSetting("pass", 1, "").length() == 0) setSetting("pass", 1, WIFI2_PASS);
  220. #endif
  221. #ifdef WIFI2_IP
  222. if (getSetting("ip", 1, "").length() == 0) setSetting("ip", 1, WIFI2_IP);
  223. #endif
  224. #ifdef WIFI2_GW
  225. if (getSetting("gw", 1, "").length() == 0) setSetting("gw", 1, WIFI2_GW);
  226. #endif
  227. #ifdef WIFI2_MASK
  228. if (getSetting("mask", 1, "").length() == 0) setSetting("mask", 1, WIFI2_MASK);
  229. #endif
  230. #ifdef WIFI2_DNS
  231. if (getSetting("dns", 1, "").length() == 0) setSetting("dns", 1, WIFI2_DNS);
  232. #endif
  233. }
  234. #if DEBUG_SUPPORT
  235. void _wifiDebug(justwifi_messages_t code, char * parameter) {
  236. if (code == MESSAGE_SCANNING) {
  237. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  238. }
  239. if (code == MESSAGE_SCAN_FAILED) {
  240. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  241. }
  242. if (code == MESSAGE_NO_NETWORKS) {
  243. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  244. }
  245. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  246. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  247. }
  248. if (code == MESSAGE_FOUND_NETWORK) {
  249. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  250. }
  251. if (code == MESSAGE_CONNECTING) {
  252. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  253. }
  254. if (code == MESSAGE_CONNECT_WAITING) {
  255. // too much noise
  256. }
  257. if (code == MESSAGE_CONNECT_FAILED) {
  258. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  259. }
  260. if (code == MESSAGE_CONNECTED) {
  261. wifiStatus();
  262. }
  263. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  264. wifiStatus();
  265. }
  266. if (code == MESSAGE_DISCONNECTED) {
  267. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  268. }
  269. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  270. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  271. }
  272. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  273. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  274. }
  275. }
  276. #endif // DEBUG_SUPPORT
  277. void wifiRegister(wifi_callback_f callback) {
  278. jw.subscribe(callback);
  279. }
  280. void wifiSetup() {
  281. #if WIFI_SLEEP_ENABLED
  282. wifi_set_sleep_type(LIGHT_SLEEP_T);
  283. #endif
  284. wifiInject();
  285. wifiConfigure();
  286. // Message callbacks
  287. #if DEBUG_SUPPORT
  288. wifiRegister(_wifiDebug);
  289. #endif
  290. #if WEB_SUPPORT
  291. wsOnSendRegister(_wifiWebSocketOnSend);
  292. wsOnAfterParseRegister(wifiConfigure);
  293. #endif
  294. }
  295. void wifiLoop() {
  296. jw.loop();
  297. }