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微妙为低电平(输出模式),DS18B20持续15-60微妙为高电平,之后单片机处于输入模式
在这里插入图片描述
在这里插入图片描述
逻辑分析仪抓取波形:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3,读取温度数据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 *)request, request_len);
    EXAMPLE_TRACE("Post Property Message ID: %d", res);

    return 0;
}


static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
        const char *request, const int request_len,
        char **response, int *response_len)
{
    int add_result = 0;
    int ret = -1;
    cJSON *root = NULL, *item_number_a = NULL, *item_number_b = NULL;
    const char *response_fmt = "{\"Result\": %d}";
    EXAMPLE_TRACE("Service Request Received, Service ID: %.*s, Payload: %s", serviceid_len, serviceid, request);

    /* Parse Root */
    root = cJSON_Parse(request);
    if (root == NULL || !cJSON_IsObject(root)) {
        EXAMPLE_TRACE("JSON Parse Error");
        return -1;
    }
 do{
        if (strlen("Operation_Service") == serviceid_len && memcmp("Operation_Service", serviceid, serviceid_len) == 0) {
            /* Parse NumberA */
            item_number_a = cJSON_GetObjectItem(root, "NumberA");
            if (item_number_a == NULL || !cJSON_IsNumber(item_number_a)) {
                break;
            }
            EXAMPLE_TRACE("NumberA = %d", item_number_a->valueint);

            /* Parse NumberB */
            item_number_b = cJSON_GetObjectItem(root, "NumberB");
            if (item_number_b == NULL || !cJSON_IsNumber(item_number_b)) {
                break;
            }
            EXAMPLE_TRACE("NumberB = %d", item_number_b->valueint);
            add_result = item_number_a->valueint + item_number_b->valueint;
            ret = 0;
            /* Send Service Response To Cloud */
        }
    }while(0);

    *response_len = strlen(response_fmt) + 10 + 1;
    *response = (char *)HAL_Malloc(*response_len);
    if (*response != NULL) {
        memset(*response, 0, *response_len);
        HAL_Snprintf(*response, *response_len, response_fmt, add_result);
        *response_len = strlen(*response);
    }

    cJSON_Delete(root);
    return ret;
}
static int user_timestamp_reply_event_handler(const char *timestamp)
{
    EXAMPLE_TRACE("Current Timestamp: %s", timestamp);

    return 0;
}

/** fota event handler **/
static int user_fota_event_handler(int type, const char *version)
{
    char buffer[128] = {0};
    int buffer_length = 128;

    /* 0 - new firmware exist, query the new firmware */
    if (type == 0) {
        EXAMPLE_TRACE("New Firmware Version: %s", version);

        IOT_Linkkit_Query(EXAMPLE_MASTER_DEVID, ITM_MSG_QUERY_FOTA_DATA, (unsigned char *)buffer, buffer_length);
    }

    return 0;
}
/* cota event handler */
static int user_cota_event_handler(int type, const char *config_id, int config_size, const char *get_type,
                                   const char *sign, const char *sign_method, const char *url)
{
    char buffer[128] = {0};
    int buffer_length = 128;

    /* type = 0, new config exist, query the new config */
    if (type == 0) {
        EXAMPLE_TRACE("New Config ID: %s", config_id);
        EXAMPLE_TRACE("New Config Size: %d", config_size);
        EXAMPLE_TRACE("New Config Type: %s", get_type);
        EXAMPLE_TRACE("New Config Sign: %s", sign);
        EXAMPLE_TRACE("New Config Sign Method: %s", sign_method);
        EXAMPLE_TRACE("New Config URL: %s", url);

        IOT_Linkkit_Query(EXAMPLE_MASTER_DEVID, ITM_MSG_QUERY_COTA_DATA, (unsigned char *)buffer, buffer_length);
    }

    return 0;
}

void user_post_property(void)
{
    static int cnt = 0;
    int res = 0;
    
    char property_payload[30] = {0};
   // HAL_Snprintf(property_payload, sizeof(property_payload), "{\"Counter\": %d}", cnt++);
	temp = readTempFrom18b20();
	temp = temp/16;
	
	if(countIndex == 0){
		sprintf(property_payload,"{\"temperature\":%f}",temp);
		countIndex++;
	}else if(countIndex == 1){
		sprintf(property_payload,"{\"EnvTemp_High_Threshold\":%f}",tempHigh);
		countIndex++;
	}else if(countIndex == 2){
		sprintf(property_payload,"{\"EnvTemp_low_Threshold\":%f}",tempLow);
		countIndex++;
	}else{
		countIndex++;
	}
	
    res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_POST_PROPERTY,
                             (unsigned char *)property_payload, strlen(property_payload));

    EXAMPLE_TRACE("Post Property Message ID: %d", res);
}

void user_post_TL_event(void)
{
    int res = 0;
    char *event_id = "xxxxxx";  //设置好的设备事件的标识符
    char *event_payload = "";

    res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
                                   event_payload, strlen(event_payload));
    EXAMPLE_TRACE("Post Event Message ID: %d", res);
}

void user_post_TH_event(void)
{
    int res = 0;
    char *event_id = "xxxx";
    char *event_payload = "";

    res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
                                   event_payload, strlen(event_payload));
    EXAMPLE_TRACE("Post Event Message ID: %d", res);
}

void user_deviceinfo_update(void)
{
    int res = 0;
    char *device_info_update = "[{\"attrKey\":\"abc\",\"attrValue\":\"hello,world\"}]";

    res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_DEVICEINFO_UPDATE,
                             (unsigned char *)device_info_update, strlen(device_info_update));
    EXAMPLE_TRACE("Device Info Update Message ID: %d", res);
}
void user_deviceinfo_delete(void)
{
    int res = 0;
    char *device_info_delete = "[{\"attrKey\":\"abc\"}]";

    res = IOT_Linkkit_Report(EXAMPLE_MASTER_DEVID, ITM_MSG_DEVICEINFO_DELETE,
                             (unsigned char *)device_info_delete, strlen(device_info_delete));
    EXAMPLE_TRACE("Device Info Delete Message ID: %d", res);
}

static int user_cloud_error_handler(const int code, const char *data, const char *detail)
{
    EXAMPLE_TRACE("code =%d ,data=%s, detail=%s", code, data, detail);
    return 0;
}

void set_iotx_info()
{
    char _product_key[IOTX_PRODUCT_KEY_LEN + 1] = {0};
    char _device_name[IOTX_DEVICE_NAME_LEN + 1] = {0};

    HAL_GetProductKey(_product_key);
    if (strlen(_product_key) == 0) {
        HAL_SetProductKey(PRODUCT_KEY);
        HAL_SetProductSecret(PRODUCT_SECRET);
    }

    HAL_GetDeviceName(_device_name);
	if (strlen(_device_name) == 0) {
        HAL_SetDeviceName(DEVICE_NAME);
        HAL_SetDeviceSecret(DEVICE_SECRET);
    }
}

int linkkit_main(void *paras)
{
    int res = 0;
    int cnt = 0;
    int auto_quit = 0;
    iotx_linkkit_dev_meta_info_t master_meta_info;
    int domain_type = 0, dynamic_register = 0, post_reply_need = 0, fota_timeout = 30;
    int   argc = 0;
    char  **argv = NULL;

    if (paras != NULL) {
        argc = ((app_main_paras_t *)paras)->argc;
        argv = ((app_main_paras_t *)paras)->argv;
    }
#ifdef ATM_ENABLED
    if (IOT_ATM_Init() < 0) {
        EXAMPLE_TRACE("IOT ATM init failed!\n");
        return -1;
    }
#endif


    if (argc >= 2 && !strcmp("auto_quit", argv[1])) {
        auto_quit = 1;
        cnt = 0;
}
    memset(&g_user_example_ctx, 0, sizeof(user_example_ctx_t));

    memset(&master_meta_info, 0, sizeof(iotx_linkkit_dev_meta_info_t));
    HAL_GetProductKey(master_meta_info.product_key);
    HAL_GetDeviceName(master_meta_info.device_name);
    HAL_GetProductSecret(master_meta_info.product_secret);
    HAL_GetDeviceSecret(master_meta_info.device_secret);

    IOT_SetLogLevel(IOT_LOG_INFO);

    /* Register Callback */
    IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
    IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
    IOT_RegisterCallback(ITE_SERVICE_REQUEST, user_service_request_event_handler);
    IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
    IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
    IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
    IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
    IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
    IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
    IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
    IOT_RegisterCallback(ITE_CLOUD_ERROR, user_cloud_error_handler);


    domain_type = IOTX_CLOUD_REGION_SHANGHAI;
    IOT_Ioctl(IOTX_IOCTL_SET_DOMAIN, (void *)&domain_type);

    /* Choose Login Method */
    dynamic_register = 0;
    IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)&dynamic_register);

    /* post reply doesn't need */
    post_reply_need = 1;
    IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_reply_need);

    IOT_Ioctl(IOTX_IOCTL_FOTA_TIMEOUT_MS, (void *)&fota_timeout);
#if defined(USE_ITLS)
    {
        char url[128] = {0};
        int port = 1883;
        snprintf(url, 128, "%s.itls.cn-shanghai.aliyuncs.com",master_meta_info.product_key);
        IOT_Ioctl(IOTX_IOCTL_SET_MQTT_DOMAIN, (void *)url);
        IOT_Ioctl(IOTX_IOCTL_SET_CUSTOMIZE_INFO, (void *)"authtype=id2");
        IOT_Ioctl(IOTX_IOCTL_SET_MQTT_PORT, &port);
    }
#endif
    /* Create Master Device Resources */
    do {
        g_user_example_ctx.master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, &master_meta_info);
        if (g_user_example_ctx.master_devid >= 0) {
            break;
        }
        EXAMPLE_TRACE("IOT_Linkkit_Open failed! retry after %d ms\n", 2000);
        HAL_SleepMs(2000);
    } while (1);
    /* Start Connect Aliyun Server */
   do {
        res = IOT_Linkkit_Connect(g_user_example_ctx.master_devid);
        if (res >= 0) {
            break;
        }
        EXAMPLE_TRACE("IOT_Linkkit_Connect failed! retry after %d ms\n", 5000);
        HAL_SleepMs(5000);
    } while (1);

    while (1) {
        IOT_Linkkit_Yield(EXAMPLE_YIELD_TIMEOUT_MS);

        /* Post Proprety Example */

        if ((cnt % 20) == 0) {
            user_post_property();
        }

        /* Post Event Example */
        if ((cnt % 50) == 0) {
        	if(temp>tempHigh){
           		 user_post_TH_event();
        	}
        	if(temp<tempLow){
        		user_post_TL_event();
        	}
        }
        

        cnt++;

        if (auto_quit == 1 && cnt > 3600) {
            break;
        }
       }

    IOT_Linkkit_Close(g_user_example_ctx.master_devid);

    IOT_DumpMemoryStats(IOT_LOG_DEBUG);

    return 0;
}

在这里插入图片描述
编译前先进行aos make menuconfig配置(需要选上JSON Libarary),aos make进行编译,烧录。
配网成功后,使用putty查看log信息。通过运行状态查看温度是否上传到云平台。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值