Search code examples
c++arduinobluetooth-lowenergyarduino-unoarduino-ide

Ardunio HM-10 BLE Scanner development


I'm using an Ardunio uno with a HM-10 module. I am trying to scan for all beacons around me and then store the beacon names

#include <SPI.h>
#include <SoftwareSerial.h>

String inputTXT;

SoftwareSerial mySerial(10, 11); // RX, TX  


void setup() {  
  Serial.begin(9600);

  mySerial.begin(9600);

  //setup
  mySerial.write("AT");
  delay(100);
  mySerial.write("AT+ROLE1"); // Master mode
  delay(100);
  mySerial.write("AT+IMME1"); //wait for a connection command before connecting 
  delay(100);
  mySerial.write("AT+RESET");
  delay(50);

}

void loop() {  

  delay(3000);


  mySerial.write("AT+DISI?");

  if (mySerial.available()) {
    inputTXT = mySerial.readString();

    Serial.println(inputTXT);

    inputTXT = ""; 
  }
}

I get this output repeated at every loop

OK+DISISOK+DISC:00000000:00000000000000000000000000000000:00000OK+DISISOK+DISCEOK+DISC:00000000:00000000000000000000000000000000:0000000000:38F9D379C9E5:-065OK+DISC:00000000:00000000000000000000000000000000:0000000000:4CAA0DE091B7:-066OK+DISC:00000000:00000000000000000000000000000000:0000000000:72363EC2C661:-084OK+DISC:4C000C0E:008D37DBECB6B76115D006C9B3FA1005:1B1C1E7B5B:76854777DBD7:-072OK+DISC:00000000:00000000000000000000000000000000:0000000000:38F55DEDE396:-079OK+DISC:00000000:00000000000000000000000000000000:0000000000:4D023B8ED54D:-083OK+DISC:00000000:00000000000000000000000000000000:0000000000:6B5DB3EB2

So i now want to save the 12 digit string before the final colon of each iteration such as '4CAA0DE091B7' can someone show me or advice me how i would go about doing that please?


Solution

  • Unfortunately, there is no regular expression for Arduino, so you need to do it manually.

    #include <SPI.h>
    #include <SoftwareSerial.h>
    
    String inputTXT;
    
    SoftwareSerial mySerial(10, 11); // RX, TX
    
    void setup()
    {
        //setup
        Serial.begin(9600);
        mySerial.begin(9600);
        mySerial.write("AT");
        delay(100);
        mySerial.write("AT+ROLE1"); // Master mode
        delay(100);
        mySerial.write("AT+IMME1"); //wait for a connection command before connecting
        delay(100);
        mySerial.write("AT+RESET");
        delay(50);
    }
    
    void loop()
    {
    
        delay(3000);
        mySerial.write("AT+DISI?");
        if (mySerial.available())
        {
            inputTXT = mySerial.readString();
            int pos = 0;
            String result = "";
            const String regx = "00000000:00000000000000000000000000000000:0000000000:";
            const int regx_len = regx.length();
            while ((pos = inputTXT.indexOf(regx, pos)) != -1)
            {
                // substring from <starting point> to >starting point + 12>
                result = inputTXT.substring(pos + regx_len, pos + regx_len + 12);
                Serial.println(result);
                // move starter point to end of last result
                pos = pos + regx_len + 12;
            }
        }
    }