10a7ce71fSopenharmony_ci# HiSpark WiFi-IoT 鸿蒙套件样例开发--I2C(i2c_aht20)
20a7ce71fSopenharmony_ci
30a7ce71fSopenharmony_ci![hihope_illustration](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/hihope_illustration.png)
40a7ce71fSopenharmony_ci
50a7ce71fSopenharmony_ci[HiSpark WiFi-IoT鸿蒙开发套件](https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w5003-23341819265.1.bf644a82Da9PZK&id=622343426064&scene=taobao_shop) 首发于HDC 2020,是首批支持HarmonyOS 2.0的开发套件,亦是鸿蒙官方推荐套件,由润和软件HiHope量身打造,已在鸿蒙社区和广大鸿蒙开发者中得到广泛应用。
60a7ce71fSopenharmony_ci
70a7ce71fSopenharmony_ci![wifi_iot](https://gitee.com/hihopeorg/hispark-hm-pegasus/raw/master/docs/figures/wifi_iot.png)
80a7ce71fSopenharmony_ci
90a7ce71fSopenharmony_ci## 一、I2C API
100a7ce71fSopenharmony_ci
110a7ce71fSopenharmony_ci| API名称                                                      | 说明                            |
120a7ce71fSopenharmony_ci| ------------------------------------------------------------ | ------------------------------- |
130a7ce71fSopenharmony_ci| I2cInit (WifiIotI2cIdx id, unsigned int baudrate)            | 用指定的波特速率初始化I2C设备   |
140a7ce71fSopenharmony_ci| I2cDeinit (WifiIotI2cIdx id)                                 | 取消初始化I2C设备               |
150a7ce71fSopenharmony_ci| I2cWrite (WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData) | 将数据写入I2C设备               |
160a7ce71fSopenharmony_ci| I2cRead (WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData) | 从I2C设备中读取数据             |
170a7ce71fSopenharmony_ci| I2cWriteread (WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData) | 向I2C设备发送数据并接收数据响应 |
180a7ce71fSopenharmony_ci| I2cRegisterResetBusFunc (WifiIotI2cIdx id, WifiIotI2cFunc pfn) | 注册I2C设备回调                 |
190a7ce71fSopenharmony_ci| I2cSetBaudrate (WifiIotI2cIdx id, unsigned int baudrate)     | 设置I2C设备的波特率             |
200a7ce71fSopenharmony_ci
210a7ce71fSopenharmony_ci## 二、Aht20 API
220a7ce71fSopenharmony_ci
230a7ce71fSopenharmony_ci本样例提供了一个鸿蒙AHT20数字温湿度传感器驱动库,其功能如下:
240a7ce71fSopenharmony_ci
250a7ce71fSopenharmony_ci* 使用Harmony OS的IoT硬件接口;
260a7ce71fSopenharmony_ci* 接口简洁易于使用、易于移植;
270a7ce71fSopenharmony_ci* 内置了测试程序,可直接进行测试;
280a7ce71fSopenharmony_ci
290a7ce71fSopenharmony_ci| API名称                                                   | 说明                    |
300a7ce71fSopenharmony_ci| --------------------------------------------------------- | ----------------------- |
310a7ce71fSopenharmony_ci| uint32_t AHT20_Calibrate(void)                            | 校准,成功返回0         |
320a7ce71fSopenharmony_ci| uint32_t AHT20_StartMeasure(void)                         | 触发测量,成功返回0     |
330a7ce71fSopenharmony_ci| uint32_t AHT20_GetMeasureResult(float* temp, float* humi) | 读取测量结果,成功返回0 |
340a7ce71fSopenharmony_ci
350a7ce71fSopenharmony_ci## 三、如何编译
360a7ce71fSopenharmony_ci
370a7ce71fSopenharmony_ci1. 将此目录复制到openharmony源码的根目录下
380a7ce71fSopenharmony_ci
390a7ce71fSopenharmony_ci2. 修改openharmony源码的`build/lite/product/wifiiot.json`文件:
400a7ce71fSopenharmony_ci
410a7ce71fSopenharmony_ci   将`//applications/sample/wifi-iot/app`替换为`//10_i2caht20:app`保存;
420a7ce71fSopenharmony_ci
430a7ce71fSopenharmony_ci3. 在openharmony源码目录下执行:`python build.py wifiiot`
440a7ce71fSopenharmony_ci
450a7ce71fSopenharmony_ci## 四、编译错误解决
460a7ce71fSopenharmony_ci
470a7ce71fSopenharmony_ci本项目代码使用了鸿蒙IoT硬件子系统的I2C API接口,需要连接到hi3861的I2C相关接口;默认情况下,Hi3861的I2C编译配置没有打开,编译时会有如下错误:
480a7ce71fSopenharmony_ci
490a7ce71fSopenharmony_ci```txt
500a7ce71fSopenharmony_ciriscv32-unknown-elf-ld: ohos/libs/libhal_iothardware.a(hal_wifiiot_i2c.o): in function `.L0 ':
510a7ce71fSopenharmony_cihal_wifiiot_i2c.c:(.text.HalI2cWrite+0x12): undefined reference to `hi_i2c_write'
520a7ce71fSopenharmony_ciriscv32-unknown-elf-ld: hal_wifiiot_i2c.c:(.text.HalI2cInit+0x12): undefined reference to `hi_i2c_init'
530a7ce71fSopenharmony_ciscons: *** [output/bin/Hi3861_wifiiot_app.out] Error 1
540a7ce71fSopenharmony_ciBUILD FAILED!!!!
550a7ce71fSopenharmony_ci```
560a7ce71fSopenharmony_ci
570a7ce71fSopenharmony_ci**解决方法**
580a7ce71fSopenharmony_ci
590a7ce71fSopenharmony_ci需要修改vendor\hisi\hi3861\hi3861\build\config\usr_config.mk文件:
600a7ce71fSopenharmony_ci`# CONFIG_I2C_SUPPORT is not set`行,修改为:`CONFIG_I2C_SUPPORT=y`
610a7ce71fSopenharmony_ci
620a7ce71fSopenharmony_ci## 五、运行结果
630a7ce71fSopenharmony_ci
640a7ce71fSopenharmony_ci烧录文件后,按下reset按键,程序开始运行,串口工具会输出测量后的温湿度数据
650a7ce71fSopenharmony_ci
660a7ce71fSopenharmony_ci
670a7ce71fSopenharmony_ci
680a7ce71fSopenharmony_ci### 【套件支持】
690a7ce71fSopenharmony_ci
700a7ce71fSopenharmony_ci##### 1. 套件介绍  http://www.hihope.org/pro/pro1.aspx?mtt=8
710a7ce71fSopenharmony_ci
720a7ce71fSopenharmony_ci##### 2. 套件购买  https://item.taobao.com/item.htm?id=622343426064&scene=taobao_shop
730a7ce71fSopenharmony_ci
740a7ce71fSopenharmony_ci##### 3. 技术资料
750a7ce71fSopenharmony_ci
760a7ce71fSopenharmony_ci- Gitee码云网站(OpenHarmony Sample Code等) **https://gitee.com/hihopeorg**
770a7ce71fSopenharmony_ci
780a7ce71fSopenharmony_ci- HiHope官网-资源中心(SDK包、技术文档下载)[**www.hihope.org**](http://www.hihope.org/)
790a7ce71fSopenharmony_ci
800a7ce71fSopenharmony_ci##### 4. 互动交流
810a7ce71fSopenharmony_ci
820a7ce71fSopenharmony_ci- 润和HiHope鸿蒙技术交流-微信群(加群管理员微信13605188699,发送文字#申请加入润和官方鸿蒙群#,予以邀请入群)
830a7ce71fSopenharmony_ci- HiHope开发者社区-论坛 **https://bbs.elecfans.com/group_1429**
840a7ce71fSopenharmony_ci- 润和HiHope鸿蒙售后服务群(QQ:980599547)
850a7ce71fSopenharmony_ci- 售后服务电话(025-52668590)
860a7ce71fSopenharmony_ci
870a7ce71fSopenharmony_ci
880a7ce71fSopenharmony_ci
89