mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
[Conversions] Add unitnr to IP address conversion
- Uses UDP Network member list to lookup the IP address - Inspired by a [forum question](https://www.letscontrolit.com/forum/viewtopic.php?f=6&t=8066)
This commit is contained in:
@@ -189,3 +189,6 @@ The conversion always outputs a string, but not all of these can be converted ba
|
||||
* - To HEX: ``%c_2hex%(100000)``
|
||||
- To HEX: ``186A0``
|
||||
- Convert integer value to HEX notation. (Added: 2020/10/07)
|
||||
* - Unit to IP: ``%c_u2ip%(%unit%)``
|
||||
- Unit to IP: ``192.168.1.67``
|
||||
- Convert a (known) unit number to its IP Address. (Added: 2020/11/08)
|
||||
|
||||
@@ -334,14 +334,9 @@ void SendUDPCommand(byte destUnit, const char *data, byte dataLength)
|
||||
}
|
||||
|
||||
/*********************************************************************************************\
|
||||
Send UDP message (unit 255=broadcast)
|
||||
Get IP address for unit
|
||||
\*********************************************************************************************/
|
||||
void sendUDP(byte unit, const byte *data, byte size)
|
||||
{
|
||||
if (!NetworkConnected(10)) {
|
||||
return;
|
||||
}
|
||||
|
||||
IPAddress getIPAddressForUnit(byte unit) {
|
||||
IPAddress remoteNodeIP;
|
||||
|
||||
if (unit == 255) {
|
||||
@@ -351,14 +346,30 @@ void sendUDP(byte unit, const byte *data, byte size)
|
||||
NodesMap::iterator it = Nodes.find(unit);
|
||||
|
||||
if (it == Nodes.end()) {
|
||||
return;
|
||||
return remoteNodeIP;
|
||||
}
|
||||
|
||||
if (it->second.ip[0] == 0) {
|
||||
return;
|
||||
return remoteNodeIP;
|
||||
}
|
||||
remoteNodeIP = it->second.ip;
|
||||
}
|
||||
return remoteNodeIP;
|
||||
}
|
||||
|
||||
/*********************************************************************************************\
|
||||
Send UDP message (unit 255=broadcast)
|
||||
\*********************************************************************************************/
|
||||
void sendUDP(byte unit, const byte *data, byte size)
|
||||
{
|
||||
if (!NetworkConnected(10)) {
|
||||
return;
|
||||
}
|
||||
|
||||
IPAddress remoteNodeIP = getIPAddressForUnit(unit);
|
||||
if (remoteNodeIP[0] == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifndef BUILD_NO_DEBUG
|
||||
|
||||
|
||||
@@ -30,6 +30,11 @@ void checkUDP();
|
||||
\*********************************************************************************************/
|
||||
void SendUDPCommand(byte destUnit, const char *data, byte dataLength);
|
||||
|
||||
/*********************************************************************************************\
|
||||
Get IP address for unit
|
||||
\*********************************************************************************************/
|
||||
IPAddress getIPAddressForUnit(byte unit);
|
||||
|
||||
/*********************************************************************************************\
|
||||
Send UDP message (unit 255=broadcast)
|
||||
\*********************************************************************************************/
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "../Helpers/Convert.h"
|
||||
#include "../Helpers/ESPEasy_Storage.h"
|
||||
#include "../Helpers/Misc.h"
|
||||
#include "../Helpers/Networking.h"
|
||||
#include "../Helpers/Numerical.h"
|
||||
#include "../Helpers/StringParser.h"
|
||||
#include "../Helpers/SystemVariables.h"
|
||||
@@ -790,6 +791,7 @@ void parseStandardConversions(String& s, boolean useURLencode) {
|
||||
SMART_CONV(F("%c_m2dhm%"), minutesToDayHourMinute(arg1))
|
||||
SMART_CONV(F("%c_s2dhms%"), secondsToDayHourMinuteSecond(arg1))
|
||||
SMART_CONV(F("%c_2hex%"), formatToHex(arg1, F("")))
|
||||
SMART_CONV(F("%c_u2ip%"), getIPAddressForUnit(arg1).toString())
|
||||
#undef SMART_CONV
|
||||
|
||||
// Conversions with 2 parameters
|
||||
|
||||
@@ -200,6 +200,8 @@ void handle_sysvars() {
|
||||
addSysVar_html(F("Secs to dhms: %c_s2dhms%(100000)"));
|
||||
addFormSeparator(3);
|
||||
addSysVar_html(F("To HEX: %c_2hex%(100000)"));
|
||||
addFormSeparator(3);
|
||||
addSysVar_html(F("Unit to IP: %c_u2ip%(%unit%)"));
|
||||
|
||||
html_end_table();
|
||||
html_end_form();
|
||||
|
||||
Reference in New Issue
Block a user