蓝猫淘气

Bluetooth smart lock solution based on MM32W wireless MCU series

 
Overview

Introducing a Bluetooth application solution with very rich usage scenarios - a Bluetooth smart lock solution developed based on the MM32W series.

 

 

 

picture

 

 

 

 

The MM32W series low-power MCU is used in the smart lock solution . It can be used with the mobile phone to read the Bluetooth information of the smart lock through the APP , and try to pair it. If the pairing is successful, normal communication can be achieved. The mobile APP sends instructions to the smart lock through Bluetooth to unlock. The whole process is simple. Reliable, this solution will completely bid farewell to traditional key unlocking. Bluetooth smart locks are safe and convenient, meet the needs of users, and can be used in a wide range of daily scenarios, such as: door locks (home doors, warehouse doors, truck doors, etc.); car locks (electric car locks, bicycle locks, motorcycle locks, etc.); private Cabinet locks (drawer locks, lockers, suitcases, toolboxes, etc.).

 

Hardware resources are as follows:

This solution is tested and verified based on the MM32 BLE_Test Board. It is equipped with a miniature bidirectional DC motor as the carrier of the unlocking method. Here, only the motor is used to drive the unlocking. The locking is manually operated by default. The user can operate the unlocking through the mobile phone APP . In terms of hardware principles, the DC motor used in this solution is driven by a dedicated IC chip, and its control pins are connected to PD2 and PD3 of the MCU . By controlling the levels of the two IOs , the motor can be in different motion states. Operation; use PB1 to connect to the green LED indicator light, which can be used as an indication of Bluetooth connection status; the simulated lock body status function pin PA1 is configured as a pull-up input, and the low-power wake-up pin selection PA0 is configured as a pull-up input; Bluetooth related The functional pins are consistent with the previously introduced solution, so we will not elaborate too much here. The following is the DC motor drive schematic diagram:

 

 

 

picture

 

 

 

 

Software resources are as follows:

Combined with the hardware resources used above, below we focus on the software implementation process and related configuration code. Since this application solution requires the pairing function to be used when establishing a connection between the BLE chip and the APP , it can be implemented using the interface function of the Bluetooth library. Here, the pairing key is set to "123456" . It needs to be set correctly during the connection to successfully pair and connect. on; the functional pins that control the DC motor are all configured in push-pull output mode, directly connected to the driver IC , and the forward and reverse rotation, start and stop of the motor are controlled by changing the combination of high and low levels; in addition, the test and simulation function pins are The pins are configured to more intuitively observe and verify the application effect, and the watchdog reset function is enabled to ensure the stable performance of the Bluetooth service; due to the need for low power consumption in the battery power supply of the solution, this solution has low power consumption Use STOP mode.

 

The following initializes the configuration content of the main function. It mainly initializes all peripheral resources and the Bluetooth protocol stack, and runs Bluetooth in the form of an interrupt service routine. The main function implemented in the loop of the main function is to determine whether to enter low-power mode, and After waking up , perform different operations on the DC motor based on the received Bluetooth data . The code is as follows:

 

 

 

picture

 

The following is a brief introduction to several functions related to Bluetooth low energy and DC motor operation:

 

// Initialize DC motor control pins PD2 and PD3 , and set them all to low level

void MOTOR_Init(void);// Stop the rotation of the DC motor

static void MOTOR_Stop(void);// Control the DC motor operation according to different direction parameters

static void MOTOR_Run(unsigned char direction);

 

       In the MOTOR_Proc() processing function, the password data received by Bluetooth is analyzed. If it completely matches the default 4 -digit 0-9 digital password, the operation is unlocked and the DC motor is controlled to run in the set direction. For easy verification, Simulate a signal to indicate successful unlocking. This is achieved by collecting the input of the specified IO pin PA1 . If the input is high level, the DC motor will stop rotating and the lock flag will be cleared. The following is the code implementation content:

 

picture

 

We send data to the mobile phone in the gatt_user_send_notify_data_callback function. This function is a callback function. The protocol stack will call back this function (asynchronously) when the system allows it. This function is used by the Bluetooth module to actively send unlock status information to the mobile APP . Function No blocking code may be added internally. The detailed implementation code is as follows:

 

// After the Bluetooth connection is successful, the protocol will call this callback function when it is idle.

 void gatt_user_send_notify_data_callback(void)

{

static u8 LockFlagBak = 1;

if (LockFlagBak != LockFlag)

{

LockFlagBak = LockFlag;//LockFlag flag will be cleared and set in MOTOR_Proc()      

sconn_notifydata(&LockFlagBak,1);

}

}

 

The Bluetooth protocol will periodically call back this UsrProcCallback() function, whether in broadcast state or connection state. In this function, you can count the timeout between running and low-power mode switching, and give IrqMcuGotoSleepAndWakeup() to handle whether to enter low-power mode. , and use LED indicators to display different statuses based on the obtained connection information . The detailed implementation code is as follows:

 

// The Bluetooth protocol will call back this function periodically

void UsrProcCallback(void) 

{

static unsigned char led_flash = 0;    

IWDG_ReloadCounter();

StandbyTimeout++; 

if(gConnectedFlag){ // Connection successful

StandbyTimeout = 0;

LED_ONOFF(1);

}else{

led_flash++;

LED_ONOFF(!(led_flash%10)); // Bluetooth is not connected, the indicator light flashes quickly

}

}

 

In addition to the key Bluetooth data sending functions mentioned above, the following briefly introduces some Bluetooth-related characteristic value definitions. The two characteristic values ​​in this case are defined in const BLE_CHAR AttCharList[]:

 

{TYPE_CHAR,0x0011,{ATT_CHAR_PROP_W_NORSP,0x12,0,0xf1,0xff}, UUID16_FORMAT},// Unlock command

{TYPE_CHAR,0x0013,{ATT_CHAR_PROP_RD|ATT_CHAR_PROP_NTF,0x14,0,0xf2,0xff}, UUID16_FORMAT},// Lock body status

Implemented custom characteristic value service declaration in void att_server_rdByGrType(u8 pdu_type, u8 attOpcode, u16 st_hd, u16 end_hd, u16 att_type) ;

In void ser_write_rsp() , receive and store the Bluetooth data sent by the mobile APP :

if(StartEncryption)

{

    if (valueLen_w < 9)// Parse and store Bluetooth data

    {

     Password_wr[0] = valueLen_w;

     memcpy(&Password_wr[1], attValue, valueLen_w);

     }

     }else{ // Invalid data, not saved

     Password_wr[0] = 0;

}

 

In void server_rd_rsp(u8 attOpcode, u16 attHandle, u8 pdu_type), the lock body status is returned to the mobile APP :

att_server_rd( pdu_type, attOpcode, attHandle, &LockFlag, 1); // Return the LockFlag  flag to the mobile APP

 

The mobile phone operation process is as follows:

Use the phone's native Bluetooth interface to find the device, find MM32_Lock and click to pair. The default pairing password is 123456. After successful pairing, the MM32_LOCK device is automatically saved to the paired device list. This step is not required in the future;

 

Open the App on your phone , start searching for BLE devices, select the Bluetooth device with the corresponding name ( MM32_LOCK ) and pair it, and wait for the connection to be successful. After the connection is successful, there will be a corresponding prompt, and the name of the button Connect will change to Disconnect ;

 

After the connection is successful, write 0x31323334 to the characteristic value with UUID fff1 (simulating the user entering the password "1234" ), and the motor connected to the test board starts to rotate and perform the unlocking action;

 

Input a low level to PA1 to simulate the lock opening. The characteristic value result of UUID fff2 is 0 , which means the lock body is open. Input a high level to PA1 to simulate the lock body lock. The characteristic value result of UUID fff2 is 1 , which means the lock body is locked. superior. Here only the motor is used to drive the lock, and the lock is manually operated by default.

 

 

  

 

picture

 



 

 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-05-08 18:12:09

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号