Save output of an AT command hc-05 and remove colon

Hi, I want to have a script that would run and output the address of the hc-05 so that I can place it to the other hc-05 without having to copy a paste.

Currently I have this which prints out the AT command to the serial monitor. The current output of the ATcommand for the Apprentice's address is: +ADDR:0000:00:000000

What I want to do is grab 0000:00:000000 and replace the colons to commas and place it into masterSerial.print("AT+BIND="+String(APPREN_ADDRESS)+"\r\n"); .

Is this something that is possible? Thank you for your help!

On which computer? The PC running the Arduino IDE/Serial monitor?

I can place it to the other hc-05

How?

Try explaining your idea again. It makes little sense.

char input  [] = "+ADDR:0000:00:000000";
char output [20];
uint8_t y = 0;

void setup()
{
  Serial.begin(115200);
  
  for(uint8_t x = 6; x<20; x++)
  {
    if(input[x] >= '0' && input[x] <= '9')
      output[y++] = input[x];

    if(input[x] == ':')
      output[y++] = ',';

  }

  output[y] = '\0';

  Serial.println(output);
}

void loop()
{}

Your post is utterly incoherent but, if it is what you actually want, you can configure HC-05 with a one-shot Arduino programme simply by putting all the AT commands in the setup. I put a delay 1000 between each command.

Hi, so the script runs on my pc and has the output on the serial monitor.

I want to grab what the apprentice outputs at apprenSerial.print("AT+ADDR?\r\n"); to the masterSerial.print("AT+BIND="+String(APPREN_ADDRESS)+"\r\n"

I'm not totally clear about your issue. Please post the code of your best attempt to do what you want..

My thinking is that instead of printing the AT response, read it into a buffer and process what's in that buffer to get what you are after.

#include <SoftwareSerial.h>

#define BT_NAME "BT_APPRENTICE"

SoftwareSerial mySerial(3, 2); // RX, TX
char response[50];

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

  mySerial.begin(38400);
  Serial.println("Arduino receiveras");

  mySerial.print("AT+ADDR?\r\n");
  if (Serial.available()){
    for(int i = 0 ; Serial.available() > 0 && i<10 ; i++) {
      response[i++] = Serial.read();
      }
  }
  Serial.print(response+"Test");
  delay(100);

  mySerial.print("AT+NAME="+String(BT_NAME)+"\r\n");
  delay(100);
  mySerial.print("AT+UART=38400,0,0\r\n");
  delay(500);
  Serial.println(response);
}

void loop()
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

I have posted the code.

Your code does not compile, so how could you be getting a response?

Hi I've been working on the code today and this is the updated version. Ive been testing and this one compiles.

#include <SoftwareSerial.h>

#define BT_NAME "BT_APPRENTICE"

SoftwareSerial mySerial(3, 2); // RX, TX

const byte buffSize = 40;
char inputBuffer[buffSize];
const char startMarker = '+';
const char endMarker = '7';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;

char messageFromPC[buffSize] = {0};

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

  mySerial.begin(38400);
  Serial.println("Arduino receiver");

  mySerial.print("AT\r\n");
  delay(100);
  mySerial.print("AT+RMAAD\r\n");
  delay(100);
  mySerial.print("AT+ADDR?\r\n");
  delay(100);
  mySerial.print("AT+NAME="+String(BT_NAME)+"\r\n");
  delay(100);
  mySerial.print("AT+PSWD=\"1234\"\r\n");
  delay(100);
  mySerial.print("AT+ROLE?\r\n");
  delay(100);
  mySerial.print("AT+UART=38400,0,0\r\n");
  delay(500);
}

void loop()
{
  // getDataFromPC();
  // replyToPC();
  
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

//=============

void getDataFromPC() {
    // receive data from PC and save it into inputBuffer
    
  if(Serial.available() > 0) {
    char x = Serial.write(Serial.read());

      // the order of these IF clauses is significant
      
    if (x == endMarker) {
      readInProgress = false;
      newDataFromPC = true;
      inputBuffer[bytesRecvd] = 0;
      parseData();
    }
    
    if(readInProgress) {
      inputBuffer[bytesRecvd] = x;
      bytesRecvd ++;
      if (bytesRecvd == buffSize) {
        bytesRecvd = buffSize - 1;
      }
    }

    if (x == startMarker) 
    { 
      bytesRecvd = 0; 
      readInProgress = true;
    }
  }
}

//=============
 
void parseData() 
{
    // split the data into its parts
  char * strtokIndx; // this is used by strtok() as an index
  
  strtokIndx = strtok(inputBuffer,",");      // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
}

void replyToPC() 
{
  if (newDataFromPC) 
  {
    newDataFromPC = false;
    Serial.print("the address");
    Serial.print(messageFromPC);
    Serial.println("reached");
  }
}

Can you now successfully receive the Address of the HC05 and you need to replace the colons with commas for the AT+BIND command?

Why are you trying to do this dynamically in the code. Perhaps you should just be configuring the two devices following this tutorial

https://howtomechatronics.com/tutorials/arduino/how-to-configure-pair-two-hc-05-bluetooth-module-master-slave-commands/

I am able to print out the address for the Apprentice to the Serial Monitor, but unable to put it into a buffer to then parse it and replace the colon with commas and place it to the master's code to then pair them together.

I am trying to do this because I would need to pair about 40 HC-05s.

You can simply iterate over the character array and replace the ':' with ','.
Then only print from after the first ','. You know that point is after the six characters of "+ADDR:".

Here's a basic example.

void setup() {
  char addressRead[] = "+ADDR:98d3:34:905d3f";
  Serial.begin(115200);
  for (byte i = 0; i <= strlen(addressRead); i++)
  {
    if (addressRead[i] == ':')
      addressRead[i] = ',' ;
  }
  Serial.println(&addressRead[6]);//print from from after first ':' to end
}
void loop() {}

Did you read post #3 ?

I saw the post, I just haven't tested it as I needed to save the output of mySerial.print("AT+ADDR?\r\n"); from the serial monitor to a variable and then replace the colon to commas.

Thank you!

are you copying the addr output from the serial monitor and placing it to char addressRead[]?

You are going to need to send the AT request for the address and then read the response into character array variable. I just demonstrated how to change the ':' to ',' once you have that array.

Thank you for your help! Everything is working fine now! This is great, saves a lot of time! Thanks again!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.