Created
January 2, 2022 22:15
-
-
Save gmag11/9a57c2a13be94ab2c111b639e2679b69 to your computer and use it in GitHub Desktop.
Get MAC address from IP with ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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, ðadress, &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