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.

170 lines
4.7 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. ESPurna
  3. WIFI MODULE
  4. Copyright (C) 2016 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. if (getSetting("ssid0").length() > 0) jw.addNetwork(getSetting("ssid0").c_str(), getSetting("pass0").c_str());
  43. if (getSetting("ssid1").length() > 0) jw.addNetwork(getSetting("ssid1").c_str(), getSetting("pass1").c_str());
  44. if (getSetting("ssid2").length() > 0) jw.addNetwork(getSetting("ssid2").c_str(), getSetting("pass2").c_str());
  45. }
  46. void wifiSetup() {
  47. wifiConfigure();
  48. // Message callbacks
  49. jw.onMessage([](justwifi_messages_t code, char * parameter) {
  50. #ifdef DEBUG_PORT
  51. if (code == MESSAGE_SCANNING) {
  52. DEBUG_MSG("[WIFI] Scanning\n");
  53. }
  54. if (code == MESSAGE_SCAN_FAILED) {
  55. DEBUG_MSG("[WIFI] Scan failed\n");
  56. }
  57. if (code == MESSAGE_NO_NETWORKS) {
  58. DEBUG_MSG("[WIFI] No networks found\n");
  59. }
  60. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  61. DEBUG_MSG("[WIFI] No known networks found\n");
  62. }
  63. if (code == MESSAGE_FOUND_NETWORK) {
  64. DEBUG_MSG("[WIFI] %s\n", parameter);
  65. }
  66. if (code == MESSAGE_CONNECTING) {
  67. DEBUG_MSG("[WIFI] Connecting to %s\n", parameter);
  68. }
  69. if (code == MESSAGE_CONNECT_WAITING) {
  70. // too much noise
  71. }
  72. if (code == MESSAGE_CONNECT_FAILED) {
  73. DEBUG_MSG("[WIFI] Could not connect to %s\n", parameter);
  74. }
  75. if (code == MESSAGE_CONNECTED) {
  76. DEBUG_MSG("[WIFI] MODE STA -------------------------------------\n");
  77. DEBUG_MSG("[WIFI] SSID %s\n", WiFi.SSID().c_str());
  78. DEBUG_MSG("[WIFI] IP %s\n", WiFi.localIP().toString().c_str());
  79. DEBUG_MSG("[WIFI] MAC %s\n", WiFi.macAddress().c_str());
  80. DEBUG_MSG("[WIFI] GW %s\n", WiFi.gatewayIP().toString().c_str());
  81. DEBUG_MSG("[WIFI] MASK %s\n", WiFi.subnetMask().toString().c_str());
  82. DEBUG_MSG("[WIFI] DNS %s\n", WiFi.dnsIP().toString().c_str());
  83. DEBUG_MSG("[WIFI] HOST %s\n", WiFi.hostname().c_str());
  84. DEBUG_MSG("[WIFI] ----------------------------------------------\n");
  85. }
  86. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  87. DEBUG_MSG("[WIFI] MODE AP --------------------------------------\n");
  88. DEBUG_MSG("[WIFI] SSID %s\n", jw.getAPSSID().c_str());
  89. DEBUG_MSG("[WIFI] PASS %s\n", getSetting("adminPass", ADMIN_PASS).c_str());
  90. DEBUG_MSG("[WIFI] IP %s\n", WiFi.softAPIP().toString().c_str());
  91. DEBUG_MSG("[WIFI] MAC %s\n", WiFi.softAPmacAddress().c_str());
  92. DEBUG_MSG("[WIFI] ----------------------------------------------\n");
  93. }
  94. if (code == MESSAGE_DISCONNECTED) {
  95. DEBUG_MSG("[WIFI] Disconnected\n");
  96. }
  97. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  98. DEBUG_MSG("[WIFI] Creating access point\n");
  99. }
  100. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  101. DEBUG_MSG("[WIFI] Could not create access point\n");
  102. }
  103. #endif
  104. // Disconnect from MQTT server if no WIFI
  105. if (code != MESSAGE_CONNECTED) {
  106. if (mqttConnected()) mqttDisconnect();
  107. }
  108. // Configure mDNS
  109. if (code == MESSAGE_CONNECTED) {
  110. if (MDNS.begin((char *) WiFi.hostname().c_str())) {
  111. MDNS.addService("http", "tcp", 80);
  112. DEBUG_MSG("[MDNS] OK\n");
  113. } else {
  114. DEBUG_MSG("[MDNS] FAIL\n");
  115. }
  116. }
  117. // Configure captive portal
  118. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  119. dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
  120. }
  121. if (code == MESSAGE_DISCONNECTED) {
  122. dnsServer.stop();
  123. }
  124. });
  125. }
  126. void wifiLoop() {
  127. jw.loop();
  128. if (WiFi.getMode() == WIFI_AP) {
  129. dnsServer.processNextRequest();
  130. }
  131. }