Get MAC Address Into Node-RED


#1

If you need the MAC address of your device, or just some unique identifier for your groov EPIC PR1 or groov box AR1, you can use Node-RED to get it, then put it wherever you need from there. Just use the exec node to run the command ifconfig and filter out the hardware address using a little JavaScript.
Here’s the flow:

The exec node just runs ifconfig without the payload attached, then this code is in the function node:

ifconfig = msg.payload;
eth0 = ifconfig.indexOf("eth0");
start = ifconfig.indexOf("HWaddr", eth0) + 7;
addr = ifconfig.substring(start, ifconfig.indexOf(' ', start));
return { payload : addr };

Note that it searches for eth0 specifically, but you could change that string to find the hardware address of some other network adapter if you want. This will work on both the AR1 and PR1 processors, without any need for shell access.

If you want to try this out for yourself you can import the flow using the JSON below — happy coding!

[{"id":"83665b10.7f39f8","type":"debug","z":"e7108e55.3fa88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":690,"y":360,"wires":[]},{"id":"8606686a.ef7008","type":"function","z":"e7108e55.3fa88","name":"extract HWaddr","func":"ifconfig = msg.payload; // Save the payload string to a more convenient variable name\neth0 = ifconfig.indexOf(\"eth0\"); // Search for the hardware address of eth0 specifically\nstart = ifconfig.indexOf(\"HWaddr\", eth0) + 7; // Offset the start of the address from \"HWaddr \"\naddr = ifconfig.substring(start, ifconfig.indexOf(' ', start)); // Get up until the next space\nreturn { payload : addr }; // return the address as the new payload","outputs":1,"noerr":0,"x":520,"y":360,"wires":[["83665b10.7f39f8"]]},{"id":"52753337.7b5a8c","type":"exec","z":"e7108e55.3fa88","command":"ifconfig","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":340,"y":360,"wires":[["8606686a.ef7008"],[],[]]},{"id":"51c9a2b2.d2a9fc","type":"inject","z":"e7108e55.3fa88","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":360,"wires":[["52753337.7b5a8c"]]}]