smart-green-house/rtt-uart-nb/applications/main.c

243 lines
7.8 KiB
C
Raw Permalink Normal View History

2023-10-28 18:00:47 +08:00
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-10-08 RT-Thread first version
*/
#include <rtthread.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include <rtdevice.h>
#include "string.h"
#include "stdlib.h"
#define LED0_PIN 21
#define LED1_PIN 69
#define BEEP_PIN 80
rt_device_t uart2;
rt_device_t uart3; // 设备
rt_sem_t uart3_recv_sem; // 接收数据的中间信号
rt_sem_t uart2_recv_sem;
static char *cmd=NULL;
static int idle = 0;
static rt_uint8_t buf[128];
static rt_uint8_t buf_len = 128;
static rt_uint8_t buf2[128];
static rt_uint8_t buf2_len = 128;
void toHex(char *dst, char *src)
{
while (*src)
{
char c = *src;
char high = (c >> 4) & 0x0F, low = c & 0x0F;
high = high < 10 ? high + '0' : high + 'A' - 10;
low = low < 10 ? low + '0' : low + 'A' - 10;
char buf[2] = "";
rt_sprintf(buf, "%c%c", high, low);
strncat(dst, buf, 2);
src++;
}
}
void unHex(char *dst, char *hexstr, int hex_len)
{
unsigned short *p = (unsigned short *)hexstr;
int i = 0;
while (i < hex_len)
{
unsigned char *q = (unsigned char *)p;
unsigned char h_char = *q;
unsigned char l_char = *(q + 1);
h_char = h_char < 'A' ? h_char - 48 : h_char - 65 + 10;
l_char = l_char < 'A' ? l_char - 48 : l_char - 65 + 10;
unsigned char c = (h_char << 4) + l_char;
char tmp_buf[2] = "";
rt_sprintf(tmp_buf, "%c", c);
strncat(dst, tmp_buf, 1);
i += 2;
p++;
}
}
void uart3_init(){
// 1. 查找uart3设备
uart3 = rt_device_find("uart3");
//2. 控制uart3设备波特率、数据位、停止位、校验位等
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate=BAUD_RATE_9600;
rt_device_control(uart3, RT_DEVICE_CTRL_CONFIG, &config);
// 3. 打开uart3设备 【DMA接收方式打开的】
rt_device_open(uart3, RT_DEVICE_OFLAG_RDWR|RT_DEVICE_FLAG_DMA_RX);
}
void uart2_init(){
uart2 = rt_device_find("uart2");
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate=BAUD_RATE_115200;
rt_device_control(uart2, RT_DEVICE_CTRL_CONFIG, &config);
rt_device_open(uart2, RT_DEVICE_OFLAG_RDWR|RT_DEVICE_FLAG_DMA_RX);
}
static rt_thread_t beep_th;
void beep_on_entry(void *arg){
rt_kprintf("beep starting\n");
while(1){
rt_pin_write(BEEP_PIN, PIN_LOW);
rt_thread_delay(1);
rt_pin_write(BEEP_PIN, PIN_HIGH);
rt_thread_delay(1);
}
}
// 接收uart3设备的数据回调函数
// 重要说明:
// 1 size 在中断接收时,只是 1个 字节
// 2) size 在DMA方式接收时表示DMA内存中存放的字节个数, 默认最大的字节数为64(RT-Thread Settings配置的)
rt_err_t uart3_rx_callback(rt_device_t dev, rt_size_t size){
rt_kprintf("dma rx uart3 data size: %d\n", size);
buf_len = size;
rt_sem_release(uart3_recv_sem);
return RT_EOK;
}
rt_err_t uart2_rx_callback(rt_device_t dev, rt_size_t size){
rt_kprintf("dma rx uart2 data size: %d\n", size);
buf2_len = size;
rt_sem_release(uart2_recv_sem);
return RT_EOK;
}
rt_thread_t uart3_read_th1;
rt_thread_t uart2_read_th2;
void uart2_read_task(void *arg)
{
//char pub_buf[128]="";
while(idle){
while(rt_device_read(uart2, 0, buf2, buf2_len) != buf_len){
rt_sem_take(uart2_recv_sem, RT_WAITING_FOREVER);
}
cmd = "AT+MQCON=0,4,\"client2\",3600,1,0\r\n";
rt_device_write(uart3, 0, cmd, strlen(cmd)+1);
rt_thread_mdelay(1000);
cmd = "AT+MQPUB=0,\"dht\",2,0,0,3,313233\r\n";
rt_device_write(uart3, 0, cmd, strlen(cmd)+1);
// toHex((char *)pub_buf,(char *) buf2);
// char length=strlen(pub_buf)/2;
// char *ptr = &length;
// cmd = "AT+MQPUB=0,\"temp\",2,1,0";
// rt_device_write(uart3, 0, cmd, strlen(cmd));
// rt_device_write(uart3, 0,ptr, 1);
// rt_device_write(uart3, 0, pub_buf, strlen(pub_buf));
// rt_device_write(uart3, 0,"\r\n", 1);
}
memset(buf2, 0, sizeof(buf2));
}
void uart3_read_task(void *arg){
while(1){
while(rt_device_read(uart3, 0, buf, buf_len) != buf_len){
rt_sem_take(uart3_recv_sem, RT_WAITING_FOREVER);
}
rt_kprintf("data: %s\n", buf);
char *m=NULL;
if((m=rt_strstr((char *)buf, "+MQPUB:")) != NULL){
int8_t mq_cmd_len = 0;
char *q=strtok((char *)(m+21), ",");
mq_cmd_len = atoi(q);
q = strtok(NULL, ",");
char cmd_buf[64]="";
unHex(cmd_buf, q, mq_cmd_len*2);
cmd_buf[strlen((cmd_buf)-1)] = 0;
rt_kprintf("%s\n", cmd_buf);
// 接收指令
if(rt_strncmp("beep:", (char *)cmd_buf, 5) == 0){
if(rt_strstr((char *)cmd_buf, "on") != RT_NULL){
beep_th = rt_thread_create("beep_thread", beep_on_entry,
RT_NULL, 512, 10, 5);
if(beep_th != RT_NULL){
rt_thread_startup(beep_th);
}
}else if(beep_th != RT_NULL)
rt_thread_delete(beep_th);
}else if(rt_strncmp("led0:", (char *)cmd_buf, 4) == 0){
if(rt_strstr((char *)cmd_buf, "on") != RT_NULL)
rt_pin_write(LED1_PIN, PIN_LOW);
else
rt_pin_write(LED1_PIN, PIN_HIGH);
}
else if(rt_strncmp("get_data",(char*)cmd_buf,8)==0){
idle=1;
//rt_device_write(uart2, 0,cmd_buf, strlen(cmd_buf));
if(rt_device_set_rx_indicate(uart2,uart2_rx_callback)!=RT_EOK){
rt_kprintf("rt_device_set_rx_indicate fail!");
}
}
}
}
//rt_device_write(uart3, 0, "OK\r", sizeof("OK\r"));
// 解析buf中的消息
memset(buf, 0, sizeof(buf));
}
int main(void)
{
// 创建信号量
uart3_recv_sem = rt_sem_create("sem1", 0, RT_IPC_FLAG_PRIO);
uart2_recv_sem = rt_sem_create("sem2", 0, RT_IPC_FLAG_PRIO);
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); // 使能led0的GPIO
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT); // 使能led1的GPIO
rt_pin_mode(BEEP_PIN, PIN_MODE_OUTPUT); // 使能Beep的GPIO
rt_pin_write(LED0_PIN, PIN_HIGH);
rt_pin_write(LED1_PIN, PIN_HIGH);
uart3_init();
// 4. 发送数据
cmd = "AT+MQNEW=47.109.34.218,1883,60000,128\r\n";
rt_device_write(uart3, 0, cmd, strlen(cmd)+1);
rt_thread_mdelay(1000);
cmd = "AT+MQCON=0,4,\"client1\",3600,1,0\r\n";
rt_device_write(uart3, 0, cmd, strlen(cmd)+1);
rt_thread_mdelay(1000);
cmd = "AT+MQSUB=0,\"stm32\",2\r\n";
rt_device_write(uart3, 0, cmd, strlen(cmd)+1);
// 5. 设置设备的接收回调
if(rt_device_set_rx_indicate(uart3, uart3_rx_callback) != RT_EOK){
rt_kprintf("rt_device_set_rx_indicate fail!");
}
uart3_read_th1 = rt_thread_create("th1", uart3_read_task, RT_NULL, 512, 10,
5);
uart2_read_th2=rt_thread_create("th2",uart2_read_task, RT_NULL, 512, 10, 5);
if(uart3_read_th1 != RT_NULL){
rt_kprintf("th1 create ok!\n");
rt_thread_startup(uart3_read_th1);
}
if(uart2_read_th2 != RT_NULL){
rt_kprintf("th2 create ok!\n");
rt_thread_startup(uart2_read_th2);
}
while(1);
return RT_EOK;
}