当前位置:网站首页>基于STM32+华为云IOT设计的酒驾监控系统
基于STM32+华为云IOT设计的酒驾监控系统
2022-07-04 12:46:00 【InfoQ】
1. 前言





2. 登录华为云创建云端设备
2.1 创建产品





MQTT (1883) a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com



2.2 创建设备



设备ID
625cd29cecf9c41c38215ba5_1126626497
设备密钥
1126626497
{
"device_id": "625cd29cecf9c41c38215ba5_1126626497",
"secret": "1126626497"
}
2.3 产品模型定义




2.4 生成MQTT登录密匙



2.5 使用MQTT客户端软件登录
a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com
121.36.42.100
2.6 上报数据


//订阅主题: 平台下发消息给设备
$oc/devices/625cd29cecf9c41c38215ba5_1126626497/sys/messages/down
//设备上报数据
$oc/devices/625cd29cecf9c41c38215ba5_1126626497/sys/properties/report
//上报的属性消息 (一次可以上报多个属性,在json里增加就行了)
{"services": [{"service_id": "MQ3","properties":{"MQ3":50}}]}
3. STM32代码实现
3.1 系统原理图


3.2 程序下载


3.3 字体取模

3.4 main.c代码
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "usart.h"
#include <string.h>
#include "timer.h"
#include "oled.h"
#include "adc.h"
#include <string.h>
#include <stdlib.h>
#include "font.h"
#include "sim800c.h"
#include "mqtt.h"
/*
硬件连接方式:
1. TFT 1.44 寸彩屏接线
GND 电源地
VCC 接5V或3.3v电源
SCL 接PC8(SCL)
SDA 接PC9(SDA)
RST 接PC10
DC 接PB7
CS 接PB8
BL 接PB11
2. 蜂鸣器
VCC--->3.3V
DAT--->PA6
GND--->GND
3. 酒精检测传感器MQ3
VCC--->3.3V
GND--->GND
DAT--->PA1
4. 板载LED灯接线
LED1---PA8
LED2---PD2
5. 板载按键接线
K0---PA0
K1---PC5
K2---PA15
6. SIM800C--GSM模块
GND----GND
VCC--->3.3V
PA2----SIM800C_RXD
PA3----SIM800C_TXD
*/
//华为物联网服务器的设备信息
#define MQTT_ClientID "62381267575fb713ee164ad2_xl_1_0_0_2022032106"
#define MQTT_UserName "62381267575fb713ee164ad2_xl_1"
#define MQTT_PassWord "124344feff3e3d96ff6af13cf36af36766619ff1eeee40e99cbae9b7b9739fe4"
//订阅与发布的主题
#define SET_TOPIC "$oc/devices/62381267575fb713ee164ad2_xl_1/sys/messages/down" //订阅
#define POST_TOPIC "$oc/devices/62381267575fb713ee164ad2_xl_1/sys/properties/report" //发布
//设置连接的路由器信息
#define CONNECT_WIFI "Xiaomi_meizi6" //将要连接的路由器名称 --不要出现中文、空格等特殊字符
#define CONNECT_PASS "12170307yu" //将要连接的路由器密码
#define CONNECT_SERVER_IP "a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com" //服务器IP地址
#define CONNECT_SERVER_PORT 1883 //服务器端口
//JTAG模式设置,用于设置JTAG的模式
//mode:jtag,swd模式设置;00,全使能;01,使能SWD;10,全关闭;
#define JTAG_SWD_DISABLE 0X02
#define SWD_ENABLE 0X01
#define JTAG_SWD_ENABLE 0X00
void JTAG_Set(u8 mode)
{
u32 temp;
temp=mode;
temp<<=25;
RCC->APB2ENR|=1<<0; //开启辅助时钟
AFIO->MAPR&=0XF8FFFFFF; //清除MAPR的[26:24]
AFIO->MAPR|=temp; //设置jtag模式
}
u16 MQ3_data=0; //酒精浓度
float MQ3_data_float=0; //mg/l
char data_buff[100];
//酒精超标提示
u8 sim800c_buff[100]="Drunk driving reminder";
int main()
{
u8 state=0;
u8 key=0;
u32 time_cnt=0;
JTAG_Set(JTAG_SWD_DISABLE); //释放PA15
LED_Init(); //LED灯初始化
BEEP_Init(); //蜂鸣器初始化
KEY_Init(); //按键初始化
USART1_Init(115200); //串口1初始化-打印调试信息
Lcd_Init(); //LCD初始化
Lcd_Clear(0); //清屏为黑色
LCD_LED_SET; //通过IO控制背光亮
AdcInit(); //ADC初始化--检测酒精传感器的值
TIMER2_Init(72,20000);//辅助串口2接收,超时时间为20ms
USART2_Init(115200); //可能的波特率(测试): 57600 、9600、115200
USART3_Init(115200);//串口-WIFI
TIMER3_Init(72,20000); //超时时间20ms
//预热视频
LCD_ShowChineseFont(0,16*3,16,HZ_FONT_16[20],RED,0);
LCD_ShowChineseFont(16,16*3,16,HZ_FONT_16[21],RED,0);
LCD_ShowChineseFont(16*2,16*3,16,HZ_FONT_16[22],RED,0);
LCD_ShowChineseFont(16*3,16*3,16,HZ_FONT_16[23],RED,0);
LCD_ShowChineseFont(16*4,16*3,16,HZ_FONT_16[24],RED,0);
LCD_ShowChineseFont(16*5,16*3,16,HZ_FONT_16[25],RED,0);
LCD_ShowChineseFont(16*6,16*3,16,HZ_FONT_16[26],RED,0);
LCD_ShowChineseFont(16*7,16*3,16,HZ_FONT_16[27],RED,0);
//delay_ms(30000); //初始化时间30秒
delay_ms(5000); //初始化时间5秒
Lcd_Clear(0); //清屏为黑色
//采集数据的真实数据
LCD_ShowChineseFont(0,16*2,16,HZ_FONT_16[0],RED,0);
LCD_ShowChineseFont(16,16*2,16,HZ_FONT_16[1],RED,0);
LCD_ShowChineseFont(16*2,16*2,16,HZ_FONT_16[2],RED,0);
LCD_ShowChineseFont(16*3,16*2,16,HZ_FONT_16[3],RED,0);
//酒精含量计算结果
LCD_ShowChineseFont(0,16*3,16,HZ_FONT_16[4],RED,0);
LCD_ShowChineseFont(16,16*3,16,HZ_FONT_16[5],RED,0);
LCD_ShowChineseFont(16*2,16*3,16,HZ_FONT_16[6],RED,0);
LCD_ShowChineseFont(16*3,16*3,16,HZ_FONT_16[7],RED,0);
//酒精超标显示
Gui_DrawFont_GBK16(0,16*4,RED,0,(u8*)" ");
LCD_ShowChineseFont(0,16*4,16,HZ_FONT_16[16],RED,0);
LCD_ShowChineseFont(16,16*4,16,HZ_FONT_16[17],RED,0);
LCD_ShowChineseFont(16*2,16*4,16,HZ_FONT_16[18],RED,0);
LCD_ShowChineseFont(16*3,16*4,16,HZ_FONT_16[19],RED,0);
//初始化SIM800C
state=SIM800C_InitCheck();
printf("SIM800C初始化状态:%d\r\n",state);
DelayMs(1000);
//设置文本模式
state=SIM800C_SetNoteTextMode();
printf("设置文本模式状态:%d\r\n",state);
DelayMs(1000);
//
printf("正在初始化WIFI请稍等.\r\n");
for(i=0;i<5;i++)
{
if(ESP8266_Init()==0)
{
esp8266_state=1;
break;
}
else
{
esp8266_state=0;
printf("ESP8266硬件检测错误.\n");
}
}
if(esp8266_state)
{
printf("准备连接服务器....\r\n");
//非加密端口
printf("WIFI:%d\n",ESP8266_STA_TCP_Client_Mode(CONNECT_WIFI,CONNECT_PASS,CONNECT_SERVER_IP,CONNECT_SERVER_PORT,1));
//2. MQTT协议初始化
MQTT_Init();
//3. 连接服务器
for(i=0;i<5;i++)
{
if(MQTT_Connect(MQTT_ClientID,MQTT_UserName,MQTT_PassWord)==0)
{
esp8266_state=1;
break;
}
esp8266_state=0;
printf("服务器连接失败,正在重试...\n");
delay_ms(500);
}
printf("服务器连接成功.\n");
//3. 订阅主题
if(MQTT_SubscribeTopic(SET_TOPIC,0,1))
{
printf("主题订阅失败.\n");
}
else
{
printf("主题订阅成功.\n");
}
}
while(1)
{
key=KEY_Scan();
if(key)
{
printf("key=%d\r\n",key);
}
//控制LED灯
if(key==2)
{
LED2=!LED2;
}
//手动控制蜂鸣器测试
//手动发送短信测试
if(key==1)
{
BEEP=1;
delay_ms(100);
BEEP=0;
sprintf((char*)sim800c_buff,"Drunk driving reminder:%d",MQ3_data);
//发送短信
if(SIM800C_SendNote((u8*)"13800138000",sim800c_buff,strlen((char*)sim800c_buff))==0)
printf("短信发送成功\r\n");
else
printf("短信发送失败\r\n");
}
//轮询时间到达
if(time_cnt>300)
{
time_cnt=0;
LED1=!LED1;
//获取空气质量
MQ3_data=GetAdcCHxDATA(1);
printf("采集MQ3数据:%d\r\n",MQ3_data);
//上报数据
sprintf(data_buff,"{\"services\": [{\"service_id\":\"STM32\",\"properties\":{\"MQ3\":%d}]}",
MQ3_data);
MQTT_PublishData(POST_TOPIC,data_buff,0);
//上报数据
sprintf(data_buff,"{\"services\": [{\"service_id\":\"STM32\",\"properties\":{\"MQ3\":%d}]}",
MQ3_data);
MQTT_PublishData(POST_TOPIC,data_buff,0);
//LCD屏实时显示酒精浓度
//采集数据
sprintf(data_buff,"%d",MQ3_data);
Gui_DrawFont_GBK16(72,16*2,RED,0,(u8*)" ");
Gui_DrawFont_GBK16(72,16*2,RED,0,(u8*)data_buff);
//酒精含量
//MQ3_data_float=((float)MQ3_data*(5.0/4096))*0.36-1.08;
MQ3_data_float=((float)MQ3_data*(5.0/4096))*0.36;
sprintf(data_buff,"%0.2f mg/L",MQ3_data_float);
printf("计算酒精浓度:%s\r\n",data_buff);
Gui_DrawFont_GBK16(72,16*3,RED,0,(u8*)" ");
Gui_DrawFont_GBK16(72,16*3,RED,0,(u8*)data_buff);
//判断酒精浓度是否超标,设置阀值
if(MQ3_data>2000)
{
sprintf((char*)sim800c_buff," :%d",MQ3_data);
//发送短信
if(SIM800C_SendNote((u8*)"18171571217",sim800c_buff,strlen((char*)sim800c_buff))==0)
printf("短信发送成功\r\n");
else
printf("短信发送失败\r\n");
}
//采集的值越小,表明酒精浓度越高。反之越大。
//根据阀值显示不同的文字提示
if(MQ3_data>2000)
{
//你已酒驾
Gui_DrawFont_GBK16(0,16*4,RED,0,(u8*)" ");
LCD_ShowChineseFont(0,16*4,16,HZ_FONT_16[8],RED,0);
LCD_ShowChineseFont(16,16*4,16,HZ_FONT_16[9],RED,0);
LCD_ShowChineseFont(16*2,16*4,16,HZ_FONT_16[10],RED,0);
LCD_ShowChineseFont(16*3,16*4,16,HZ_FONT_16[11],RED,0);
}
//正常稳定范围采集的值是500左右
else
{
//安全范围
Gui_DrawFont_GBK16(0,16*4,RED,0,(u8*)" ");
LCD_ShowChineseFont(0,16*4,16,HZ_FONT_16[16],RED,0);
LCD_ShowChineseFont(16,16*4,16,HZ_FONT_16[17],RED,0);
LCD_ShowChineseFont(16*2,16*4,16,HZ_FONT_16[18],RED,0);
LCD_ShowChineseFont(16*3,16*4,16,HZ_FONT_16[19],RED,0);
}
}
DelayMs(10);
time_cnt++;
}
}
边栏推荐
- 用fail2ban阻止密码尝试攻
- 光环效应——谁说头上有光的就算英雄
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- 8 expansion sub packages! Recbole launches 2.0!
- C basic supplement
- Backgroundworker usage example
- 数据库公共字段自动填充
- [AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
- Oracle 被 Ventana Research 评为数字创新奖总冠军
- Using nsproxy to forward messages
猜你喜欢
高质量软件架构的唯一核心指标
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
Practice: fabric user certificate revocation operation process
数据库公共字段自动填充
诸神黄昏时代的对比学习
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
Etcd storage, watch and expiration mechanism
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
随机推荐
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article
Zhongang Mining: in order to ensure sufficient supply of fluorite, it is imperative to open source and save flow
光环效应——谁说头上有光的就算英雄
模块化笔记软件综合评测:Craft、Notion、FlowUs
Annual comprehensive analysis of China's mobile reading market in 2022
c#数组补充
C语言课程设计题
C foundation in-depth study I
.NET 使用 redis
Scripy framework learning
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
Introduction to XML I
AI painting minimalist tutorial
室外LED屏幕防水吗?
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
"Tips" to slim down Seurat objects
AI 绘画极简教程
XML入门一