Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By stern0m1
#71477 Im trying to record the amount of wifi devices broadcasting by sniffing WiFi frames in promiscuous.
I pull the mac id from the frame and put it in an array. Before i put in the array I iterate through the array to check if its there already.
For some reason I end up with a few duplicates.
I am at my wits end. Any suggestions?
Code: Select allvoid promisc_cb(uint8_t* buf, uint16_t len) {
  wifi_promiscuous_enable(false);

  sniffer = (struct sniffer_buf2*) buf;
  String mac = "";

  if ((bitRead(3, sniffer->buf[0]) == 0) && (bitRead(2, sniffer->buf[0])) == 0) {

    //grab mac of sender
    for (int i = 10; i < 16; i++)
    {
      mac = mac + String(sniffer->buf[i], HEX);
      if (i < 15) {
        mac = mac + ":";
      }

    }

    if (!exists(mac, amountMacs)) {
      amountMacs++;
      macs[amountMacs] = mac;

      printData(amountMacs);
    }
  }
}



void printData(int amount) {
  for (int i = 0; i < amount; i++) {
    Serial.print(String(i) + " " + macs[i] + " ** ");
  }
  Serial.println();
}


boolean exists(String mac, int amount) {
  if (amount == 0) {
    return false;
  }
  for (int i = 0; i < amount; i++) {
    if (mac == macs[i]) {
      return true;
    }
  }
  return false;
}
User avatar
By McChubby007
#71481
philbowles wrote:There's a much easier way using std:: library. Define a "set" instead of making your own array and having to manage it, i.e. reinventing a wheel.

Then just insert the new mac. std::set doesn't allow duplicates. It will just "fail" silently when you try to add one.

#include<set>
...
set<string> macs={}; // declare + initialise
... // fill
macs.insert(newmac);

Note also the small s in string. My example uses std::strings not Arduino Strings, but I'm sure set<String> would work too.

Three lines of (guaranteed to work) code: end of story


Hey Phil, just reading your reply, and not wishing to derail the Op's question, but I had no idea libstdc++ was available by default. Am I stupid, or missing something? (I'm sure I tried this once, a couple of years ago).
User avatar
By stern0m1
#71491 Thanks.
I was able to use a set.
Like this: std::set<String> macs;

I am not familiar at all with c++ only c. I am familiar with java.

Could someone explain me the syntax std::set<String> macs;? Whats std and whats the ::? (Yes, im lazy to google it :( , really just very busy...) Sorry if I shouldn't be asking very basic questions...
User avatar
By McChubby007
#71496
philbowles wrote:
McChubby007 wrote:Am I stupid, or missing something? (I'm sure I tried this once, a couple of years ago).


I doubt very much you are stupid (proof being you agreed with me in another post :))

You are missing that A CUT-DOWN version of C++ std library is present in the ESP8266 core.

e.g. it has no Typeinfo (busts the deafult heap cos its so big) but all the containers are there and std::functions - TBH I couldn't have produced my various libraries without it, I use it pretty extensively.

Excellent, well thanks for educating me, I'm very grateful. I've no excuse for my old C bad habits by the sound of it.