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.

98 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. /*
  2. MDNS MODULE
  3. Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. // -----------------------------------------------------------------------------
  6. // mDNS Server
  7. // -----------------------------------------------------------------------------
  8. #include "mdns.h"
  9. #include "mqtt.h"
  10. #include "utils.h"
  11. #if MDNS_SERVER_SUPPORT
  12. #include <ESP8266mDNS.h>
  13. #if MQTT_SUPPORT
  14. void _mdnsFindMQTT() {
  15. int count = MDNS.queryService("mqtt", "tcp");
  16. DEBUG_MSG_P(PSTR("[MQTT] MQTT brokers found: %d\n"), count);
  17. for (int i=0; i<count; i++) {
  18. DEBUG_MSG_P(PSTR("[MQTT] Broker at %s:%d\n"), MDNS.IP(i).toString().c_str(), MDNS.port(i));
  19. mqttSetBrokerIfNone(MDNS.IP(i), MDNS.port(i));
  20. }
  21. }
  22. #endif
  23. void _mdnsServerStart() {
  24. if (MDNS.begin(getSetting("hostname", getIdentifier()))) {
  25. DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
  26. } else {
  27. DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
  28. }
  29. }
  30. // -----------------------------------------------------------------------------
  31. void mdnsServerSetup() {
  32. bool done { false };
  33. #if WEB_SUPPORT
  34. {
  35. MDNS.addService("http", "tcp", getSetting("webPort", static_cast<uint16_t>(WEB_PORT)));
  36. done = true;
  37. }
  38. #endif
  39. #if TELNET_SUPPORT
  40. {
  41. MDNS.addService("telnet", "tcp", TELNET_PORT);
  42. done = true;
  43. }
  44. #endif
  45. #if OTA_ARDUINOOTA_SUPPORT
  46. {
  47. MDNS.addServiceTxt("arduino", "tcp", "app_name", APP_NAME);
  48. MDNS.addServiceTxt("arduino", "tcp", "app_version", getVersion());
  49. MDNS.addServiceTxt("arduino", "tcp", "build_date", buildTime());
  50. MDNS.addServiceTxt("arduino", "tcp", "mac", WiFi.macAddress());
  51. MDNS.addServiceTxt("arduino", "tcp", "target_board", getBoardName());
  52. MDNS.addServiceTxt("arduino", "tcp", "mem_size",
  53. String(static_cast<int>(ESP.getFlashChipRealSize() / 1024), 10));
  54. MDNS.addServiceTxt("arduino", "tcp", "sdk_size",
  55. String(static_cast<int>(ESP.getFlashChipSize() / 1024), 10));
  56. MDNS.addServiceTxt("arduino", "tcp", "free_space",
  57. String(static_cast<int>(ESP.getFreeSketchSpace() / 1024), 10));
  58. done = true;
  59. }
  60. #endif
  61. if (!done) {
  62. return;
  63. }
  64. wifiRegister([](justwifi_messages_t code, char * parameter) {
  65. if (code == MESSAGE_CONNECTED) {
  66. _mdnsServerStart();
  67. #if MQTT_SUPPORT
  68. _mdnsFindMQTT();
  69. #endif
  70. }
  71. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  72. _mdnsServerStart();
  73. }
  74. });
  75. }
  76. #endif // MDNS_SERVER_SUPPORT