diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 74d957df..5d1ea992 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -74,7 +74,8 @@ //#define INFLUXDB_SUPPORT 1 //#define IR_SUPPORT 1 //#define LLMNR_SUPPORT 1 // Only with Arduino Core 2.4.0 -//#define MDNS_SUPPORT 0 +//#define MDNS_SERVER_SUPPORT 0 +//#define MDNS_CLIENT_SUPPORT 0 //#define MQTT_SUPPORT 0 //#define NETBIOS_SUPPORT 1 // Only with Arduino Core 2.4.0 //#define NOFUSS_SUPPORT 1 diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 32a56b51..2319a6ca 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -375,8 +375,12 @@ PROGMEM const char* const custom_reset_string[] = { // MDNS / LLMNR / NETBIOS / SSDP // ----------------------------------------------------------------------------- -#ifndef MDNS_SUPPORT -#define MDNS_SUPPORT 1 // Publish services using mDNS by default (1.84Kb) +#ifndef MDNS_SERVER_SUPPORT +#define MDNS_SERVER_SUPPORT 1 // Publish services using mDNS by default (1.48Kb) +#endif + +#ifndef MDNS_CLIENT_SUPPORT +#define MDNS_CLIENT_SUPPORT 1 // Resolve mDNS names (3.44Kb) #endif #ifndef LLMNR_SUPPORT @@ -451,7 +455,7 @@ PROGMEM const char* const custom_reset_string[] = { #define MQTT_SSL_FINGERPRINT "" // SSL fingerprint of the server #define MQTT_ENABLED 0 // Do not enable MQTT connection by default -#define MQTT_AUTOCONNECT 1 // If enabled and MDNS_SUPPORT=1 will perform an autodiscover and +#define MQTT_AUTOCONNECT 1 // If enabled and MDNS_SERVER_SUPPORT=1 will perform an autodiscover and // autoconnect to the first MQTT broker found if none defined #define MQTT_SERVER "" // Default MQTT broker address #define MQTT_USER "" // Default MQTT broker usename diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index d3f33820..0cb5b277 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -158,7 +158,7 @@ void welcome() { #if LLMNR_SUPPORT DEBUG_MSG_P(PSTR(" LLMNR")); #endif - #if MDNS_SUPPORT + #if MDNS_SERVER_SUPPORT DEBUG_MSG_P(PSTR(" MDNS")); #endif #if NETBIOS_SUPPORT @@ -328,8 +328,8 @@ void setup() { mqttSetup(); #endif - #if MDNS_SUPPORT - mdnsSetup(); + #if MDNS_SERVER_SUPPORT + mdnsServerSetup(); #endif #if LLMNR_SUPPORT llmnrSetup(); @@ -445,6 +445,9 @@ void loop() { #if SCHEDULER_SUPPORT schLoop(); #endif + #if MDNS_CLIENT_SUPPORT + mdnsClientLoop(); + #endif // Power saving delay delay(_loopDelay); diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index 1dc1bde0..3abe4a41 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -41,8 +41,12 @@ template bool idbSend(const char * topic, T payload) { if (!_idb_enabled) return true; if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true; - char host[64]; - getSetting("idbHost", INFLUXDB_HOST).toCharArray(host, sizeof(host)); + String h = getSetting("idbHost", INFLUXDB_HOST); + #if MDNS_CLIENT_SUPPORT + h = mdnsResolve(h); + #endif + char * host = strdup(h.c_str()); + int port = getSetting("idbPort", INFLUXDB_PORT).toInt(); DEBUG_MSG("[INFLUXDB] Sending to %s:%d\n", host, port); diff --git a/code/espurna/mdns.ino b/code/espurna/mdns.ino index 69db986a..630073dd 100644 --- a/code/espurna/mdns.ino +++ b/code/espurna/mdns.ino @@ -6,12 +6,17 @@ Copyright (C) 2017-2018 by Xose PĂ©rez */ -#if MDNS_SUPPORT +// ----------------------------------------------------------------------------- +// mDNS Server +// ----------------------------------------------------------------------------- + +#if MDNS_SERVER_SUPPORT #include #if MQTT_SUPPORT -void mdnsFindMQTT() { + +void _mdnsFindMQTT() { int count = MDNS.queryService("mqtt", "tcp"); DEBUG_MSG_P(PSTR("[MQTT] MQTT brokers found: %d\n"), count); for (int i=0; i +#include + +using namespace mDNSResolver; +WiFiUDP _mdns_udp; +Resolver _mdns_resolver(_mdns_udp); + +String mdnsResolve(char * name) { + + if (strlen(name) == 0) return String(); + if (WiFi.status() != WL_CONNECTED) return String(); + + _mdns_resolver.setLocalIP(WiFi.localIP()); + IPAddress ip = _mdns_resolver.search(name); + + if (ip == INADDR_NONE) return String(name); + DEBUG_MSG_P(PSTR("[MDNS] '%s' resolved to '%s'\n"), name, ip.toString().c_str()); + return ip.toString(); + +} + +String mdnsResolve(String name) { + return mdnsResolve((char *) name.c_str()); +} + +void mdnsClientLoop() { + _mdns_resolver.loop(); +} + +#endif // MDNS_CLIENT_SUPPORT diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 5f2c7102..2de271e7 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -343,8 +343,12 @@ void mqttConnect() { _mqtt_reconnect_delay = MQTT_RECONNECT_DELAY_MAX; } - char * host = strdup(getSetting("mqttServer", MQTT_SERVER).c_str()); - if (strlen(host) == 0) return; + String h = getSetting("mqttServer", MQTT_SERVER); + #if MDNS_CLIENT_SUPPORT + h = mdnsResolve(h); + #endif + char * host = strdup(h.c_str()); + unsigned int port = getSetting("mqttPort", MQTT_PORT).toInt(); if (_mqtt_user) free(_mqtt_user); diff --git a/code/espurna/nofuss.ino b/code/espurna/nofuss.ino index 004ba713..1112131f 100644 --- a/code/espurna/nofuss.ino +++ b/code/espurna/nofuss.ino @@ -27,6 +27,10 @@ void _nofussWebSocketOnSend(JsonObject& root) { void _nofussConfigure() { String nofussServer = getSetting("nofussServer", NOFUSS_SERVER); + #if MDNS_CLIENT_SUPPORT + nofussServer = mdnsResolve(nofussServer); + #endif + if (nofussServer.length() == 0) { setSetting("nofussEnabled", 0); _nofussEnabled = false; diff --git a/code/platformio.ini b/code/platformio.ini index 583697dc..9bfb87f7 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -24,6 +24,7 @@ lib_deps = https://github.com/krosk93/espsoftwareserial#a770677 SparkFun BME280 PMS Library + https://github.com/madpilot/mDNSResolver#4cfcda1 https://bitbucket.org/xoseperez/justwifi.git#1.1.6 https://bitbucket.org/xoseperez/hlw8012.git#1.1.0 https://bitbucket.org/xoseperez/fauxmoesp.git#2.4.1