basic working mdns configuration

master
Kenneth Barbour 2020-06-21 16:35:17 -04:00
parent 1a65ef30c6
commit 3c704ff51b
3 changed files with 53 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include "bme280.h"
#include "config.h"
#include "time.h"
#include "mdns.h"
#ifndef SERIAL_BAUD
#define SERIAL_BAUD 74880
@ -62,6 +63,9 @@ void setup()
// Start Filesystem
SPIFFS.begin();
// Start mDNS
uweather_mdns_init();
// Setup Web Server
uweather_web_init();
// if disabled and connected, shutdown web server
@ -82,5 +86,6 @@ void loop()
uweather_web_handle();
uweather_update_handle();
uweather_time_handle();
uweather_mdns_handle();
delay(10);
}

41
src/mdns.cpp 100644
View File

@ -0,0 +1,41 @@
#include <mdns.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266mDNS.h>
#include <Arduino.h>
#include "config.h"
#define DNS_PORT 53
DNSServer DNS;
bool is_active = false;
void uweather_mdns_init()
{
// TODO: this should also handle re-inits after hostname has changed?
const char* configured_host = (const char*) Config._data.mdns_hostname;
// If a host is configured, start mDNS
is_active = false;
if (WiFi.isConnected() && configured_host != nullptr && configured_host[0] != '\0') {
if (MDNS.begin("weather", WiFi.localIP())) {
MDNS.addService("http", "tcp", 80);
is_active = true;
Serial.printf("Started mDNS responder for hostname %s.local\n", configured_host);
} else {
Serial.println("Error starting mDNS responder");
}
}
// TODO: only use this when using softAP
DNS.start(DNS_PORT, "*", WiFi.softAPIP());
}
void uweather_mdns_handle()
{
DNS.processNextRequest(); // TODO only when using softap
if (is_active) {
MDNS.update();
}
}

7
src/mdns.h 100644
View File

@ -0,0 +1,7 @@
#ifndef _UWEATHER_MDNS_H_
#define _UWEATHER_MDNS_H_
void uweather_mdns_init();
void uweather_mdns_handle();
#endif /* End _UWEATHER_MDNS_H include guard */