1 Star 0 Fork 2

BayMaxLee / ART-PI-HCHO

forked from 归心 上路 / ART-PI-HCHO 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

基于ART-PI的智能甲醛检测仪

基于 RT-Thread 的智能甲醛检测仪设计

前言

本文档用于参加 RT-Thread 举办的全连接大赛。文档中对利用 ART-PI 设计的智能甲醛检测仪进行说明。

硬件介绍

  • ART-PI
  • WZ-S-K 达特甲醛传感器
  • DHT11 温湿度传感器
  • ALIENTEK 4.3 RGBLCD

硬件接口使用说明

art-pi-sensor

art-pi 甲醛传感器扩展板

笔者为 ART-PI 设计了一款兼容达特甲醛传感器的扩展板,EDA工具使用的是立创EDA,工程较为简单,当然也会开源给各位开发者使用,扩展板工程链接

sensor-sch sensor-pcb

硬件成品

hcho-sensor-1 hcho-sensor-2 hcho-sensor-3 art-pi-hcho

ART-PI 介绍

WZ-S-K 达特甲醛传感器介绍

WZ-S-K 型甲醛检测传感器模组是英国达特公司设计的一款基于电化学的甲醛检测传感器,具有测量精度高、响应速度快、使用寿命长、功耗低等特点。该传感器能够直接将周围环境中的甲醛含量转换为浓度值,通过串口标准化输出,这款甲醛检测传感器能够非常方便的集成到产品中去,适用于智能家居、便携式测量仪表等产品。

ALIENTEK 4.3 RGBLCD 介绍

ART-PI 开发板上面提供了一个 LCD 接口,该接口支持正点原子的 LCD 屏幕,rt-thread 官方也提供了 LCD驱动支持包。手头上刚好有这种 LCD 屏幕,这次准备在 ART-PI 上开发一个 GUI 界面,用于显示测量的甲醛浓度值等信息。

软件设计

本项目所有代码均使用 RT-Thread Studio 编写,经过一段时间的熟悉学习,发现使用 RT-Thread Studio 开展基于 RT-Thread 系统开发工作还是非常方便的,推荐各位开发者熟悉使用。

软件框架

RT-Thread 软件包及硬件驱动使用说明

能够使用 ART-PI 快速开发原型机的原因主要是基于RT-Thread 开放的生态资源,由于其本身开放的特性,我们可以在RT-Thread 软件包中快速找到适合我们硬件的相关软件包,并无缝适配 ART-PI,对于物联网开发者而言,我们只需要关心我们自己要实现的产品功能即可。笔者认为这也许成为了目前国内做大的物联网开放生态,希望我们共同维护,加油RT-Thread。

在本项目种,主要使用到了RT-Thread 种以下几个软件包和硬件驱动模块。

  • 硬件驱动模块
    • LCD Module
    • UART Module
    • WIFI Module
  • 软件模块
    • lwip
    • finsh

软件模块设计

显示模块

ART-PI 提供有基于正点原子的 LCD 驱动支持包,可以直接在 RT-Thread Studio 中对应的工程中勾选 LCD 外设驱动让工程支持 LCD 。

enable-lcd 该 LCD 驱动支持包仅仅提供了用于测试 LCD 相关的刷屏函数,直接使用该驱动还不能进行简单的 GUI 绘制,因此我们需要基于该驱动来编写相关的 GUI 图形绘制接口,这里笔者分享一下自己编写的绘图函数接口。

传感器数据读取

WZ-S-K 传感器使用的是串行通信方式来完成数据输出,串口配置参数为 9600-8-1-N ,即波特率为 9600、数据位8位、停止位1位、无校验位。该传感器支持两种通信模式,分别位主动上传模式和问答模式,传感器默认为主动上传模式,每间隔1秒上传一次数据。在主动上传模式下,通信协议如下。

|0|1|2|3|4|5|6|7|8| |----|----| |起始位|气体名称|单位|小数位数(无)|气体浓度(高位)|气体浓度(低位)|满量程(高位)|满量程(低位)|校验值| |0xFF|HCHO=0X17|Ppb=0x04|0x00|0x00|0x25|0x07|0xD0|0x25|

该通信协议中的校验值采用的是和校验,校验计算相关函数如下。

/*************************************************************************
*函数名: unsigned char FucCheckSum(uchar *i,ucharln)
*功能描述:求和校验(取发送、接收协议的 1\2\3\4\5\6\7 的和取反+1)
*函数说明:将组数的元素 1-倒数第二个元素相加后取反+1(元素个数必须大于 2)
*************************************************************************/
unsigned char FucCheckSum(unsigned char *i, unsigned char ln)
{
unsigned char j, tempq=0;
i+=1;
for(j=0; j<(ln-2); j++)
{
tempq+=*i;
i++;
}
tempq=(~tempq)+1;
return(tempq);
}

根据该通信协议,可以将气体的浓度值输出出来,气体浓度值的计算方法如下。

气体浓度值= 气体浓度高位*256+气体浓度低位

气体浓度的单位为 ppb

举个例子,上述通信协议中传输的气体浓度值为 0*256+0x25 = 37(十进制) ppb

这里说明一下气体浓度单位相关换算,气体中 ppb 、ppm 和 ppt 的单位换算如下。

1ppm = 1000 ppb 1ppb = 1000 ppt

ppm 为 10 的 -6 次方 ppb 为 10 的 -9 次方 ppt 为 10 的 -12 次方

那么 ppb 和 mg/m3 之间该如何换算呢?经过和供应商沟通,他们提供了如下的换算关系。

1000 ppb 约等于 1230 ug/m3

1ppm = 1000 ppb 因此,1ppm 约为 1230 ug/m3

根据测得的甲醛浓度值,通过分析,我们可以对当前环境进行一个甲醛含量的简单评估,国家标准中《居室空气中甲醛的卫生标准》所规定的居室空气中甲醛的最高容许浓度为0.08毫克/立方米。

下面开始编写代码,利用 ART-PI 上的 UART1 连接 WZ-S-K 甲醛传感器,获取甲醛浓度值。

首先在 RT-Thread Settings 中使能 UART1。

enable-uart1

然后,参考 RT-Thread 的官方文档中心中UART设备-串口设备使用示例,来编写甲醛传感器的串口读写程序,这里使用的是中断接收及轮询发送方式

注意这里使用线程间通信中的消息队列的方式,将读取到的甲醛浓度值传递给LCD显示的app,中,用来显示甲醛浓度。

RT-Thread 软件包中提供了 DHT11 温湿度传感器相关驱动,可以直接再软件包中添加使用。

甲醛浓度信息上传 onenet 物联网平台

onenet 物联网平台配置

前提是要在 onenet 上面注册一个账号,然后创建自己的产品。

onenet 现在的功能已经非常丰富了,之前在这个平台上开发物联网产品时,还比较简单,下面用截图说明下如何在 onenet上面创建一个产品并添加一台设备。

进入控制台,找到全部产品服务中的多协议接入,点击进入。

onenet-mqtt-protocol-add

在多协议接入中找到 MQTT(旧版),添加自己的产品。

onenet-add-product

添加完产品之后,进入产品详情,需要关注两个产品相关的信息,一个是产品 ID ,另一个是 Master-APIkey,将这两个信息记录下来。

onenet-product-info

进入设备列表中,添加设备,注意在添加设备的时候,要自定义该设备的鉴权信息,这个鉴权信息后面也有用到,需要设置并记录下来。

onenet-add-device

添加完成设备之后,进入设备的详细信息页面,添加设备对应的 APIKey

onenet-add-device-info

至此,在onenet平台上已经创建完成一个支持mqtt协议接入的设备,需要记录产品和设备对应的信息有:

  • 产品 ID
  • Master-APIkey
  • 设备 ID
  • 鉴权信息
  • 设备APIKey
art-pi 工程配置

进入 RT-Thread Settings 中,为工程添加 onenet 的软件包。可以直接点击 Add 按钮,然后搜索 onenet 即可。

onenet-packet

添加完成之后,右键点击 onenet 软件包,进入详细配置界面,这里就要对 onenet 平台上面设备相关的信息进行配置了,使用的就是刚刚在新建产品及设备的时候记录的产品及设备信息,这些信息将会被保存在rtconfig.h中.按照自己添加的产品及设备信息填写完成之后,保存工程。

onenet-device-info

数据上传线程代码编写
API 说明
int onenet_mqtt_init(void);

OneNET 初始化函数,需要在使用 OneNET 功能前调用。

参数 描述
返回 --
0 成功
-1 获得设备信息失败
-2 mqtt 客户端初始化失败

mqtt 上传数字到 OneNET

rt_err_t onenet_mqtt_upload_digit(const char *ds_name, const double digit);

利用 mqtt 向 OneNET 平台发送数字数据。

参数 描述
ds_name 数据流名称
digit 要上传的数字
返回 --
0 上传成功
-5 内存不足

onenet 软件包中提供有mqtt协议上传数字mqtt协议上传字符串mqtt协议上传二进制文件 等接口,这是使用上传数字接口即可。

效果展示

art-pi-show.jpg

onenet-hcho-data

优化

后续可以根据需要对 LCD 部分的 GUI 进行优化,也可以利用onenet平台和其他的物联网设备互联,利用甲醛浓度数据来自动调整空气净化器或者新风机的运行。

参考文档

  • 《WZ-S-K 型甲醛检测模组使用说明书》
  • 《正点原子RGB-LCD 驱动》
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] 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.

简介

基于 ART-PI 开发板的智能甲醛检测仪 RT-Thread X STM32 全连接大赛 展开 收起
C
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/baymaxlee/art-pi-hcho.git
git@gitee.com:baymaxlee/art-pi-hcho.git
baymaxlee
art-pi-hcho
ART-PI-HCHO
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891