Add support for mDNS #161

This commit is contained in:
proddy
2021-10-18 13:02:22 +02:00
parent e17ce9c3b5
commit 45fc6daa4a
7 changed files with 40 additions and 20 deletions

View File

@@ -14,6 +14,7 @@
- Add current room influence for RC300[#136] - Add current room influence for RC300[#136]
- Added Home Assistant device_class to sensor entities - Added Home Assistant device_class to sensor entities
- Added another Buderus RC10 thermostat with Product ID 65 [#160](https://github.com/emsesp/EMS-ESP32/issues/160) - Added another Buderus RC10 thermostat with Product ID 65 [#160](https://github.com/emsesp/EMS-ESP32/issues/160)
- Added support for mDNS [#161](https://github.com/emsesp/EMS-ESP32/issues/161)
## Fixed ## Fixed

View File

@@ -62,6 +62,7 @@ void NetworkSettingsService::manageSTA() {
} }
WiFi.setHostname(_state.hostname.c_str()); // set hostname WiFi.setHostname(_state.hostname.c_str()); // set hostname
// www.esp32.com/viewtopic.php?t=12055 // www.esp32.com/viewtopic.php?t=12055
read([&](NetworkSettings & networkSettings) { read([&](NetworkSettings & networkSettings) {
if (networkSettings.bandwidth20) { if (networkSettings.bandwidth20) {
@@ -74,31 +75,17 @@ void NetworkSettingsService::manageSTA() {
WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE
} }
}); });
WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network
} }
} }
// handles both WiFI and Ethernet // handles if wifi stopped
void NetworkSettingsService::WiFiEvent(WiFiEvent_t event) { void NetworkSettingsService::WiFiEvent(WiFiEvent_t event) {
switch (event) { if (event == SYSTEM_EVENT_STA_STOP) {
case SYSTEM_EVENT_STA_DISCONNECTED:
WiFi.disconnect(true);
break;
case SYSTEM_EVENT_STA_STOP:
if (_stopping) { if (_stopping) {
_lastConnectionAttempt = 0; _lastConnectionAttempt = 0;
_stopping = false; _stopping = false;
} }
break;
case SYSTEM_EVENT_ETH_START:
if (_state.staticIPConfig) {
ETH.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2); // configure for static IP
}
break;
default:
break;
} }
} }

View File

@@ -59,6 +59,7 @@ void OTASettingsService::configureArduinoOTA() {
Serial.println(F("End Failed")); Serial.println(F("End Failed"));
}); });
_arduinoOTA->setMdnsEnabled(false); // disable as handled in NetworkSettingsService.cpp. https://github.com/emsesp/EMS-ESP32/issues/161
_arduinoOTA->begin(); _arduinoOTA->begin();
} }
} }

View File

@@ -4,8 +4,6 @@
#include <HttpEndpoint.h> #include <HttpEndpoint.h>
#include <FSPersistence.h> #include <FSPersistence.h>
#include <ESPmDNS.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>

View File

@@ -1 +1 @@
#define EMSESP_APP_VERSION "3.2.2b12" #define EMSESP_APP_VERSION "3.2.2b13"

View File

@@ -35,6 +35,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
switch (event) { switch (event) {
case SYSTEM_EVENT_STA_DISCONNECTED: case SYSTEM_EVENT_STA_DISCONNECTED:
EMSESP::logger().info(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason); EMSESP::logger().info(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason);
WiFi.disconnect(true);
break; break;
case SYSTEM_EVENT_STA_GOT_IP: case SYSTEM_EVENT_STA_GOT_IP:
@@ -47,11 +48,20 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
EMSESP::system_.syslog_start(); EMSESP::system_.syslog_start();
} }
}); });
mDNS_start();
break; break;
case SYSTEM_EVENT_ETH_START: case SYSTEM_EVENT_ETH_START:
EMSESP::logger().info(F("Ethernet initialized")); EMSESP::logger().info(F("Ethernet initialized"));
ETH.setHostname(EMSESP::system_.hostname().c_str()); ETH.setHostname(EMSESP::system_.hostname().c_str());
// configure for static IP
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
if (networkSettings.staticIPConfig) {
ETH.config(networkSettings.localIP, networkSettings.gatewayIP, networkSettings.subnetMask, networkSettings.dnsIP1, networkSettings.dnsIP2);
}
});
break; break;
case SYSTEM_EVENT_ETH_GOT_IP: case SYSTEM_EVENT_ETH_GOT_IP:
@@ -67,6 +77,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
} }
}); });
EMSESP::system_.ethernet_connected(true); EMSESP::system_.ethernet_connected(true);
mDNS_start();
} }
break; break;
@@ -105,6 +116,7 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
} }
EMSESP::system_.send_heartbeat(); EMSESP::system_.send_heartbeat();
EMSESP::system_.syslog_start(); EMSESP::system_.syslog_start();
mDNS_start();
break; break;
#endif #endif
@@ -127,4 +139,22 @@ void WebStatusService::webStatusService(AsyncWebServerRequest * request) {
request->send(response); request->send(response);
} }
// start the multicast UDP service so EMS-ESP is discoverable via .local
void WebStatusService::mDNS_start() {
if (!MDNS.begin(EMSESP::system_.hostname().c_str())) {
EMSESP::logger().warning(F("Failed to start mDNS responder service"));
return;
}
std::string address_s = EMSESP::system_.hostname() + ".local";
MDNS.addService("http", "tcp", 80); // add our web server and rest API
MDNS.addService("telnet", "tcp", 23); // add our telnet console
MDNS.addServiceTxt("http", "tcp", "version", EMSESP_APP_VERSION);
MDNS.addServiceTxt("http", "tcp", "address", address_s.c_str());
EMSESP::logger().info(F("mDNS responder service started"));
}
} // namespace emsesp } // namespace emsesp

View File

@@ -25,6 +25,8 @@
#include <SecurityManager.h> #include <SecurityManager.h>
#include <AsyncMqttClient.h> #include <AsyncMqttClient.h>
#include <ESPmDNS.h>
#define EMSESP_STATUS_SERVICE_PATH "/rest/emsespStatus" #define EMSESP_STATUS_SERVICE_PATH "/rest/emsespStatus"
namespace emsesp { namespace emsesp {
@@ -36,6 +38,7 @@ class WebStatusService {
private: private:
void webStatusService(AsyncWebServerRequest * request); void webStatusService(AsyncWebServerRequest * request);
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info); void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info);
void mDNS_start();
}; };
} // namespace emsesp } // namespace emsesp