Grove Air Quality Sensor.
Fork of Grove_Air_Quality_Sensor_Library by
Air_Quality.cpp@3:c25365a52d78, 2016-12-23 (annotated)
- Committer:
- lucian@192-168-0-106.rdsnet.ro
- Date:
- Fri Dec 23 22:31:17 2016 +0200
- Revision:
- 3:c25365a52d78
- Parent:
- 2:95009177a860
- Child:
- 4:0ca4a9fd1b5d
IEM-192 Refactor forked lib and our air quality sensor code
Who changed what in which revision?
User | Revision | Line number | New 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 | |
mbedAustin | 0:885417624ec2 | 23 | //Get the avg voltage in 5 minutes. |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 24 | void AirQuality::avgVoltage() { |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 25 | if (i == 150) { //sum 5 minutes |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 26 | standard_vol = temp / 150; |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 27 | temp = 0; |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 28 | debug_if(DEBUG_AIR_QUALITY, "Vol_standard in 5 minutes: %d", standard_vol); |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 29 | i = 0; |
mbedAustin | 0:885417624ec2 | 30 | } else { |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 31 | temp += first_vol; |
mbedAustin | 0:885417624ec2 | 32 | i++; |
mbedAustin | 0:885417624ec2 | 33 | } |
mbedAustin | 0:885417624ec2 | 34 | } |
mbedAustin | 0:885417624ec2 | 35 | |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 36 | void AirQuality::init(PinName pin) { |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 37 | _pin = pin; |
mbedAustin | 0:885417624ec2 | 38 | AnalogIn sensor(_pin); |
mbedAustin | 0:885417624ec2 | 39 | unsigned char i = 0; |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 40 | debug_if(DEBUG_AIR_QUALITY, "Air Quality Sensor Starting Up...(20s)"); |
mbedAustin | 0:885417624ec2 | 41 | wait(20); //20s |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 42 | 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 | 43 | debug_if(DEBUG_AIR_QUALITY, "The initial voltage is %d%% of source ", init_voltage / 10); |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 44 | while (init_voltage) { |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 45 | if ((init_voltage < 798) && (init_voltage > 10)) { |
mbedAustin | 0:885417624ec2 | 46 | // the init voltage is ok |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 47 | first_vol = (uint16_t) (sensor.read() * 1000); //initialize first value |
mbedAustin | 0:885417624ec2 | 48 | last_vol = first_vol; |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 49 | standard_vol = last_vol; |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 50 | debug_if(DEBUG_AIR_QUALITY, "Sensor ready."); |
mbedAustin | 0:885417624ec2 | 51 | error = false;; |
mbedAustin | 0:885417624ec2 | 52 | break; |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 53 | } else if (init_voltage > 798 || init_voltage <= 10) { |
mbedAustin | 0:885417624ec2 | 54 | // The sensor is not ready, wait a bit for it to cool off |
mbedAustin | 0:885417624ec2 | 55 | i++; |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 56 | debug_if(DEBUG_AIR_QUALITY, "Sensor not ready (%d), try %d/5, waiting 60 seconds...", init_voltage, i); |
mbedAustin | 0:885417624ec2 | 57 | wait(60);//60s |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 58 | init_voltage = (uint16_t) (sensor.read() * 1000); |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 59 | if (i == 5) { |
mbedAustin | 0:885417624ec2 | 60 | // After 5 minutes warn user that the sensor may be broken |
mbedAustin | 0:885417624ec2 | 61 | i = 0; |
mbedAustin | 0:885417624ec2 | 62 | error = true; |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 63 | debug_if(DEBUG_AIR_QUALITY, "Sensor Error! You may have a bad sensor. :-("); |
mbedAustin | 0:885417624ec2 | 64 | } |
mbedAustin | 0:885417624ec2 | 65 | } else |
mbedAustin | 0:885417624ec2 | 66 | break; |
mbedAustin | 0:885417624ec2 | 67 | } |
mbedAustin | 0:885417624ec2 | 68 | } |
mbedAustin | 0:885417624ec2 | 69 | |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 70 | air_quality_values AirQuality::slope(void) { |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 71 | while (timer_index) { |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 72 | if (first_vol - last_vol > 400 || first_vol > 700) { |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 73 | debug_if(DEBUG_AIR_QUALITY, "Very high pollution! Force signal active."); |
mbedAustin | 0:885417624ec2 | 74 | timer_index = 0; |
mbedAustin | 0:885417624ec2 | 75 | avgVoltage(); |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 76 | return VERY_HIGH_POLLUTION; |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 77 | } else if ((first_vol - last_vol > 400 && first_vol < 700) || first_vol - standard_vol > 150) { |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 78 | debug_if(DEBUG_AIR_QUALITY, "sensor_value:%d", first_vol); |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 79 | debug_if(DEBUG_AIR_QUALITY, "High pollution!"); |
mbedAustin | 0:885417624ec2 | 80 | timer_index = 0; |
mbedAustin | 0:885417624ec2 | 81 | avgVoltage(); |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 82 | return HIGH_POLLUTION; |
mbedAustin | 0:885417624ec2 | 83 | |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 84 | } else if ((first_vol - last_vol > 200 && first_vol < 700) || first_vol - standard_vol > 50) { |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 85 | // debug_if( DEBUG_AIR_QUALITY, first_vol - last_vol); |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 86 | debug_if(DEBUG_AIR_QUALITY, "sensor_value:%d", first_vol); |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 87 | debug_if(DEBUG_AIR_QUALITY, "Low pollution!"); |
mbedAustin | 0:885417624ec2 | 88 | timer_index = 0; |
mbedAustin | 0:885417624ec2 | 89 | avgVoltage(); |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 90 | return LOW_POLLUTION; |
mbedAustin | 0:885417624ec2 | 91 | } else { |
mbedAustin | 0:885417624ec2 | 92 | avgVoltage(); |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 93 | debug_if(DEBUG_AIR_QUALITY, "sensor_value:%d", first_vol); |
lucian@192-168-0-106.rdsnet.ro | 3:c25365a52d78 | 94 | debug_if(DEBUG_AIR_QUALITY, "No pollution"); |
mbedAustin | 0:885417624ec2 | 95 | timer_index = 0; |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 96 | return NO_POLLUTION; |
mbedAustin | 0:885417624ec2 | 97 | } |
mbedAustin | 0:885417624ec2 | 98 | } |
lucian@192-168-0-106.rdsnet.ro | 2:95009177a860 | 99 | return UNKNOWN; |
mbedAustin | 0:885417624ec2 | 100 | } |