diff --git a/.vscode/settings.json b/.vscode/settings.json index 268cce5..02c6a99 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -33,7 +33,8 @@ "at.h": "c", "ADC.C": "cpp", "PM2.5.C": "cpp", - "sensor.h": "c" + "sensor.h": "c", + "status.h": "c" }, // "cortex-debug.openocdPath": "d:/Develop/" } \ No newline at end of file diff --git a/README.md b/README.md index bfc0859..8228805 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ ADC传感器 PA1 pm25 PA5 空气质量 PA6 压力 +风扇 +PA8 +PB2 # 红外遥控贪吃蛇/显示+上传温度等数据 diff --git a/applications/assistant.c b/applications/assistant.c index 0eec5c7..73bf89f 100644 --- a/applications/assistant.c +++ b/applications/assistant.c @@ -4,12 +4,13 @@ * 命令调用格式:uart_dma_sample uart1 * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备 * 程序功能:通过串口输出字符串 "hello RT-Thread!",并通过串口输出接收到的数据,然后打印接收到的数据。 -*/ + */ #include #include +#include "status.h" -#define SAMPLE_UART_NAME "uart3" /* 串口设备名称 */ +#define SAMPLE_UART_NAME "uart3" /* 串口设备名称 */ #define LOG_TAG "assistant" #define DBG_LVL DBG_LOG @@ -51,13 +52,35 @@ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) } return result; } - +void assist_control(char *rx_buffer) +{ + if (rt_strstr(rx_buffer, "cc")!=RT_NULL) + { + fan_on(); + rt_kprintf("加大抽风\n"); + } + else if (rt_strstr(rx_buffer, "dd")!=RT_NULL) + { + fan_off(); + rt_kprintf("关闭抽风\n"); + } + else if (rt_strstr(rx_buffer, "ee")!=RT_NULL) + { + light_on(); + rt_kprintf("开灯\n"); + } + else if (rt_strstr(rx_buffer, "ff")!=RT_NULL) + { + light_off(); + rt_kprintf("关灯\n"); + } +} static void serial_thread_entry(void *parameter) { struct rx_msg msg; rt_err_t result; rt_uint32_t rx_length; - static char rx_buffer[BSP_UART3_RX_BUFSIZE+ 1]; + static char rx_buffer[BSP_UART3_RX_BUFSIZE + 1]; while (1) { @@ -69,32 +92,15 @@ static void serial_thread_entry(void *parameter) /* 从串口读取数据 */ rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size); rx_buffer[rx_length] = '\0'; - if(!rt_strcasecmp(rx_buffer, "cc")) - { - rt_kprintf("加大抽风\n"); - } - else if(!rt_strcasecmp(rx_buffer, "dd")) - { - rt_kprintf("关闭抽风\n"); - - } - else if(!rt_strcasecmp(rx_buffer, "ee")) - { - rt_kprintf("开灯\n"); - } - else if(!rt_strcasecmp(rx_buffer, "ff")) - { - rt_kprintf("关灯\n"); - } + assist_control(rx_buffer); /* 通过串口设备 serial 输出读取到的消息 */ // rt_device_write(serial, 0, rx_buffer, rx_length); /* 打印数据 */ - rt_kprintf("%s\n",rx_buffer); + rt_kprintf("%s\n", rx_buffer); } } } - void serial_send(char *str) { rt_device_write(serial, 0, str, (sizeof(str) - 1)); @@ -120,10 +126,10 @@ int aiv_init(void) /* 初始化消息队列 */ rt_mq_init(&rx_mq, "rx_mq", - msg_pool, /* 存放消息的缓冲区 */ - sizeof(struct rx_msg), /* 一条消息的最大长度 */ - sizeof(msg_pool), /* 存放消息的缓冲区大小 */ - RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ + msg_pool, /* 存放消息的缓冲区 */ + sizeof(struct rx_msg), /* 一条消息的最大长度 */ + sizeof(msg_pool), /* 存放消息的缓冲区大小 */ + RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ /* 以 DMA 接收及轮询发送方式打开串口设备 */ rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING); @@ -147,4 +153,4 @@ int aiv_init(void) return ret; } /* 导出到 msh 命令列表中 */ -MSH_CMD_EXPORT(aiv_init,siri start); +MSH_CMD_EXPORT(aiv_init, siri start); diff --git a/applications/assistant.h b/applications/assistant.h index 5122f05..cebda82 100644 --- a/applications/assistant.h +++ b/applications/assistant.h @@ -1 +1,2 @@ -void serial_send(char *str); \ No newline at end of file +void serial_send(char *str); +void assist_control(char *rx_buffer); \ No newline at end of file diff --git a/applications/motor.c b/applications/motor.c index ee8fef4..8b88396 100644 --- a/applications/motor.c +++ b/applications/motor.c @@ -24,8 +24,13 @@ struct rt_device_pwm *motor_dev; // pwm设备句柄 -rt_uint32_t period = 1000000000; // 单位us 向左6位,变毫秒 1s -rt_uint32_t pulse = 1000000; +rt_uint32_t period = 100000000; // 单位us 向左6位,变毫秒 10s +// rt_uint32_t pulse = 1000000; + + + +#define PIN_MOTOR2 GET_PIN(B, 2) +int motor1=PIN_HIGH; /** @@ -36,20 +41,16 @@ rt_uint32_t pulse = 1000000; */ void motor_speed(int num) { - rt_pwm_set(motor_dev, MOTOR_PWM_DEV_CHANNEL, period, 100-(num * period / 100)); + rt_pwm_set(motor_dev, MOTOR_PWM_DEV_CHANNEL, period, (100-num)/ 100 * period ); + rt_pin_write(PIN_MOTOR2, num>0?PIN_LOW:PIN_HIGH); } -#define PIN_MOTOR1 GET_PIN(B, 2) -int motor1=PIN_HIGH; - - - int mot_init(void) { - // rt_pin_mode(PIN_MOTOR1, PIN_MODE_OUTPUT); - // rt_pin_write(PIN_MOTOR1, PIN_HIGH); + rt_pin_mode(PIN_MOTOR2, PIN_MODE_OUTPUT); + rt_pin_write(PIN_MOTOR2, PIN_HIGH); motor_dev = (struct rt_device_pwm *)rt_device_find(MOTOR_PWM_DEV_NAME); if (motor_dev == RT_NULL) { @@ -57,7 +58,8 @@ int mot_init(void) return RT_ERROR; } /* 设置PWM周期和脉冲宽度默认值 */ - motor_speed(100); + motor_speed(0); + rt_pwm_enable(motor_dev, MOTOR_PWM_DEV_CHANNEL); LOG1("motor init success!"); } void motor_set(int argc, char **argv) diff --git a/applications/myproject.c b/applications/myproject.c index dd9ee61..592ded4 100644 --- a/applications/myproject.c +++ b/applications/myproject.c @@ -17,6 +17,7 @@ #include "my_func.h" #include "indicator_led.h" #include "sensor.h" +#include "assistant.h" #define THREAD_PRIORITY 25 #define THREAD_STACK_SIZE 4096 #define THREAD_TIMESLICE 5 @@ -119,6 +120,12 @@ static void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_even EXAMPLE_TRACE("Topic : %.*s", topic_info->topic_len, topic_info->ptopic); EXAMPLE_TRACE("Payload: %.*s", topic_info->payload_len, topic_info->payload); EXAMPLE_TRACE("\n"); + + // topic_info->payload[strlen(topic_info->payload)] = '\0'; + char str_tmp[32]; + rt_strncpy(str_tmp, topic_info->payload, topic_info->payload_len); + str_tmp[topic_info->payload_len] = '\0'; + assist_control(str_tmp); break; default: break; @@ -172,9 +179,12 @@ void show_lcd() { lcd_y = 10; easy_show_lcd("Temperature:", Temp); - easy_show_lcd("Humidity:", Humi); - easy_show_lcd("Brightness:(lux)", brightness); - easy_show_lcd("Ps data:", (float)ps_data); + // easy_show_lcd("Humidity:", Humi); + // easy_show_lcd("Brightness:(lux)", brightness); + // easy_show_lcd("Ps data:", (float)ps_data); + easy_show_lcd("Air quality:", ADC_air); + easy_show_lcd("PM2.5:", ADC_PM25); + easy_show_lcd("Pressure:",ADC_pressure); // lcd_show_string(10, plus_lcd_y(10), 24, "Temperature:"); // rt_sprintf(tmp, "%f", Temp); // lcd_show_string(10, plus_lcd_y(24), 32, tmp); @@ -394,7 +404,6 @@ static void mqtt_example_main(void *parameter) res = example_subscribe(pclient); if (res < 0) { - wlan_connected = RT_FALSE; IOT_MQTT_Destroy(&pclient); return; } @@ -436,33 +445,35 @@ void wlan_connect_thread(void *parameter) { (void *)parameter; wlan_connected=RT_FALSE; - // rt_wlan_connect(WLAN_NAME,WLAN_PASSWORD); - while(rt_wlan_connect(WLAN_NAME,WLAN_PASSWORD)!= RT_EOK) - { - rt_thread_mdelay(2000); - } + rt_wlan_connect(WLAN_NAME,WLAN_PASSWORD); + rt_wlan_connect(WLAN_NAME,WLAN_PASSWORD); + // while(rt_wlan_connect(WLAN_NAME,WLAN_PASSWORD)!= RT_EOK) + // { + // rt_thread_mdelay(2000); + // } rt_wlan_config_autoreconnect(RT_TRUE); while (1) { if (rt_wlan_is_connected() == RT_TRUE) { - if(!wlan_connected) + // if(!wlan_connected) { wlan_connected = RT_TRUE; - rt_thread_mdelay(10000); + // rt_wlan_connect(WLAN_NAME,WLAN_PASSWORD); + rt_thread_mdelay(5000); mqt_init(); } } - else - { - if(wlan_connected) - { - wlan_connected = RT_FALSE; - IOT_MQTT_Destroy(&pclient); - rt_thread_delete(MQTT_Thread); - } - } - rt_thread_mdelay(500); + // else + // { + // if(wlan_connected) + // { + // wlan_connected = RT_FALSE; + // IOT_MQTT_Destroy(&pclient); + // rt_thread_delete(MQTT_Thread); + // } + // } + rt_thread_mdelay(5000); } } // /* 用于接收消息的信号量 */ diff --git a/applications/sensor.c b/applications/sensor.c index c2f38c8..936637f 100644 --- a/applications/sensor.c +++ b/applications/sensor.c @@ -169,7 +169,7 @@ static void sensor_thread(void *parameter) warning_range("air", ADC_air, 0, 4095); warning_range("PM25", ADC_PM25, 0, 500); warning_range("pressure", ADC_pressure, 0, 4095); - rt_thread_mdelay(1000); + rt_thread_mdelay(2000); } } void sensor_init(void) diff --git a/applications/status.c b/applications/status.c index 0b87009..2dd458c 100644 --- a/applications/status.c +++ b/applications/status.c @@ -45,7 +45,7 @@ void fan_on(void) void fan_off(void) { LOG3("fan off!"); - motor_speed(20); + motor_speed(0); } void light_on(void) @@ -59,5 +59,5 @@ void light_off(void) { LOG3("light off!"); rt_pin_mode(PIN_LED_B, PIN_MODE_OUTPUT); - rt_pin_write(PIN_LED_B, PIN_LOW); + rt_pin_write(PIN_LED_B, PIN_HIGH); } \ No newline at end of file diff --git a/applications/status.h b/applications/status.h index a48a7d3..71b4d93 100644 --- a/applications/status.h +++ b/applications/status.h @@ -1 +1,5 @@ -void danger_status(void); \ No newline at end of file +void danger_status(void); +void fan_on(void); +void fan_off(void); +void light_on(void); +void light_off(void); \ No newline at end of file