K210+RT-Thread的DHT11温湿度监测 C语言
时间: 2024-05-30 20:16:04 浏览: 135
#include <stdio.h>
#include <rtthread.h>
#include <rthw.h>
#include <board.h>
#include <dht11.h>
#define DHT11_DATA_PIN GET_PIN(B, 0)
static void dht11_thread_entry(void *parameter)
{
rt_uint8_t humi, temp;
rt_err_t err;
while (1)
{
err = dht11_read_data(&humi, &temp, DHT11_DATA_PIN);
if (err == RT_EOK)
{
rt_kprintf("Humidity: %d.%d%%, Temperature: %d.%d℃\n", humi / 10, humi % 10, temp / 10, temp % 10);
}
else
{
rt_kprintf("DHT11 read data error: %d\n", err);
}
rt_thread_delay(rt_tick_from_millisecond(2000));
}
}
int dht11_sample(void)
{
rt_thread_t thread;
thread = rt_thread_create("dht11", dht11_thread_entry, RT_NULL, 512, 25, 5);
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
return 0;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(dht11_sample, dht11 sample);
#endif /* RT_USING_FINSH */
阅读全文