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.

57 lines
1.5 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. void mdnsFindMQTT() {
  10. int count = MDNS.queryService("mqtt", "tcp");
  11. DEBUG_MSG_P("[MQTT] MQTT brokers found: %d\n", count);
  12. for (int i=0; i<count; i++) {
  13. DEBUG_MSG_P("[MQTT] Broker at %s:%d\n", MDNS.IP(i).toString().c_str(), MDNS.port(i));
  14. mqttSetBrokerIfNone(MDNS.IP(i), MDNS.port(i));
  15. }
  16. }
  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", DEVICE_NAME);
  35. _mdns_wifi_onSTA = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) {
  36. _mdnsStart();
  37. });
  38. _mdns_wifi_onAP = WiFi.onSoftAPModeStationConnected([](WiFiEventSoftAPModeStationConnected ipInfo) {
  39. _mdnsStart();
  40. });
  41. }
  42. #endif // MDNS_SUPPORT