BT4502 transmit from UART to BLE app

Hi,

I’ve bought your BT4502 modules and they seem to be great. I can successfully connect and transmit and receive TTM commands to my microcontroller (e.g. TTM:NAM-?\r\n\0 successfully returns my device name). I can also receive data from my bluetooth app when I send data to the FFE9 characteristic fine.

My problem however is sending the data from my microcontroller to my BLE app. I am listening in on FFE4 but I only seem to receive data sporadically, sometimes the data comes through, sometimes multiple data comes through. I’ve tried sending it with “\r\n\0”, “\r\n” or “\0” with no avail. What do I need to do to successfully send data from UART through BLE to my phone?

Thanks,

Hi @harris.shallcross, are you used correct baud rate’s? if possible ca you share the MCU programme here?

You can find the BT4502 documents here: https://s3-us-west-2.amazonaws.com/files.seeedstudio.com/products/113990814/document/HM-BT4502+Bluetooth+Low+Energy+(BLE)+Pass-through+Module+Specification.pdf

1 Like

Hi @salman, here is my code

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 */
}

This is the line that I’m not sure about, when I want to transmit a string to my phone, what format do I need to send it as? do I need to pad it with anything?

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