Grove Air Quality Sensor.

Fork of Grove_Air_Quality_Sensor_Library by Seeed

Committer:
lucianovici
Date:
Tue Dec 27 13:47:39 2016 +0200
Revision:
7:38425a51906f
Parent:
4:0ca4a9fd1b5d
Make heating delay configurable

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:885417624ec2 1 /*
mbedAustin 0:885417624ec2 2 AirQuality library v1.0
mbedAustin 0:885417624ec2 3 2010 Copyright (c) Seeed Technology Inc. All right reserved.
mbedAustin 0:885417624ec2 4
mbedAustin 0:885417624ec2 5 Original Author: Bruce.Qin
mbedAustin 0:885417624ec2 6
mbedAustin 0:885417624ec2 7 This library is free software; you can redistribute it and/or
mbedAustin 0:885417624ec2 8 modify it under the terms of the GNU Lesser General Public
mbedAustin 0:885417624ec2 9 License as published by the Free Software Foundation; either
mbedAustin 0:885417624ec2 10 version 2.1 of the License, or (at your option) any later version.
mbedAustin 0:885417624ec2 11
mbedAustin 0:885417624ec2 12 This library is distributed in the hope that it will be useful,
mbedAustin 0:885417624ec2 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
mbedAustin 0:885417624ec2 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
mbedAustin 0:885417624ec2 15 Lesser General Public License for more details.
mbedAustin 0:885417624ec2 16
mbedAustin 0:885417624ec2 17 You should have received a copy of the GNU Lesser General Public
mbedAustin 0:885417624ec2 18 License along with this library; if not, write to the Free Software
mbedAustin 0:885417624ec2 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
mbedAustin 0:885417624ec2 20 */
lucian@192-168-0-106.rdsnet.ro 2:95009177a860 21 #include "Air_Quality.h"
mbedAustin 0:885417624ec2 22
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 23 AirQuality::AirQuality() {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 24 _s = 0;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 25 _sum_vol = 0;
lucianovici 7:38425a51906f 26 _heating_delay_s = 20;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 27 }
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 28
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 29 void AirQuality::set_calc_avg_volt_period(uint16_t seconds) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 30 _calc_avg_volt_period_s = seconds;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 31 }
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 32
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 33 void AirQuality::set_sampling_period(uint16_t seconds) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 34 _sampling_period_s = seconds;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 35 }
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 36
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 37 void AirQuality::calcAvgVoltageFor(uint16_t seconds) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 38 uint16_t s = (uint16_t) (seconds / _sampling_period_s);
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 39 if (_s == s) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 40 standard_vol = _sum_vol / s;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 41 _sum_vol = 0;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 42 _s = 0;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 43 debug_if(DEBUG_AIR_QUALITY, "Vol standard in %d seconds: %d\r\n", seconds, standard_vol);
mbedAustin 0:885417624ec2 44 } else {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 45 _sum_vol += first_vol;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 46 _s++;
mbedAustin 0:885417624ec2 47 }
mbedAustin 0:885417624ec2 48 }
mbedAustin 0:885417624ec2 49
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 50 void AirQuality::init(PinName pin) {
lucian@192-168-0-106.rdsnet.ro 2:95009177a860 51 _pin = pin;
mbedAustin 0:885417624ec2 52 AnalogIn sensor(_pin);
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 53 unsigned char minutes = 0;
lucianovici 7:38425a51906f 54 debug_if(DEBUG_AIR_QUALITY, "Air Quality Sensor Starting Up... (%d s)", _heating_delay_s);
lucianovici 7:38425a51906f 55 wait(_heating_delay_s);
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 56 init_voltage = (uint16_t) (sensor.read() * 1000); // boost the value to be on a 0 -> 1000 scale for compatibility
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 57 debug_if(DEBUG_AIR_QUALITY, "The initial voltage is %d%% of source ", init_voltage / 10);
lucian@192-168-0-106.rdsnet.ro 2:95009177a860 58 while (init_voltage) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 59 if ((init_voltage > 10) && (init_voltage < 798)) {
mbedAustin 0:885417624ec2 60 // the init voltage is ok
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 61 first_vol = (uint16_t) (sensor.read() * 1000); //initialize first value
mbedAustin 0:885417624ec2 62 last_vol = first_vol;
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 63 standard_vol = last_vol;
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 64 debug_if(DEBUG_AIR_QUALITY, "Sensor ready.");
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 65 _has_error = false;
mbedAustin 0:885417624ec2 66 break;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 67 } else if (init_voltage <= 10 || init_voltage > 798) {
mbedAustin 0:885417624ec2 68 // The sensor is not ready, wait a bit for it to cool off
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 69 minutes++;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 70 debug_if(DEBUG_AIR_QUALITY, "Sensor not ready (%d), try %d/5, waiting 60 seconds...", init_voltage,
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 71 minutes);
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 72 wait(60);
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 73 init_voltage = (uint16_t) (sensor.read() * 1000);
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 74 if (minutes == 5) {
mbedAustin 0:885417624ec2 75 // After 5 minutes warn user that the sensor may be broken
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 76 minutes = 0;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 77 _has_error = true;
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 78 debug_if(DEBUG_AIR_QUALITY, "Sensor Error! You may have a bad sensor. :-(");
mbedAustin 0:885417624ec2 79 }
mbedAustin 0:885417624ec2 80 } else
mbedAustin 0:885417624ec2 81 break;
mbedAustin 0:885417624ec2 82 }
mbedAustin 0:885417624ec2 83 }
mbedAustin 0:885417624ec2 84
lucian@192-168-0-106.rdsnet.ro 2:95009177a860 85 air_quality_values AirQuality::slope(void) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 86 air_quality_values ret = UNKNOWN;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 87
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 88 while (_was_sampled) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 89 debug_if(DEBUG_AIR_QUALITY, "sensor_value: %d\r\n", first_vol);
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 90
lucian@192-168-0-106.rdsnet.ro 2:95009177a860 91 if (first_vol - last_vol > 400 || first_vol > 700) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 92 debug_if(DEBUG_AIR_QUALITY, "Very high pollution! Force signal active.\r\n");
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 93 ret = VERY_HIGH_POLLUTION;
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 94 } else if ((first_vol - last_vol > 400 && first_vol < 700) || first_vol - standard_vol > 150) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 95 debug_if(DEBUG_AIR_QUALITY, "High pollution!\r\n");
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 96 ret = HIGH_POLLUTION;
mbedAustin 0:885417624ec2 97
lucian@192-168-0-106.rdsnet.ro 3:c25365a52d78 98 } else if ((first_vol - last_vol > 200 && first_vol < 700) || first_vol - standard_vol > 50) {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 99 debug_if(DEBUG_AIR_QUALITY, "Low pollution!\r\n");
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 100 ret = LOW_POLLUTION;
mbedAustin 0:885417624ec2 101 } else {
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 102 debug_if(DEBUG_AIR_QUALITY, "No pollution\r\n");
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 103 ret = NO_POLLUTION;
mbedAustin 0:885417624ec2 104 }
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 105
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 106 _was_sampled = false;
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 107 calcAvgVoltageFor(_calc_avg_volt_period_s);
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 108 return ret;
mbedAustin 0:885417624ec2 109 }
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 110 return ret;
mbedAustin 0:885417624ec2 111 }
lucian@192-168-0-101.rdsnet.ro 4:0ca4a9fd1b5d 112
lucianovici 7:38425a51906f 113 void AirQuality::set_heating_delay(uint16_t seconds) {
lucianovici 7:38425a51906f 114 _heating_delay_s = seconds;
lucianovici 7:38425a51906f 115 }
lucianovici 7:38425a51906f 116