完成接收网关指令并向网关发送数据功能

This commit is contained in:
lxbpxylps@126.com 2021-10-17 15:32:59 +08:00
parent f9e3536619
commit 1cb597f4ad
4 changed files with 104 additions and 30 deletions

@ -1 +1 @@
Subproject commit a7a42360525ea26f45b459f37c06580706f030b6
Subproject commit d57a2a85dfd2d34f91483ad0ad6ede816c7c2e84

View File

@ -10,7 +10,6 @@
#include "stdio.h"
#include "sys.h"
#include "systick.h"
#include "uart.h"

View File

@ -15,9 +15,11 @@
#ifndef __UART_H
#define __UART_H
#include "sys.h"
//注释此处语句可禁用特定串口
#define UART1_FIFO_EN 1
#define UART2_FIFO_EN 1
#define UART2_FIFO_EN 0
//定义端口号
typedef enum
@ -35,8 +37,8 @@ typedef enum
#if UART2_FIFO_EN == 1
#define UART2_BAUD 115200
#define UART2_TX_BUF_SIZE 1 * 64
#define UART2_RX_BUF_SIZE 1 * 64
#define UART2_TX_BUF_SIZE 1 * 512
#define UART2_RX_BUF_SIZE 1 * 512
#endif
//回调函数定义

View File

@ -1,13 +1,14 @@
/**
* @file main.c
* @author Myth
* @version 0.8
* @date 2021.10.15
* @version 1.0
* @date 2021.10.17
* @brief
* @details
* @note
* PC13 LED
*
*
* LoRa
*
* JTAG 使 SWD
*/
@ -17,8 +18,19 @@
#include "led.h"
#include "lora.h"
#include "aht20.h"
#include "bh1750.h"
void Echo(uint8_t byte);
#define NODE_SEQ_NUM '2' //节点标号2 ~ 5
typedef struct
{
uint8_t seq;
float humi;
float temp;
float light;
uint8_t end;
} DataPack; //数据包定义,由于平台相同,网关和节点通讯无需考虑对齐问题
int main(void)
{
@ -31,35 +43,96 @@ int main(void)
SysTick_Init(); //初始化 SysTick 和软件定时器
UART_Init(); //初始化串口
LED_Init(); //初始化 LED
LoRa_Init(); //初始化 LoRa 模块
LED_Init(); //初始化 LED
LoRa_Init(); //初始化 LoRa 模块
AHT20_Init(); //初始化 AHT20
BH1750_Init(); //初始化 BH1750
UART_BindReceiveHandle(COM1, Echo); //绑定 COM1 串口接收中断至 Echo 函数
DataPack pack; //发送的数据包
pack.seq = NODE_SEQ_NUM; //数据包序号,标明发送节点
pack.end = '@'; //数据包结束字节,用于网关校验
uint8_t buffer[255];
uint8_t size;
uint8_t buffer[3]; //接收缓冲区
uint8_t size; //接收到的包大小
int32_t time; //当前时间,控制时序
//首先获取一次数据,防止发送空数据
AHT20_Start(); // AHT20 开始测量
BH1750_Start(); // BH1750 开始测量
Delay_ms(80);
AHT20_Read(&(pack.humi), &(pack.temp));
Delay_ms(100);
pack.light = BH1750_Read();
while (1)
{
//程序主循环
size = LoRa_Receive(buffer); //接收
if (size == 0)
continue;
AHT20_Start(); // AHT20 开始测量
BH1750_Start(); // BH1750 开始测量
printf("Receive %d bytes: %s\n", size, buffer);
//在等待测量的过程中检测是否有命令包索取数据
//说明:
//下面一段代码显然应该封装为函数。但在封装为函数后出现恶性 Bug猜测为溢出导致正在排除
//即使不封装为函数,将 buffer size 声明为全局变量也会导致此 Bug表现为接收后死机
time = SysTick_GetRunTime();
while (SysTick_CheckRunTime(time) < 80) //等待 80 ms
{
size = LoRa_Receive(buffer); //接收
if (size < 1)
{
continue;
}
else if (size != 3 || buffer[0] != '#' || buffer[1] != NODE_SEQ_NUM || buffer[2] != '@') //判断命令包是否正确,是否向自己查询
{
printf("Get Wrong Command[%d]: %c%c%c\n", size, buffer[0], buffer[1], buffer[2]);
continue;
}
LED1_Toggle;
//命令包正确
LED1_Toggle; // LED 翻转
printf("Get Right Command[%d]: %c%c%c\n", size, buffer[0], buffer[1], buffer[2]);
//返回最近一个获得的数据。由于无 DIO0 的 LoRa 通讯不可靠,发送 3 次
for (uint8_t i = 0; i < 3; i++)
{
printf("Send!\n");
LoRa_Send(&pack, sizeof(DataPack));
Delay_ms(300);
}
}
AHT20_Read(&(pack.humi), &(pack.temp)); // 80ms 后可读取 AHT20
time = SysTick_GetRunTime();
while (SysTick_CheckRunTime(time) < 100) //等待 100 ms
{
size = LoRa_Receive(buffer); //接收
if (size < 1)
{
continue;
}
else if (size != 3 || buffer[0] != '#' || buffer[1] != NODE_SEQ_NUM || buffer[2] != '@') //判断命令包是否正确,是否向自己查询
{
printf("Get Wrong Command[%d]: %c%c%c\n", size, buffer[0], buffer[1], buffer[2]);
continue;
}
//命令包正确
LED1_Toggle; // LED 翻转
printf("Get Right Command[%d]: %c%c%c\n", size, buffer[0], buffer[1], buffer[2]);
//返回最近一个获得的数据。由于无 DIO0 的 LoRa 通讯不可靠,发送 3 次
for (uint8_t i = 0; i < 3; i++)
{
printf("Send!\n");
LoRa_Send(&pack, sizeof(DataPack));
Delay_ms(300);
}
}
pack.light = BH1750_Read(); // 180ms 后才能读取 BH1750
}
return 1;
}
/**
* @brief
* @param byte:
*/
void Echo(uint8_t byte)
{
LED1_Slow_Toggle;
UART_SendChar(COM1, byte);
}