Browse Source

mq303a: Initial implementation of the mq303a alcohol sensor

The module implements support for the mq303a alcohol sensor.  It was
tested on the Grove Alcohol Sensor.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Zion Orent <zorent@ics.com>
Signed-off-by: Sarah Knepper <sarah.knepper@intel.com>
Jon Trulson 10 years ago
parent
commit
509cc3a6d3

+ 3
- 0
examples/CMakeLists.txt View File

@@ -70,6 +70,7 @@ add_executable (ublox6-example ublox6.cxx)
70 70
 add_executable (yg1006-example yg1006.cxx)
71 71
 add_executable (wt5001-example wt5001.cxx)
72 72
 add_executable (ppd42ns-example ppd42ns.cxx)
73
+add_executable (mq303a-example mq303a.cxx)
73 74
 
74 75
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
75 76
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -127,6 +128,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ublox6)
127 128
 include_directories (${PROJECT_SOURCE_DIR}/src/yg1006)
128 129
 include_directories (${PROJECT_SOURCE_DIR}/src/wt5001)
129 130
 include_directories (${PROJECT_SOURCE_DIR}/src/ppd42ns)
131
+include_directories (${PROJECT_SOURCE_DIR}/src/mq303a)
130 132
 
131 133
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
132 134
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -200,3 +202,4 @@ target_link_libraries (ublox6-example ublox6 ${CMAKE_THREAD_LIBS_INIT})
200 202
 target_link_libraries (yg1006-example yg1006 ${CMAKE_THREAD_LIBS_INIT})
201 203
 target_link_libraries (wt5001-example wt5001 ${CMAKE_THREAD_LIBS_INIT})
202 204
 target_link_libraries (ppd42ns-example ppd42ns ${CMAKE_THREAD_LIBS_INIT})
205
+target_link_libraries (mq303a-example mq303a ${CMAKE_THREAD_LIBS_INIT})

+ 78
- 0
examples/javascript/mq303a.js View File

@@ -0,0 +1,78 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+/*global */
4
+/*
5
+* Author: Zion Orent <zorent@ics.com>
6
+* Copyright (c) 2014 Intel Corporation.
7
+*
8
+* Permission is hereby granted, free of charge, to any person obtaining
9
+* a copy of this software and associated documentation files (the
10
+* "Software"), to deal in the Software without restriction, including
11
+* without limitation the rights to use, copy, modify, merge, publish,
12
+* distribute, sublicense, and/or sell copies of the Software, and to
13
+* permit persons to whom the Software is furnished to do so, subject to
14
+* the following conditions:
15
+*
16
+* The above copyright notice and this permission notice shall be
17
+* included in all copies or substantial portions of the Software.
18
+*
19
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+*/
27
+
28
+// Load alcohol sensor module
29
+var mq303a = require('jsupm_mq303a');
30
+
31
+// Instantiate an mq303a sensor on analog pin A0
32
+// This device uses a heater powered from an analog I/O pin. 
33
+// If using A0 as the data pin, then you need to use A1, as the heater
34
+// pin (if using a grove mq303a).  For A1, we can use the D15 gpio, 
35
+// setup as an output, and drive it low to power the heater.
36
+var myAlcoholObj = new mq303a.MQ303A(0, 15);
37
+
38
+console.log("Enabling heater and waiting 2 minutes for warmup.");
39
+
40
+// give time updates every 30 seconds until 2 minutes have passed
41
+// for the alcohol sensor to warm up
42
+statusMessage(1);
43
+statusMessage(2);
44
+statusMessage(3);
45
+
46
+function statusMessage(amt)
47
+{
48
+	setTimeout(function()
49
+	{
50
+		console.log((amt * 30) + " seconds have passed");
51
+	}, 30000 * amt);
52
+}
53
+
54
+// run the alcohol sensor in 2 minutes
55
+setTimeout(runAlcoholSensor, 120000);
56
+
57
+function runAlcoholSensor()
58
+{
59
+	var notice = "This sensor may need to warm " +
60
+				"until the value drops below about 450."
61
+	console.log(notice);
62
+
63
+    // Print the detected alcohol value every second
64
+	setInterval(function()
65
+	{
66
+		var val = myAlcoholObj.value();
67
+		var msg = "Alcohol detected ";
68
+		msg += "(higher means stronger alcohol): ";
69
+		console.log(msg + val);
70
+	}, 1000);
71
+}
72
+
73
+// Print message when exiting
74
+process.on('SIGINT', function()
75
+{
76
+	console.log("Exiting...");
77
+	process.exit(0);
78
+});

+ 78
- 0
examples/mq303a.cxx View File

@@ -0,0 +1,78 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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 <unistd.h>
26
+#include <iostream>
27
+#include <signal.h>
28
+#include "mq303a.h"
29
+
30
+using namespace std;
31
+
32
+int shouldRun = true;
33
+
34
+void sig_handler(int signo)
35
+{
36
+  if (signo == SIGINT)
37
+    shouldRun = false;
38
+}
39
+
40
+int main ()
41
+{
42
+  signal(SIGINT, sig_handler);
43
+
44
+//! [Interesting]
45
+  // Instantiate an mq303a sensor on analog pin A0
46
+
47
+  // This device uses a heater powered from an analog I/O pin. 
48
+  // If using A0 as the data pin, then you need to use A1, as the heater
49
+  // pin (if using a grove mq303a).  For A1, we can use the D15 gpio, 
50
+  // setup as an output, and drive it low to power the heater.
51
+
52
+  upm::MQ303A *mq303a = new upm::MQ303A(0, 15);
53
+  
54
+  cout << "Enabling heater and waiting 2 minutes for warmup." << endl;
55
+  mq303a->heaterEnable(true);
56
+  sleep(120);
57
+
58
+  cout << "This sensor may need to warm until the value drops below about 450." 
59
+       << endl;
60
+
61
+  // Print the detected alcohol value every second
62
+  while (shouldRun)
63
+    {
64
+      int val = mq303a->value();
65
+
66
+      cout << "Alcohol detected (higher means stronger alcohol): " 
67
+           << val << endl;
68
+
69
+      sleep(1);
70
+    }
71
+//! [Interesting]
72
+
73
+  cout << "Exiting" << endl;
74
+  mq303a->heaterEnable(false);
75
+
76
+  delete mq303a;
77
+  return 0;
78
+}

+ 5
- 0
src/mq303a/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+set (libname "mq303a")
2
+set (libdescription "upm mq303a module")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/mq303a/jsupm_mq303a.i View File

@@ -0,0 +1,8 @@
1
+%module jsupm_mq303a
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "mq303a.h"
6
+%}
7
+
8
+%include "mq303a.h"

+ 57
- 0
src/mq303a/mq303a.cxx View File

@@ -0,0 +1,57 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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 <iostream>
26
+
27
+#include "mq303a.h"
28
+
29
+using namespace upm;
30
+
31
+MQ303A::MQ303A(int pin, int heaterPin)
32
+{
33
+  m_aio = mraa_aio_init(pin);
34
+
35
+  m_gpio = mraa_gpio_init(heaterPin);
36
+  mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT);
37
+}
38
+
39
+MQ303A::~MQ303A()
40
+{
41
+  heaterEnable(false);
42
+  mraa_aio_close(m_aio);
43
+  mraa_gpio_close(m_gpio);
44
+}
45
+
46
+int MQ303A::value()
47
+{
48
+  return (1023 - mraa_aio_read(m_aio));
49
+}
50
+
51
+void MQ303A::heaterEnable(bool enable)
52
+{
53
+  if (enable)
54
+    mraa_gpio_write(m_gpio, 0);  // 0 turns on the heater
55
+  else
56
+    mraa_gpio_write(m_gpio, 1);  // 1 turns off the heater
57
+}

+ 79
- 0
src/mq303a/mq303a.h View File

@@ -0,0 +1,79 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.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 <string>
27
+#include <mraa/aio.h>
28
+
29
+namespace upm {
30
+
31
+  /**
32
+   * @brief C++ API for the MQ303A Alcohol Sensor
33
+   *
34
+   * UPM module for the MQ303A Alcohol Sensor.
35
+   * This sensor needs to be warmed up before stable results are
36
+   * obtained.  The higher the value returned from value(),
37
+   * the higher the amount of alcohol that was detected.
38
+   *
39
+   * @ingroup analog gas alcohol
40
+   * @snippet mq303a.cxx Interesting
41
+   */
42
+  class MQ303A {
43
+  public:
44
+    /**
45
+     * MQ303A sensor constructor
46
+     *
47
+     * @param pin analog pin to use
48
+     * @param heaterPin digital pin mapped to analog pin to use
49
+     */
50
+    MQ303A(int pin, int heaterPin);
51
+
52
+    /**
53
+     * MQ303A Destructor
54
+     */
55
+    ~MQ303A();
56
+
57
+    /**
58
+     * Get the alcohol detected from the sensor.  
59
+     * The value read from the analog pin is inverted.  
60
+     * A higher returned value means a higher amount of alcohol was detected.
61
+     *
62
+     * @return the alcohol reading
63
+     */
64
+    int value();
65
+
66
+    /**
67
+     * Enable the heater.
68
+     *
69
+     * @param enable if true, enable the heater else disable
70
+     */
71
+    void heaterEnable(bool enable);
72
+
73
+  private:
74
+    mraa_aio_context m_aio;
75
+    mraa_gpio_context m_gpio;
76
+  };
77
+}
78
+
79
+

+ 9
- 0
src/mq303a/pyupm_mq303a.i View File

@@ -0,0 +1,9 @@
1
+%module pyupm_mq303a
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "mq303a.h"
7
+%{
8
+    #include "mq303a.h"
9
+%}