Skip to content

Instantly share code, notes, and snippets.

@gmag11
Created January 2, 2022 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmag11/9a57c2a13be94ab2c111b639e2679b69 to your computer and use it in GitHub Desktop.
Save gmag11/9a57c2a13be94ab2c111b639e2679b69 to your computer and use it in GitHub Desktop.
Get MAC address from IP with ESP32
#include <WiFi.h>
#include "lwip/etharp.h"
const char* ssid = "xxxxxx";
const char* password = "xxxxxx";
const IPAddress remote_ip (192, 168, 1, 1); // Should be in your same subnet
void setup () {
Serial.begin (115200);
// We start by connecting to a WiFi network
Serial.println ();
Serial.println ("Connecting to WiFi");
WiFi.begin (ssid, password);
while (WiFi.status () != WL_CONNECTED) {
delay (100);
Serial.print (".");
}
Serial.println ();
Serial.print ("WiFi connected with ip ");
Serial.println (WiFi.localIP ());
netif* interface = netif_list;
ip4_addr origin;
origin.addr = remote_ip;
eth_addr* ethadress;
const ip4_addr* ipaddress;
while (interface) {
if (interface->hwaddr_len == 6) {
err_t result = etharp_request (interface, &origin); // Send ARP request
Serial.printf ("Request result = %d\n", result);
delay (100); // Time to respond ARP request. Could be less
result = etharp_find_addr (interface, &origin, &ethadress, &ipaddress); // Check ARP table
Serial.printf ("Find result = %d\n", result);
if (ethadress) {
Serial.printf ("Origin MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
ethadress->addr[0], ethadress->addr[1], ethadress->addr[2],
ethadress->addr[3], ethadress->addr[4], ethadress->addr[5]
);
} else {
Serial.println ("Address not found");
}
}
interface = interface->next;
}
}
void loop () {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment