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.

69 lines
1.8 KiB

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