Browse Source

tp401: grove air quality sensor and examples

Signed-off-by: Mihai Tudor Panu <mihai.t.panu@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
Mihai Tudor Panu 10 years ago
parent
commit
7bb5fd8ec7

BIN
docs/images/tp401.jpeg View File


+ 2
- 0
examples/CMakeLists.txt View File

@@ -36,6 +36,7 @@ add_executable (mq2-example mq2.cxx)
36 36
 add_executable (mq3-example mq3.cxx)
37 37
 add_executable (mq5-example mq5.cxx)
38 38
 add_executable (mq9-example mq9.cxx)
39
+add_executable (tp401-example tp401.cxx)
39 40
 add_executable (tcs3414cs-example tcs3414cs.cxx)
40 41
 add_executable (th02-example th02.cxx)
41 42
 add_executable (lsm303-example lsm303.cxx)
@@ -121,6 +122,7 @@ target_link_libraries (mq2-example gas ${CMAKE_THREAD_LIBS_INIT})
121 122
 target_link_libraries (mq3-example gas ${CMAKE_THREAD_LIBS_INIT})
122 123
 target_link_libraries (mq5-example gas ${CMAKE_THREAD_LIBS_INIT})
123 124
 target_link_libraries (mq9-example gas ${CMAKE_THREAD_LIBS_INIT})
125
+target_link_libraries (tp401-example gas ${CMAKE_THREAD_LIBS_INIT})
124 126
 target_link_libraries (tcs3414cs-example tcs3414cs ${CMAKE_THREAD_LIBS_INIT})
125 127
 target_link_libraries (th02-example th02 ${CMAKE_THREAD_LIBS_INIT})
126 128
 target_link_libraries (lsm303-example lsm303 ${CMAKE_THREAD_LIBS_INIT})

+ 68
- 0
examples/javascript/tp401.js View File

@@ -0,0 +1,68 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+var upmTP401 = require('jsupm_gas');
26
+//var time = require('sleep');
27
+
28
+//give a qualitative meaning to the value from the sensor
29
+function airQuality(value)
30
+{
31
+    if(value < 50) return "Fresh Air";
32
+    if(value < 200) return "Normal Indoor Air";
33
+    if(value < 400) return "Low Pollution";
34
+    if(value < 600) return "High Pollution - Action Recommended";
35
+    return "Very High Pollution - Take Action Immediately";
36
+}
37
+
38
+function loop()
39
+{
40
+    //read values (consecutive reads might vary slightly)
41
+    var value = airSensor.getSample();
42
+    var ppm = airSensor.getPPM();
43
+
44
+    //write the sensor values to the console
45
+    console.log("raw: " + value + " ppm: " + (" " + ppm.toFixed(2)).substring(-5, 5) + "   " + airQuality(value));
46
+
47
+    //wait 2.5 s then call function again
48
+    setTimeout(loop, 2500);
49
+}
50
+
51
+//setup sensor on Analog pin #0 (A0)
52
+var airSensor = new upmTP401.TP401(0);
53
+
54
+//warm up sensor
55
+console.log("Sensor is warming up for 3 minutes..");
56
+var i = 1;
57
+
58
+//print a message every passing minute
59
+var waiting = setInterval(function() {
60
+        console.log(i++ + " minute(s) passed.");
61
+        if(i == 3) clearInterval(waiting);
62
+    }, 60000);
63
+
64
+//start loop in 3 minutes
65
+setTimeout(function(){
66
+    console.log("Sensor is ready!");
67
+    loop();
68
+    }, 180000);

+ 54
- 0
examples/python/tp401.py View File

@@ -0,0 +1,54 @@
1
+# Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
2
+# Copyright (c) 2014 Intel Corporation.
3
+#
4
+# Permission is hereby granted, free of charge, to any person obtaining
5
+# a copy of this software and associated documentation files (the
6
+# "Software"), to deal in the Software without restriction, including
7
+# without limitation the rights to use, copy, modify, merge, publish,
8
+# distribute, sublicense, and/or sell copies of the Software, and to
9
+# permit persons to whom the Software is furnished to do so, subject to
10
+# the following conditions:
11
+#
12
+# The above copyright notice and this permission notice shall be
13
+# included in all copies or substantial portions of the Software.
14
+#
15
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+from time import sleep
24
+import pyupm_gas as TP401
25
+
26
+# Give a qualitative meaning to the value from the sensor
27
+def airQuality(value):
28
+    if(value < 50): return "Fresh Air"
29
+    if(value < 200): return "Normal Indoor Air"
30
+    if(value < 400): return "Low Pollution"
31
+    if(value < 600): return "High Pollution - Action Recommended"
32
+    return "Very High Pollution - Take Action Immediately"
33
+
34
+# New Grove Air Quality Sensor on AIO pin 0
35
+airSensor = TP401.TP401(0)
36
+
37
+# Wait for sensor to warm up
38
+print "Sensor is warming up for 3 minutes..."
39
+for i in range (1, 4):
40
+    sleep(60)
41
+    print i, "minute(s) passed."
42
+print "Sensor is ready!"
43
+
44
+# Loop indefinitely
45
+while True:
46
+
47
+    # Read values (consecutive reads might vary slightly)
48
+    value = airSensor.getSample()
49
+    ppm = airSensor.getPPM()
50
+
51
+    print "raw: %4d" % value , " ppm: %5.2f   " % ppm , airQuality(value)
52
+
53
+    # Sleep for 2.5 s
54
+    sleep(2.5)

+ 70
- 0
examples/tp401.cxx View File

@@ -0,0 +1,70 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+
26
+#include <unistd.h>
27
+#include <iostream>
28
+#include "tp401.h"
29
+
30
+using namespace std;
31
+
32
+//! [Interesting]
33
+// Give a qualitative meaning to the value from the sensor
34
+std::string
35
+airQuality(uint16_t value)
36
+{
37
+    if(value < 50) return "Fresh Air";
38
+    if(value < 200) return "Normal Indoor Air";
39
+    if(value < 400) return "Low Pollution";
40
+    if(value < 600) return "High Pollution - Action Recommended";
41
+    return "Very High Pollution - Take Action Immediately";
42
+}
43
+
44
+int main ()
45
+{
46
+    upm::TP401* airSensor = new upm::TP401(0); // Instantiate new grove air quality sensor on analog pin A0
47
+
48
+    cout << airSensor->name() << endl;
49
+
50
+    fprintf(stdout, "Heating sensor for 3 minutes...\n");
51
+    // wait 3 minutes for sensor to warm up
52
+    for(int i = 0; i < 3; i++) {
53
+        if(i) {
54
+            fprintf(stdout, "Please wait, %d minute(s) passed..\n", i);
55
+        }
56
+        sleep(60);
57
+    }
58
+    fprintf(stdout, "Sensor ready!\n");
59
+
60
+    while(true) {
61
+        uint16_t value = airSensor->getSample(); // Read raw value
62
+        float ppm = airSensor->getPPM();    // Read CO ppm (can vary slightly from previous read)
63
+        fprintf(stdout, "raw: %4d ppm: %5.2f   %s\n", value, ppm, airQuality(value).c_str());
64
+        usleep(2500000);    // Sleep for 2.5s
65
+    }
66
+
67
+    delete airSensor;
68
+    return 0;
69
+}
70
+//! [Interesting]

+ 2
- 2
src/gas/CMakeLists.txt View File

@@ -1,5 +1,5 @@
1 1
 set (libname "gas")
2 2
 set (libdescription "Gas sensors")
3
-set (module_src ${libname}.cxx mq2.cxx mq3.cxx mq5.cxx mq9.cxx)
4
-set (module_h ${libname}.h mq2.h mq3.h mq5.h mq9.h)
3
+set (module_src ${libname}.cxx mq2.cxx mq3.cxx mq5.cxx mq9.cxx tp401.cxx)
4
+set (module_h ${libname}.h mq2.h mq3.h mq5.h mq9.h tp401.h)
5 5
 upm_module_init()

+ 3
- 1
src/gas/README.txt View File

@@ -3,4 +3,6 @@ Grove - Gas Sensor(MQ-3):Alcohol and Benzine [high sensitivity] Long warm-up
3 3
 Grove - Gas Sensor(MQ-5):LPG, Natural Gas, Town Gas [high sensitivity]
4 4
 Grove - Gas Sensor(MQ-9):LPG, CO, CH4 [low sensitivity]
5 5
 
6
-Note - Gas sensors need to be heated, first minute the data is incorrect.
6
+Note - Gas sensors need to be heated, first minute the data is incorrect. Air
7
+Quality sensor needs 48h to stabilize
8
+

+ 5
- 0
src/gas/jsupm_gas.i View File

@@ -26,3 +26,8 @@
26 26
 %{
27 27
     #include "mq9.h"
28 28
 %}
29
+
30
+%include "tp401.h"
31
+%{
32
+    #include "tp401.h"
33
+%}

+ 4
- 0
src/gas/pyupm_gas.i View File

@@ -31,3 +31,7 @@
31 31
     #include "mq9.h"
32 32
 %}
33 33
 
34
+%include "tp401.h"
35
+%{
36
+    #include "tp401.h"
37
+%}

+ 39
- 0
src/gas/tp401.cxx View File

@@ -0,0 +1,39 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include "tp401.h"
26
+
27
+using namespace upm;
28
+
29
+TP401::TP401 (int gasPin) : Gas (gasPin) {
30
+    m_name = "Grove Air Quality Sensor";
31
+}
32
+
33
+TP401::~TP401 () {
34
+}
35
+
36
+float
37
+TP401::getPPM() {
38
+    return 25.0 * (float)TP401::getSample() / 1023.0;
39
+}

+ 81
- 0
src/gas/tp401.h View File

@@ -0,0 +1,81 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+#pragma once
25
+
26
+#include <iostream>
27
+#include <string>
28
+#include "gas.h"
29
+
30
+namespace upm {
31
+    /**
32
+     * @brief C++ API for Grove TP401 Air Quality Sensor
33
+     *
34
+     * The Grove TP401 Air Quality Sensor module is useful for monitoring air purity indoors.
35
+     * It can detect CO and a wide range of other harmful gases, but due to limited sensing
36
+     * range should be used only when qualitative results are needed. Example applications
37
+     * are air recirculation, ventilation systems, and refreshing sprayers.
38
+     * The sensor is linear and should be roughly sensitive to 0 ~ 20 ppm CO from 0 ~ 4V.
39
+     * Also note that the sensor requires 2-3 minutes to warm up initially and 48 hours of
40
+     * operation to stabilize completely.
41
+     *
42
+     * @ingroup gas analog
43
+     * @snippet tp401.cxx Interesting
44
+     * @image html tp401.jpeg
45
+     */
46
+    class TP401 : public Gas {
47
+        public:
48
+            /**
49
+             * TP401 constructor
50
+             *
51
+             * @param gasPin analog pin where sensor was connected
52
+             */
53
+            TP401 (int gasPin);
54
+
55
+            /**
56
+             * TP401 destructor
57
+             */
58
+            ~TP401 ();
59
+
60
+            /**
61
+             * Return name of the component
62
+             *
63
+             * @return a string with the name of the sensor
64
+             */
65
+            std::string name()
66
+            {
67
+                return m_name;
68
+            }
69
+
70
+            /**
71
+             * Returns one sample in parts per million (ppm) of CO in the air based on
72
+             * the following sensor calibration: 0 ~ 4V is roughly 0 ~ 20 ppm CO
73
+             *
74
+             * @return a new sample converted to ppm CO
75
+             */
76
+            float getPPM();
77
+
78
+        private:
79
+            std::string m_name;
80
+    };
81
+}