cancel
Showing results for 
Search instead for 
Did you mean: 

How to send AT Commands to HC-05 using CubeIDE ?

ahmetgunduz
Associate III

I am using HC-05 bluetooth module and STM32F407. I am trying to change the name of the bluetooth module. I can enter AT Command Mode, but i can't send any AT Commands to module. I tried something in main. But it didn't work

Here is my try:

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "string.h"
 
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart3;
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
 
int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  /* USER CODE BEGIN 2 */
  char* data = "AT+NAME=charles \n\r";
  HAL_UART_Transmit(&huart3, (uint8_t *)data, strlen(data), 2000);
  /* USER CODE END 2 */
  while (1)
  {
  }
}
 
 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}
 
static void MX_USART3_UART_Init(void)
{
 
  huart3.Instance = USART3;
  huart3.Init.BaudRate = 38400;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
}
 
 
static void MX_GPIO_Init(void)
{
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOB_CLK_ENABLE();
}

5 REPLIES 5

Do they use the +++ escape sequence?​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

No, they don't

I edited the post. Can you take a look again, please?

S.Ma
Principal

The command shall be sent while the module is not paired.

Here you don't have any feedback which is tricky.

With some SW (no SmartIO IP) you could connect the STLink USART to the HC-05 and use teraterm to do the job remotely. This way you can check if you get the answer back from the HC-05. Then power cycle and verify the job is done.

Cross posted over here https://community.arm.com/developer/tools-software/tools/f/keil-forum/44531/how-to-send-at-commands-using-keil

Are you sending the data in the correct form?

Is the baud rate correct? Are the pins and clocks enabled somewhere?

Use a Debugger, check the register settings. Use Scope and check the signalling.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..