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.

185 lines
5.1 KiB

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