Naming a bluetooth module, using a text file and a variable

Hello

I have a bluetooth module. Usual naming process is:

Serial1.print("AT+TestingBluetooth");

But, I would like to add a variable to the end of that name.

I came up with (and it doesn't work)...

(I know its a string... lets ignore that for now)

char BTname[21];                     
                         
sprintf (BTname, "AT+TestingBluetooth:%3d", DeviceAddress);  // Assemble the Bluetooth name (%3d = a space for 3 variables)

Serial1.print(BTname); 
Serial.print(F("Bluetooth current name: "));Serial.println(BTname);

It is not recognising that as a valid name. It prints to the monitor correctly, so I am clearly not sending it to the device in the correct format.

Any ideas?

What exactly do you see when you print BTname ?
What type of variable is DeviceAddress ?
Is a colon a valid character in the name of a Bluetooth device ?
What type of Bluetooth module do you have ?
Does the command need to be followed by CR/LF ?
Are you sure that you are using the correct syntax of the name command ?

DeviceAddress is a 3 digit number, stored as an Int.

When you print BTname to the serial monitor, you get

AT+TestingBluetooth:123

That is exactly what you are supposed to send (but inside " ") to the Bluetooth module to rename it.

If I send Serial1.print("AT+TestingBluetooth:123"); , then it will rename correctly

Which Bluetooth module ?

AT+TestingBluetooth:123

I count more than 21 characters.

Its less than 21 characters. The name TestingBluetooth is what I used here as an example.
The actual Bluetooth name is my home address first line which is shorter.

Its a HC-06 module

SteveRC2017:
If I send Serial1.print("AT+TestingBluetooth:123"); , then it will rename correctly

Really?
So what's the problem?

EH?

I can rename it by sending: Serial1.print("AT+TestingBluetooth:123");

But, I need the 3 digit number at the end to be adjustable (DeviceAddress).

So, how do I assemble the string, and send it so that the HC-06 understands what it is sent.

If I serial print my string it appears correct.
But, how can I send that string so that the HC-06 sees it between quotes ("")?

Just sending the completed string E.G: Serial1.print(BTname); doesn't work.

Maybe it's not possible

Are you using a hardware serial port or a form of software serial for Serial1? The way you are doing it should work, although I'm surprised you don't need a line-ending character.

I'm surprised you don't need a line-ending character

The advice that I have found on line is that the HC06 does not need a specific line ending because it responds to the AT command a fixed time after receiving it unlike the HC05 which does need a CR/LF line ending, however, there is conflicting advice on how to actually set the name

No, it doesn't need a line ending. These modules can vary wildly in their command sets I have found.
But... this one works pretty standard, I just can't get it to accept a variable name.

Its on a hardware serial.

Worst case.... I will have to simply have a whole pile of print statements and it picks the one I want using a switch statement or something.

Not very elegant however

If it worked using a variable how do you envisage using it ?

? It didn't work using a variable. It only works if I actually send the number as a text file.

The variable is because this will be one of multiple units, and I need to be able to designate each machine it's own Bluetooth name.

SOLVED

Always something simple....

sprintf (BTname, "AT+NAMETestingBluetooth:%3d", DeviceAddress);

Serial1.print(BTname);

It was missing NAME after AT.

Works fine now

It was missing NAME after AT.
Works fine now

You probably missed out on getting correct advice to use the standard syntax with all these statements.

I have a bluetooth module. Usual naming process is:
Serial1.print("AT+TestingBluetooth");

If I send Serial1.print("AT+TestingBluetooth:123"); , then it will rename correctly

I can rename it by sending: Serial1.print("AT+TestingBluetooth:123");

SteveRC2017:
If I send Serial1.print("AT+TestingBluetooth:123"); , then it will rename correctly

SteveRC2017:
sprintf (BTname, "AT+NAMETestingBluetooth:%3d", DeviceAddress);
It was missing NAME after AT.

Well, there you go then...

I am still intrigued as to why you want to use the value of a variable in the first place

Because if there is more than once instance of this circuit, which there will be, they can't all communicate with bluetooth at the same time using the same name.

So, each machine needs a user configurable bluetooth name, so you can select the individual device in the bluetooth device list on the phone.

In the App I have written, you can pick a device from the list of devices that start with 'TestingBluetooth:###'

I have 'NAME' in the setup code when I renamed it directly. I just missed out 'NAME' later in the code and didn't notice.
My post in here was looking at that later bit of code. Getting old +crap eyes + tired + useless.

if there is more than once instance of this circuit, which there will be, they can't all communicate with bluetooth at the same time using the same name.

True, but I still don't understand where the need to use the value of a variable comes into the picture. Why not prompt the user for the required name ?

Because the only interaction with the user menu is through 2x hall effect switches using a magnetic pen (it is a waterproof unit). One is 'Select' and the other is 'Enter'

So, the quickest way of selecting a device number is with a variable that counts up when you tap 'Select' and then 'tap 'Enter' to store.
It is stored in EEPROM, so you don't need to change it that often, unless you have device clashes.