Building a Simple Internet of Things Server and Client-Bluetooth Control (16)

Posted by HP400 on Sun, 07 Jul 2019 23:32:48 +0200

Bluetooth is used to control the device, so USART1 of the board is used for computer serial port, USART2 is used for wifi transmission, USART3 is used for Bluetooth control. Bluetooth is chosen to prevent remote control devices (mainly wifi control protocol will not be written.)
Code address: https://github.com/klren0312/stm32_wifi
2017.4.19

Building a Simple Internet of Things Service-STM32 (I)
Building a Simple Internet of Things Server-Nodejs_net(2)
Building a Simple Internet of Things Server-Nodejs_mysql(3)
Building a Simple Internet of Things Server-net+mysql(4)
Building a Simple Internet of Things Server-First Integration (V)
Building a Simple Internet of Things Service-Nodejs_express Service (6)
Building a Simple Internet of Things Server-ECharts Data (7)
Building Simple Internet of Things Service Side-Integration (8)
Building a Simple Internet of Things Server-Maibu Display (9)
Building a Simple Internet of Things Service-DCloud Mobile Terminal (10)
Build a Simple Internet of Things Server - First Supplement (11)
Building Simple Internet of Things Server and Client - Second Supplement (12)
Building Simple Internet of Things Server and Client-Database Function Increased (13)
Building a Simple Internet of Things Server and Client-Microblog Interface (14)
Building a Simple Internet of Things Server and Client-Microblog Sending Information (15)
Building a Simple Internet of Things Server and Client-Bluetooth Control (16)

Bluetooth Control

1. Bluetooth module

(1) DX-BT05 module configuration

AT+NAME zzesiot //Set the ble name
AT+BAUD 9600 //Setting baud rate to 9600
AT+ROLE=0 //Set to slave device
AT+START //Equipment work

2.stm32 serial port configuration

(1) Configure USART3

void Usart3_Init(unsigned int baud)
{
    GPIO_InitTypeDef gpioInitStruct;
    USART_InitTypeDef usartInitStruct;
    NVIC_InitTypeDef nvicInitStruct;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
    //PB10 TXD
    gpioInitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;
    gpioInitStruct.GPIO_Pin = GPIO_Pin_10;
    gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&gpioInitStruct);
    //PB11 RXD
    gpioInitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    gpioInitStruct.GPIO_Pin = GPIO_Pin_11;
    gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&gpioInitStruct);
    //Serial port parameters
    usartInitStruct.USART_BaudRate = baud;
    usartInitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    usartInitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    usartInitStruct.USART_Parity = USART_Parity_No;
    usartInitStruct.USART_StopBits = USART_StopBits_1;
    usartInitStruct.USART_WordLength = USART_WordLength_8b;
    USART_Init(USART3,&usartInitStruct);
    USART_Cmd(USART3,ENABLE);
    USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
    //interrupt
    nvicInitStruct.NVIC_IRQChannel = USART3_IRQn;
    nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
    nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 0;
    nvicInitStruct.NVIC_IRQChannelSubPriority = 0;
    NVIC_Init(&nvicInitStruct);
}

(2) Interruption function in USART3

void USART3_IRQHandler(void)
{
    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
    {
        if(usart3Len >= 64)    
            usart3Len = 0;
        usart3Buf[usart3Len++] = USART3->DR;
        USART_ClearFlag(USART3, USART_FLAG_RXNE);
    }
}

3. Principal function

(1) Initialize USART 3 and set the baud rate to 9600

Usart3_Init(9600);

(2) Bluetooth acceptance command judgment (led configuration related code is not put up)

while(1){
    if(usart3Len > 0){
        if(strcmp(usart3Buf, "666") == 0){
            UsartPrintf(USART3, "The command sent is:\r\n%s\r\n", usart3Buf);
                ledOn(LED1);//LED 1 bright
            }
        else if(strcmp(usart3Buf, "233") == 0){
            UsartPrintf(USART3, "The command sent is:\r\n%s\r\n", usart3Buf);
            ledOff(LED1);//LED 1 extinction
        }
        memset(usart3Buf, 0, sizeof(usart3Buf));
        usart3Len = 0;
    }
    DelayXms(100);
}

4. Results

(1) sending and receiving Bluetooth data screenshots on the mobile phone

The Bluetooth Serial Port Assistant in app Mall is used, and its own Bluetooth Serial Port Assistant is being developed...


Bluetooth Data Sending and Accepting on Mobile Phone

(2) Single Chip Microcomputer Phenomenon

  • When sending 666, the LED 1 lights up

    LED 1 bright
  • When 233 is sent, LED 1 dies

    LED 1 extinction

@ Electricity cabbage 20170419

Topics: Mobile github MySQL Database