JDY-31 Bluetooth Module (USART)
Important note: This description was translated from Chinese and taken from the website :
【STM32F103】JDY-31蓝牙模块(USART)_jdy31蓝牙模块-CSDN博客
JDY has a total of 6 pins, and if we want to make it work, we only need to connect two wires, that is, VCC and GND. Connect a voltage of 3.6V~6V to VCC (recommended is 5V), connect GND to ground, and then JDY31 starts working, and we can find JDY’s Bluetooth by turning on Bluetooth on our mobile phones.
After pairing, our mobile phone can communicate with JDY31, but it is meaningless just to communicate with JDY31, so we will connect JDY31 and the microcontroller, so that we can use the mobile phone to control the microcontroller through Bluetooth.
We connect the RXD of JDY31 to the TXD of our STM32F103, and the TXD is connected to the RXT of our STM32F103 (it is recommended to use the resources of USART1, pins 9 and 10 of GPIOA, because the working voltage of JDY is higher, and STM32F103 is generally The pins cannot withstand it, but the pins of the USART1 resource can accept 5V voltage. Please refer to the pin definition table for details)
In this way, as long as our mobile phone is connected to JDY31 through Bluetooth, and JDY31 and STM32F103 are connected through the serial port, as long as the mobile phone sends serial port data through the Bluetooth assistant, then JDY will directly receive it and send it to STM32F103. The data sent by the STM32F103 through the serial port will also be transmitted to our mobile phones via JDY31.
So Bluetooth communication is not that difficult, or it is because of highly integrated modules like JDY31 that make our Bluetooth communication simple.
The remaining two pins are STATE and EN. Simply put, EN is useless. After we connect the Bluetooth from JDY, STATE will output a high level, and an external LED can be connected to visually check whether the Bluetooth is connected. But it’s not really necessary, because JDY comes with a small red light. The small red light will flash when waiting for connection, and the small red light will stay on after connecting.
AT command control JDY31 The above describes how to perform Bluetooth communication through JDY31. And if we want to modify some configuration information of JDY (such as Bluetooth name, connection password, etc.), we need to use AT commands to control JDY. In fact, AT commands sound cool, but they are actually just a bunch of specific strings.
For example, if I want to query the version number, I can directly ask the STM32F103 to send the string “AT+VERSION\r\n” through the serial port, that is, add “\r\n” to the command. Then JDY31 will send the version number through the serial port. In essence, it is still serial communication.
There is nothing much to say about the above nine general instructions. Let’s briefly talk about the instructions with parameters.
The baud rate command can add parameters to modify the baud rate, but it is not freely set. For example, if I want to set the baud rate of 9600, then the command I send should be “AT+BAUD<4>\r\ n”, other baud rate parameters can refer to the figure above.
Set the pairing password, the default is 1234, just send the password to be modified as a parameter.
Bluetooth name setting, there will be a slight delay. You have to wait for the Bluetooth name searched on the phone to be updated.
As mentioned above, under normal circumstances, after JDY31 is connected to Bluetooth, STATE will send out a high level. If we disable the serial port status output, then STATE will be of no use like EN.
Use the serial port to perform AT commands on JDY-31 through STM32F103
The following explanation is used in conjunction with the following code.
There is nothing to say about opening the serial port. Just copy the code from my previous article about USART.
For the convenience of serial port output, I added the stdio library and rewritten fputc. This is to be able to directly use printf for serial port output. keil5 also needs to be configured as shown below.
Then all we need to do is to directly send the AT command. It should be noted that there needs to be a delay between two consecutive commands, otherwise JDY31 will not be able to respond (my guess is that JDY will ignore some commands)
The results obtained are in the figure below the code.
- //interrupt function
- void USART1_IRQHandler(void){
- //Determine the data receiving flag bit
- if(SET==USART_GetFlagStatus(USART1,USART_FLAG_RXNE)){
- uint16_t data=USART_ReceiveData(USART1); //Read out the received data
- printf(“%c”,data);
- USART_ClearITPendingBit(USART1,USART_FLAG_RXNE); //Clear the data receiving flag bit
- }
- }
- void sendbyte(uint16_t Data){
- //Send data
- USART_SendData(USART1,Data);
- //Wait for data to be sent
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- }
- //Rewrite fputc here so that you can directly use printf to output the serial port
- int fputc(int ch, FILE *f){
- sendbyte(ch);
- return ch;
- }
- int main(void){
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- GPIO_InitTypeDef itd;
- itd.GPIO_Mode=GPIO_Mode_AF_PP; //Multiplex push-pull output
- itd.GPIO_Pin=GPIO_Pin_9; //TX pin
- itd.GPIO_Speed=GPIO_Speed_2MHz; //This is optional
- GPIO_Init(GPIOA,&itd);
- itd.GPIO_Mode=GPIO_Mode_IN_FLOATING; //Floating input
- itd.GPIO_Pin=GPIO_Pin_10; //RX pin
- GPIO_Init(GPIOA,&itd);
- USART_InitTypeDef uitd;
- uitd.USART_BaudRate=9600; //baud rate
- uitd.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //Hardware flow control
- uitd.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //Serial port mode
- uitd.USART_Parity=USART_Parity_No; //check
- uitd.USART_StopBits=USART_StopBits_1; //Stop bit length
- uitd.USART_WordLength=USART_WordLength_8b; //Transmitted word length
- USART_Init(USART1,&uitd);
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //Enable USART receive interrupt
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //NVIC interrupt configuration
- NVIC_InitTypeDef nitd;
- nitd.NVIC_IRQChannel=USART1_IRQn;
- nitd.NVIC_IRQChannelCmd=ENABLE;
- nitd.NVIC_IRQChannelPreemptionPriority=2;
- nitd.NVIC_IRQChannelSubPriority=2;
- NVIC_Init(&nitd);
- USART_Cmd(USART1,ENABLE); //Power on USART
- printf(“AT+NAME\r\n”); //Query Bluetooth name
- Delay_ms(100);
- printf(“AT+LADDR\r\n”); //Query mac address
- Delay_ms(100);
- printf(“AT+BAUD\r\n”); //Query baud rate
- Delay_ms(100);
- printf(“AT+PIN\r\n”); //Query Bluetooth connection password
- Delay_ms(100);
- printf(“AT+ENLOG\r\n”); //Query status output enable status
- Delay_ms(100);
- printf(“AT+VERSION\r\n”); //Query version
- Delay_ms(100);
- printf(“AT+NAME<JDY>\r\n”); //Change the Bluetooth name, but the response is slow and it will take a while to update.
- while(1){
- }
- }Ignore the last line, it is sent by JDY after Bluetooth is disconnected.
JDY communicates with mobile phone via Bluetooth
In fact, Bluetooth communication sounds very cool. In fact, after your mobile phone is connected to Bluetooth, the so-called Bluetooth communication actually uses the serial port to send information. Therefore, we can directly use printf to output the content, and then the mobile phone can receive it. However, we need to use a Bluetooth assistant to see it. I use the one provided by a certain merchant. You can find any one. It is even available in the WeChat applet. .
- //interrupt function
- void USART1_IRQHandler(void){
- //Judge the data receiving flag bit
- if(SET==USART_GetFlagStatus(USART1,USART_FLAG_RXNE)){
- uint16_t data=USART_ReceiveData(USART1); //Read the received data
- printf(“%c”,data);
- USART_ClearITPendingBit(USART1,USART_FLAG_RXNE); //Clear the data receiving flag bit
- }
- }
- void sendbyte(uint16_t Data){
- //Send data
- USART_SendData(USART1,Data);
- //Wait for data to be sent
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- }
- int fputc(int ch, FILE *f){
- sendbyte(ch);
- return ch;
- }
- int main(void){
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- GPIO_InitTypeDef itd;
- itd.GPIO_Mode=GPIO_Mode_AF_PP; //Multiplexed push-pull output
- itd.GPIO_Pin=GPIO_Pin_9; //TX pin
- itd.GPIO_Speed=GPIO_Speed_2MHz; //This is optional
- GPIO_Init(GPIOA,&itd);
- itd.GPIO_Mode=GPIO_Mode_IN_FLOATING;//floating input
- itd.GPIO_Pin=GPIO_Pin_10; //RX pin
- GPIO_Init(GPIOA,&itd);
- USART_InitTypeDef uitd;
- uitd.USART_BaudRate=9600; //baud rate
- uitd.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //Hardware flow control
- uitd.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //Serial port mode
- uitd.USART_Parity=USART_Parity_No; //check
- uitd.USART_StopBits=USART_StopBits_1; //Stop bit length
- uitd.USART_WordLength=USART_WordLength_8b; //Transmitted word length
- USART_Init(USART1,&uitd);
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //Enable USART receive interrupt
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //NVIC interrupt configuration
- NVIC_InitTypeDef nitd;
- nitd.NVIC_IRQChannel=USART1_IRQn;
- nitd.NVIC_IRQChannelCmd=ENABLE;
- nitd.NVIC_IRQChannelPreemptionPriority=2;
- nitd.NVIC_IRQChannelSubPriority=2;
- NVIC_Init(&nitd);
- USART_Cmd(USART1,ENABLE); //Power on USART
- while(1){
- printf(“Hello World”);
- Delay_ms(2000);
- }
- }
This is the complete information received by the serial port assistant, including Bluetooth connection and disconnection.
