Bluetooth HC-10 Scan and Compare

Hello,
Im looking for a little guidance with coding an Arduino. I have my Arduino connected to a bluetooth HC-10 adapter, and I can communicate with the adapter just fine. I used the tutorial (https://www.arduino.cc/en/Tutorial/SoftwareSerialExample).

I am trying to write some code to do the following:

  1. send the command AT+DISI?
  2. Receive the response in the following format:

OK+DISISOK+DISC:00000000:00000000000000000000000000000000:0000000000:FFFF00005FDA:-045OK+DISC:00000000:00000000000000000000000000000000:0000000000:FFFF200015DB:-057OK+DISCE

  1. Parse the data (There is actually two responses listed above representing two bluetooth devices in range of the arduino. I am interested in the FFFF line which is their serial number, and also the -045 or -057 values which is the signal strength)
  2. Do something (light an LED if signal strength drops below a given value)

I started with the following:

void loop() {  
  mySerial.write("AT+DISI?");  
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  } 
  delay(500);
}

but all this does is display 1 character every 500ms. I am wanting it to scan for available devices, print the results, then pause for 30 seconds, then repeat.

Thoughts as to what I am doing wrong or how to properly implement this?

Thanks

You're sending the command "AT+DISI?" every 500ms too.
You probably want to send it once, and then listen for all the response characters.

Simplest thing is probably to put the command print the command in setup, and just have loop spit out the response, just to get confidence.

I took your advice, and moved the mySerial.write to the setup, and the loop is just displaying the results. I am needing this to "re-scan" or send the command AT+DISI? to the bluetooth adapter every 30 seconds or so. I want the unit to keep refreshing... Thoughts on how to accomplish that one?

Thanks!

Have a look at the blink without delay example, without delay

Good thought… I ran through the example (https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay) but am still experiencing a similar problem. When watching the serial monitor, the arduino is printing our 1 character at a time. (one second apart, as per the led example above).

#include <SoftwareSerial.h>

SoftwareSerial mySerial(6, 7); // RX, TX  
// Connect HM10      Arduino Uno
//     Pin 1/TXD          Pin 7
//     Pin 2/RXD          Pin 8

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             //
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  Serial.begin(9600);
  // If the baudrate of the HM-10 module has been updated,
  // you may need to change 9600 by another value
  // Once you have found the correct baudrate,
  // you can update it using AT+BAUDx command 
  // e.g. AT+BAUD0 for 9600 bauds
  mySerial.begin(9600);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    mySerial.write("AT+DISI?"); 
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  } 
    
  }
}

The idea was that you would print the message once every 30 seconds, and the rest of the time you would listen for the response.

Correct. In order to accomplish that, do I run two loops? I am not understanding how I can have the "listen" loop running and also print the command every 30 sec. Is the arduino able to perform tasks outside of a loop while that loop is running?

Thanks again for your assistance!

Uncompiled, untested

void loop() {
 unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= 30000) 
  {
    previousMillis = currentMillis;

    mySerial.write("AT+DISI?"); 
  }
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  } 
}

Ahhh, I see! Thank you for the clarification!

Im having some issues with parsing the string that I am receiving and need to see if you have any thoughts. Every 30 seconds, the arduino scans for available bluetooth devices, and prints some info. Here is an example of only 1 available device:

OK+DISISOK+DISC:00000000:00000000000000000000000000000000:0000000000:FFFF00005FDA:-062OK+DISCE

And if there are two devices:

OK+DISISOK+DISC:00000000:00000000000000000000000000000000:0000000000:FFFF00005FDA:-061OK+DISC:00000000:00000000000000000000000000000000:0000000000:E8A5BB0ABDB9:-085OK+DISCE

I am trying to extract the serial number (FFFF... or E8A...) and also the signal strength (-061 or -085)