BT4502 transmit from UART to BLE app

Unfortunately I cannot answer to your question, because I am trying to get 4502 to work with Arduino.
But can you tell me how did you get it paired with BLE APP ? Any trick there, because I cannot do that.

Yep, I used the app “Serial Bluetooth Terminal” but you have to use a custom bluetooth LE profile with the device.

You can select the relevant characteristics as they’re listed in the datasheet (FFE4 and FFE9). With these settings, I can communicate bidirectionally, the issue comes with sending data from my microcontroller to the app unfortunately.

I’ll help you test it today. @harris.shallcross

1 Like

Thank you! Let me know if you manage to consistently send data across.

Hi, I downloaded Serial Bluetooth Terminall APP and managed to go to Custom LE profile, but cannot figure out how to give those UUIDs. It is not possible to write anything to service UUID, if I click it, app tries to connect, but fails.
It appears that I have the same problem as you. You can send configuration commands (TTM) via Bluetooth, but trying to send something from MCU (I have Seeduino XIAO) via TX,RX fails.
I think someone in Seeedstudio should explain what is the problem here

This code needs to be modified a bit, as follows. @harris.shallcross @heikki728
1, the wakeup pin has to pull down the delay by about 3ms before sending data to the serial port
2, If you want faster data response, you can modify the connection interval a bit via the AT command. You can send this data to modify the parameters, do not immediately pull up after sending the data, also delay 1~3ms.

TTM:CIT-30ms\r\n\0
54 54 4D 3A 43 49 54 2D 33 30 6D 73 0D 0A 00
void U_TxC(char c){
	huart2.Instance->TDR = (char)c;
	while(!__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TC));
}

void U_TxS(const char *s, uint8_t null){
	HAL_GPIO_WritePin(BL_WKP_GPIO_Port, BL_WKP_Pin, 0);
	HAL_Delay(2);

	while(*s){
		U_TxC(*s);
		s++;
	}

	if(null) U_TxC('\0');

	HAL_GPIO_WritePin(BL_WKP_GPIO_Port, BL_WKP_Pin, 1);
}

void U_TxSL(const char *s, uint8_t l){
	while(l--){
		U_TxC(*s);
		s++;
	}
}

#define BL_BUFLEN	256
volatile char ubuf[BL_BUFLEN] = {0};
volatile uint8_t newbldata = 0;
volatile uint16_t cc = 0;

void UART_Handler(void){
	char c;
	if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE)){
		c = (char)huart2.Instance->RDR;
		if(cc<BL_BUFLEN){
			ubuf[cc++] = c;
		}
		if(c=='\0'){
			newbldata = 1;
			cc = 0;
		}
	}
}

/* USER CODE END 0 */

int main(void)
{
	/* USER CODE BEGIN 1 */

	/* USER CODE END 1 */


	/* MCU Configuration--------------------------------------------------------*/

	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	HAL_Init();

	/* USER CODE BEGIN Init */

	/* USER CODE END Init */

	/* Configure the system clock */
	SystemClock_Config();

	/* USER CODE BEGIN SysInit */

	/* USER CODE END SysInit */

	/* Initialize all configured peripherals */
	MX_GPIO_Init();
	MX_ADC_Init();
	MX_TIM21_Init();
	MX_USART2_UART_Init();
	/* USER CODE BEGIN 2 */

	//Wakeup bluetooth module
	HAL_GPIO_WritePin(BL_PDN_GPIO_Port, BL_PDN_Pin, 1);
	HAL_Delay(1);
	HAL_GPIO_WritePin(BL_PDN_GPIO_Port, BL_PDN_Pin, 0);
	HAL_Delay(1);

	__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
	__HAL_UART_ENABLE(&huart2);

	newbldata = 0;

	U_TxS("TTM:NAM-?\r\n", 1);
	while(!newbldata);
	newbldata = 0; //Breakpoint to check for received name
	uint8_t isconnect = 0, n=0;
	/* USER CODE END 2 */



	/* Infinite loop */
	/* USER CODE BEGIN WHILE */
	while (1)
	{
		if(newbldata){
			if(strstr(ubuf, "TTM:CONNECT") != NULL){
				isconnect = 1; //Successfully connected
			}
			else if(strstr(ubuf, "TTM:DISCONNECT") != NULL){
				isconnect = 0; //Successfully disconnected
			}
			else{
				n++;
			}
			newbldata = 0;
		}

		//When connected, send string every second
		if(isconnect){
			HAL_Delay(1000);
			U_TxS("hi\r\n",1);
		}
		/* USER CODE END WHILE */

		/* USER CODE BEGIN 3 */
	}
	/* USER CODE END 3 */
}

Hi Baozhu,

I’ve added delays between the high to low and low to high transition of 5ms. I can now receive data on my phone but it seems to receive twice and every two seconds, even though I’m sending it once and every one second.

You can see in the time stamps above that the first three messages are every second but then it becomes every two seconds.

I then tried to change the CIT to 20ms by sending the TTM command after I had connected and I still get the same issue - messages send after 2s and come in pairs!

Is there anything else I’m missing here?

Thanks,

Second image once I’ve added the CIT command:

@harris.shallcross

void U_TxC(char c){
	huart2.Instance->TDR = (char)c;
	while(!__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TC));
}

void U_TxS(const char *s, uint8_t null){
	HAL_GPIO_WritePin(BL_WKP_GPIO_Port, BL_WKP_Pin, 0);
	HAL_Delay(2);

	while(*s){
		U_TxC(*s);
		s++;
	}

	if(null) U_TxC('\0');

	HAL_GPIO_WritePin(BL_WKP_GPIO_Port, BL_WKP_Pin, 1);
}

void U_TxSL(const char *s, uint8_t l){
	while(l--){
		U_TxC(*s);
		s++;
	}
}

#define BL_BUFLEN	256
volatile char ubuf[BL_BUFLEN] = {0};
volatile uint8_t newbldata = 0;
volatile uint16_t cc = 0;

void UART_Handler(void){
	char c;
	if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE)){
		c = (char)huart2.Instance->RDR;
		if(cc<BL_BUFLEN){
			ubuf[cc++] = c;
		}
		if(c=='\0'){
			newbldata = 1;
			cc = 0;
		}
	}
}

/* USER CODE END 0 */

int main(void)
{
	/* USER CODE BEGIN 1 */

	/* USER CODE END 1 */


	/* MCU Configuration--------------------------------------------------------*/

	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	HAL_Init();

	/* USER CODE BEGIN Init */

	/* USER CODE END Init */

	/* Configure the system clock */
	SystemClock_Config();

	/* USER CODE BEGIN SysInit */

	/* USER CODE END SysInit */

	/* Initialize all configured peripherals */
	MX_GPIO_Init();
	MX_ADC_Init();
	MX_TIM21_Init();
	MX_USART2_UART_Init();
	/* USER CODE BEGIN 2 */

	//Wakeup bluetooth module
	HAL_GPIO_WritePin(BL_PDN_GPIO_Port, BL_PDN_Pin, 1);
	HAL_Delay(1);
	HAL_GPIO_WritePin(BL_PDN_GPIO_Port, BL_PDN_Pin, 0);
	HAL_Delay(1);

	__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
	__HAL_UART_ENABLE(&huart2);

	newbldata = 0;

	U_TxS("TTM:NAM-?\r\n", 1);
	while(!newbldata);
	newbldata = 0; //Breakpoint to check for received name
	uint8_t isconnect = 0, n=0;
	/* USER CODE END 2 */


        U_TxS("TTM:CIT-30ms\r\n", 1);

	/* Infinite loop */
	/* USER CODE BEGIN WHILE */
	while (1)
	{
		if(newbldata){
			if(strstr(ubuf, "TTM:CONNECT") != NULL){
				isconnect = 1; //Successfully connected
			}
			else if(strstr(ubuf, "TTM:DISCONNECT") != NULL){
				isconnect = 0; //Successfully disconnected
			}
			else{
				n++;
			}
			newbldata = 0;
		}

		//When connected, send string every second
		if(isconnect){
			HAL_Delay(1000);
			U_TxS("hi\r\n",1);
		}
		/* USER CODE END WHILE */

		/* USER CODE BEGIN 3 */
	}
	/* USER CODE END 3 */
}

Ah brilliant! That’s solved it.

Thanks @Baozhu!

1 Like

You are also great and a great developer. @harris.shallcross

1 Like

Hi All,
I am also using this same module for my real time temperature update in to my android phone and i m having issues of lag from the data on serial terminal app, i try code with hm10 ,hc05 and other esp modules work fine without delay.
Why this module is giving me delay over the time?
Any idea or setting related to arduino programing

PS Sorry for my English.

How long do you say the delay is?

Delay of 2 to 4 sec and over the time it increases

The previous chat screenshot shows that communication is not more than 3 seconds.

hi, ı have a problem with bt4502. how can ı change password with mcu?
please help

I thought it is cheap so i bought it for my project. that i usually did with HC-05. HC-06 but, it worked not stably, i controlled it by arduino Mega 256, with 3.3 v supply. the longest time for sending data is 2 Mins. it disconnected after 5 second usually .
here is my code (really simple, i used all default )
Do you know the reason for not work long time? Please guide me. Thanks for your time.
And do you know how to fast reconnect from Android App. Sometime failed to reconnect .
void setup() {
// initialize both serial ports:
Serial1.begin(115200);
pinMode(14, OUTPUT); // PDN :14
pinMode(15, OUTPUT); // WAKEUP:15
digitalWrite(14, 1); // make sure high before low
digitalWrite(15, 1); // make sure high before low
delay(100);
digitalWrite(14, 0); // stop Sleep
digitalWrite(15, 0); // before send command
delay(5);
Serial1.write(“TTM”); //
}

void loop() {
digitalWrite(15, 1); // go to sleep
delay(100); // interval time between send
digitalWrite(15, 0); // wakeup before send
delay(5); // delay before send
Serial1.write(“Hello there, are you there”);
delay(10); // make sure data are sent; 1ms also OK
}

I changed your code like this and its Working

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

void setup() {
// initialize both serial ports:
Serial.begin(9600);
mySerial.begin(115200);

pinMode(4, OUTPUT); // WAKEUP:4
pinMode(5, INPUT); // int:5

digitalWrite(4, HIGH); // make sure high before low
delay(1);
digitalWrite(5, HIGH);

digitalWrite(4, LOW); // before send command
delay(1);

digitalWrite(5, LOW);

// mySerial.println(“TTM”);
// delay(2000);

digitalWrite(4, HIGH); // make sure high after sending data
delay(1);
}

void loop()
{
mySerial.println(analogRead(A0));
// delay(10);
}

Hi Baozhu

Used HM-BT4502, i can connect with Android phone through “Serial Bluetooth Terminal”( I think every one use it to test Bluetooth). When I wrote App to connect with HM-BT4502, i used base code of Mr Kai-morich. I think he did “Serial Bluetooth Terminal”. the App link below.
(https://github.com/kai-morich/SimpleBluetoothTerminal/commits?author=kai-morich) some warning fixes

Problem is that i could not connect BT4502 with my Andrdoid phone thought new APP. After i debuged i saw “BLUETOOTH_LE_TIO_CHAR_TX_CREDITS” is not correct. (at SerialSocket, line 58-59), i tried to modify this number (i modified with number i received thought “Serial Bluetooth Terminal”), but was not successed. TX_CREDITS called by Mr Kai-morich, i dont understand what is this,How can i get that number.

I know it long text, and bother you much, but can you check this App ?
If you know some Open source APP can connect with HM-BT4502 , please tell me.

Thanks so much for your kindness.

hi
I wish to receive data from HM-BT4502 module on my BLUETOOTH SERIAL APP (which everyone use)
I am using Arduino Uno for it, can you share the code.
i have tried a lot but I don’t get it.