UART.h文件
/************************************************************************UART.h****************************/
#ifndef _USART2_H_
#define _USART2_H_
#include "stm32f10x.h"
#include "stdio.h"
extern u8 flag;
typedef struct
{
unsigned char RxBuff[256];
unsigned char TxBuff[256];
unsigned short FlagLen;
unsigned char RecFlag;
}USART2_InitStructure;
extern USART2_InitStructure Usart2;
void Usart2_Init(u32 bound);
void SendUsart2Data(u8 *data);
void SendUsart2Package(u8 *data,u16 len);
void Usart2_Send_byte(u8 data);
void ffff(void);
#endif
/**********************************************************UART.C*****************************************/
#include "USART2.h"
#include "stdio.h"
#include "esp8266.h"
#include "stdbool.h"
#include "stdio.h"
#include "USART1.h"
#include "string.h"
#include "motor.h"
#include "paj7620u2.h"
#include "oled.h"
#include "exti.h"
#include "led.h"
#include "rtc.h"
#include "gettime.h"
#define WIFI_USART USART2
USART2_InitStructure Usart2;
/***********************************
函数名称:void USAR_Init(u32 bound)
函数功能:串口1的初始化
入口参数:u32 bound (自己设定 一般为115200 9600)
出口参数:无
返回值: 无
说明:四要数(开启串口时钟 gpio口时钟 波特率寄存器 控制寄存器以及设置GPIO口相应的模式)
************************************/
void Usart2_Init(u32 bound)
{
USART_InitTypeDef USART_InitStruct; //定义串口类型结构体变量
GPIO_InitTypeDef GPIO_InitStruct; //定义GPIO类型结构体变量
NVIC_InitTypeDef NVIC_InitStruct; //定义中断向量控制器的变量
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 开启串口1时钟 开启A口时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // 开启串口1时钟 开启A口时钟
//PA3 发送引脚 RX
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3; //引脚9设置
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStruct); //初始化GPIOA
// PA2 接收引脚 TX
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; //引脚9设置
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //输出速率50M
GPIO_Init(GPIOA, &GPIO_InitStruct); //初始化GPIOA
USART_InitStruct.USART_BaudRate = bound;//设置波特率
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制失能
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //全双工模式(发送使能 接收使能)
USART_InitStruct.USART_Parity = USART_Parity_No; //奇偶失能(无校验)
USART_InitStruct.USART_StopBits = USART_StopBits_1;//停止位一个
USART_InitStruct.USART_WordLength = USART_WordLength_8b; //8位数据
USART_Init(USART2, &USART_InitStruct); //初始化串口1
USART_Cmd(USART2, ENABLE); // 串口1使能
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //使能串口接收中断
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); //空闲中断 使能
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn; //通道选择(USART1 全局中断)
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; //设置抢占优先级(范围0~3)
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; //设置响应优先级(范围0~3)
NVIC_Init(&NVIC_InitStruct); //初始化
}
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE)) //如果是接收中断
{
if (Usart2.FlagLen >= 256)
Usart2.FlagLen = 0;
Usart2.RxBuff[Usart2.FlagLen++] = USART_ReceiveData(USART2); //把收到的数据存起来
}
else if (USART_GetITStatus(USART2, USART_IT_IDLE)) //如果是空闲中断
{
Usart2.RxBuff[Usart2.FlagLen] = '\0'; //添加空字符
Usart2.FlagLen |= 0x8000; //最高位置1
Usart2.RecFlag = true;
USART_ReceiveData(USART2); //读取DR,相当于清空SR的RXNE和IDLE位
}
}
void SendUsart2Data(u8 *data) //发送数据,末尾才有‘\0’
{
while (*data != '\0')
{
while (!USART_GetFlagStatus(WIFI_USART, USART_FLAG_TC));
USART_SendData(WIFI_USART, *data++);
}
}
void SendUsart2Package(u8 *data, u16 len) //发送数据包,中间包含‘\0’,所以需要依赖长度进行发送
{
while (len--)
{
while (!USART_GetFlagStatus(WIFI_USART, USART_FLAG_TC));
USART_SendData(WIFI_USART, *data++);
}
}
void Usart2_Send_byte(u8 data)
{
while (!USART_GetFlagStatus(USART2, USART_FLAG_TXE));
USART_SendData(USART2, data);
}
/************************************************esp8266.c*************************************************/
#include "esp8266.h"
#include <string.h>
#include <stdbool.h>
#include "delay.h"
//#include "systick.h"
#include "USART1.h"
#include "hmacsha1.h"
#include "led.h"
#include "USART2.h"
#include <stdlib.h>
#include "DHT11.h"
//#include "rgb.h"
//#include "lcd.h"
#include "paj7620u2.h"
#include "oled.h"
static unsigned char ConnectFlag = false;//true:已连接,false:未连接
static unsigned char mqtt_message[256]; //打包数据缓冲区
static unsigned char ssid[MESSAGE_MAX/2] = {SSID}, pwsd[MESSAGE_MAX/2] = {PWSD};
static void CalculateSha1(unsigned char *password);
static void mstrcat(char *s1, const char *s2);
static unsigned short CRC16_Compute(unsigned char *buffer, unsigned short len);
/*********************************************************************************************************
* 函 数 名 : Esp12_PublishMessage
* 功能说明 : 上报数据
* 形 参 : StructMessage:自行强转指针类型即可上报任意类型的数据
* 返 回 值 : 无
* 备 注 : MQTT固件内部自带15S心跳,所以不需要间隔上报数据也可以保持长连接
*********************************************************************************************************/
void Esp12_PublishMessage(void *StructMessage)
{ /*一次上报的数据包不能大于缓冲区的大小,字段多时应分段上报,否则......*/
memset(mqtt_message, 0, sizeof(mqtt_message));
sprintf((char *)mqtt_message,
"AT+MQTTPUB=0,\"%s\",\"{\\\"method\\\":\\\"thing.service.property.set\\\"\\,\\\"id\\\":\\\"2012934115\\\"\\,\
\\\"params\\\":{\\\"temperature\\\":%0.2f}\\,\\\"version\\\":\\\"1.0.0\\\"}\",1,0\r\n",
PublishMessageTopPost,
25.5
);
SendUsart2Data(mqtt_message);
}
/*********************************************************************************************************
* 函 数 名 : Esp12_Connect //标记 主要是这个函数 发送流程
* 功能说明 : esp12连接云服务器
* 形 参 : 无
* 返 回 值 : 无
* 备 注 : 10ms的刷新间隔
*********************************************************************************************************/
void Esp12_Connect(void)
{
unsigned char PassWord[50] = {0};
unsigned char cnt = 5;
Restoration:
printf("\r\nS