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.

179 lines
4.9 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
  1. /*
  2. WIFI MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "JustWifi.h"
  6. #include <DNSServer.h>
  7. DNSServer dnsServer;
  8. // -----------------------------------------------------------------------------
  9. // WIFI
  10. // -----------------------------------------------------------------------------
  11. String getIP() {
  12. if (WiFi.getMode() == WIFI_AP) {
  13. return WiFi.softAPIP().toString();
  14. }
  15. return WiFi.localIP().toString();
  16. }
  17. String getNetwork() {
  18. if (WiFi.getMode() == WIFI_AP) {
  19. return jw.getAPSSID();
  20. }
  21. return WiFi.SSID();
  22. }
  23. void wifiDisconnect() {
  24. jw.disconnect();
  25. }
  26. void resetConnectionTimeout() {
  27. jw.resetReconnectTimeout();
  28. }
  29. bool wifiConnected() {
  30. return jw.connected();
  31. }
  32. bool createAP() {
  33. return jw.createAP();
  34. }
  35. void wifiConfigure() {
  36. jw.scanNetworks(true);
  37. jw.setHostname(getSetting("hostname", HOSTNAME).c_str());
  38. jw.setSoftAP(getSetting("hostname", HOSTNAME).c_str(), getSetting("adminPass", ADMIN_PASS).c_str());
  39. jw.setAPMode(AP_MODE_ALONE);
  40. jw.cleanNetworks();
  41. for (int i = 0; i< WIFI_MAX_NETWORKS; i++) {
  42. if (getSetting("ssid" + String(i)).length() == 0) break;
  43. if (getSetting("ip" + String(i)).length() == 0) {
  44. jw.addNetwork(
  45. getSetting("ssid" + String(i)).c_str(),
  46. getSetting("pass" + String(i)).c_str()
  47. );
  48. } else {
  49. jw.addNetwork(
  50. getSetting("ssid" + String(i)).c_str(),
  51. getSetting("pass" + String(i)).c_str(),
  52. getSetting("ip" + String(i)).c_str(),
  53. getSetting("gw" + String(i)).c_str(),
  54. getSetting("mask" + String(i)).c_str(),
  55. getSetting("dns" + String(i)).c_str()
  56. );
  57. }
  58. }
  59. }
  60. void wifiSetup() {
  61. wifiConfigure();
  62. // Message callbacks
  63. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  64. #ifdef DEBUG_PORT
  65. if (code == MESSAGE_SCANNING) {
  66. DEBUG_MSG("[WIFI] Scanning\n");
  67. }
  68. if (code == MESSAGE_SCAN_FAILED) {
  69. DEBUG_MSG("[WIFI] Scan failed\n");
  70. }
  71. if (code == MESSAGE_NO_NETWORKS) {
  72. DEBUG_MSG("[WIFI] No networks found\n");
  73. }
  74. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  75. DEBUG_MSG("[WIFI] No known networks found\n");
  76. }
  77. if (code == MESSAGE_FOUND_NETWORK) {
  78. DEBUG_MSG("[WIFI] %s\n", parameter);
  79. }
  80. if (code == MESSAGE_CONNECTING) {
  81. DEBUG_MSG("[WIFI] Connecting to %s\n", parameter);
  82. }
  83. if (code == MESSAGE_CONNECT_WAITING) {
  84. // too much noise
  85. }
  86. if (code == MESSAGE_CONNECT_FAILED) {
  87. DEBUG_MSG("[WIFI] Could not connect to %s\n", parameter);
  88. }
  89. if (code == MESSAGE_CONNECTED) {
  90. DEBUG_MSG("[WIFI] MODE STA -------------------------------------\n");
  91. DEBUG_MSG("[WIFI] SSID %s\n", WiFi.SSID().c_str());
  92. DEBUG_MSG("[WIFI] IP %s\n", WiFi.localIP().toString().c_str());
  93. DEBUG_MSG("[WIFI] MAC %s\n", WiFi.macAddress().c_str());
  94. DEBUG_MSG("[WIFI] GW %s\n", WiFi.gatewayIP().toString().c_str());
  95. DEBUG_MSG("[WIFI] MASK %s\n", WiFi.subnetMask().toString().c_str());
  96. DEBUG_MSG("[WIFI] DNS %s\n", WiFi.dnsIP().toString().c_str());
  97. DEBUG_MSG("[WIFI] HOST %s\n", WiFi.hostname().c_str());
  98. DEBUG_MSG("[WIFI] ----------------------------------------------\n");
  99. }
  100. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  101. DEBUG_MSG("[WIFI] MODE AP --------------------------------------\n");
  102. DEBUG_MSG("[WIFI] SSID %s\n", jw.getAPSSID().c_str());
  103. DEBUG_MSG("[WIFI] PASS %s\n", getSetting("adminPass", ADMIN_PASS).c_str());
  104. DEBUG_MSG("[WIFI] IP %s\n", WiFi.softAPIP().toString().c_str());
  105. DEBUG_MSG("[WIFI] MAC %s\n", WiFi.softAPmacAddress().c_str());
  106. DEBUG_MSG("[WIFI] ----------------------------------------------\n");
  107. }
  108. if (code == MESSAGE_DISCONNECTED) {
  109. DEBUG_MSG("[WIFI] Disconnected\n");
  110. }
  111. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  112. DEBUG_MSG("[WIFI] Creating access point\n");
  113. }
  114. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  115. DEBUG_MSG("[WIFI] Could not create access point\n");
  116. }
  117. #endif
  118. // Configure mDNS
  119. if (code == MESSAGE_CONNECTED) {
  120. if (MDNS.begin((char *) WiFi.hostname().c_str())) {
  121. MDNS.addService("http", "tcp", 80);
  122. DEBUG_MSG("[MDNS] OK\n");
  123. } else {
  124. DEBUG_MSG("[MDNS] FAIL\n");
  125. }
  126. }
  127. // Configure captive portal
  128. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  129. dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
  130. }
  131. if (code == MESSAGE_DISCONNECTED) {
  132. dnsServer.stop();
  133. }
  134. });
  135. }
  136. void wifiLoop() {
  137. jw.loop();
  138. if (WiFi.getMode() == WIFI_AP) {
  139. dnsServer.processNextRequest();
  140. }
  141. }