haas-python-mq3 0.0.7
This is mq3 driver for HaaS Python. HaaS Python is forked from micropython.
License: Apache License, Version 2.0


## HaaS Python介绍
HaaS Python是阿里云IoT团队最新研发的一套低代码编程框架，兼容MicroPython编程规范，依托HaaS平台软硬件积木提供AI、支付、蓝牙配网、云连接、UI等物联网场景常用的能力，从而解决了物联网应用开发难的问题。有了Python轻应用框架，物联网编程不再局限于专业软件开发人员，一般的技术员也可以快速实现复杂的物联网需求。
更多HaaS Python介绍和开发资料见[HaaS Python官网](https://haas.iot.aliyun.com/haasapi/index.html?spm=a2cpu.b16145223.0.0.595660b1dZDX71#/?from=corp),[创意案例](https://haas.iot.aliyun.com/solution?from=crop), [硬件积木](https://haas.iot.aliyun.com/solution/hardware?from=corp)

HaaS Python is a set of low-code programming frameworks newly developed by Alibaba Cloud IoT team. It is compatible with MicroPython programming specifications. It relies on the hardware and software building blocks of the HaaS platform to provide capabilities commonly used in IoT scenarios such as AI, payment, Bluetooth network configuration, cloud connection, and UI. Solve the difficult problem of IoT application development. With the Python light application framework, IoT programming is no longer limited to professional software developers, and general technicians can quickly implement complex IoT requirements.
For more HaaS Python introduction and development materials, see [HaaS Python official website](https://haas.iot.aliyun.com/haasapi/index.html?spm=a2cpu.b16145223.0.0.595660b1dZDX71#/?from=corp), [Creative Case](https://haas.iot.aliyun.com/solution?from=crop), [Hardware Building Blocks](https://haas.iot.aliyun.com/solution/hardware?from=corp)

## 安装方法
* 在[HaaS Python官网](https://haas.iot.aliyun.com/haasapi/index.html?spm=a2cpu.b50127119.0.0.4e1260b1ebMIf1#/)下载开发板对应的固件，并完成固件烧录

Download the firmware corresponding to the development board at [HaaS Python website](https://haas.iot.aliyun.com/haasapi/index.html?spm=a2cpu.b50127119.0.0.4e1260b1ebMIf1#/) and complete the firmware burning

* 在开发板上运行以下命令完成安装和使用,如果您在使用过程中遇到问题，欢迎在[github上向我们提交issue](https://github.com/alibaba/AliOS-Things/issues)，我们的工程师会及时解答

Run the following commands on the development board to complete the installation and use. If you encounter problems during use, please submit an issue to us on [github](https://github.com/alibaba/AliOS-Things/issues), Our engineers will answer in time
```
import upip
upip.install("haas-python-mq3")
import haas-python-mq3
```
# 乙醇传感器 - MQ3

## 一、产品简介
MQ3是汉威电子出品的乙醇传感器，它有快速相应、高敏感度、稳定、寿命长以及外围电路简单等多个优点，常用于酒精监测、呼吸分析仪等场景。

<div align="center">
<img src=./../../docs/images/ext_mq3_实物.png  width=50%/>
</div>

### 引脚定义
* VCC: 3.3V
* A0:  host ADC
* D0:  host GPIO
* GND: GND

## 二、技术参数
* 预热时间:不少于48小时
* 监测乙醇浓度范围:0.05mg/L - 10mg/L
* 使用温度范围: -10 - 50摄氏度

## 三、软件接口

MQ3乙醇传感器HaaS Python驱动：[下载地址](https://github.com/alibaba/AliOS-Things/tree/master/haas_lib_bundles/python/libraries/mq3)
<br>

### MQ3(adcObj) - 创建MQ3驱动对象
* 函数原型:
> mq3Obj = MQ3(adcObj)

* 参数说明:

|参数|类型|必选参数？|说明|
|-----|----|:---:|----|
|adcObj|ADC|是|调用此函数前需确保adcObj对象已经处于open状态|
<br>

* 返回值:
创建对象成功，返回MQ3对象；MQ3对象创建失败，抛出Exception

* 示例代码:

```python
from mq3 import *                        # MQ3温湿度传感器驱动库

# 初始化ADC类型的设备
adc = ADC()
adc.open("mq3")
mq3Dev = MQ3(adc)
```

* 输出:
无

### getVoltage - 测量乙醇浓度电压值

* 函数功能:
量测乙醇并返回测量结果

* 函数原型:
> MQ3.getVoltage()

* 参数说明:
无

* 返回值:
成功返回温度测量结果（数据类型：浮点型；单位：mV）；失败抛出Exception

* 示例:

```python
from mq3 import *                    # MQ3温湿度传感器驱动库
adc = ADC()
adc.open("mq3")
mq3Dev = MQ3(adc)
while 1:
    voltage = mq3Dev.getVoltage()
    print("voltage:%d" % voltage)
    sleep_ms(500)
```

* 输出:
```log
voltage: 800.0
```

## 四、接口案例
此使用实例在board.json中定义了名为mq3的ADC类型的对象。在Python脚本中周期性的获取温湿度测量结果并打印在日志中。

* 代码:
```json
# board.json配置：
{
    "name": "board-name",
    "version": "1.0.0",
    "io": {
        "mq3": {
            "type": "ADC",
            "port": 7,
            "sampling": 12000000
        }
    },
    "debugLevel": "ERROR",
    "repl": "disable"
}
```

```python
# Python代码
import utime                         # 延时API所在组件
from mq3 import *
print("Testing mq3  ...")
adc = ADC()
adc.open("mq3")
mq3Dev = MQ3(adc)
while 1:
    voltage = mq3Dev.getVoltage()
    print("voltage:%d" % voltage)
    sleep_ms(500)
adc.close()
del mq3Dev
print("Test mq3 done!")
```

* 输出：
```log
...
Testing mq3  ...
voltage: 500
...

```

<br>

## 五、通信协议
主控芯片通过ADC读取MQ3传感器的乙醇数据。

## 六、工作原理
mq-3 需要预热， 从下面对结构图中可以看到它周围有两层叫做防爆网的钢罩，这样可以确保当我们测量乙醇浓度的时候传感器不会发生爆炸。
<div align="center">
<img src=./../../docs/images/ext_mq3_structure.png  width=50%/>
</div>

从下图可以看到mq-3内部集成的乙醇传感器对乙醇最敏感，并且随着乙醇浓度的升高，乙醇传感器的电阻会变小。
<div align="center">
<img src=./../../docs/images/ext_mq3_工作原理.png  width=50%/>
</div>

## 参考文献及购买链接
[1] [MQ3乙醇传感器](https://components101.com/sites/default/files/component_datasheet/MQ-3%20Sensor%20Datasheet%20In%20Detail.pdf)<br>
[2] [商品链接]()