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.

79 lines
2.0 KiB

  1. /*
  2. MDNS MODULE
  3. Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if MDNS_SUPPORT
  6. #include <ESP8266mDNS.h>
  7. #if MQTT_SUPPORT
  8. void mdnsFindMQTT() {
  9. int count = MDNS.queryService("mqtt", "tcp");
  10. DEBUG_MSG_P(PSTR("[MQTT] MQTT brokers found: %d\n"), count);
  11. for (int i=0; i<count; i++) {
  12. DEBUG_MSG_P(PSTR("[MQTT] Broker at %s:%d\n"), MDNS.IP(i).toString().c_str(), MDNS.port(i));
  13. mqttSetBrokerIfNone(MDNS.IP(i), MDNS.port(i));
  14. }
  15. }
  16. #endif
  17. void _mdnsStart() {
  18. if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) {
  19. DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
  20. } else {
  21. DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
  22. }
  23. }
  24. void mdnsSetup() {
  25. #if WEB_SUPPORT
  26. MDNS.addService("http", "tcp", getSetting("webPort", WEB_PORT).toInt());
  27. #endif
  28. #if TELNET_SUPPORT
  29. MDNS.addService("telnet", "tcp", TELNET_PORT);
  30. #endif
  31. // Public ESPurna related txt for OTA discovery
  32. MDNS.addServiceTxt("arduino", "tcp", "app_name", APP_NAME);
  33. MDNS.addServiceTxt("arduino", "tcp", "app_version", APP_VERSION);
  34. MDNS.addServiceTxt("arduino", "tcp", "target_board", getBoardName());
  35. {
  36. char buffer[6];
  37. itoa(ESP.getFlashChipRealSize() / 1024, buffer, 10);
  38. MDNS.addServiceTxt("arduino", "tcp", "mem_size", (const char *) buffer);
  39. }
  40. {
  41. char buffer[6];
  42. itoa(ESP.getFlashChipSize() / 1024, buffer, 10);
  43. MDNS.addServiceTxt("arduino", "tcp", "sdk_size", (const char *) buffer);
  44. }
  45. {
  46. char buffer[6];
  47. itoa(ESP.getFreeSketchSpace(), buffer, 10);
  48. MDNS.addServiceTxt("arduino", "tcp", "free_space", (const char *) buffer);
  49. }
  50. wifiRegister([](justwifi_messages_t code, char * parameter) {
  51. if (code == MESSAGE_CONNECTED) {
  52. _mdnsStart();
  53. #if MQTT_SUPPORT
  54. mdnsFindMQTT();
  55. #endif // MQTT_SUPPORT
  56. }
  57. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  58. _mdnsStart();
  59. }
  60. });
  61. }
  62. #endif // MDNS_SUPPORT