#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++;
}
}
评论