当前位置:网站首页>基于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++;
}
}
边栏推荐
- Use fail2ban to prevent password attempts
- 请问大佬们有遇到这个情况吗,cdc 1.4 连接MySQL 5.7 无法使用 timestamp
- 三星量产3纳米产品引台媒关注:能否短期提高投入产出率是与台积电竞争关键
- Etcd storage, watch and expiration mechanism
- n++也不靠谱
- WPF双滑块控件以及强制捕获鼠标事件焦点
- ViewBinding和DataBinding的理解和区别
- The old-fashioned synchronized lock optimization will make it clear to you at once!
- 【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
- 7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
猜你喜欢
字节面试算法题
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Runc hang causes the kubernetes node notready
实战:fabric 用户证书吊销操作流程
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
Reptile exercises (I)
Solution: how to delete the information of Jack in two tables with delete in one statement in Oracle
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article
N++ is not reliable
随机推荐
remount of the / superblock failed: Permission denied
mysql三级分销代理关系存储
CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud
Meituan Ali's Application Practice on multimodal recall
The only core indicator of high-quality software architecture
使用宝塔部署halo博客
Excuse me, have you encountered this situation? CDC 1.4 cannot use timestamp when connecting to MySQL 5.7
XML入门三
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
C language dormitory management query software
CA: efficient coordinate attention mechanism for mobile terminals | CVPR 2021
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
FS4056 800mA充电ic 国产快充电源ic
Simple understanding of binary search
CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer
字节面试算法题
WPF double slider control and forced capture of mouse event focus
Don't turn down, three sentences to clarify the origin of cross domain resource request errors
Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)
Rsyslog配置及使用教程