Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ cwd = GetCurrentDir()
src = []
inc = [cwd]

src += ['sensor_dallas_dht11.c']
src += ['rt_hw_dht11.c']

if GetDepend(['PKG_USING_DHT11_SAMPLE']):
src += ['dht11_sample.c']

if GetDepend('PKG_DHT11_USING_SENSOR_V1'):
src += ['sensor_dallas_dht11.c']

group = DefineGroup('dht11', src, depend = ['PKG_USING_DHT11'], CPPPATH = inc)
Return('group')
38 changes: 34 additions & 4 deletions dht11_sample.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* Date Author Notes
* 2019-08-01 LuoGong the first version.
* 2019-08-15 MurphyZhao add lock and modify code style
* 2024-07-02 KurisaW sensor mode separation
*
*/

#include <rtthread.h>
#include <rtdevice.h>
#include "sensor.h"
#include "sensor_dallas_dht11.h"
#include "drv_gpio.h"
#include "rt_hw_dht11.h"
#include <drv_gpio.h>

/* Modify this pin according to the actual wiring situation */
#define DHT11_DATA_PIN GET_PIN(B, 12)

static void read_temp_entry(void *parameter)
{
#ifndef RT_USING_SENSOR
rt_uint8_t ret;
rt_uint8_t humidity, temp;

while (1)
{
ret = dht11_read_Data(DHT11_DATA_PIN, &temp, &humidity);
if (ret == 0)
{
rt_kprintf("Temperature: %dC, Humidity: %d%%\n", temp, humidity);
}

rt_thread_mdelay(2000);
}
#else
rt_device_t dev = RT_NULL;
struct rt_sensor_data sensor_data;
rt_size_t res;
Expand Down Expand Up @@ -61,6 +78,7 @@ static void read_temp_entry(void *parameter)

rt_thread_delay(1000);
}
#endif
}

static int dht11_read_temp_sample(void)
Expand All @@ -84,11 +102,23 @@ INIT_APP_EXPORT(dht11_read_temp_sample);

static int rt_hw_dht11_port(void)
{
#ifndef RT_USING_SENSOR
rt_uint8_t ret;
ret = dht11_init(DHT11_DATA_PIN);
if (ret != 0)
{
rt_kprintf("DHT11 init failed!\n");
return 1;
}
return 0;
#else
struct rt_sensor_config cfg;

cfg.intf.user_data = (void *)DHT11_DATA_PIN;
rt_hw_dht11_init("dht11", &cfg);

return RT_EOK;
#endif
}
INIT_COMPONENT_EXPORT(rt_hw_dht11_port);

179 changes: 179 additions & 0 deletions rt_hw_dht11.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-08-01 LuoGong the first version.
* 2019-08-15 MurphyZhao add lock and modify code style
* 2024-07-02 KurisaW sensor mode separation
*
*/

#include <rtdevice.h>
#include <rthw.h>
#include "board.h"
#include <stdint.h>
#include "rt_hw_dht11.h"

#define SENSOR_DEBUG
#define DBG_TAG "sensor.dht11"

#ifdef SENSOR_DEBUG
#define DBG_LVL DBG_LOG
#else
#define DBG_LVL DBG_ERROR
#endif /* SENSOR_DEBUG */
#include <rtdbg.h>

#ifndef RT_USING_PIN
#error "Please enable RT_USING_PIN"
#endif

#ifndef rt_hw_us_delay
rt_weak void rt_hw_us_delay(rt_uint32_t us)
{
rt_uint32_t delta;

us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND));
delta = SysTick->VAL;

while (delta - SysTick->VAL < us) continue;
}
#endif

static void dht11_reset(rt_base_t pin)
{
rt_pin_mode(pin, PIN_MODE_OUTPUT);

rt_pin_write(pin, PIN_LOW);
rt_thread_mdelay(20); /* 20ms */

rt_pin_write(pin, PIN_HIGH);
rt_hw_us_delay(30); /* 30us*/
}

static uint8_t dht11_check(rt_base_t pin)
{
uint8_t retry = 0;
rt_pin_mode(pin, PIN_MODE_INPUT);

while (rt_pin_read(pin) && retry < 100)
{
retry++;
rt_hw_us_delay(1);
}

if(retry >= 100)
{
return CONNECT_FAILED;
}

retry = 0;
while (!rt_pin_read(pin) && retry < 100)
{
retry++;
rt_hw_us_delay(1);
};

if(retry >= 100)
{
return CONNECT_FAILED;
}

return CONNECT_SUCCESS;
}

static uint8_t dht11_read_bit(rt_base_t pin)
{
uint8_t retry = 0;
while (rt_pin_read(pin) && retry < 100)
{
retry++;
rt_hw_us_delay(1);
}
retry = 0;

while (!rt_pin_read(pin) && retry < 100)
{
retry++;
rt_hw_us_delay(1);
}

rt_hw_us_delay(40);
if(rt_pin_read(pin))
return 1;
return 0;
}

static uint8_t dht11_read_byte(rt_base_t pin)
{
uint8_t i, dat = 0;

for (i = 1; i <= 8; i++)
{
dat <<= 1;
dat |= dht11_read_bit(pin);
}

return dat;
}

uint8_t dht11_read_Data(rt_base_t pin,uint8_t *temp,uint8_t *humi)
{
uint8_t i, buf[5];
dht11_reset(pin);

if(dht11_check(pin) == 0)
{
for(i=0; i<5; i++) /* read 40 bits */
{
buf[i] = dht11_read_byte(pin);
}

if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
{
*humi = buf[0];
*temp = buf[2];
}
}
else
{
return 1;
}

return 0;
}

uint8_t dht11_init(rt_base_t pin)
{
uint8_t ret = 0;

dht11_reset(pin);
ret = dht11_check(pin);
if (ret != 0)
{
dht11_reset(pin);
ret = dht11_check(pin);
}

return ret;
}

int32_t dht11_get_temperature(rt_base_t pin)
{
static int32_t temOLD = 0;
uint8_t humi=0, temp = 0;
int32_t temNEW;

dht11_read_Data(pin, &temp, &humi);

temNEW = (humi << 16)|(temp<<0);

if((temNEW != temOLD) && (temNEW !=0))
{
temOLD = temNEW;
}
return temOLD;
}
26 changes: 26 additions & 0 deletions rt_hw_dht11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-08-01 LuoGong the first version.
* 2019-08-15 MurphyZhao add lock and modify code style
* 2024-07-02 KurisaW sensor mode separation
*
*/

#ifndef __RT_HW_DHT11_H__
#define __RT_HW_DHT11_H__

#include <rtthread.h>

#define CONNECT_SUCCESS 0
#define CONNECT_FAILED 1

uint8_t dht11_init(rt_base_t pin);
uint8_t dht11_read_Data(rt_base_t pin,uint8_t *temp,uint8_t *humi);
int32_t dht11_get_temperature(rt_base_t pin);

#endif /* __RT_HW_DHT11_H__ */
Loading