一、功能介绍

项目名:基于物联网的消防机器人集群协同检测与管理平台设计
项目编号:mcu-1492
单片机:STM32F103C8T6
具体功能:
1、通过DS18B20测量环境温度,如果温度过高,则将提示信息发送到手机端,手机探测震动提醒;
2、通过MQ-2检测当前环境的烟雾浓度,如果烟雾过高,则将提示信息发送到手机端,手机探测震动提醒;
3、通过火焰传感器检测是否发送火灾,如果发生,则将提示信息发送到手机端,手机探测震动提醒;
4、通过超声波测量前方距离,自动进行避障;
5、通过4G模块将数据远程发送到手机端,手机端可遥控小车

二、资料总览

实物资料

请添加图片描述

仿真资料

请添加图片描述

三、部分资料展示

1、实物展示

供电接口:TYPE-C
板子类型:PCB集成板,两层板(上下层覆铜接地)
器件类型:元器件基本上为插针式,个别芯片会使用贴片式。
在这里插入图片描述

2、仿真展示

仿真软件版本:proteus8.17
电路连线方式:网络标号连线方式
注意:部分实物元器件仿真中没有,仿真中会用其他工作原理相似的元件代替,这样可能导致实物程序和仿真程序不一样
在这里插入图片描述

3、原理图展示

软件版本:AD2013
电路连线方式:网络标号连线方式
注意:原理图只是画出了模块的引脚图,而并不是模块的内部结构图
在这里插入图片描述

4、PCB图展示

由原理图导出,两层板,上下覆铜接地。
在这里插入图片描述

5、系统框图

绘制软件:VISIO
在这里插入图片描述

6、软件设计流程

绘制软件:VISIO
在这里插入图片描述

7、部分程序展示

软件版本:keil5

/* USER CODE BEGIN Header */

/**********************************
作者:单片机俱乐部
网站:https://www.mcuclub.cn/
**********************************/

/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#include "./DELAY/delay.h"
#include "./HC-SR04/hc-sr04.h"
#include "./MOTOR/motor.h"
#include "./4G/4g.h"
#include "./DS18B20/ds18b20.h"


/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

uint8_t huart1_tx_buf[100];
#define Huart1_Send_Str(...)  HAL_UART_Transmit(&huart1,huart1_tx_buf,sprintf((char *)huart1_tx_buf,__VA_ARGS__),0xffff)		//重定义串口1发送函数
	
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

uint8_t key_num = 0;										//按键键值
uint8_t display_num = 0;								//显示界面
uint32_t time_num = 0;									//计时值
char display_buf[32] = {0};							//显示数组

uint8_t huart1_rx_data = 0;							//串口1接收单个数据

extern uint8_t air_4g_rx_buf[1024];			//4g接收数组
extern uint8_t air_4g_rx_finish;				//4g接收完成标志位

uint8_t flag_begin_5s = 0;							//5s计时开始标志位
uint8_t flag_finish_5s = 0;;						//5s计时完成标志位

uint16_t motor_speed = 10;							//直流电机速度值
uint8_t flag_control = 0;							  //控制标志位
uint8_t flag_direction = 0;							//方向标志位

uint32_t distance_value = 0;						//距离值
uint32_t distance_min = 50;							//距离最小值

float temp_value = 0;										//温度值
uint16_t temp_max = 50;            			//温度最大值
uint16_t tc_temp = 0;            				//温度弹窗

uint16_t smog_value = 0;            		//烟雾值
uint16_t smog_max = 100;            		//烟雾最大值
uint16_t tc_smog = 0;            				//烟雾弹窗

uint16_t tc_fire = 0;										//火焰弹窗

	
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

void Monitor_Function(void);						//监测函数
void Manage_Function(void);							//处理函数

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */


/**********************************
函数名:读取指定ADC通道的数据
传参值:通道
返回值:ADC值
**********************************/
uint32_t ADC_Get_Value(uint32_t Channel)
{
	ADC_ChannelConfTypeDef sConfig;
	
  sConfig.Channel = Channel;
  sConfig.Rank = 1;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
  
  HAL_ADC_Start(&hadc1);									//启动转换并等待结束
  HAL_ADC_PollForConversion(&hadc1, 10); 	//等待转换完成,10是超时时间(单位ms)
  
  return HAL_ADC_GetValue(&hadc1);				//返回转换后的数据
}

/**********************************
函数名:监测函数
传参值:无
返回值:无
**********************************/
void Monitor_Function(void)
{
	if(time_num % 200 == 0)									//监测数据
	{
		HAL_TIM_Base_Stop_IT(&htim1);					//暂停定时器1
		HAL_TIM_Base_Stop_IT(&htim2);					//暂停定时器2
		temp_value = Ds18b20_Read_Temp();			//DS18B20读取温度
		HAL_TIM_Base_Start_IT(&htim1);				//启动定时器1
		HAL_TIM_Base_Start_IT(&htim2);				//启动定时器2

		distance_value = Hcsr04_GetDistance(temp_value);		//获取距离值
		
		if(MQ_DO == 0)
		{
			smog_value = 150 * (ADC_Get_Value(ADC_CHANNEL_0) / 4096.0 * 3.3);  			//获取烟雾值
		}
		else
		{
			smog_value = 0;
		}
	}

	Air_4G_GetData();											//获取4G模块下发数据
}


/**********************************
函数名:处理函数
传参值:无
返回值:无
**********************************/
void Manage_Function(void)
{
	switch(flag_control)
	{
		case 0:				//停止
			flag_direction = 0;
		break;
		
		case 1:				//前进
			if(distance_value < distance_min)     	//当检测到障碍物距离小于最小值,右转避开障碍物
			{
				flag_direction = 4;	
			}
			else																		//否则,小车正常行驶
			{
				flag_direction = 1;	
			}
		break;
		
		case 2:				//后退
			flag_direction = 2;
		break;
		
		case 3:				//左转
			flag_direction = 3;
		break;
		
		case 4:				//右转
			flag_direction = 4;
		break;
		
		default:
			
		break;
	}
	
	if(temp_value > temp_max || smog_value > smog_max || FIRE == 0)							//监测到发生火灾,小车停止
	{
		flag_direction = 0;
	}
	
	if(temp_value > temp_max)							//监测到温度大于设置最大值,弹窗提醒,发送短息
	{
		if(tc_temp == 0)
		{
			tc_temp = 1;
			
			Air_4G_Send_Msg_Chinese(24,"0001000D91685155052799F800080A6E295EA65F025E38FF01");		//发送短息内容:温度异常!
		}
	}
	else
	{
		if(tc_temp == 2)
		{
			tc_temp = 0;
		}
	}
	
	if(smog_value > smog_max)							//监测到烟雾大于设置最大值,弹窗提醒,发送短息
	{
		if(tc_smog == 0)
		{
			tc_smog = 1;
			
			Air_4G_Send_Msg_Chinese(24,"0001000D91685155052799F800080A70DF96FE5F025E38FF01");		//发送短息内容:烟雾异常!
		}
	}
	else
	{
		if(tc_smog == 2)
		{
			tc_smog = 0;
		}
	}
	
	if(FIRE == 0)												//监测到发生火灾,弹窗提醒,发送短息
	{
		if(tc_fire == 0)
		{
			tc_fire = 1;
			
			Air_4G_Send_Msg_Chinese(24,"0001000D91685155052799F800080A53D173B0706B7130FF01");		//发送短息内容:发现火焰!
		}
	}
	else
	{
		if(tc_fire == 2)
		{
			tc_fire = 0;
		}
	}

	if(flag_finish_5s == 1)											//5秒上传一次数据
  {
    flag_begin_5s = 0;
    Air_4G_SendData();
    flag_begin_5s = 1;
		
		if(tc_temp == 1)
		{
			tc_temp = 2;
		}
		
		if(tc_smog == 1)
		{
			tc_smog = 2;
		}
		
		if(tc_fire == 1)
		{
			tc_fire = 2;
		}
  }
}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM1_Init();
  MX_USART1_UART_Init();
  MX_ADC1_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */

	Delay_Init();													//延时初始化
	HC_SR04_Init();												//HC_SR04初始化
	Motor_Init();													//电机初始化
	Ds18b20_Init();  											//DS18B20初始化
	
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	
	HAL_TIM_Base_Start_IT(&htim1);				//启动定时器
	
	HAL_UARTEx_ReceiveToIdle_IT(&HUART_4G, air_4g_rx_buf, sizeof(air_4g_rx_buf) / sizeof(air_4g_rx_buf[0]));		//获取串口接收数据

	temp_value = Ds18b20_Read_Temp();			//DS18B20读取温度
	Delay_ms(500);
	temp_value = Ds18b20_Read_Temp();			//DS18B20读取温度
	Delay_ms(500);
	temp_value = Ds18b20_Read_Temp();			//DS18B20读取温度
	Delay_ms(500);
	temp_value = Ds18b20_Read_Temp();			//DS18B20读取温度
	Delay_ms(500);
	
	Air_4G_Init();												//4G初始化
	Delay_ms(500);
	Air_4G_MQTT_Init();										//MQTT初始化
	Delay_ms(500);
	Air_4G_SendData();										//数据上报初始状态
  flag_begin_5s = 1;										//开启定时

  while (1)
  {
		Monitor_Function();									//监测函数
		Manage_Function();									//处理函数

		if(time_num % 100 == 0)							//指示灯闪烁
		{
			SYSTEM_LED = !SYSTEM_LED;
		}
		
		Delay_ms(1);
		time_num++;
		if(time_num >= 6000)
		{
			time_num = 0;
		}

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */


/**********************************
函数名:定时器中断回调函数
传参值:无
返回值:无
**********************************/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	static uint32_t time_count_5s = 0;
	static uint16_t motor_zhiliu_count = 0;	//直流电机计时值

	if(htim->Instance == htim1.Instance)		//定时器1触发中断
	{
		if(flag_begin_5s == 1)
		{
			time_count_5s++;
			
			if(time_count_5s >= 5000)						//5s计时
			{
				time_count_5s = 0;
				flag_finish_5s = 1;
			}
		}
		else
		{
			time_count_5s = 0;
			flag_finish_5s = 0;
		}
		
		motor_zhiliu_count++;
		if(motor_zhiliu_count >= 100)
		{
			motor_zhiliu_count = 0;
		}
		
		switch(flag_direction)
		{
			case 0:													//停止
				MOTOR1_IN0 = 0;
				MOTOR1_IN1 = 0;
				MOTOR1_IN2 = 0;
				MOTOR1_IN3 = 0;
			
				MOTOR2_IN0 = 0;
				MOTOR2_IN1 = 0;
				MOTOR2_IN2 = 0;
				MOTOR2_IN3 = 0;
			break;
			
			case 1:													//前
				if(motor_zhiliu_count < motor_speed)
				{
					MOTOR1_IN0 = 1;
					MOTOR1_IN1 = 0;
					MOTOR1_IN2 = 1;
					MOTOR1_IN3 = 0;
				
					MOTOR2_IN0 = 0;
					MOTOR2_IN1 = 1;
					MOTOR2_IN2 = 0;
					MOTOR2_IN3 = 1;
				}
				else
				{
					MOTOR1_IN0 = 0;
					MOTOR1_IN1 = 0;
					MOTOR1_IN2 = 0;
					MOTOR1_IN3 = 0;
					
					MOTOR2_IN0 = 0;
					MOTOR2_IN1 = 0;
					MOTOR2_IN2 = 0;
					MOTOR2_IN3 = 0;
				}
			break;
				
			case 2:													//后
				if(motor_zhiliu_count < motor_speed)
				{
					MOTOR1_IN0 = 0;
					MOTOR1_IN1 = 1;
					MOTOR1_IN2 = 0;
					MOTOR1_IN3 = 1;
				
					MOTOR2_IN0 = 1;
					MOTOR2_IN1 = 0;
					MOTOR2_IN2 = 1;
					MOTOR2_IN3 = 0;
				}
				else
				{
					MOTOR1_IN0 = 0;
					MOTOR1_IN1 = 0;
					MOTOR1_IN2 = 0;
					MOTOR1_IN3 = 0;
					
					MOTOR2_IN0 = 0;
					MOTOR2_IN1 = 0;
					MOTOR2_IN2 = 0;
					MOTOR2_IN3 = 0;
				}
			break;
			
			case 3:													//左
				if(motor_zhiliu_count < motor_speed)
				{
					MOTOR1_IN0 = 0;
					MOTOR1_IN1 = 1;
					MOTOR1_IN2 = 0;
					MOTOR1_IN3 = 1;
				
					MOTOR2_IN0 = 0;
					MOTOR2_IN1 = 1;
					MOTOR2_IN2 = 0;
					MOTOR2_IN3 = 1;
				}
				else
				{
					MOTOR1_IN0 = 0;
					MOTOR1_IN1 = 0;
					MOTOR1_IN2 = 0;
					MOTOR1_IN3 = 0;
					
					MOTOR2_IN0 = 0;
					MOTOR2_IN1 = 0;
					MOTOR2_IN2 = 0;
					MOTOR2_IN3 = 0;
				}
			break;

			case 4:													//右
				if(motor_zhiliu_count < motor_speed)
				{
					MOTOR1_IN0 = 1;
					MOTOR1_IN1 = 0;
					MOTOR1_IN2 = 1;
					MOTOR1_IN3 = 0;
				
					MOTOR2_IN0 = 1;
					MOTOR2_IN1 = 0;
					MOTOR2_IN2 = 1;
					MOTOR2_IN3 = 0;
				}
				else
				{
					MOTOR1_IN0 = 0;
					MOTOR1_IN1 = 0;
					MOTOR1_IN2 = 0;
					MOTOR1_IN3 = 0;
					
					MOTOR2_IN0 = 0;
					MOTOR2_IN1 = 0;
					MOTOR2_IN2 = 0;
					MOTOR2_IN3 = 0;
				}
			break;

			default:
			break;
		}
	}
}

/**********************************
函数名:串口事件回调函数
传参值:huart:串口号		Size:数据大小
返回值:无
**********************************/
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if(huart->Instance == HUART_4G.Instance)				//4g串口触发中断
	{
		HAL_UARTEx_ReceiveToIdle_IT(&HUART_4G, air_4g_rx_buf, sizeof(air_4g_rx_buf) / sizeof(air_4g_rx_buf[0]));
		air_4g_rx_finish = 1;
	}
}

/**********************************
函数名:串口错误回调函数
传参值:huart:串口号
返回值:无
**********************************/
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
	HAL_UART_DeInit(huart);
	HAL_UART_Init(huart);
	
	if(huart->Instance == HUART_4G.Instance)
	{
		HAL_UARTEx_ReceiveToIdle_IT(&HUART_4G, air_4g_rx_buf, sizeof(air_4g_rx_buf) / sizeof(air_4g_rx_buf[0]));
		air_4g_rx_finish = 1;
	}
}


/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐