前言
大约前后两个月时间做了一个“智慧公交”小项目,可实现将车内的温湿度、人体温度、车辆地理位置信息、车内人数灯信息通过4G模块上传至阿里云物联网平台,同时后端从平台抓包存放在数据库,并且在前端展示。本系统是裸机开发。作者只是负责硬件端实现,所以我只写硬件设计和入网。
这个项目可以说是我学习STM32的第一个较为具体的项目,本人技术有限,缺漏较多,还请各位大佬多多指正。
开源链接在最后。
设计流程
主要代码
由于基于裸机开发,代码繁琐,逻辑不是很清晰,在此只展示核心代码。
主函数
/*********************************************************************************
* 描述 :将地理位置信息,温湿度等信息通过MQTT协议上发到阿里云 ;
(可以接受Qos等级为 0 的MQTT消息,无相关功能)
* 版本:V3.4
* 实验平台:STM32F103ZE(开发板)/STM32F103C8T6(核心板)
*
* 作者 :ORI
* 联系方式:Q1327408725 邮箱ori_zh@outlook.com
* 时间 :2020-11
**********************************************************************************/
#include "bsp_led.h"
#include "bsp_usart1.h"
#include "string.h"
#include "bsp_usart2.h"
#include "bsp_ec20.h"
#include "bsp_systick.h"
#include "bsp_dma.h"
#include "bsp_tim.h"
#include <stdio.h>
#include <stdlib.h>
#include "./gps/gps_config.h"
#include "nema_decode_test.h"
#include "./dht11/bsp_dht11.h"
#include "bsp_exti.h"
#include "stm32f10x_it.h"
#include "mlx90614.h"
#include "OLED_I2C.h"
/*阿里云设备证书信息
*/
/******************************************************************************/
#if 1
#define PRODUCTKEY xxxxxxxxxxx
#define DEVICENAME xxxxxxxxxxx
#define DEVICESECRET xxxxxxxxxxx
#else
#define PRODUCTKEY xxxxxxxxxxx
#define DEVICENAME xxxxxxxxxxx
#define DEVICESECRET xxxxxxxxxxx
#endif
/******************************************************************************/
int errcont = 0;
int main(void) {
uint8_t res=1;
errcont = 0;
unsigned char i;
unsigned char m;
/*** 初始化配置 ***/
SysTick_Init(); // 初始化延迟函数
USART1_Config(); // 初始化USART1,与上位机相连接,调试用
USART2_Config(); // 初始化USART2,与EC20通信
GPS_Config(); // 初始化USART3,与ZXIAT模块相连接,初始化定位模块相关配置
DHT11_Init (); // 初始化DHT11
EXTI_Key_Config(); // 初始化红外对射
LED_GPIO_Config(); // 初始化LED灯
SMBus_Init(); // 初始化MXL90614
I2C_Configuration(); // 配置CPU的硬件I2C
OLED_Init(); // 初始化OLED
printf("\r\n ############ Demo ############\r\n ");
printf("\r\n 实验前请连接好各模块 \r\n ");
while(1)
{
OLED_Fill(0xFF);//全屏点亮
Delay_ms(1000); // 2s
OLED_Fill(0x00);//全屏灭
Delay_ms(1000); // 2s
for(i=0;i<4;i++)
{
OLED_ShowCN(30+i*16,0,i); //测试显示中文
}
OLED_ShowStr(0,2,(unsigned char*)"--------------------",1);
for(m=0;m<8;m++)
{
OLED_ShowCN2(0+m*16,3,m); //测试显示中文
}
while(res)
{
res=EC20_INIT(); //等待EC20初始化完成
Delay_ms(2000);
errcont++;
printf("正在尝试连接: %d次\r\n",errcont);
if(errcont > 50)
{
printf("错误:连接超时,进行复位处理\r\n");
NVIC_SystemReset(); //超时重启,调用复位函数,进行强制复位
}
}
errcont = 0;
res=1;
while(1)
{
while(res)
{
res=EC20_CONNECT_SERVER_CFG_INFOR((u8 *)PRODUCTKEY,(u8 *)DEVICENAME,(u8 *)DEVICESECRET); //接入阿里云
errcont++;
printf("正在尝试连接: %d次\r\n",errcont);
if(errcont > 50)
{
NVIC_SystemReset(); //超时重启
}
}
printf("已连接阿里云接服务器,准备消息发送\r\n");
while(1)
{
nmea_decode_test(); //进入消息发送状态
}
}
}
}
/*********************************************************************************
* ZXIAT初始化函数及数据发送接受函数
**********************************************************************************/
#include "stm32f10x.h"
#include "bsp_usart1.h"
#include "./gps/gps_config.h"
#include "nmea/nmea.h"
#include "nema_decode_test.h"
#include "bsp_ec20.h"
#include "bsp_led.h"
#include "mlx90614.h"
#include "OLED_I2C.h"
#define GPS_PRINTF 1 //是否串口打印定位信息
double deg_lat;//纬度
double deg_lon;//经度
double deg_elv;//转换后的高度
double deg_speed;
float body_temperature;
unsigned char m;<