mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-09-14 10:35:30 +00:00
- UDP / ESPEasyP2P now works for ETHERNET
- Hostname Handling refactored for better overview: - String NetworkGetHostNameFromSettings() returns Settings.getHostname() - String NetworkCreateRFCCompliantHostname() creates a RFCcompliant Hostname from Settings.cpp - String NetworkGetHostname() returns the Hostname prevoiusly set in ETH or Wifi dependong on Mode - MacAddress Handling refactored - String NetworkMacAddress() returns the Mac addres of ETH or Wifi depending on Mode - String WifiSoftAPmacAddress() returns WiFi.softAPmacAddress(mac) as String
This commit is contained in:
+6
-1
@@ -995,7 +995,12 @@ void backgroundtasks()
|
||||
if (webserverRunning) {
|
||||
web_server.handleClient();
|
||||
}
|
||||
if (WiFi.getMode() != WIFI_OFF) {
|
||||
if (WiFi.getMode() != WIFI_OFF
|
||||
// This makes UDP working for ETHERNET
|
||||
#ifdef HAS_ETHERNET
|
||||
|| eth_connected
|
||||
#endif
|
||||
) {
|
||||
checkUDP();
|
||||
}
|
||||
}
|
||||
|
||||
+14
-2
@@ -1,9 +1,10 @@
|
||||
#include "ESPEasyEth.h"
|
||||
|
||||
#ifdef HAS_ETHERNET
|
||||
|
||||
#include "ESPEasyEth.h"
|
||||
#include "ESPEasyNetwork.h"
|
||||
#include "ETH.h"
|
||||
#include "ESPEasy-Globals.h"
|
||||
#include "eth_phy/phy.h"
|
||||
|
||||
bool ethUseStaticIP() {
|
||||
return Settings.ETH_IP[0] != 0 && Settings.ETH_IP[3] != 255;
|
||||
@@ -53,6 +54,9 @@ bool ethPrepare() {
|
||||
addLog(LOG_LEVEL_ERROR, F("ETH: Settings not correct!!!"));
|
||||
return false;
|
||||
}
|
||||
char hostname[40];
|
||||
safe_strncpy(hostname, NetworkCreateRFCCompliantHostname().c_str(), sizeof(hostname));
|
||||
ETH.setHostname(hostname);
|
||||
ETH.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
|
||||
ethSetupStaticIPconfig();
|
||||
return true;
|
||||
@@ -98,6 +102,14 @@ void ethPrintSettings() {
|
||||
addLog(LOG_LEVEL_INFO, settingsDebugLog);
|
||||
}
|
||||
|
||||
String ETHMacAddress() {
|
||||
uint8_t mac[6];
|
||||
char macStr[18] = { 0 };
|
||||
esp_eth_get_mac(mac);
|
||||
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return String(macStr);
|
||||
}
|
||||
|
||||
void ETHConnectRelaxed() {
|
||||
ethPrintSettings();
|
||||
ETH.begin(Settings.ETH_Phy_Addr,
|
||||
|
||||
+32
-21
@@ -105,29 +105,13 @@ IPAddress NetworkDnsIP (uint8_t dns_no) {
|
||||
#endif
|
||||
}
|
||||
|
||||
uint8_t * NetworkMacAddressAsBytes(uint8_t* mac) {
|
||||
#ifdef HAS_ETHERNET
|
||||
if(eth_wifi_mode == ETHERNET) {
|
||||
if(eth_connected) {
|
||||
return WiFi.macAddress(mac);
|
||||
} else {
|
||||
addLog(LOG_LEVEL_ERROR, F("Call NetworkMacAddressAsBytes(uint8_t* mac) only on connected Ethernet!"));
|
||||
return mac;
|
||||
}
|
||||
} else {
|
||||
return WiFi.macAddress(mac);
|
||||
}
|
||||
#else
|
||||
return WiFi.macAddress(mac);
|
||||
#endif
|
||||
return WiFi.macAddress(mac);
|
||||
}
|
||||
|
||||
String NetworkMacAddress() {
|
||||
#ifdef HAS_ETHERNET
|
||||
if(eth_wifi_mode == ETHERNET) {
|
||||
if(!eth_connected) {
|
||||
addLog(LOG_LEVEL_ERROR, F("Call NetworkMacAddress() only on connected Ethernet!"));
|
||||
} else {
|
||||
return ETH.macAddress();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -140,16 +124,35 @@ String NetworkMacAddress() {
|
||||
return String(macaddress);
|
||||
}
|
||||
|
||||
uint8_t * NetworkMacAddressAsBytes(uint8_t* mac) {
|
||||
return WiFi.macAddress(mac);
|
||||
}
|
||||
|
||||
String NetworkGetHostname() {
|
||||
#ifdef ESP32
|
||||
#ifdef HAS_ETHERNET
|
||||
if(Settings.ETH_Wifi_Mode == ETHERNET) {
|
||||
return String(ETH.getHostname());
|
||||
}
|
||||
return String(WiFi.getHostname());
|
||||
#else
|
||||
return String(WiFi.getHostname());
|
||||
#endif
|
||||
#else
|
||||
return String(WiFi.hostname());
|
||||
#endif
|
||||
}
|
||||
|
||||
// ********************************************************************************
|
||||
// Determine Wifi AP name to set. (also used for mDNS)
|
||||
// ********************************************************************************
|
||||
String NetworkGetAPssid()
|
||||
String NetworkGetHostNameFromSettings()
|
||||
{
|
||||
return Settings.getHostname();
|
||||
}
|
||||
|
||||
String NetworkGetHostname() {
|
||||
return createRFCCompliantHostname(NetworkGetAPssid());
|
||||
String NetworkCreateRFCCompliantHostname() {
|
||||
return createRFCCompliantHostname(NetworkGetHostNameFromSettings());
|
||||
}
|
||||
|
||||
// Create hostname with - instead of spaces
|
||||
@@ -159,4 +162,12 @@ String createRFCCompliantHostname(String oldString) {
|
||||
result.replace(" ", "-");
|
||||
result.replace("_", "-"); // See RFC952
|
||||
return result;
|
||||
}
|
||||
|
||||
String WifiSoftAPmacAddress() {
|
||||
uint8_t mac[] = { 0, 0, 0, 0, 0, 0 };
|
||||
uint8_t *macread = WiFi.softAPmacAddress(mac);
|
||||
char macaddress[20];
|
||||
formatMAC(macread, macaddress);
|
||||
return String(macaddress);
|
||||
}
|
||||
@@ -11,9 +11,11 @@ IPAddress NetworkGatewayIP();
|
||||
IPAddress NetworkDnsIP (uint8_t dns_no);
|
||||
uint8_t * NetworkMacAddressAsBytes(uint8_t* mac);
|
||||
String NetworkMacAddress();
|
||||
String NetworkGetAPssid();
|
||||
String NetworkGetHostNameFromSettings();
|
||||
String NetworkGetHostname();
|
||||
String NetworkCreateRFCCompliantHostname();
|
||||
String createRFCCompliantHostname(String oldString);
|
||||
String WifiSoftAPmacAddress();
|
||||
|
||||
|
||||
#endif // NETWORK_H
|
||||
@@ -112,14 +112,6 @@ void WiFiEvent(system_event_id_t event, system_event_info_t info) {
|
||||
#ifdef HAS_ETHERNET
|
||||
case SYSTEM_EVENT_ETH_START:
|
||||
addLog(LOG_LEVEL_INFO, F("ETH Started"));
|
||||
char hostname[40];
|
||||
safe_strncpy(hostname, NetworkGetHostname()).c_str(), sizeof(hostname));
|
||||
ETH.setHostname(hostname);
|
||||
{
|
||||
String log = F("ETH Hostname: ");
|
||||
log += String(hostname);
|
||||
addLog(LOG_LEVEL_INFO, log);
|
||||
}
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_CONNECTED:
|
||||
addLog(LOG_LEVEL_INFO, F("ETH Connected"));
|
||||
|
||||
+1
-1
@@ -384,7 +384,7 @@ void setAPinternal(bool enable)
|
||||
if (enable) {
|
||||
// create and store unique AP SSID/PW to prevent ESP from starting AP mode with default SSID and No password!
|
||||
// setup ssid for AP Mode when needed
|
||||
String softAPSSID = NetworkGetAPssid();
|
||||
String softAPSSID = NetworkCreateRFCCompliantHostname();
|
||||
String pwd = SecuritySettings.WifiAPKey;
|
||||
IPAddress subnet(DEFAULT_AP_SUBNET);
|
||||
|
||||
|
||||
+6
-5
@@ -346,7 +346,8 @@ void sendSysInfoUDP(byte repeats)
|
||||
for (byte counter = 0; counter < repeats; counter++)
|
||||
{
|
||||
uint8_t mac[] = { 0, 0, 0, 0, 0, 0 };
|
||||
uint8_t *macread = WiFi.macAddress(mac);
|
||||
uint8_t *macread = NetworkMacAddressAsBytes(mac);
|
||||
|
||||
byte data[80];
|
||||
data[0] = 255;
|
||||
data[1] = 1;
|
||||
@@ -406,7 +407,7 @@ void SSDP_schema(WiFiClient& client) {
|
||||
return;
|
||||
}
|
||||
|
||||
const IPAddress ip = WiFi.localIP();
|
||||
const IPAddress ip = NetworkLocalIP();
|
||||
const uint32_t chipId = ESP.getChipId();
|
||||
char uuid[64];
|
||||
sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
|
||||
@@ -498,7 +499,7 @@ bool SSDP_begin() {
|
||||
_server->ref();
|
||||
|
||||
ip_addr_t ifaddr;
|
||||
ifaddr.addr = WiFi.localIP();
|
||||
ifaddr.addr = NetworkLocalIP();
|
||||
ip_addr_t multicast_addr;
|
||||
multicast_addr.addr = (uint32_t)SSDP_MULTICAST_ADDR;
|
||||
|
||||
@@ -544,7 +545,7 @@ bool SSDP_begin() {
|
||||
Send SSDP messages (notify & responses)
|
||||
\*********************************************************************************************/
|
||||
void SSDP_send(byte method) {
|
||||
uint32_t ip = WiFi.localIP();
|
||||
uint32_t ip = NetworkLocalIP();
|
||||
|
||||
// FIXME TD-er: Why create String objects of these flashstrings?
|
||||
String _ssdp_response_template = F(
|
||||
@@ -563,7 +564,7 @@ void SSDP_send(byte method) {
|
||||
"CACHE-CONTROL: max-age=%u\r\n" // SSDP_INTERVAL
|
||||
"SERVER: Arduino/1.0 UPNP/1.1 ESPEasy/%u\r\n" // _modelNumber
|
||||
"USN: uuid:%s\r\n" // _uuid
|
||||
"LOCATION: http://%u.%u.%u.%u:80/ssdp.xml\r\n" // WiFi.localIP(),
|
||||
"LOCATION: http://%u.%u.%u.%u:80/ssdp.xml\r\n" // NetworkLocalIP(),
|
||||
"\r\n");
|
||||
{
|
||||
char uuid[64] = { 0 };
|
||||
|
||||
@@ -127,12 +127,8 @@ String getValue(LabelType::Enum label) {
|
||||
{
|
||||
case LabelType::UNIT_NR: return String(Settings.Unit);
|
||||
case LabelType::UNIT_NAME: return String(Settings.Name); // Only return the set name, no appended unit.
|
||||
case LabelType::HOST_NAME:
|
||||
#ifdef ESP32
|
||||
return WiFi.getHostname();
|
||||
#else
|
||||
return WiFi.hostname();
|
||||
#endif
|
||||
case LabelType::HOST_NAME: return NetworkGetHostname();
|
||||
|
||||
|
||||
case LabelType::LOCAL_TIME: return node_time.getDateTimeString('-',':',' ');
|
||||
case LabelType::UPTIME: return String(wdcounter / 2);
|
||||
@@ -172,7 +168,7 @@ String getValue(LabelType::Enum label) {
|
||||
case LabelType::DNS_2: return NetworkDnsIP(1).toString();
|
||||
case LabelType::ALLOWED_IP_RANGE: return describeAllowedIPrange();
|
||||
case LabelType::STA_MAC: return NetworkMacAddress();
|
||||
case LabelType::AP_MAC: break;
|
||||
case LabelType::AP_MAC: return WifiSoftAPmacAddress();
|
||||
case LabelType::SSID: return WiFi.SSID();
|
||||
case LabelType::BSSID: return WiFi.BSSIDstr();
|
||||
case LabelType::CHANNEL: return String(WiFi.channel());
|
||||
|
||||
@@ -66,24 +66,13 @@ void handle_sysinfo_json() {
|
||||
json_prop(F("dns1"), formatIP(NetworkDnsIP(0)));
|
||||
json_prop(F("dns2"), formatIP(NetworkDnsIP(1)));
|
||||
json_prop(F("allowed_range"), describeAllowedIPrange());
|
||||
|
||||
|
||||
uint8_t mac[] = { 0, 0, 0, 0, 0, 0 };
|
||||
uint8_t *macread = WiFi.macAddress(mac);
|
||||
char macaddress[20];
|
||||
formatMAC(macread, macaddress);
|
||||
|
||||
json_prop(F("sta_mac"), macaddress);
|
||||
|
||||
macread = WiFi.softAPmacAddress(mac);
|
||||
formatMAC(macread, macaddress);
|
||||
|
||||
json_prop(F("ap_mac"), macaddress);
|
||||
json_prop(F("ssid"), WiFi.SSID());
|
||||
json_prop(F("bssid"), WiFi.BSSIDstr());
|
||||
json_number(F("channel"), String(WiFi.channel()));
|
||||
json_prop(F("connected"), format_msec_duration(timeDiff(lastConnectMoment, millis())));
|
||||
json_prop(F("ldr"), getLastDisconnectReason());
|
||||
json_prop(F("sta_mac"), NetworkMacAddress());
|
||||
json_prop(F("ap_mac"), WifiSoftAPmacAddress());
|
||||
json_prop(F("ssid"), WiFi.SSID());
|
||||
json_prop(F("bssid"), WiFi.BSSIDstr());
|
||||
json_number(F("channel"), String(WiFi.channel()));
|
||||
json_prop(F("connected"), format_msec_duration(timeDiff(lastConnectMoment, millis())));
|
||||
json_prop(F("ldr"), getLastDisconnectReason());
|
||||
json_number(F("reconnects"), String(wifi_reconnects));
|
||||
json_close();
|
||||
|
||||
@@ -391,20 +380,8 @@ void handle_sysinfo_Network() {
|
||||
addRowLabelValue(LabelType::CLIENT_IP);
|
||||
addRowLabelValue(LabelType::DNS);
|
||||
addRowLabelValue(LabelType::ALLOWED_IP_RANGE);
|
||||
addRowLabel(getLabel(LabelType::STA_MAC));
|
||||
|
||||
{
|
||||
uint8_t mac[] = { 0, 0, 0, 0, 0, 0 };
|
||||
uint8_t *macread = WiFi.macAddress(mac);
|
||||
char macaddress[20];
|
||||
formatMAC(macread, macaddress);
|
||||
addHtml(macaddress);
|
||||
|
||||
addRowLabel(getLabel(LabelType::AP_MAC));
|
||||
macread = WiFi.softAPmacAddress(mac);
|
||||
formatMAC(macread, macaddress);
|
||||
addHtml(macaddress);
|
||||
}
|
||||
addRowLabelValue(LabelType::STA_MAC);
|
||||
addRowLabelValue(LabelType::AP_MAC);
|
||||
|
||||
addRowLabel(getLabel(LabelType::SSID));
|
||||
{
|
||||
|
||||
+1
-1
@@ -246,7 +246,7 @@ bool CPlugin_014(CPlugin::Function function, struct EventStruct *event, String&
|
||||
CPlugin_014_sendMQTTdevice(pubname,"$localip",formatIP(NetworkLocalIP()).c_str(),errorCounter);
|
||||
|
||||
// $mac Device → Controller Mac address of the device network interface. The format MUST be of the type A1:B2:C3:D4:E5:F6 Yes Yes
|
||||
CPlugin_014_sendMQTTdevice(pubname,"$mac",WiFi.macAddress().c_str(),errorCounter);
|
||||
CPlugin_014_sendMQTTdevice(pubname,"$mac",NetworkMacAddress().c_str(),errorCounter);
|
||||
|
||||
// $implementation Device → Controller An identifier for the Homie implementation (example esp8266) Yes Yes
|
||||
#if defined(ESP8266)
|
||||
|
||||
@@ -71,7 +71,7 @@ String Command_Settings_Print(struct EventStruct *event, const char* Line)
|
||||
serialPrintln();
|
||||
|
||||
serialPrintln(F("System Info"));
|
||||
serialPrint(F(" IP Address : ")); serialPrintln(WiFi.localIP().toString());
|
||||
serialPrint(F(" IP Address : ")); serialPrintln(NetworkLocalIP().toString());
|
||||
serialPrint(F(" Build : ")); serialPrintln(String((int)BUILD));
|
||||
serialPrint(F(" Name : ")); serialPrintln(Settings.Name);
|
||||
serialPrint(F(" Unit : ")); serialPrintln(String((int)Settings.Unit));
|
||||
|
||||
Reference in New Issue
Block a user