The Approximate library is a WiFi Arduino Library for building proximate interactions between your Internet of Things and the ESP8266 or ESP32.
Technically this library makes it easy to use WiFi signal strength (RSSI) to estimate the physical distance to a device on your home network, then obtain its MAC address and optionally its IP address. The network activity of these devices can also be observed.
This short video demonstrates the basic capabilities of the Approximate library. When the Google Home is brought close to the ESP32 it becomes paired and we read both its MAC and IP addresses, then the LED 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);
}
}
Comments