- Home >> News >> Blog articles >> Internet of things
Bluetooth anti-lost device control board design
With the advancement of technology and the impact of various new things, the amount of tasks and information processed by individuals within a unit of time has significantly increased compared with the past. In high-intensity life, forgetting phenomenon is very common. How to find forgotten objects has also become a difficult problem in people's lives, such as keys, coins, ornaments, credit cards, personal documents, earphones and other items that are placed at hand. It is very practical to design an anti-lost device specially for small objects. The anti-lost device can be bound to small objects to provide alarm and retrieval functions when the owner and the object are separated. This design uses the now popular smart phone as the carrier to design a smart anti-lost device based on the Android platform and low-power Bluetooth technology.
1. Function realization plan of anti-lost device
The Bluetooth anti-lost device consists of two parts. One part is the Bluetooth device slave bundled with the object, and the other part is the Bluetooth device host carried by the user. Users and their anti-lost objects often need to maintain a relatively short distance, so the use of Bluetooth short-distance wireless communication makes the design very convenient. First, a Bluetooth pairing connection must be established between the slave and the host. The user searches for the slave that is broadcasting through the host, and selects the slave in the pairing table of the host to complete the connection. When the slave establishes a connection with the master, the master periodically obtains a signal strength value between the master and the slave. When the signal strength value is lower than the preset signal strength threshold, the host starts an alarm to remind the user that the object is about to be lost.
When the Android mobile phone detects that the signal strength is too weak and calls the police, it records the location information at this time through GPS. If the benefactor does not hear the alarm, he can find the approximate location of the lost item based on the location record and realize the recovery. This location information can also be combined with some map software APIs to more intuitively display the location of the lost item on the map.
2. Design of low-power bluetooth slave
The slave machine of this design is composed of CC2540 Bluetooth chip, which realizes simple Bluetooth broadcasting function. With the popularity of smart phones, most Android phones on the market support Bluetooth, but not all Android phones can support Bluetooth Low Energy based on Bluetooth 4.0. Therefore, the host uses an Android phone (Android 4.3.1 and above) that supports Bluetooth Low Energy Protocol 4.0. Every time the Bluetooth chip sends a data packet to the phone, it provides the phone with an estimate of the signal strength. The main process of this design is based on this realization.
2.1 CC2540 software design
The software of the CC2540 Bluetooth chip is composed of the OSAL operating system abstraction layer and the hardware abstraction layer HAL. The OSAL operating system abstraction layer is a small operating system added to the Bluetooth 4.0 BLE protocol stack. The BLE protocol stack, profile and all applications are based on OSAL. The hardware abstraction layer HAL is located at the interface between the operating system kernel and the hardware circuit. It hides the specific details of the hardware, facilitates the transplantation of software on various platforms and makes the software and hardware testing functions possible.
Brief analysis of the operation process of the Bluetooth low energy protocol stack: the main() function of the entire program contains the entry of the entire program, and the operation process of the Bluetooth low energy protocol stack is roughly as follows: (1) Initialize the hardware. (2) Initialize the I/O board. (3) Initialize the HAL driver. (4) Initialize the NV system. (5) Initialize the operating system. (6) Enable interrupts. (7) Finally initialize the circuit board. (8) Run the OSAL operating system. When the system is initialized, it enters the low-power mode, and if there is no task, it will continue to be in the low-power mode. If a task is added, wake up the system, leave the low power consumption mode, and return to the low power consumption mode after the task processing is completed.
The main method of low-power Bluetooth to achieve low power consumption: Connection gap: Low-power Bluetooth devices use a frequency hopping mechanism to achieve connection, that is, the channel selected by the device to send data changes with time. Devices can maintain connections through data exchange at the link layer instead of receiving and sending data. The time interval between sending and receiving data after a channel switch to the next channel switching is called the connection interval. Power consumption can be significantly saved by setting a larger connection interval, while the data sending speed is slower.
Slave delay: When there is no data to send, the slave can choose to skip the connection gap until there is data to send. There is a certain limit to the number of skip gaps, but it can be greatly reduced by skipping the limited connection gap. Small power consumption.
2.2 The key realization of the low-power Bluetooth anti-lost device slave
(1) Configuration of equipment and initialization of Bluetooth protocol stack: The CC2540 module is selected mainly as a tracker bound with personal objects, so the state of the chip module is set as a slave. Initialize the configuration file GAP as the external device mode, and set the slave to broadcast when it is powered on.
(2) Set part codes of GAP parameters, set GAP characteristics, update speed of signal strength value, and part codes of broadcast gaps.
(3) Calculate the distance between the two devices according to the Rssi signal strength value. According to a large number of investigations, it is found that only within a limited range, the distance between two devices has a not-so-standard linear relationship with the signal strength value. The accuracy of the algorithm for calculating the distance is not high, and the reference value of the distance is lower than the signal strength value. Therefore, the calculated distance between the two devices is only for reference, and is not used for actual alarm judgment.
(4) Send the obtained Rssi signal strength value to the host (Android mobile phone device) after processing, and Rssi is the signal strength value, which is the main reference data of the Bluetooth anti-lost device.
3. Design of anti-lost device for Android mobile phone
Because only Android 4.3 and above APIs are used, this design software is developed with Android 4.3.1 system. The devices equipped with Android 4.3.1 can be smart phones, smart watches, smart TVs, etc. Compared with other Android devices, smart phones are more popular and more portable. This software is designed to adapt to mobile phones.
3.1 Mobile phone software interface
The mobile phone software interface is divided into two parts: the first part is mainly the scanning of Bluetooth devices and the establishment of connection. We call it the device scan interface. On this page, the software scans external connectable Bluetooth devices, builds a device directory of connectable devices, and displays the device name, UUID address, Rssi signal strength value and estimated distance value of each connectable device. The scrolling directory of the display device in the interface is assisted by defining the ViewHolder class. The directory obtains the items to be displayed through the getView method, and in the getView method, it mainly searches through the findViewById method. findViewById is a tree-based search method, which consumes a lot of time and needs to generate View objects from XML layout files . So when there are many items that need to be listed, the running speed of the software will be greatly reduced. ViewHolder is mainly aimed at scrollable directories, and greatly reduces system consumption by optimizing the operations performed when directory items are scrolled out of the screen.
The second part of the mobile phone interface design: After the mobile phone and the Bluetooth chip are connected, the signal strength value, connection status and other information are displayed: after selecting a device in the above connectable device list, it will enter the formal monitoring. This interface mainly displays some monitoring data information. This section mainly consists of a scrollable ScrollView. Scroll to display some real-time detected data in this ScrollView.
3.2 Service and Activity in Android phone
Activity includes the GUI that the user is accessing and the programs that need to be run when creating the GUI, and the GUI and these programs are mutually blocked. When the time complexity of the code running in an Activity is too large, this code will cause the interface to crash due to long-term blocking of the GUI. Therefore, programs that need to run for a long time must be placed in the Service corresponding to each Activity. Service runs independently of Activity, and the two can communicate with each other. The process of continuously transmitting data after Bluetooth establishes a connection is a long-term process. Therefore, it is necessary to establish a Service for Bluetooth transmission data service.
3.3 Establishment of Bluetooth connection between the mobile terminal and the device
BLE Bluetooth establishes a connection through GATT. After the connection is established, the data transmission starts, and the Bluetooth transmits the obtained data in the background, and controls the real-time update of the Activity by passing messages. The software flow design is as follows: (1) Initialize the Bluetooth adapter; (2) Turn on Bluetooth: In the BlueToothClass class, some callback functions are defined, and these callback functions return the communication results between the mobile phone and the Bluetooth device; (3) The connection status changes Through this callback, you can know whether the device is connected: (4) callback for discovery service; (5) callback for read operation; (6) callback for data return: open the TimerTask thread in the connection state callback function onConnectionStateChange in the BlueToothCalss class , periodically read the Rssi signal strength value. The slave of the Bluetooth anti-lost device constantly sends the value of Rssi to the host, and the mobile phone as the host realizes the reading of the Rssi value through the readRemoteRssi method of Android: (7) decide what to do when receiving the Rssi value through the callback function onReadRemoteRssi What work; (8) In Android, the Intent object is responsible for completing the message transfer between Activity and Service and between Activities. This function passes the Rssi information obtained by the Bluetooth process to the main process Activity through Intent for processing.
3.4 Realization of mobile phone alarm
Accept the Rssi value transmitted by the Bluetooth thread. When the Rssi value is greater than the allowable range, the mobile phone regards this state as a dangerous state and starts to alarm. But there is a big error in this method: First, there is a certain error in the estimation of the Rssi value by the slave, which is likely to have a large difference due to factors such as weather and temperature. Second, if the carrier of the item is moving, there is a good chance that there will be an obstacle between the mobile phone and the anti-lost item. Obstacles will interfere with the signal strength value reported by the device, thereby affecting the judgment. In order to reduce the error caused by the above reasons, when we receive the Rssi value sent by the device, we do not judge it immediately. Instead, the Rssi value received each time is stored in a cache array. When the cache array is full of ten values. Take an average of these ten values, and use this average as the main basis for judgment. After testing, this will greatly reduce the error and make the judgment more accurate. When the average value exceeds the specified value, the music alarm thread is opened, and when the average value is within the allowed range, the alarm thread is closed.
4. Test of anti-lost device
The programming of CC2540 chip is carried out in IAR Embedded Workbench. This is a C language compiler provided by IAR, which supports many microprocessors and CC2540 low-power bluetooth chip, and the writing and debugging of mobile phone programs are carried out on the eclipse platform. Eclipse is an open source development environment based on java. By installing the Android system support plug-in and combining with the Android SDK, Android programs can be developed.
Test process and results: Program the CC2540 chip through the matching emulator, and bind it to small objects after power-on. The smart phone is connected to the computer with a data cable, and the debugged program is downloaded to the mobile phone on the eclipse for installation. After everything is ready, start testing the anti-lost device. Open the software with the Bluetooth enabled on the mobile phone, and a scrolling list will appear in the software, displaying the searched Bluetooth devices that are broadcasting.
Choose our CC2540 Bluetooth chip to establish a connection. After a few seconds, the connection is successfully established, and the mobile phone enters the monitoring and alarm mode. After entering the alarm mode, CC2540 continuously sends the Rssi value to the mobile phone. At the same time, each received Rssi value and the distance value calculated according to the Rssi value are scrolled and displayed on the screen of the mobile phone. Move the mobile phone, when the mobile phone is within the safe range, it will not call the police. Move the mobile phone far away, and find that when the distance between the mobile phone and the object is too far, the mobile phone will start to alarm.
epilogue
With the release of Bluetooth 4.2, Bluetooth Low Energy has become the primary choice for short-range wireless communication. Bluetooth low power consumption can improve the battery life, and the belongings just need to be with the owner for a long time. The support provided by Bluetooth technology greatly facilitates the design of the anti-lost device. The key issue in the design of the anti-lost device is how to prevent it from being lost, and how to judge whether the object is in a lost state. We use the signal strength value between the mobile phone and the device to judge, because the signal strength value weakens with the increase of the distance. When the distance between the owner and the object is far, it can be seen that the owner has lost the object. Experiment It is proved that the simple judgment of alarming when the signal is weak can deal with most situations, and the design has good practicability.
The above is the design and development example of the smart Bluetooth anti-lost device based on the Android platform introduced by Shenzhen Zuchuang Microelectronics Co., Ltd. for you. If you have bluetooth headset solution development needs, you can trust us. We represent a variety of single-chip microcomputers, voice chips, dual-mode Bluetooth ICs, and wifi chips. Brands include Songhan MCU, Yingguang MCU, Jerry Bluetooth, Ankai Bluetooth, Allwinner, and Realtek. Our technical services include: PCB design, microcontroller development, Bluetooth solutions, software and hardware custom development, APP development, small program development, WeChat official account development, etc. It can also undertake the design of intelligent electronic products, the development of living appliances, the research and development of beauty equipment, the application of Internet of things platform, the smart home control system, the development of TWS earphones, Bluetooth earphone speakers, the development of children's educational toys, the design of electronic education products, etc.
Proposal recommendation
- TOP