The Approximate Library makes it easy to identify a close by device and then watch its traffic. In this video the ESP32 reads the Google Home's MAC and IP addresses; with this information it forms a pair and the LED now flashes to show the network traffic.
This behaviour can be written in just a few lines of code (the use of the OLED display has been omitted for clarity):
#include <Approximate.h>
Approximate approx;
const int LED_PIN = 22;
char *pairedMacAddress = new char[18];
bool paired = false;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
if (approx.init("MyHomeWifi", "password", /*ipAddressResolution*/ true)) {
digitalWrite(LED_PIN, LOW);
approx.setProximateDeviceHandler(onProximateDevice, APPROXIMATE_PERSONAL_RSSI);
approx.setActiveDeviceHandler(onActiveDevice, /*inclusive*/ false);
approx.begin();
}
}
void loop() {
approx.loop();
}
void onProximateDevice(Device *device, Approximate::DeviceEvent event) {
switch(event) {
case Approximate::ARRIVE:
paired = true;
device->getMacAddressAs_c_str(pairedMacAddress);
approx.setActiveDeviceFilter(pairedMacAddress);
Serial.println(device->getMacAddressAsString());
Serial.println(device->getIPAddressAsString());
break;
case Approximate::DEPART:
paired = false;
break;
}
}
void onActiveDevice(Device *device, Approximate::DeviceEvent event) {
if(paired) {
int payloadSizeByte = device -> getPayloadSizeBytes();
digitalWrite(LED_PIN, HIGH);
delay(payloadSizeByte);
digitalWrite(LED_PIN, LOW);
}
}
As well as watching a device, knowing its MAC and IP addresses, we can also interact with it - like a universal remote control. Approximate makes it easy to identify devices by manufacturer (OUI) and then write different behaviours for each:
Approximate works with 2.4GHz WiFi networks, but not 5GHz networks - neither ESP8266 or ESP32 support this technology. This means that devices that are connected to a 5GHz WiFi network will be invisible to this library. Approximate will not work as expected where MAC address randomisation is enabled - the default iOS setting.
The Three WiFi Meters project makes extensive use of the Approximate Library.
All these examples and more are fully documented here:
https://github.com/davidchatting/Approximate