Browse Source

Resolve mDNS (.local) names in MQTT, InfluxDB and NoFUSS modules

i18n
Xose Pérez 7 years ago
parent
commit
16f2226805
8 changed files with 85 additions and 19 deletions
  1. +2
    -1
      code/espurna/config/arduino.h
  2. +7
    -3
      code/espurna/config/general.h
  3. +6
    -3
      code/espurna/espurna.ino
  4. +6
    -2
      code/espurna/influxdb.ino
  5. +53
    -8
      code/espurna/mdns.ino
  6. +6
    -2
      code/espurna/mqtt.ino
  7. +4
    -0
      code/espurna/nofuss.ino
  8. +1
    -0
      code/platformio.ini

+ 2
- 1
code/espurna/config/arduino.h View File

@ -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


+ 7
- 3
code/espurna/config/general.h View File

@ -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


+ 6
- 3
code/espurna/espurna.ino View File

@ -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);


+ 6
- 2
code/espurna/influxdb.ino View File

@ -41,8 +41,12 @@ template<typename T> 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);


+ 53
- 8
code/espurna/mdns.ino View File

@ -6,12 +6,17 @@ Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
*/
#if MDNS_SUPPORT
// -----------------------------------------------------------------------------
// mDNS Server
// -----------------------------------------------------------------------------
#if MDNS_SERVER_SUPPORT
#include <ESP8266mDNS.h>
#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<count; i++) {
@ -19,9 +24,10 @@ void mdnsFindMQTT() {
mqttSetBrokerIfNone(MDNS.IP(i), MDNS.port(i));
}
}
#endif
void _mdnsStart() {
void _mdnsServerStart() {
if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) {
DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
} else {
@ -29,7 +35,9 @@ void _mdnsStart() {
}
}
void mdnsSetup() {
// -----------------------------------------------------------------------------
void mdnsServerSetup() {
#if WEB_SUPPORT
MDNS.addService("http", "tcp", getSetting("webPort", WEB_PORT).toInt());
@ -62,18 +70,55 @@ void mdnsSetup() {
wifiRegister([](justwifi_messages_t code, char * parameter) {
if (code == MESSAGE_CONNECTED) {
_mdnsStart();
_mdnsServerStart();
#if MQTT_SUPPORT
mdnsFindMQTT();
_mdnsFindMQTT();
#endif // MQTT_SUPPORT
}
if (code == MESSAGE_ACCESSPOINT_CREATED) {
_mdnsStart();
_mdnsServerStart();
}
});
}
#endif // MDNS_SUPPORT
#endif // MDNS_SERVER_SUPPORT
// -----------------------------------------------------------------------------
// mDNS Client
// -----------------------------------------------------------------------------
#if MDNS_CLIENT_SUPPORT
#include <WiFiUdp.h>
#include <mDNSResolver.h>
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

+ 6
- 2
code/espurna/mqtt.ino View File

@ -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);


+ 4
- 0
code/espurna/nofuss.ino View File

@ -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;


+ 1
- 0
code/platformio.ini View File

@ -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


Loading…
Cancel
Save