HarmonyOS小熊派 | HarmonyOS傳感器驅動開發--E53_IA1讀取溫度 、濕度、光照強度

      網友投稿 1357 2022-05-30

      BearPi-HM_Nano開發板傳感器驅動開發——E53_IA1讀取溫度 、濕度、光照強度

      本示例將演示如何在BearPi-HM_Nano開發板上使用E53_IA1讀取溫度 、濕度、光照強度,當溫度 、濕度超標時開啟電機通風,當光照強度過低時,開啟補光燈補光。

      E53_IA1 API分析

      本案例主要使用了以下API完成溫度 、濕度、光照強度讀取

      1、InitE53IA1()

      // 初始化E53_IA1 接口 void InitE53IA1(void);

      描述:

      初始化E53_IA1

      2、ReadDataE53IA1()

      // 從E53_IA1 接口讀取測量到的溫度、濕度及光照強度的數據 void ReadDataE53IA1(void);

      描述:?讀取溫度 、濕度、光照強度

      3、LightStatusSet()

      // 設置燈的狀態 void LightStatusSet(E53_IA1_Status_ENUM status);

      描述:

      控制補光燈開關?參數:

      4、MotorStatusSet()

      // 設置電機的狀態 void MotorStatusSet(E53_IA1_Status_ENUM status);

      描述:

      控制電機開關

      參數:

      HarmonyOS小熊派 | HarmonyOS傳感器驅動開發--E53_IA1讀取溫度 、濕度、光照強度

      硬件設計

      本案例將用到 E53_IA1 智慧農業擴展板與 BearPi-HM_Nano 開發板,其中E53_IA1擴展板接口原理圖如下,溫濕度傳感器sht30和光照強度傳感器BH1750都是通過I2C來驅動,電機和補光燈分別通過GPIO_8和GPIO_7來控制。

      E53_IA1 智慧農業擴展板與 BearPi-HM_Nano 開發板安裝

      軟件設計

      主要代碼分析

      首先調用 InitE53IA1()? 函數初始化E53_IA1所接的引腳的功能,然后循環調用 ReadDataE53IA1() 函數讀取溫度 、濕度、光照強度并通過串口打印出來,當光照強度過低時,開啟補光燈補光,當溫度 、濕度超標時開啟電機通風。

      yuchuan_e53_ia1.c

      #include #include #include #include "ohos_init.h" #include "cmsis_os2.h" #include "e53_ia1.h" #define E53_TASK_CB_SIZE 0U #define E53_TASK_STACK_SIZE 1024 * 4 #define E53_TASK_PRIORITY 24 E53_IA1_Data_TypeDef E53_IA1_Data; /* 處理E53_IA1接口的函數 */ void yuchuanE53IA1Task(void *argument) { (void)argument; //初始化E53_IA1接口 InitE53IA1(); while (1) { /* code */ printf("\r\n====================================================\r\n"); printf("\r\n*************Yuchuan E53_IA1_Task_Example***********\r\n"); printf("\r\n====================================================\r\n"); // 從E53_IA1接口讀取數據 ReadDataE53IA1(); printf("\r\n******************************Lux Value is %.2f\r\n", E53_IA1_Data.Lux); printf("\r\n******************************Humidity is %.2f\r\n", E53_IA1_Data.Humidity); printf("\r\n******************************Temperature is %.2f\r\n", E53_IA1_Data.Temperature); if ((int)E53_IA1_Data.Lux < 20) { // 開燈 LightStatusSet(ON); printf("開燈\n"); } else { // 關燈 LightStatusSet(OFF); printf("關燈\n"); } if (((int)E53_IA1_Data.Humidity > 70) | ((int)E53_IA1_Data.Temperature > 35)) { // 開電機 MotorStatusSet(ON); printf("開電機\n"); } else { // 關電機 MotorStatusSet(OFF); printf("關電機\n"); } usleep(2000000); } } static void YuchuanE53IA1Entry(void) { osThreadAttr_t e53TaskAttr; e53TaskAttr.attr_bits = 0U; e53TaskAttr.cb_mem = NULL; e53TaskAttr.cb_size = E53_TASK_CB_SIZE; e53TaskAttr.stack_mem = NULL; e53TaskAttr.stack_size = E53_TASK_STACK_SIZE; e53TaskAttr.priority = E53_TASK_PRIORITY; e53TaskAttr.name = "YuchuanE53IA1Task"; if (osThreadNew((osThreadFunc_t)yuchuanE53IA1Task, NULL, &e53TaskAttr) == NULL) { /* code */ printf("Falied to Create YuchuanE53IA1Task!!!\n"); } } APP_FEATURE_INIT(YuchuanE53IA1Entry);

      e53_ia1.h

      /* * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __E53_IA1_H__ #define __E53_IA1_H__ /* 寄存器宏定義 --------------------------------------------------------------------*/ #define BH1750_ADDR 0x23 #define SHT30_ADDR 0x44 /*************************************************************** * 名 稱: GasStatus_ENUM * 說 明:枚舉狀態結構體 ***************************************************************/ typedef enum { OFF = 0, ON } E53_IA1_Status_ENUM; /* E53_IA1傳感器數據類型定義 ------------------------------------------------------------*/ typedef struct { // 光照強度 float Lux; // 濕度 float Humidity; // 溫度 float Temperature; } E53_IA1_Data_TypeDef; //存儲共享數據 extern E53_IA1_Data_TypeDef E53_IA1_Data; /* E53_IA1 API */ // 初始化E53_IA1 接口 void InitE53IA1(void); // 從E53_IA1 接口讀取測量到的溫度、濕度及光照強度的數據 void ReadDataE53IA1(void); // 設置燈的狀態 void LightStatusSet(E53_IA1_Status_ENUM status); // 設置電機的狀態 void MotorStatusSet(E53_IA1_Status_ENUM status); #endif /* __E53_IA1_H__ */

      e53_ia1.c

      /* * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include "cmsis_os2.h" #include "e53_ia1.h" #include "iot_gpio.h" #include "iot_gpio_ex.h" #include "iot_errno.h" #include "iot_i2c.h" #include "iot_i2c_ex.h" #define MOTOR_GPIO_8 8 #define FILL_LIGHT_GPIO_7 7 #define IOT_I2C1_SDA_GPIO_0 0 #define IOT_I2C1_SCL_GPIO_1 1 #define IOT_I2C_IDX_1 1 /*************************************************************** * 函數名稱: IoInitE53IA1 * 說 明: E53_IA1_GPIO初始化 * 參 數: 無 * 返 回 值: 無 ***************************************************************/ static void IoInitE53IA1(void) { IoTGpioInit(MOTOR_GPIO_8); IoTGpioSetDir(MOTOR_GPIO_8, IOT_GPIO_DIR_OUT); IoTGpioSetFunc(MOTOR_GPIO_8, IOT_GPIO_FUNC_GPIO_8_GPIO); IoTGpioInit(FILL_LIGHT_GPIO_7); IoTGpioSetDir(FILL_LIGHT_GPIO_7, IOT_GPIO_DIR_OUT); IoTGpioSetFunc(FILL_LIGHT_GPIO_7, IOT_GPIO_FUNC_GPIO_7_GPIO); IoTGpioInit(IOT_I2C1_SDA_GPIO_0); IoTGpioSetDir(IOT_I2C1_SDA_GPIO_0, IOT_GPIO_DIR_OUT); IoTGpioSetFunc(IOT_I2C1_SDA_GPIO_0, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA); IoTGpioInit(IOT_I2C1_SCL_GPIO_1); IoTGpioSetDir(IOT_I2C1_SCL_GPIO_1, IOT_GPIO_DIR_OUT); IoTGpioSetFunc(IOT_I2C1_SCL_GPIO_1, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL); /* baudrate: 400kbps */ IoTI2cInit(IOT_I2C_IDX_1, 400000); } /*************************************************************** * 函數名稱: InitBH1750 * 說 明: 寫命令初始化BH1750光照強度傳感器 * 參 數: 無 * 返 回 值: 無 ***************************************************************/ void InitBH1750(void) { uint8_t send_data[1] = {0x01}; // unsigned int IoTI2cWrite(unsigned int id, unsigned short deviceAddr, const unsigned char *data, unsigned int dataLen); IoTI2cWrite(IOT_I2C_IDX_1, (BH1750_ADDR << 1) | 0x00, send_data, 1); /* uint8_t send_data[1] = {0x01}; IoTI2cWrite(IOT_I2C_IDX_1, (BH1750_Addr << 1) | 0x00, send_data, 1); */ } /*************************************************************** * 函數名稱: InitSHT30 * 說 明: 初始化SHT30溫濕度傳感器,設置測量周期 * 參 數: 無 * 返 回 值: 無 ***************************************************************/ void InitSHT30(void) { uint8_t send_data[2] = {0x22, 0x36}; IoTI2cWrite(IOT_I2C_IDX_1, (SHT30_ADDR << 1) | 0x00, send_data, 2); } /*************************************************************** * 函數名稱: InitE53IA1 * 說 明: 初始化E53_IA1 * 參 數: 無 * 返 回 值: 無 ***************************************************************/ void InitE53IA1(void) { // 初始化E53相關GPIO IoInitE53IA1(); // 初始化BH1750 光照傳感器 InitBH1750(); // 初始化SHT30 溫濕度傳感器 InitSHT30(); } /*************************************************************** * 函數名稱: StartBH1750 * 說 明: 啟動BH1750 采集光照強度的數據 * 參 數: 無 * 返 回 值: 無 ***************************************************************/ void StartBH1750(void) { uint8_t send_data[1] = {0x10}; // unsigned int IoTI2cWrite(unsigned int id, unsigned short deviceAddr, const unsigned char *data, unsigned int dataLen); IoTI2cWrite(IOT_I2C_IDX_1, (BH1750_ADDR << 1) | 0x00, send_data, 1); } /*************************************************************** * 函數名稱: CheckCrcSHT3x * 說 明: 檢查數據正確性 * 參 數: data:讀取到的數據 nbrOfBytes:需要校驗的數量 checksum:讀取到的校對比驗值 * 返 回 值: 校驗結果,0-成功 1-失敗 ***************************************************************/ static uint8_t CheckCrcSHT3x(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum) { uint8_t crc = 0xFF; uint8_t bit = 0; uint8_t byteCtr; const uint16_t POLYNOMIAL = 0x131; //calculates 8-Bit checksum with given polynomial for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) { /* code */ crc ^= (data[byteCtr]); for (bit = 8; bit > 0; --bit) { /* code */ if (crc & 0x80) { /* code */ crc = (crc << 1) ^ POLYNOMIAL; } else { crc = (crc << 1); } } } if (crc != checksum) { /* code */ return 1; } else { /* code */ return 0; } } /*************************************************************** * 函數名稱: CalcTemperatureCSHT3x * 說 明: 溫度計算 * 參 數: u16sT:讀取到的溫度原始數據 * 返 回 值: 計算后的溫度數據 ***************************************************************/ static float CalcTemperatureCSHT3x(uint16_t u16sT) { // variable for result float temperatureC = 0; // clear bits [1..0] (status bits) u16sT &= ~0x0003; //-- calculate temperature [℃] -- //T = -45 + 175 * rawValue / (2^16-1) temperatureC = (175 * (float)u16sT / 65535 - 45); return temperatureC; } /*************************************************************** * 函數名稱: CalcRHSHT3x * 說 明: 濕度計算 * 參 數: u16sRH:讀取到的濕度原始數據 * 返 回 值: 計算后的濕度數據 ***************************************************************/ static float CalcRHSHT3x(uint16_t u16sRH) { // variable for result float humidityRH = 0; // clear bits [1..0] (status bits) u16sRH &= ~0x0003; //-- calculate relative humidity [%RH] -- // RH = rawValue / (2^16-1) * 10 humidityRH = (100 * (float)u16sRH / 65535); return humidityRH; } /*************************************************************** * 函數名稱: ReadDataE53IA1 * 說 明: 測量光照強度、溫度、濕度 * 參 數: 無 * 返 回 值: 無 ***************************************************************/ void ReadDataE53IA1(void) { uint8_t recv_data[2] = {0}; // 啟動光照傳感器采集光照強度的數據 StartBH1750(); //延時1s usleep(1000000); // 讀取光照強度傳感器的數據 // unsigned int IoTI2cRead(unsigned int id, unsigned short deviceAddr, unsigned char *data, unsigned int dataLen); IoTI2cRead(IOT_I2C_IDX_1, (BH1750_ADDR << 1) | 0x01, recv_data, 2); // 光照強度 E53_IA1_Data.Lux = (float)(((recv_data[0] << 8) + recv_data[1]) / 1.2); //data array for checksum verification uint8_t data[3]; uint16_t dat, temp; //byte 0,1 is temperature byte 4,5 is humidity uint8_t SHT3X_Data_Buffer[6]; IotI2cData sht30_i2c_data = {0}; uint8_t send_data[2] = {0xE0, 0x00}; sht30_i2c_data.sendBuf = send_data; sht30_i2c_data.sendLen = 2; sht30_i2c_data.receiveBuf = SHT3X_Data_Buffer; sht30_i2c_data.receiveLen = 6; //Read bh1750 sensor data // unsigned int IoTI2cWriteread(unsigned int id, unsigned short deviceAddr, const IotI2cData *i2cData); IoTI2cWriteread(IOT_I2C_IDX_1, (SHT30_ADDR << 1) | 0x00, &sht30_i2c_data); // /* check tem */ data[0] = SHT3X_Data_Buffer[0]; data[1] = SHT3X_Data_Buffer[1]; data[2] = SHT3X_Data_Buffer[2]; temp = CheckCrcSHT3x(data, 2, data[2]); if (!temp) { /* code */ dat = ((uint16_t)data[0] << 8) | data[1]; // 溫度 E53_IA1_Data.Temperature = CalcTemperatureCSHT3x(dat); } // /* check humidity 檢查濕度*/ data[0] = SHT3X_Data_Buffer[3]; data[1] = SHT3X_Data_Buffer[4]; data[2] = SHT3X_Data_Buffer[5]; temp = CheckCrcSHT3x(data, 2, data[2]); /* value is ture */ if (!temp) { /* code */ dat = ((uint16_t)data[0] << 8) | data[1]; E53_IA1_Data.Humidity = CalcRHSHT3x(dat); } } /*************************************************************** * 函數名稱: LightStatusSet * 說 明: 燈狀態設置 * 參 數: status,ENUM枚舉的數據 * OFF,關 * ON,開 * 返 回 值: 無 ***************************************************************/ void LightStatusSet(E53_IA1_Status_ENUM status) { if (status == ON) { // 設置GPIO_14 為高電平點亮燈 IoTGpioSetOutputVal(FILL_LIGHT_GPIO_7, IOT_GPIO_VALUE1); } if (status == OFF) { // 設置GPIO_14 為低電平熄滅燈 IoTGpioSetOutputVal(FILL_LIGHT_GPIO_7, IOT_GPIO_VALUE0); } } /*************************************************************** * 函數名稱: MotorStatusSet * 說 明: 電機狀態設置 * 參 數: status,ENUM枚舉的數據 * OFF,關 * ON,開 * 返 回 值: 無 ***************************************************************/ void MotorStatusSet(E53_IA1_Status_ENUM status) { if (status == ON) { // 設置GPIO_8 為高電平輸出打開電機 IoTGpioSetOutputVal(MOTOR_GPIO_8, IOT_GPIO_VALUE1); } if (status == OFF) { // 設置GPIO_8 為低電平輸出關閉電機 IoTGpioSetOutputVal(MOTOR_GPIO_8, IOT_GPIO_VALUE0); } }

      編譯調試

      //applications/sample/BearPi/BearPi-HM_Nano/sample/C2_YuchuanE53IA1

      static_library("yuchuanE53IA1"){ sources = [ "scr/e53_ia1.c", "yuchuan_e53_ia1.c", ] include_dirs = [ "http://utils/native/lite/include", "http://kernel/liteos_m/kal/cmsis", "http://base/iot_hardware/peripheral/interfaces/kits", "include", ] }

      修改 BUILD.gn 文件

      修改applications\BearPi\BearPi-HM_Nano\sample路徑下 BUILD.gn 文件,指定 yuchuanE53IA1?參與編譯。

      # Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import("http://build/lite/config/component/lite_component.gni") lite_component("sample") { features = [ #"A1_kernal_thread:thread_example", #"A2_kernel_timer:timer_example", #"A3_kernel_event:event_example", #"A4_kernel_mutex:mutex_example", #"A5_kernel_semaphore:semaphore_example", #"A6_kernel_message:message_example", #"B1_basic_led_blink:led_example", #"B2_basic_button:button_example", #"B3_basic_pwm_led:pwm_example", #"B4_basic_adc:adc_example", #"B5_basic_i2c_nfc:i2c_example", #"B6_basic_uart:uart_example", #"C1_e53_sf1_mq2:e53_sf1_example", #"C2_e53_ia1_temp_humi_pls:e53_ia1_example", #"C3_e53_sc1_pls:e53_sc1_example", #"C4_e53_sc2_axis:e53_sc2_example", #"C5_e53_is1_infrared:e53_is1_example", #"D1_iot_wifi_ap:wifi_ap", #"D2_iot_wifi_sta_connect:wifi_sta_connect", #"D3_iot_udp_client:udp_client", #"D4_iot_tcp_server:tcp_server", #"D5_iot_mqtt:iot_mqtt", #"D6_iot_cloud_oc:oc_mqtt", #"D7_iot_cloud_onenet:onenet_mqtt", #"D8_iot_cloud_oc_smoke:cloud_oc_smoke", #"D9_iot_cloud_oc_light:cloud_oc_light", #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover", #"D11_iot_cloud_oc_infrared:cloud_oc_infrared", #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture", #"D13_iot_cloud_oc_gps:cloud_oc_gps", #"B1_YuchuanBasicLEDBlink:yuchuanLED", #"B2_YuchuanBasicButton:yuchuanButton", #"C1_YuchuanE53_SF1_MQ2:YuchuanE53SF1Mq2", "C2_YuchuanE53IA1:yuchuanE53IA1", ] }

      運行結果

      示例代碼編譯燒錄代碼后,按下開發板的RESET按鍵,通過串口助手查看日志,會打印溫濕度及光照強度信息。用手遮住擴展板,補光燈會自動開啟,控制溫度或者濕度超標,電機會自動開啟。

      ready to OS start sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00 FileSystem mount ok. wifi init success! hiview init success.00 00:00:00 0 196 D 0/HIVIEW: log limit init success. 00 00:00:00 0 196 I 1/SAMGR: Bootstrap core services(count:3). 00 00:00:00 0 196 I 1/SAMGR: Init servi ==================================================== *************Yuchuan E53_IA1_Task_Example*********** ==================================================== ce:0x4b0ea8 TaskPool:0xe4b7c 00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b0eb4 TaskPool:0xe4b9c 00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b1380 TaskPool:0xe4bbc 00 00:00:00 0 228 I 1/SAMGR: Init service 0x4b0eb4 success! 00 00:00:00 0 128 I 1/SAMGR: Init service 0x4b0ea8 success! 00 00:00:00 0 72 I 1/SAMGR: Init service 0x4b1380 success! 00 00:00:00 0 72 I 1/SAMGR: Initialized all core system services! 00 00:00:00 0 128 I 1/SAMGR: Bootstrap system and application services(count:0). 00 00:00:00 0 128 I 1/SAMGR: Initialized all system and application services! 00 00:00:00 0 128 I 1/SAMGR: Bootstrap dynamic registered services(count:0). ******************************Lux Value is 330.83 ******************************Humidity is 0.00 ******************************Temperature is 0.00 關燈 關電機 ==================================================== *************Yuchuan E53_IA1_Task_Example*********** ==================================================== ******************************Lux Value is 329.17 ******************************Humidity is 0.00 ******************************Temperature is 0.00 關燈 關電機

      C++ IoT 單片機 硬件開發

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:【沃土方案--金融】銀豐新融-反洗錢自主監測系統(一)
      下一篇:Excel2007數據如何設置只讀和修改密碼
      相關文章
      亚洲hairy多毛pics大全| 亚洲成av人片不卡无码久久| 亚洲高清国产拍精品青青草原 | 亚洲另类春色校园小说| 亚洲欧洲国产精品你懂的| 曰韩亚洲av人人夜夜澡人人爽| 亚洲国产精品人人做人人爱| 在线观看亚洲专区| 婷婷亚洲综合一区二区| 亚洲av纯肉无码精品动漫| 亚洲日本在线电影| 亚洲日韩精品无码专区| 亚洲av乱码一区二区三区按摩| 亚洲狠狠婷婷综合久久| 亚洲av综合av一区二区三区| 色噜噜噜噜亚洲第一| 亚洲av日韩av欧v在线天堂| 亚洲国产成人AV网站| 亚洲成A人片在线观看无码3D| 亚洲Av无码乱码在线znlu| jlzzjlzz亚洲乱熟在线播放| 亚洲中文字幕无码日韩| 日韩亚洲欧洲在线com91tv| 亚洲AV区无码字幕中文色| 亚洲AV无码久久精品色欲| 亚洲日本va午夜中文字幕一区| 67pao强力打造67194在线午夜亚洲| 久久精品国产亚洲av水果派| 亚洲第一页在线视频| tom影院亚洲国产一区二区| 国产成人亚洲综合一区| 国产精品亚洲av色欲三区| 亚洲成a人无码av波多野按摩| 久久久久噜噜噜亚洲熟女综合| 亚洲女久久久噜噜噜熟女| 亚洲欧洲国产精品你懂的| 亚洲午夜电影一区二区三区| 亚洲中文无码mv| 亚洲成av人片不卡无码久久| 亚洲啪啪综合AV一区| 91亚洲精品视频|