Anti-lost Device Based on Bluetooth

Function introduction:
1. After the Bluetooth is disconnected, it will send out an audible and visual alarm.
2. When the Bluetooth is connected, you can use the APP to send control commands.
3. When the Bluetooth is disconnected, it will send out an audible and visual alarm. After connecting in time, the alarm will stop.
4. If it is not connected in time, the alarm will stop. Call the user's mobile phone number to remind, and send a text message to the mobile phone at the same time, the content of the text message is the location information

Hardware preparation:
1. stm32f103c8t6 minimum system board
2. Bluetooth 4.0 module
3. Passive buzzer for playing MIDI music
4. SIM868 module for sending text messages, calling, positioning
5. Booster module, due to SIM868 module 5V-18V power supply is required, and when the power supply voltage is 5V, the current must be guaranteed to be 1.5A or above.
6. LED light, using the small light at the PC13 pin on the board.
7. ST-LINK downloader, used to download programs.
Hardware wiring:
Bluetooth module: TX—PB11 RX—PB10 VCC–5V GND–GND STATE–PA4
Active buzzer: IO–PA5
Passive buzzer: IO–PA6
SIM868 module: VCC–5~18V RX–PA2 TX–PA3 GND–GND
demonstration pictures:
1. Real object
insert image description here
2. Serial port output information
Because it is indoors, the positioning is not accurate.
insert image description here
3. The mobile phone will receive calls and text messages.
insert image description here
4. The mobile APP
uses the HC Bluetooth Assistant APP download link
. Send character 0 to sound and light alarm, send character 1 to turn off sound and light, and send character 2 to turn on the light.
insert image description here

Code implementation:
main.c file

/*
蓝牙模块4.0测试代码
蓝牙模块与串口3相接
即:TX---PB11  RX---PB10 VCC--5V GND--GND  STATE--PA4
有源蜂鸣器接线:IO--PA5
无源蜂鸣器接线:IO--PA6
SIM868模块接线:
VCC--5~18V,5V时,电流要保证1.5A以上
RX--PA2
TX--PA3
GND--GND
VDD--3.3V/5V.
VDD是用来做电平匹配的,当外部系统为 5V 时,VDD接5V。当外部系统是3.3V时,VDD接3.3V

蓝牙断开连接时,STATE引脚输出低电平,即蓝牙模块上的红灯处于闪烁状态
蓝牙连接时,STATE引脚输出高电平,红灯处于常亮状态
通过PA4的外部中断即可判断蓝牙是否断开

要修改的地方
main.c文件中:callPhone,拨打的号码改为你自己的号码
sim868.c文件中:短信接收号码和短信中心号码
*/
#include <stm32f10x.h>
#include <stdbool.h>
#include "led.h"
#include "usart.h"
#include "delay.h"
#include "exti.h"
#include "beep.h"
#include "buzzer.h"
#include "timer.h"
#include "sim868.h"

int g_isBreak = true; //是断开状态
int g_handle = 0;  //断开后如何处理,1:声光报警,2:打电话,3:发定位信息
char PUB_BUF[100] = {
    
    0};    //存储定位信息
char time[20],longitude[20],latitude[20];//存储:定位时间、经纬度
char *callPhone = "ATD10086;\r\n";  //拨打电话,修改这里可以修改拨打的电话号

int main(void)
{
    
    
	//设置NVIC优先级分组
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	SysTick_Init(72);  //初始化延时函数
	//初始化硬件
	led0_init();  //PC13
	BEEP_Init(); //有源蜂鸣器初始化PA5
	BUZZER_Init();//无源蜂鸣器初始化PA6
	BUZZER_BEEP1();//无源蜂鸣器响一声
	
	EXTIX_PA4_Init();  //外部中断初始化
	USART1_Init(9600);	//设置波特率为9600--调试用的
	USART2_Init(9600);	//设置波特率为9600--SIM868使用
	USART3_Init(9600);	//设置波特率为9600--蓝牙使用
	Timer2_Init_Config();  //定时1S,用于sim868定时发送AT指令
	printf("蓝牙模块开始配置\r\n");
	ble_config_set();  //蓝牙模块的配置
	printf("蓝牙模块配置完毕\r\n");
	printf("SIM868模块开始配置\r\n");
	Wait_CREG();    //查询等待模块注册成功
	Set_Text_Mode();//设置短信为TEXT模式
	GPS_Init();      //初始化GPS功能
	GET_GPS(PUB_BUF,time, longitude,latitude); 	//获取定位信息,放入PUB_BUF数组中
	//Send_GPS_Msg(PUB_BUF);  //发送定位消息
	printf("SIM868模块配置完毕\r\n");

	while(1)
	{
    
    
		if(g_isBreak==false) //连接状态
		{
    
    
			printf("蓝牙已连接\r\n");
			g_handle = 1;  //下次断开时可以声光报警
			//LED0 = 1;
		}
		
		else                 //断开状态
		{
    
    
			printf("蓝牙未连接\r\n"); 
			if(g_handle == 3)
			{
    
    
				g_handle = 4;
				printf("正在发送定位信息...\r\n");
				
				GET_GPS(PUB_BUF,time, longitude,latitude); //获取定位信息,放入PUB_BUF数组中
				printf("PUB_BUF = %s\r\n",PUB_BUF); //定位消息
				Send_GPS_Msg(PUB_BUF);  //发送定位消息
				//Send_GPS_Msg("Something has been lost");  //发送自定义消息,不能中文
			}	
			else if(g_handle==2)
			{
    
    
				g_handle = 3;
				printf("正在拨打电话....\r\n");
				UART2_SendString(callPhone);     //拨打指定电话
			}
			else if(g_handle==1)  //蓝牙未连接时g_handle == 1;
			{
    
    
				g_handle = 2;    //当连上蓝牙后再断开才会再次报警
				printf("正在声光报警....\r\n");
				MIDI_PLAY(); //播放MIDI音乐并且闪烁LED灯
			}
		}
		 delay_ms(1000);
	}
}

Guess you like

Origin blog.csdn.net/NICHUN12345/article/details/123780790