Browse Source

Moving MDNS code to its own module

fastled
Xose Pérez 6 years ago
parent
commit
b34da11508
6 changed files with 94 additions and 68 deletions
  1. +10
    -7
      code/espurna/espurna.ino
  2. +57
    -0
      code/espurna/mdns.ino
  3. +7
    -18
      code/espurna/mqtt.ino
  4. +18
    -4
      code/espurna/ntp.ino
  5. +0
    -5
      code/espurna/ota.ino
  6. +2
    -34
      code/espurna/wifi.ino

+ 10
- 7
code/espurna/espurna.ino View File

@ -268,11 +268,11 @@ void setup() {
ledSetup();
mqttSetup();
#ifdef ITEAD_SONOFF_RFBRIDGE
rfbSetup();
#if MDNS_SUPPORT
mdnsSetup();
#endif
#if POWER_PROVIDER != POWER_PROVIDER_NONE
powerSetup();
#if LLMNR_SUPPORT
llmnrSetup();
#endif
#if NTP_SUPPORT
ntpSetup();
@ -280,6 +280,12 @@ void setup() {
#if I2C_SUPPORT
i2cSetup();
#endif
#ifdef ITEAD_SONOFF_RFBRIDGE
rfbSetup();
#endif
#if POWER_PROVIDER != POWER_PROVIDER_NONE
powerSetup();
#endif
#if ALEXA_SUPPORT
alexaSetup();
#endif
@ -313,9 +319,6 @@ void setup() {
#if HOMEASSISTANT_SUPPORT
haSetup();
#endif
#if LLMNR_SUPPORT
llmnrSetup();
#endif
// Prepare configuration for version 2.0
hwUpwardsCompatibility();


+ 57
- 0
code/espurna/mdns.ino View File

@ -0,0 +1,57 @@
/*
MDNS MODULE
Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
*/
#if MDNS_SUPPORT
#include <ESP8266mDNS.h>
WiFiEventHandler _mdns_wifi_onSTA;
WiFiEventHandler _mdns_wifi_onAP;
void mdnsFindMQTT() {
int count = MDNS.queryService("mqtt", "tcp");
DEBUG_MSG_P("[MQTT] MQTT brokers found: %d\n", count);
for (int i=0; i<count; i++) {
DEBUG_MSG_P("[MQTT] Broker at %s:%d\n", MDNS.IP(i).toString().c_str(), MDNS.port(i));
mqttSetBrokerIfNone(MDNS.IP(i), MDNS.port(i));
}
}
void _mdnsStart() {
if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) {
DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
} else {
DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
}
}
void mdnsSetup() {
#if WEB_SUPPORT
MDNS.addService("http", "tcp", getSetting("webPort", WEB_PORT).toInt());
#endif
#if TELNET_SUPPORT
MDNS.addService("telnet", "tcp", TELNET_PORT);
#endif
// Public ESPurna related txt for OTA discovery
MDNS.addServiceTxt("arduino", "tcp", "app_name", APP_NAME);
MDNS.addServiceTxt("arduino", "tcp", "app_version", APP_VERSION);
MDNS.addServiceTxt("arduino", "tcp", "target_board", DEVICE_NAME);
_mdns_wifi_onSTA = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) {
_mdnsStart();
});
_mdns_wifi_onAP = WiFi.onSoftAPModeStationConnected([](WiFiEventSoftAPModeStationConnected ipInfo) {
_mdnsStart();
});
}
#endif // MDNS_SUPPORT

+ 7
- 18
code/espurna/mqtt.ino View File

@ -486,26 +486,15 @@ void mqttConfigure() {
}
#if MDNS_SUPPORT
boolean mqttDiscover() {
int count = MDNS.queryService("mqtt", "tcp");
DEBUG_MSG_P("[MQTT] MQTT brokers found: %d\n", count);
for (int i=0; i<count; i++) {
DEBUG_MSG_P("[MQTT] Broker at %s:%d\n", MDNS.IP(i).toString().c_str(), MDNS.port(i));
if ((i==0) && (getSetting("mqttServer").length() == 0)) {
setSetting("mqttServer", MDNS.IP(i).toString());
setSetting("mqttPort", MDNS.port(i));
mqttEnabled(MQTT_AUTOCONNECT);
}
}
void mqttSetBroker(IPAddress ip, unsigned int port) {
setSetting("mqttServer", ip.toString());
setSetting("mqttPort", port);
mqttEnabled(MQTT_AUTOCONNECT);
}
void mqttSetBrokerIfNone(IPAddress ip, unsigned int port) {
if (!hasSetting("mqttServer")) mqttSetBroker(ip, port);
}
#endif // MDNS_SUPPORT
void mqttSetup() {


+ 18
- 4
code/espurna/ntp.ino View File

@ -11,11 +11,19 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <WiFiClient.h>
#include <Ticker.h>
WiFiEventHandler _ntp_wifi_onSTA;
Ticker _ntp_delay;
// -----------------------------------------------------------------------------
// NTP
// -----------------------------------------------------------------------------
void _ntpDebug() {
DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
}
void ntpConfigure() {
NTP.begin(
getSetting("ntpServer1", NTP_SERVER),
@ -46,23 +54,29 @@ String ntpDateTime() {
}
void ntpSetup() {
NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
if (error) {
#if WEB_SUPPORT
wsSend_P(PSTR("{\"ntpStatus\": false}"));
#endif
if (error == noResponse) {
DEBUG_MSG_P(PSTR("[NTP] Error: NTP server not reachable\n"));
} else if (error == invalidAddress) {
DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n"));
}
#if WEB_SUPPORT
wsSend_P(PSTR("{\"ntpStatus\": false}"));
#endif
} else {
DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
#if WEB_SUPPORT
wsSend_P(PSTR("{\"ntpStatus\": true}"));
#endif
_ntp_delay.once_ms(100, _ntpDebug);
}
});
_ntp_wifi_onSTA = WiFi.onStationModeGotIP([](WiFiEventStationModeGotIP ipInfo) {
ntpConfigure();
});
}
void ntpLoop() {


+ 0
- 5
code/espurna/ota.ino View File

@ -54,11 +54,6 @@ void otaSetup() {
ArduinoOTA.begin();
// Public ESPurna related txt for OTA discovery
MDNS.addServiceTxt("arduino", "tcp", "app_name", APP_NAME);
MDNS.addServiceTxt("arduino", "tcp", "app_version", APP_VERSION);
MDNS.addServiceTxt("arduino", "tcp", "target_board", DEVICE_NAME);
}
void otaLoop() {


+ 2
- 34
code/espurna/wifi.ino View File

@ -7,7 +7,6 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
*/
#include "JustWifi.h"
#include <ESP8266mDNS.h>
// -----------------------------------------------------------------------------
// WIFI
@ -240,40 +239,9 @@ void wifiSetup() {
#endif // DEBUG_SUPPORT
// Configure mDNS
#if MDNS_SUPPORT
if (code == MESSAGE_CONNECTED || code == MESSAGE_ACCESSPOINT_CREATED) {
if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) {
DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
#if WEB_SUPPORT
MDNS.addService("http", "tcp", getSetting("webPort", WEB_PORT).toInt());
#endif
#if TELNET_SUPPORT
MDNS.addService("telnet", "tcp", TELNET_PORT);
#endif
if (code == MESSAGE_CONNECTED) mqttDiscover();
} else {
DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
}
}
#endif
// NTP connection reset
#if NTP_SUPPORT
if (code == MESSAGE_CONNECTED) {
ntpConfigure();
}
#endif
if (code == MESSAGE_CONNECTED) mdnsFindMQTT();
#endif // MDNS_SUPPORT
});


Loading…
Cancel
Save