资讯详情

WiFi温湿度传感器开发

1.介绍温度传感器 a,常用的温度测量传感器 模拟传感器(热电阻,热电偶) 数字传感器 热电阻:

  • PT100
  • PT1000
  • 原理:电阻组织随温度变化
  • 测温方法:电桥测量电压差 热电偶:
  • K(镍络-镍硅)型
  • S类型(铂烙-康铜)
  • 热电偶原理: 在这里插入图片描述 b,初步识别温度传感器 DS18B20特性:
  • 单总线温度传感器
  • 测量温度范围-55C 125C
  • -10 85C精度在范围内±0.5C
  • 供电电压:3-5.5V,可使用数据线供电
  • 每个传感器都有独立的序列号,支持多路传感器
  • AD转换位数9-12位(可配置)
  • 转换时间750ms(最大)
  • 支持报警设置,报警断电不丢失 c,引脚定义:
    • 1地线
    • 2数据线或电源
    • 3电源 内部结构: 单总线供电注意事项:
  • 有足够的电流驱动能力
  • 转换电流约1.5Ma
  • 多个温度传感器同时转换

供电方式1: 供电方式2: 传感器操作:

  • 配置9,10,11,12bit(默认12bit)
  • 最小分辨率为:0.5C,0.25C, 0.125C , 0.0625C(温度) 转换时间: 转换时序:
  • 初始化
  • 配置ROM功能命令
  • 内存功能命令
  • 转换数据

ROM命令

  • 读ROM-33H
  • 匹配ROM 55H
  • 跳过ROM CCH(如果有多个温度传感器会导致数据丢失)
  • 搜索ROM FOH
  • 告警搜素 ECH

功能命令

  • 温度转换 44H(启动转换后,数据以两个字节的形式存储在高速暂存区中)
  • 读暂存区 BEh 写暂存区 4EH(先写TH和TL寄存器,后写配置寄存器)
  • 拷贝暂存区 48H (把THTL写到EEPROM)
  • 召回EEPROM B8H(把THTL内容从E2prom读到暂存区
  • 读电流模式 B4H

2.温度传感器的读写顺序 时序分析:

  • 初始化
  • 写时序
  • 读时序

初始化时序:单片机总线连续大于480微妙为低电平(输出模式),DS18B高电平持续15-60,然后单片机处于输入模式 逻辑分析仪捕获波形: 三、读取温度数据1 创建工程模板:aos create project 18b20 -b mk3080 -t blink_demo -d ./temp aos make menuconfig 配置是否需要修改(application为blink_demo,BSP为mk3080) 修改blink_demo.c中的内容

#include <stdio.h> #include "aos/kernel.h" #include "ulog/ulog.h"
#include "board.h"
#include "aos/hal/gpio.h"

/** * Brief: * This test code shows how to configure LED gpio. */
#ifdef LED1
#define GPIO_LED_IO LED1
#elif LED2
#define GPIO_LED_IO LED2
#elif LED3
#define GPIO_LED_IO LED3
#elif LED4
#define GPIO_LED_IO LED4
#else
#define GPIO_LED_IO 0xffff
#endif

gpio_dev_t led_nucleo;

#define G18b20IO 6
gpio_dev_t GPIO_18b20io;


void initForInput()
{ 
       
   GPIO_18b20.config=INPUT_PULL_UP;
   if(GPIO_18b20io.priv){ 
       
   		free(GPIO_18b20io.priv);
   		GPIO_18b20io.priv = NULL;
   	}
   hal_gpio_init(&GPIO_18b20);
}

void initForOutput()
{ 
       
   GPIO_18b20.config = OUTPUT_PUSH_PULL;
   hal_gpio_init(&GPIO_18b20);
}
//初始化
void init_18b20()
{ 
       
   int inputValue=0;
   hal_gpio_output_high(&GPIO_18b20);
   DelayUs(100);
   hal_gpio_output_low(&GPIO_18b20);
   DelayUs(750);
   hal_gpio_output_high(&GPIO_18B20);
   initForInput();
   DelayUs(50);
   hal_gpio_input_get(&GPIO_18b20,inputValue);

   if(inputValue){ 
       
        printf("18b20 init failed\n");
        return -1;
   }
 initForOutput();
   hal_gpio_output_high(&GPIO_18b20);
   DelayUs(15);
   retrun ;

}
//写时序
void write_18b20_byte(unsigned char data)
{ 
       
   int i;
   for(i=0;i<8;i++){ 
       
        hal_gpio_output_low(&GPIO_18B20);
        if(data&0x01){ 
           //write 1 to 18b20
            DelayUs(10);
            hal_gpio_output_high(&GPIO_18b20);
            DelayUs(45);
        }else{ 
       
            DelayUs(60);
            hal_gpio_output_high(&GPIO_18b20);
        }
        DelayUs(10);
        data>>=1;
   }
}
//读时序
unsigned char read_18b20_byte()
{ 
       
   int i;
	int inputValue;
   int reByte=0;

   for(i=0;i<8;i++){ 
       
        hal_gpio_output_low(&GPIO_18b20);
        initForInput();
        DelayUs(5);

        hal_gpio_input_get(&GPIO_18b20,&inputValue);
        reByte = reByte>>1;
        if(inputValue){ 
       
           reByte |= 0x80;
        }

        initForOutput();
        hal_gpio_output_high(&GPIO_18b20);

        DelayUs(50);
   }
   retrun  reByte;
}
//从18b20温度传感器读温度值到暂存寄存器中
unsigned short readTempFrom18b20()
{ 
       
   int ret;
   unsigned char tempH;
   unsigned char tempL;
   unsigned short tmep;
   ret = init_18b20();
   if(ret<0){ 
       
        return -1;
   }

   write_18b20_byte(0xCC);
   write_18b20_byte(0x44);

   DelayUs(2000);

   write_18b20_byte(0xCC);
   write_18b20_byte(0xbe);
   DelayUs(500);

   tempL= read_18b20_byte();
   tempH = read_18b20_byte();

   temp = tempH*256 + tempL;
   printf("read th=%d,tl=%d,temp=%d\n",tempH,tempL,temp);
   return temp;

}

int application_start(int argc, char *argv[])
{ 
       
    float temp;
     /* gpio port config */
    GPIO_18b20io.port = G18b20IO;
    /* set as output mode */

    GPIO_18b20io.config = OUTPUT_PUSH_PULL;
    /* configure GPIO with the given settings */
    hal_gpio_init(&GPIO_18b20io);

    while (1)
    { 
       
        /* Insert delay 1000 ms */
        temp = readTempFrom18b20();
        printf("current temp=%f\n",(float)temp/16;
        aos_msleep(1000);
        
    }

    return 0;
}

aos make 进行编译,然后烧录到开发板中。 接线方式:传感器的DQ数据线接到开发板的PA0口,GND接到开发板的GND,VCC接到开发板3.3v上。 4,温度数据上云1,温度异常事件上报,温度异常参数设置 使用连网程序,让温度数据上阿里云。 首先创建一个工程(可以读取温度数据,并且可以将数据上传到阿里云)

aos create project templink -b mk3080 -t linkkit_demo -d ./temp

将blink_demo.c 下添加的读取温度传感器的值得代码移植到templink目录的相关连网代码中。 a,在项目管理-生活物联网平台我的天猫精灵WiFi项目中创建一个新产品:https://living.aliyun.com 选择物理模型为温湿度传感器。 将创建好的设备的三元组写入到linkkit_example_solo.c 中,并将blink_demo.c的application_start()函数中的gpio的初始化写入到templink目录下app_entry.c的aos_loop_run()函数之前。

#include "aos/kernel.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "linkkit/infra/infra_types.h"
#include "linkkit/infra/infra_defs.h"
#include "linkkit/infra/infra_compat.h"
#include "linkkit/dev_model_api.h"
#include "linkkit/infra/infra_config.h"
#include "linkkit/wrappers/wrappers.h"
#include "aos/hal/gpio.h" //添加的

#ifdef INFRA_MEM_STATS
    #include "linkkit/infra/infra_mem_stats.h"
#endif

#include "cJSON.h"
#ifdef ATM_ENABLED
    #include "at_api.h"
#endif
#include "app_entry.h"

// for demo only
#define PRODUCT_KEY "a15JbJC8ibT"
#define PRODUCT_SECRET "8sOLdfoddWuIsz56"
#define DEVICE_NAME "tempsensor"
#define DEVICE_SECRET "e80bc6df67206eca0455ed726d820688"

#define G18b20IO 6
gpio_dev_t GPIO_18b20io;


float tempHigh = 30;
float tempLow = 15;
float temp;

#define EXAMPLE_TRACE(...) \ do { 
          \ HAL_Printf("\033[1;32;40m%s.%d: ", __func__, __LINE__); \ HAL_Printf(__VA_ARGS__); \ HAL_Printf("\033[0m\r\n"); \ } while (0)

#define EXAMPLE_MASTER_DEVID (0)
#define EXAMPLE_YIELD_TIMEOUT_MS (200)

typedef struct { 
       
    int master_devid;
    int cloud_connected;
    int master_initialized;
} user_example_ctx_t;

/** * These PRODUCT_KEY|PRODUCT_SECRET|DEVICE_NAME|DEVICE_SECRET are listed for demo only * * When you created your own devices on iot.console.com, you SHOULD replace them with what you got from console * */


void initForInput()
{ 
       
   GPIO_18b20.config=INPUT_PULL_UP;
   if(GPIO_18b20io.priv){ 
       
   		free(GPIO_18b20io.priv);
   		GPIO_18b20io.priv = NULL;
   	}
   hal_gpio_init(&GPIO_18b20);
}

void initForOutput()
{ 
       
   GPIO_18b20.config = OUTPUT_PUSH_PULL;
   hal_gpio_init(&GPIO_18b20);
}
//初始化
void init_18b20()
{ 
       
   int inputValue=0;
   hal_gpio_output_high(&GPIO_18b20);
   DelayUs(100);
   hal_gpio_output_low(&GPIO_18b20);
   DelayUs(750);
   hal_gpio_output_high(&GPIO_18B20);
   initForInput();
   DelayUs(50);
   hal_gpio_input_get(&GPIO_18b20,inputValue);

   if(inputValue){ 
       
        printf("18b20 init failed\n");
        return -1;
   }
 initForOutput();
   hal_gpio_output_high(&GPIO_18b20);
   DelayUs(15);
   retrun ;

}
//写时序
void write_18b20_byte(unsigned char data)
{ 
       
   int i;
   for(i=0;i<8;i++){ 
       
        hal_gpio_output_low(&GPIO_18B20);
        if(data&0x01){ 
           //write 1 to 18b20
            DelayUs(10);
            hal_gpio_output_high(&GPIO_18b20);
            DelayUs(45);
        }else{ 
       
            DelayUs(60);
            hal_gpio_output_high(&GPIO_18b20);
        }
        DelayUs(10);
        data>>=1;
   }
}
//读时序
unsigned char read_18b20_byte()
{ 
       
   int i;
	int inputValue;
   int reByte=0;

   for(i=0;i<8;i++){ 
       
        hal_gpio_output_low(&GPIO_18b20);
        initForInput();
        DelayUs(5);

        hal_gpio_input_get(&GPIO_18b20,&inputValue);
        reByte = reByte>>1;
        if(inputValue){ 
       
           reByte |= 0x80;
        }

        initForOutput();
        hal_gpio_output_high(&GPIO_18b20);

        DelayUs(50);
   }
   retrun  reByte;
}
//从18b20温度传感器读温度值到暂存寄存器中
unsigned short readTempFrom18b20()
{ 
       
   int ret;
   unsigned char tempH;
   unsigned char tempL;
   unsigned short tmep;
   ret = init_18b20();
   if(ret<0){ 
       
        return -1;
   }

   write_18b20_byte(0xCC);
   write_18b20_byte(0x44);

   DelayUs(2000);

   write_18b20_byte(0xCC);
   write_18b20_byte(0xbe);
   DelayUs(500);

   tempL= read_18b20_byte();
   tempH = read_18b20_byte();

   temp = tempH*256 + tempL;
   printf("read th=%d,tl=%d,temp=%d\n",tempH,tempL,temp);
   return temp;

}
static user_example_ctx_t g_user_example_ctx;

/** cloud connected event callback */
static int user_connected_event_handler(void)
{ 
       
    EXAMPLE_TRACE("Cloud Connected");
    g_user_example_ctx.cloud_connected = 1;

    return 0;
}

/** cloud disconnected event callback */
static int user_disconnected_event_handler(void)
{ 
       
    EXAMPLE_TRACE("Cloud Disconnected");
    g_user_example_ctx.cloud_connected = 0;

    return 0;
}

void initGPIO18b20()
{ 
       
    GPIO_18b20io.port = G18b20IO;
    /* set as output mode */

    GPIO_18b20io.config = OUTPUT_PUSH_PULL;
    /* configure GPIO with the given settings */
    hal_gpio_init(&GPIO_18b20io);
}


/* device initialized event callback */
static int user_initialized(const int devid)
{ 
       
    EXAMPLE_TRACE("Device Initialized");
    g_user_example_ctx.master_initialized = 1;

    return 0;
}

/** recv property post response message from cloud **/
/** recv property post response message from cloud **/
static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
        const int reply_len)
{ 
       
    EXAMPLE_TRACE("Message Post Reply Received, Message ID: %d, Code: %d, Reply: %.*s", msgid, code,
                  reply_len,
                  (reply == NULL) ? ("NULL") : (reply));
    return 0;
}

/** recv event post response message from cloud **/
static int user_trigger_event_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid,
        const int eventid_len, const char *message, const int message_len)
{ 
       
    EXAMPLE_TRACE("Trigger Event Reply Received, Message ID: %d, Code: %d, EventID: %.*s, Message: %.*s",
                  msgid, code,
                  eventid_len,
                  eventid, message_len, message);

    return 0;
}
/** recv event post response message from cloud **/
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
{ 
       
    int res = 0;
    cJSON  *root,*prop;
    EXAMPLE_TRACE("Property Set Received, Request: %s", request);
	root = cJSON_Parse(request);
	if(root == NULL){ 
       
		return res;
	}
	prop = cJSON_GetObjectItem(root,"EnvTemp_High_Threshodl");
	if(prop!=NULL && cJSON_IsNumber(prop){ 
       
		tempHigh = prop->valueint;
		printf("set EnvTemp_High_Threshodl:%f\n",tempHigh);
	}
	prop = cJSON_GetObjectItem(root,"EnvTemp_Low_Threshdl");
	if(prop!=NULL && cJSON_IsNumber(prop){ 
       
		tempLow = prop->valueinit;
		printf("set EnvTemp_Low_Threshodl:%f\n",tempLow);
	}
    res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
                             (unsigned char *)

标签: va传感器g无线温度传感器6m压差传感器无线温温度传感器传感器mk72f热电偶传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台