|
| 1 | +/*---------------------------------------------------------------------------- |
| 2 | + * Copyright (c) <2018>, <Huawei Technologies Co., Ltd> |
| 3 | + * All rights reserved. |
| 4 | + * Redistribution and use in source and binary forms, with or without modification, |
| 5 | + * are permitted provided that the following conditions are met: |
| 6 | + * 1. Redistributions of source code must retain the above copyright notice, this list of |
| 7 | + * conditions and the following disclaimer. |
| 8 | + * 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 9 | + * of conditions and the following disclaimer in the documentation and/or other materials |
| 10 | + * provided with the distribution. |
| 11 | + * 3. Neither the name of the copyright holder nor the names of its contributors may be used |
| 12 | + * to endorse or promote products derived from this software without specific prior written |
| 13 | + * permission. |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 18 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 21 | + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 22 | + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 | + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 24 | + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + *---------------------------------------------------------------------------*/ |
| 26 | +/*---------------------------------------------------------------------------- |
| 27 | + * Notice of Export Control Law |
| 28 | + * =============================================== |
| 29 | + * Huawei LiteOS may be subject to applicable export control laws and regulations, which might |
| 30 | + * include those applicable to Huawei LiteOS of U.S. and the country in which you are located. |
| 31 | + * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such |
| 32 | + * applicable export control laws and regulations. |
| 33 | + *---------------------------------------------------------------------------*/ |
| 34 | +/** |
| 35 | + * DATE AUTHOR INSTRUCTION |
| 36 | + * 2020-04-21 FanXinhao The first version |
| 37 | + * |
| 38 | + */ |
| 39 | +#include <stdint.h> |
| 40 | +#include <stddef.h> |
| 41 | +#include <string.h> |
| 42 | +#include <los_typedef.h> |
| 43 | +#include <los_task.h> |
| 44 | +#include <boudica120_oc.h> |
| 45 | +#include <osal.h> |
| 46 | +#include <oc_coap_al.h> |
| 47 | + |
| 48 | +#include "dht11.h" |
| 49 | +#include "oled.h" |
| 50 | + |
| 51 | +//DHT11 Data Struct |
| 52 | +DHT11_Data_TypeDef dht11_data; |
| 53 | +unsigned char DHT11_str[10] = {0}; |
| 54 | + |
| 55 | + |
| 56 | +/* LWM2M server information */ |
| 57 | +#define cn_endpoint_id "coap_001" |
| 58 | +#define cn_app_server "119.3.250.80" |
| 59 | +#define cn_app_port "5683" |
| 60 | + |
| 61 | +//if your command is very fast,please use a queue here--TODO |
| 62 | +#define cn_app_rcv_buf_len 128 |
| 63 | +static int8_t s_rcv_buffer[cn_app_rcv_buf_len]; |
| 64 | +static int s_rcv_datalen; |
| 65 | +static osal_semp_t s_rcv_sync; |
| 66 | + |
| 67 | +//use this function to push all the message to the buffer |
| 68 | +static int app_msg_deal(void *msg, int len) |
| 69 | +{ |
| 70 | + int ret = -1; |
| 71 | + |
| 72 | + if(len <= cn_app_rcv_buf_len) |
| 73 | + { |
| 74 | + memcpy(s_rcv_buffer,msg,len); |
| 75 | + s_rcv_datalen = len; |
| 76 | + |
| 77 | + osal_semp_post(s_rcv_sync); |
| 78 | + |
| 79 | + ret = 0; |
| 80 | + |
| 81 | + } |
| 82 | + return ret; |
| 83 | +} |
| 84 | + |
| 85 | +int coap_report_task(void *args) |
| 86 | +{ |
| 87 | + int* handle = NULL; |
| 88 | + oc_config_param_t oc_param; |
| 89 | + char Temp[10] = {00, 22}; |
| 90 | + oc_coap_imp_init(); |
| 91 | + |
| 92 | + memset(&oc_param,0,sizeof(oc_param)); |
| 93 | + |
| 94 | + oc_param.app_server.address = cn_app_server; |
| 95 | + oc_param.app_server.port = cn_app_port; |
| 96 | + oc_param.app_server.ep_id = cn_endpoint_id; |
| 97 | + oc_param.boot_mode = en_oc_boot_strap_mode_factory; |
| 98 | + oc_param.rcv_func = app_msg_deal; |
| 99 | + |
| 100 | + handle = oc_coap_config(&oc_param); |
| 101 | + if (NULL == handle) |
| 102 | + { |
| 103 | + printf("config_init fail!\r\n"); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + printf("start send message to CDP server!\r\n"); |
| 108 | + } |
| 109 | + while(1) |
| 110 | + { |
| 111 | + memset(DHT11_str, 0, 6); |
| 112 | + sprintf(Temp,"%02d",(int)dht11_data.temperature); |
| 113 | + oc_coap_report(handle, Temp, 2); |
| 114 | + osal_task_sleep(10*1000); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +int Read_DHT11_task(void *args) |
| 119 | +{ |
| 120 | + |
| 121 | + while(1) |
| 122 | + { |
| 123 | + DHT11_Read_TempAndHumidity(&dht11_data);//read DHT11 |
| 124 | + /* oled display */ |
| 125 | + OLED_Clear(); |
| 126 | + OLED_ShowString(9, 0, "DHT11 Data", 10); |
| 127 | + memset(DHT11_str, 0, 6); |
| 128 | + sprintf(DHT11_str,"temp %5.2f",dht11_data.temperature); |
| 129 | + OLED_ShowString(9, 2, DHT11_str, 5); |
| 130 | + memset(DHT11_str, 0, 6); |
| 131 | + sprintf(DHT11_str,"humid %5.2f",dht11_data.humidity); |
| 132 | + OLED_ShowString(9, 4, DHT11_str, 5); |
| 133 | + |
| 134 | + osal_task_sleep(5*1000); |
| 135 | + |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +int standard_app_demo_main() |
| 140 | +{ |
| 141 | + osal_task_create("coap_report",coap_report_task, NULL, 0x1000, NULL, 3); |
| 142 | + osal_task_create("Read_DHT11",Read_DHT11_task, NULL, 0x500, NULL, 3); |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
0 commit comments