TA的每日心情 | 开心 2018-3-16 10:25 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]初来乍到
|
关于产品的创建请参考某坛友的帖子,【体验】+0基础实现WiFi远程插座
此处不再废话
按照WIFI变长创建新产品
创建如下2个数据点
显示名称 | 标识名 | 备注 | 读写类型 | 数据类型 | 数值 | 枚举 | 扩展 | 分辨率 | 增量 | 数据范围最小值 | 数据范围最大值 | 选项 | 长度 | 温度 | temperature | | 只读 | 数值 | 1 | -10 | -10 | 50 | | | 湿度 | humidity | | 只读 | 数值 | 1 | 0 | 0 | 99 | | | 在MCU开发里面选择SOC方案,然后选择32M,生成代码下载。
接下来我们采用DHT11来实现温湿度的采集
在项目里面创建如下2个文件,以及文件内容
1,hal_temp_hum.c
#include "driver/hal_temp_hum.h"
#include "osapi.h"
th_typedef_t temphum_typedef;
static void ICACHE_FLASH_ATTR tempHumDelay(unsigned int us)
{
/* Define your delay function */
os_delay_us(us);
}
//Reset DHT11
static void ICACHE_FLASH_ATTR dht11Rst(void)
{
DHT11_IO_OUT; //SET OUTPUT
DHT11_OUT_LOW; //GPIOA.0=0
tempHumDelay(18*1000); //Pull down Least 18ms
DHT11_OUT_HIGH; //GPIOA.0=1
}
static uint8_t ICACHE_FLASH_ATTR dht11Check(void)
{
uint8_t retry=0;
DHT11_IO_IN; //SET INPUT
while (DHT11_IN&&retry<100) //DHT11 Pull down 40~80us
{
retry++;
tempHumDelay(1);
}
if(retry>=100)
return 1;
else
retry=0;
while (!DHT11_IN&&retry<100) //DHT11 Pull up 40~80us
{
retry++;
tempHumDelay(1);
}
if(retry>=100)
return 1; //chack error
return 0;
}
static uint8_t ICACHE_FLASH_ATTR dht11ReadBit(void)
{
uint8_t retry=0;
while(DHT11_IN&&retry<100) //wait become Low level
{
retry++;
tempHumDelay(1);
}
retry=0;
while(!DHT11_IN&&retry<100) //wait become High level
{
retry++;
tempHumDelay(1);
}
tempHumDelay(40); //wait 40us
if(DHT11_IN)
return 1;
else
return 0;
}
static uint8_t ICACHE_FLASH_ATTR hdt11ReadByte(void)
{
uint8_t i;
uint8_t dat=0;
for (i=0; i<8; i++)
{
dat<<=1;
dat |= dht11ReadBit();
}
return dat;
}
static uint8_t ICACHE_FLASH_ATTR dht11ReadData(u8 * temperature, u8 * humidity)
{
uint8_t i;
uint8_t buf[5];
dht11Rst();
if(0 == dht11Check())
{
for(i=0; i<5; i++)
{
buf = hdt11ReadByte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humidity=buf[0];
*temperature=buf[2];
}
}
else
{
return 1;
}
return 0;
}
uint8_t ICACHE_FLASH_ATTR dh11Read(uint8_t * temperature, uint8_t * humidity)
{
uint8_t ret = 0;
uint8_t cur_i = 0;
uint8_t curTem = 0;
uint8_t curHum = 0;
uint16_t temMeans = 0;
uint16_t hum_means = 0;
ret = dht11ReadData(&curTem, &curHum);
if(0 == ret)
{
//Cycle store ten times stronghold
if(MEAN_NUM > temphum_typedef.th_num)
{
temphum_typedef.th_bufs[temphum_typedef.th_num][0] = curTem;
temphum_typedef.th_bufs[temphum_typedef.th_num][1] = curHum;
temphum_typedef.th_num++;
}
else
{
temphum_typedef.th_num = 0;
temphum_typedef.th_bufs[temphum_typedef.th_num][0] = curTem;
temphum_typedef.th_bufs[temphum_typedef.th_num][1] = curHum;
temphum_typedef.th_num++;
}
}
else
{
return 1;
}
if(MEAN_NUM <= temphum_typedef.th_num)
{
temphum_typedef.th_amount = MEAN_NUM;
}
if(0 == temphum_typedef.th_amount)
{
//Calculate Before ten the mean
for(cur_i = 0; cur_i < temphum_typedef.th_num; cur_i++)
{
temMeans += temphum_typedef.th_bufs[cur_i][0];
hum_means += temphum_typedef.th_bufs[cur_i][1];
}
temMeans = temMeans / temphum_typedef.th_num;
hum_means = hum_means / temphum_typedef.th_num;
*temperature = temMeans;
*humidity = hum_means;
}
else if(MEAN_NUM == temphum_typedef.th_amount)
{
//Calculate After ten times the mean
for(cur_i = 0; cur_i < temphum_typedef.th_amount; cur_i++)
{
temMeans += temphum_typedef.th_bufs[cur_i][0];
hum_means += temphum_typedef.th_bufs[cur_i][1];
}
temMeans = temMeans / temphum_typedef.th_amount;
hum_means = hum_means / temphum_typedef.th_amount;
*temperature = (uint8_t)temMeans;
*humidity = (uint8_t)hum_means;
}
return 0;
}
uint8_t ICACHE_FLASH_ATTR dh11Init(void)
{
/* Migrate your driver code */
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5);
dht11Rst();
os_memset((uint8_t *)&temphum_typedef, 0, sizeof(th_typedef_t));
os_printf("dh11Init \r\n");
return dht11Check();
}
void ICACHE_FLASH_ATTR dh11SensorTest(void)
{
/* Test LOG model */
uint8_t curTem = 0;
uint8_t curHum = 0;
dht11ReadData(&curTem, &curHum);
os_printf("Temperature : %d , Humidity : %d", curTem, curHum);
}
2,hal_temp_hum.h
#ifndef _HAL_HEMP_HUM_H
#define _HAL_HEMP_HUM_H
#include <stdio.h>
#include <c_types.h>
#include <gpio.h>
#include <eagle_soc.h>
/* Define your drive pin */
#define DHT11_GPIO_PIN 5
/* Set GPIO Direction */
#define DHT11_IO_IN GPIO_DIS_OUTPUT(GPIO_ID_PIN(DHT11_GPIO_PIN))// gpio_output_set(0, 0, 0, GPIO_ID_PIN(DHT11_GPIO_PIN))不可用
#define DHT11_IO_OUT gpio_output_set(0, 0, GPIO_ID_PIN(DHT11_GPIO_PIN), 0)
#define DHT11_OUT_HIGH GPIO_OUTPUT_SET(GPIO_ID_PIN(DHT11_GPIO_PIN), 1)
#define DHT11_OUT_LOW GPIO_OUTPUT_SET(GPIO_ID_PIN(DHT11_GPIO_PIN), 0)
#define DHT11_IN GPIO_INPUT_GET(GPIO_ID_PIN(DHT11_GPIO_PIN))
#define MEAN_NUM 10
typedef struct
{
uint8_t th_num;
uint8_t th_amount;
uint8_t th_bufs[10][2];
}th_typedef_t;
/* Function declaration */
uint8_t dh11Read(uint8_t * temperature, uint8_t * humidity);
uint8_t dh11Init(void); //Init DHT11
void dh11SensorTest(void);
#endif /*_HAL_HEMP_HUM_H*/
DHT11的数据引脚接在ESP8266的GPIO5上面
然后在主函数和gizwits_product.c里面包含温湿度的头文件,
主函数里面吧初始化写在按键初始化后面
userHandle函数需要加入温湿度采集
温湿度读取的函数是uint8_t ICACHE_FLASH_ATTR dh11Read(uint8_t * temperature, uint8_t * humidity)
返回的第一个参数温度,第二个参数湿度,把返回值赋值给currentDataPoint.valuetemperature和currentDataPoint.valuehumidity即可 |
|