Simple arduino sketch for BlueDuino to scan for beacons


Hey everybody,

In the interest of knowledge sharing, here’s a simple Arduino sketch for using the BlueDuino to scan for beacons. If you set your beacon to use AltBeacon format, then the rssi number captured in this sketch will actually be correct. If your beacon uses iBeacon, then the rssi number will not be correct. You’ll have to figure out for yourself how to fix that. :laughing:

The sketch scans for any beacons in the area, it then collects that info in the serial buffer (though it doesn’t show this info on the serial monitor; why waste time showing it?). It then uses the “Serial1.find” command to search through the data received to see if it can find the target beacon’s address. If it finds it, then is uses parseint to capture the integer value immediately following that address. In the case of AltBeacon, that integer is the rssi of your beacon. Based on that rssi, you can tell if the beacon is close or far. Later you can add code to trigger some action if the beacon is close enough. The sketch also shows the time elapsed between each scan result received. You can use this for testing how quickly you are getting scans.

Note that the serial1.find command is “destructive”, i.e. it throws out all the data after it reads it. So you can only use this method to scan for a single target beacon. That’s a weak point of course. One the other hand, it makes the sketch super simple, and it’s much easier than using char arrays. Also it avoids using String (capital S) like AprilBro uses in their hardware serial sample sketch. According to Arduino experts, String is something you definitely should avoid using.

//Target iBeacon address is 0CF3EE093DA4 in this sketch. See below. **Change this number below to match the address of your own beacon**

unsigned long timestamp; //needed for timing how fast scan results are received

int rssi; // integer of the rssi value

void setup() 
  Serial.begin(9600);  // Begin the Serial Monitor connection at 9600bps
  Serial1.begin(9600);  // Begin the BlueDuino connection at 9600bps
  Serial1.setTimeout(900);  // length of time for BlueDuino serial to wait for BlueDuino.find to complete. You can play around with this number to see what gives the best results, i.e. fastest scans.
  Serial1.write("AT+SCAN2"); // ask BlueDuino to scan for bluetooth LE 4.0 fob
  timestamp = millis(); //sets the timestamp equal to the current millis value

void loop() 

  if (Serial1.available()) // If there is some data received from the BlueDuino, then do what follows in brackets
    if (Serial1.find("**0CF3EE093DA4**")) // Scan the incoming message from BlueDuino to see if it contains the target string; if it does, then parse the integer following it and print the rssi value
      int rssi = Serial1.parseInt();
      Serial.print("The RSSI of the target bluetooth LE 4.0 fob is: "); 
      Serial.print("Milliseconds elapsed since last string: ");
      Serial.println(millis() - timestamp); // amount of milliseconds elapsed since that line of info received
      timestamp = millis(); // reset the countdown timer
      // Timeout occurred waiting for .find()
      Serial.println("Timeout occurred"); 

  if (Serial.available()) // this allows you to type commands into the serial monitor while all this is running, for example, you can stop the scanning by typing AT+SCAN0


Thank you very much for your sharing. I will add the example to our BlueDuino examples.


I’ve added a new example here.