A while back I was trying to create a device that could detect when someone was near, so that some sort of greeting could be displayed. After some brainstorming I came up with this idea.
Nowadays everyone has a smartphone usually with wifi enabled. These phones periodically try to communicate to access points, either to exchange data or to find devices to connect. On these communications the phone has to send its MAC address (the MAC address is a unique value that every device that connects to a network has) so that the other devices can know who is sending data and where to send the response to.
So if we can listen to these communications we can use the MAC address and compare it to a list of known MACs so that we can identify when someone we know (a friend for example) is near.
The ESP8266 has some great functionalities, but one in particular is very useful for our purpose, it can work in promiscuous mode, or as it is more commonly known sniffing mode. This mode enables us to receive the information that is being sent by the devices around us, and with this we can read the addresses and compare them to our list of friends.
Here is the code I created to prototype the idea:
#include "./esppl_functions.h"
/*
* Define you friend's list size here
*/
#define LIST_SIZE 2
/*
* This is your friend's MAC address list
*/
uint8_t friendmac[LIST_SIZE][ESPPL_MAC_LEN] = {
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11}
,{0x22, 0x22, 0x22, 0x22, 0x22, 0x22}
};
/*
* This is your friend's name list
* put them in the same order as the MAC addresses
*/
String friendname[LIST_SIZE] = {
"Friend 1"
,"Friend 2"
};
bool maccmp(uint8_t *mac1, uint8_t *mac2) {
for (int i=0; i < ESPPL_MAC_LEN; i++) {
if (mac1[i] != mac2[i]) {
return false;
}
}
return true;
}
void cb(esppl_frame_info *info) {
for (int i=0; i<LIST_SIZE; i++) {
if (maccmp(info->sourceaddr, friendmac[i]) || maccmp(info->receiveraddr, friendmac[i])) {
Serial.printf("\n%s is here! :)", friendname[i].c_str());
}
}
}
void setup() {
delay(500);
Serial.begin(115200);
esppl_init(cb);
}
void loop() {
esppl_sniffing_start();
while (true) {
for (int i = ESPPL_CHANNEL_MIN; i <= ESPPL_CHANNEL_MAX; i++ ) {
esppl_set_channel(i);
while (esppl_process_frames()) {
//
}
}
}
}
This code uses a simple library written by me called ESPProLib that deals with the processing of the information received by the ESP8266. More information about the library and the source code for this project can be found on the CODE section of this article.
I hope that this project can be useful.
I'm sure that someone will come up with some great ideas to improve the code. If you need any help just let me know. I will try to reply and help you the best I can. Also if you make something cool with this feel free to send some feedback.
Thanks,
Ricardo.
Comments