<h1><span style="color:#f33b45;">一、环境介绍</span></h1>
<p><span style="color:#f33b45;"><strong>MCU:</strong> </span><strong> </strong>STM32F103C8T6</p>
<p><span style="color:#f33b45;"><strong>GSM模块:</strong></span> SIM800C</p>
<p><strong><span style="color:#f33b45;">开发软件:</span> </strong>Keil5</p>
<p>MQTT协议采用OneNet的旧版协议,登录OneNet控制台创建应用时要选择旧版本。</p>
<p>如果想使用新版本的标准MQTT协议连接OnetNet请参考这里: <a href="https://blog.csdn.net/xiaolong1126626497/article/details/107385118">https://blog.csdn.net/xiaolong1126626497/article/details/107385118</a></p>
<p><span style="color:#f33b45;"><strong>完整源代码下载: </strong></span> <a href="https://download.csdn.net/download/xiaolong1126626497/18245757">https://download.csdn.net/download/xiaolong1126626497/18245757</a></p>
<h1><span style="color:#f33b45;">二、硬件与需求</span></h1>
<p><span style="color:#3399ea;"><strong>一块STM32F103C8T6最小系统板。</strong></span></p>
<p> <img alt="" height="370" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-bc33ae82949dfb0d4c94746d1c46ad7c.jpeg" width="208"><img alt="" height="220" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-55b95ce6a0614cfb905cda4d7bf1354b.jpeg" width="388"></p>
<p style="margin-left:0cm;"><span style="color:#3399ea;"><strong>一块OLED显示屏</strong></span></p>
<p style="text-align:center;"><img alt="" height="195" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-009e6b190c8e149670b8fa80c8c8132e.jpeg" width="347"></p>
<p><span style="color:#3399ea;"><strong>一个DHT11温湿度传感器 </strong></span></p>
<p style="text-align:center;"><img alt="" height="230" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-deec05f5e8514d4c36da97dfa813e865.jpeg" width="409"></p>
<p><span style="color:#3399ea;"><strong>一个MQ-2可燃气体传感器 </strong></span></p>
<p style="text-align:center;"><img alt="" height="227" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-fe4d0904fc17910dbd67dd60ccf3d7ed.jpeg" width="404"></p>
<p><span style="color:#3399ea;"><strong>一个SIM800C模块 </strong></span></p>
<p style="text-align:center;"><img alt="" height="243" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-a5c54a04832ef5e10c3c75fb848f4d2a.png" width="433"></p>
<h3 style="margin-left:0cm;"><span style="color:#f33b45;"><strong> 软件要求</strong></span></h3>
<p style="margin-left:0cm;">采集DHT11温度、湿度数据、采集MQ-2烟雾传感器数据实时在OLED显示屏上显示、当烟雾浓度超过阀值时,可以通过SIM800C向指定手机号码发送短信。</p>
<p style="margin-left:0cm;">并需要使用SIM800C连接GPRS网络,将温度、湿度、烟雾浓度上传到OneNet服务器进行可视化显示。</p>
<p style="margin-left:0cm;"> </p>
<h1 style="margin-left:0cm;"><span style="color:#f33b45;">三、核心代码</span></h1>
<h2 style="margin-left:0cm;"><span style="color:#3399ea;">3.1 main.c</span></h2>
<pre class="blockcode"><code class="language-cpp">//
// 功能描述 : 智能环境检测系统
// 时间 : 20190605
// 版本 : v3.3
// 版权所有,盗版必究。
//Copyright(C) DS小龙哥 2016 - 2020
///
#include "stm32f10x.h"
#include "delay.h"
#include "usart.h"
#include <stdio.h>
#include "dht11.h"
#include "oled.h"
#include "adc.h"
#include "timer.h"
#include "sim800c.h"
#include "gps.h"
/*
DTH11接线说明:
VCC---3.3V
GND---GND
DAT---PA5
MQ-2接线说明:
VCC---3.3V
GND---GND
AO----PB0
使用IIC接口的OLED显示屏接线说明:
GND---GND
VCC---3.3V
SDA---PB7
SCL---PB6
使用SPI接口的OLED显示屏接线说明:
GND---GND
VCC---3.3V
D0-----PB5
D1-----PB6
RES----PB7
DC-----PB8
CS-----PB9
SIM800C接线说明:
GND----GND
PA2----SIM800C_RXD
PA3----SIM800C_TXD
CH340模块接线说明:
GND----GND
RX-----PA9
GPS接线说明: (波特率需要根据GPS模块实际情况进行修改)
GND----GND
VCC---3.3V
PB11----GPS_TX
*/
u8 dht11_temp,dht11_humi;
u32 time_cnt=0;
u32 OneNet_Sendtime=0;
u8 data_select=0; //发送的数据选择
u8 DisplayState=0;
char DisplayDataBuffer[20];
u16 MQ_2; //存放ADC的数据
void DisplayPage1(void); //第一页
void DisplayPage2(void); //第二页
//烟雾超标提示
u8 sim800c_buff[]="MQ-2 smoke exceeded";
char tmp_buffer[100];
double Longitude=103.718463; //经度
double latitude=36.107013; //纬度
int main(void)
{
u8 state;
USART_X_Init(USART1,72,115200); //串口初始化
printf("串口初始化完成!\r\n");
OLED_Init(); //OLED显示屏初始化
ADC1_Init();
if(DHT11_Init())printf("DHT11检测错误!\r\n");
TIM2_Init(72,20000);//辅助串口3接收,超时时间为20ms
USART_X_Init(USART2,36,9600); //可能的波特率(测试): 57600 、9600、115200
USART_X_Init(USART3,36,9600);//接GPS模块
TIM3_Init(72,20000);//辅助串口3接收,超时时间为20ms
//显示初始化信息
OLED_Clear_GRAM();
OLED_DisplayString(0,0,16,"System Init..");
OLED_Refresh_GRAM(); //刷新显存
/ |
|