Skip to content

Commit

Permalink
Implemented mDNS
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderRedYT committed Oct 30, 2023
1 parent 2991af7 commit 7c87518
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ struct CONFIG_T {
char WiFi_Hostname[WIFI_MAX_HOSTNAME_STRLEN + 1];
uint32_t WiFi_ApTimeout;

bool Mdns_Enabled;

char Ntp_Server[NTP_MAX_SERVER_STRLEN + 1];
char Ntp_Timezone[NTP_MAX_TIMEZONE_STRLEN + 1];
char Ntp_TimezoneDescr[NTP_MAX_TIMEZONEDESCR_STRLEN + 1];
Expand Down
2 changes: 2 additions & 0 deletions include/NetworkSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class NetworkSettingsClass {
private:
void setHostname();
void setStaticIp();
void handleMDNS();
void setupMode();
void NetworkEvent(WiFiEvent_t event);
bool adminEnabled = true;
Expand All @@ -76,6 +77,7 @@ class NetworkSettingsClass {
network_mode _networkMode = network_mode::Undefined;
bool _ethConnected = false;
std::vector<NetworkEventCbList_t> _cbEventList;
bool lastMdnsEnabled = false;
};

extern NetworkSettingsClass NetworkSettings;
4 changes: 3 additions & 1 deletion include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define WIFI_PASSWORD ""
#define WIFI_DHCP true

#define MDNS_ENABLED false

#define NTP_SERVER "pool.ntp.org"
#define NTP_TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3"
#define NTP_TIMEZONEDESCR "Europe/Berlin"
Expand Down Expand Up @@ -96,4 +98,4 @@
#define DISPLAY_CONTRAST 60U
#define DISPLAY_LANGUAGE 0U

#define REACHABLE_THRESHOLD 2U
#define REACHABLE_THRESHOLD 2U
6 changes: 6 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ bool ConfigurationClass::write()
wifi["hostname"] = config.WiFi_Hostname;
wifi["aptimeout"] = config.WiFi_ApTimeout;

JsonObject mdns = doc.createNestedObject("mdns");
mdns["enabled"] = config.Mdns_Enabled;

JsonObject ntp = doc.createNestedObject("ntp");
ntp["server"] = config.Ntp_Server;
ntp["timezone"] = config.Ntp_Timezone;
Expand Down Expand Up @@ -191,6 +194,9 @@ bool ConfigurationClass::read()
config.WiFi_Dhcp = wifi["dhcp"] | WIFI_DHCP;
config.WiFi_ApTimeout = wifi["aptimeout"] | ACCESS_POINT_TIMEOUT;

JsonObject mdns = doc["mdns"];
config.Mdns_Enabled = mdns["enabled"] | MDNS_ENABLED;

JsonObject ntp = doc["ntp"];
strlcpy(config.Ntp_Server, ntp["server"] | NTP_SERVER, sizeof(config.Ntp_Server));
strlcpy(config.Ntp_Timezone, ntp["timezone"] | NTP_TIMEZONE, sizeof(config.Ntp_Timezone));
Expand Down
34 changes: 33 additions & 1 deletion src/NetworkSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "PinMapping.h"
#include "Utils.h"
#include "defaults.h"
#include <ESPmDNS.h>
#include <ETH.h>

NetworkSettingsClass::NetworkSettingsClass()
Expand Down Expand Up @@ -110,6 +111,35 @@ void NetworkSettingsClass::raiseEvent(network_event event)
}
}

void NetworkSettingsClass::handleMDNS()
{
bool mdnsEnabled = Configuration.get().Mdns_Enabled;

if (lastMdnsEnabled == mdnsEnabled) {
return;
}

lastMdnsEnabled = mdnsEnabled;

MDNS.end();

if (!mdnsEnabled) {
return;
}

if (MDNS.begin(getHostname())) {
MessageOutput.print("MDNS responder starting...");

MDNS.addService("http", "tcp", 80);
MDNS.addService("opendtu", "tcp", 80);
MDNS.addServiceTxt("opendtu", "tcp", "git_hash", AUTO_GIT_HASH);

MessageOutput.println("done");
} else {
MessageOutput.println("Error setting up MDNS responder!");
}
}

void NetworkSettingsClass::setupMode()
{
if (adminEnabled) {
Expand Down Expand Up @@ -218,6 +248,8 @@ void NetworkSettingsClass::loop()
if (dnsServerStatus) {
dnsServer->processNextRequest();
}

handleMDNS();
}

void NetworkSettingsClass::applyConfig()
Expand Down Expand Up @@ -417,4 +449,4 @@ network_mode NetworkSettingsClass::NetworkMode()
return _networkMode;
}

NetworkSettingsClass NetworkSettings;
NetworkSettingsClass NetworkSettings;
2 changes: 2 additions & 0 deletions src/WebApi_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void WebApiNetworkClass::onNetworkAdminGet(AsyncWebServerRequest* request)
root["ssid"] = config.WiFi_Ssid;
root["password"] = config.WiFi_Password;
root["aptimeout"] = config.WiFi_ApTimeout;
root["mdnsenabled"] = config.Mdns_Enabled;

response->setLength();
request->send(response);
Expand Down Expand Up @@ -236,6 +237,7 @@ void WebApiNetworkClass::onNetworkAdminPost(AsyncWebServerRequest* request)
config.WiFi_Dhcp = false;
}
config.WiFi_ApTimeout = root["aptimeout"].as<uint>();
config.Mdns_Enabled = root["mdnsenabled"].as<bool>();
Configuration.write();

retMsg["type"] = "success";
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@
"ApTimeout": "AccessPoint Zeitlimit:",
"ApTimeoutHint": "Zeit die der AccessPoint offen gehalten wird. Ein Wert von 0 bedeutet unendlich.",
"Minutes": "Minuten",
"Save": "@:dtuadmin.Save"
"Save": "@:dtuadmin.Save",
"EnableMdns": "mDNS aktivieren",
"MdnsSettings": "mDNS-Einstellungen"
},
"mqttadmin": {
"MqttSettings": "MQTT-Einstellungen",
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@
"ApTimeout": "AccessPoint Timeout:",
"ApTimeoutHint": "Time which the AccessPoint is kept open. A value of 0 means infinite.",
"Minutes": "minutes",
"Save": "@:dtuadmin.Save"
"Save": "@:dtuadmin.Save",
"EnableMdns": "Enable mDNS",
"MdnsSettings": "mDNS Settings"
},
"mqttadmin": {
"MqttSettings": "MQTT Settings",
Expand Down
6 changes: 4 additions & 2 deletions webapp/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@
"ApTimeout": "Délai d'attente du point d'accès",
"ApTimeoutHint": "Durée pendant laquelle le point d'accès reste ouvert. Une valeur de 0 signifie infini.",
"Minutes": "minutes",
"Save": "@:dtuadmin.Save"
"Save": "@:dtuadmin.Save",
"EnableMdns": "Activer mDNS",
"MdnsSettings": "mDNS Settings"
},
"mqttadmin": {
"MqttSettings": "Paramètres MQTT",
Expand Down Expand Up @@ -580,4 +582,4 @@
"ValueSelected": "Sélectionné",
"ValueActive": "Activé"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface NetworkConfig {
dns1: string;
dns2: string;
aptimeout: number;
}
mdnsenabled: boolean;
}
10 changes: 8 additions & 2 deletions webapp/src/views/NetworkAdminView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
type="text" maxlength="32"/>
</CardElement>

<CardElement :text="$t('networkadmin.MdnsSettings')" textVariant="text-bg-primary" add-space>
<InputElement :label="$t('networkadmin.EnableMdns')"
v-model="networkConfigList.mdnsenabled"
type="checkbox"/>
</CardElement>

<CardElement :text="$t('networkadmin.AdminAp')" textVariant="text-bg-primary" add-space>
<InputElement :label="$t('networkadmin.ApTimeout')"
v-model="networkConfigList.aptimeout"
Expand All @@ -67,7 +73,7 @@ import BasePage from '@/components/BasePage.vue';
import BootstrapAlert from "@/components/BootstrapAlert.vue";
import CardElement from '@/components/CardElement.vue';
import InputElement from '@/components/InputElement.vue';
import type { NetworkConfig } from "@/types/NetworkkConfig";
import type { NetworkConfig } from "@/types/NetworkConfig";
import { authHeader, handleResponse } from '@/utils/authentication';
import { defineComponent } from 'vue';
Expand Down Expand Up @@ -122,4 +128,4 @@ export default defineComponent({
},
},
});
</script>
</script>

0 comments on commit 7c87518

Please sign in to comment.