Browse Source

rhusb: Initial implementation

The driver implements support for the Omega RH-USB Humidity Probe with
Temperature sensor.  It connects via an integrated USB cable, and
appears as a serial port.

It does not currently work with Edison (as of this date) due to
missing ftdi_sio and usbserial kernel support.

It was implemented and tested on the Galileo 2.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Jon Trulson 9 years ago
parent
commit
e1c66c351a

+ 1
- 0
examples/c++/CMakeLists.txt View File

@@ -238,6 +238,7 @@ if (MODBUS_FOUND)
238 238
   add_example (hwxpxx)
239 239
 endif()
240 240
 add_example (hdxxvxta)
241
+add_example (rhusb)
241 242
 
242 243
 # These are special cases where you specify example binary, source file and module(s)
243 244
 include_directories (${PROJECT_SOURCE_DIR}/src)

+ 88
- 0
examples/c++/rhusb.cxx View File

@@ -0,0 +1,88 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2016 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
+
29
+#include "rhusb.h"
30
+
31
+using namespace std;
32
+
33
+bool shouldRun = true;
34
+
35
+void sig_handler(int signo)
36
+{
37
+  if (signo == SIGINT)
38
+    shouldRun = false;
39
+}
40
+
41
+int main(int argc, char **argv)
42
+{
43
+  signal(SIGINT, sig_handler);
44
+
45
+//! [Interesting]
46
+
47
+  string defaultDev = "/dev/ttyUSB0";
48
+
49
+  // if an argument was specified, use it as the device instead
50
+  if (argc > 1)
51
+    defaultDev = string(argv[1]);
52
+
53
+  cout << "Using device " << defaultDev << endl;
54
+  cout << "Initializing..." << endl;
55
+
56
+  // Instantiate an RHUSB instance on defaultDev
57
+  upm::RHUSB sensor(defaultDev);
58
+
59
+  // output the firmware ID
60
+  cout << "Firmware ID: " << sensor.getFirmwareID() << endl;
61
+  cout << endl;
62
+
63
+  // update and print available values every second
64
+  while (shouldRun)
65
+    {
66
+      // update our values from the sensor
67
+      sensor.update();
68
+
69
+      // we show both C and F for temperature
70
+      cout << "Temperature: " << sensor.getTemperature()
71
+           << " C / " << sensor.getTemperature(true) << " F"
72
+           << endl;
73
+
74
+      cout << "Humidity: " << sensor.getHumidity()
75
+           << " %" << endl;
76
+
77
+      cout << endl;
78
+
79
+      sleep(1);
80
+    }
81
+
82
+
83
+  cout << "Exiting..." << endl;
84
+
85
+//! [Interesting]
86
+
87
+  return 0;
88
+}

+ 76
- 0
examples/javascript/rhusb.js View File

@@ -0,0 +1,76 @@
1
+/*jslint node:true, vars:true, bitwise:true, unparam:true */
2
+/*jshint unused:true */
3
+
4
+/*
5
+ * Author: Jon Trulson <jtrulson@ics.com>
6
+ * Copyright (c) 2016 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
+
29
+var sensorObj = require('jsupm_rhusb');
30
+
31
+
32
+/************** Main code **************/
33
+
34
+var defaultDev = "/dev/ttyUSB0";
35
+
36
+// if an argument was specified, use it as the device instead
37
+if (process.argv.length > 2)
38
+{
39
+    defaultDev = process.argv[2];
40
+}
41
+
42
+console.log("Using device " + defaultDev);
43
+console.log("Initializing...");
44
+
45
+// Instantiate an RHUSB instance on defaultDev
46
+var sensor = new sensorObj.RHUSB(defaultDev);
47
+
48
+// output the firmware ID
49
+console.log("Firmware ID:", sensor.getFirmwareID());
50
+console.log("");
51
+
52
+// update and print available values every second
53
+setInterval(function()
54
+{
55
+    // update our values from the sensor
56
+    sensor.update();
57
+
58
+    // we show both C and F for temperature
59
+    console.log("Temperature:", sensor.getTemperature(),
60
+                "C /", sensor.getTemperature(true), "F");
61
+
62
+    console.log("Humidity:", sensor.getHumidity(), "%");
63
+
64
+    console.log("");
65
+
66
+}, 1000);
67
+
68
+
69
+process.on('SIGINT', function()
70
+{
71
+    sensor = null;
72
+    sensorObj.cleanUp();
73
+    sensorObj = null;
74
+    console.log("Exiting...");
75
+    process.exit(0);
76
+});

+ 69
- 0
examples/python/rhusb.py View File

@@ -0,0 +1,69 @@
1
+#!/usr/bin/python
2
+# Author: Jon Trulson <jtrulson@ics.com>
3
+# Copyright (c) 2016 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
+import time, sys, signal, atexit
25
+import pyupm_rhusb as sensorObj
26
+
27
+## Exit handlers ##
28
+# This function stops python from printing a stacktrace when you hit control-C
29
+def SIGINTHandler(signum, frame):
30
+	raise SystemExit
31
+
32
+# This function lets you run code on exit
33
+def exitHandler():
34
+	print "Exiting..."
35
+	sys.exit(0)
36
+
37
+# Register exit handlers
38
+atexit.register(exitHandler)
39
+signal.signal(signal.SIGINT, SIGINTHandler)
40
+
41
+defaultDev = "/dev/ttyUSB0"
42
+
43
+# if an argument was specified, use it as the device instead
44
+if (len(sys.argv) > 1):
45
+        defaultDev = sys.argv[1]
46
+
47
+print "Using device", defaultDev
48
+print "Initializing..."
49
+
50
+# Instantiate an RHUSB instance on defaultDev
51
+sensor = sensorObj.RHUSB(defaultDev)
52
+
53
+# output the firmware ID
54
+print "Firmware ID:", sensor.getFirmwareID()
55
+print
56
+
57
+# update and print available values every second
58
+while (1):
59
+        # update our values from the sensor
60
+        sensor.update()
61
+
62
+        # we show both C and F for temperature
63
+        print "Temperature:", sensor.getTemperature(), "C /",
64
+        print sensor.getTemperature(True), "F"
65
+
66
+        print "Humidity:", sensor.getHumidity(), "%"
67
+
68
+        print
69
+	time.sleep(1)

+ 6
- 0
src/rhusb/CMakeLists.txt View File

@@ -0,0 +1,6 @@
1
+set (libname "rhusb")
2
+set (libdescription "upm module for the Omega RH-USB sensor")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()
6
+

+ 23
- 0
src/rhusb/javaupm_rhusb.i View File

@@ -0,0 +1,23 @@
1
+%module javaupm_rhusb
2
+%include "../upm.i"
3
+%include "typemaps.i"
4
+%include "cpointer.i"
5
+%include "arrays_java.i";
6
+%include "../java_buffer.i"
7
+
8
+%{
9
+    #include "rhusb.h"
10
+%}
11
+
12
+%include "rhusb.h"
13
+
14
+%pragma(java) jniclasscode=%{
15
+    static {
16
+        try {
17
+            System.loadLibrary("javaupm_rhusb");
18
+        } catch (UnsatisfiedLinkError e) {
19
+            System.err.println("Native code library failed to load. \n" + e);
20
+            System.exit(1);
21
+        }
22
+    }
23
+%}

+ 8
- 0
src/rhusb/jsupm_rhusb.i View File

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

+ 12
- 0
src/rhusb/pyupm_rhusb.i View File

@@ -0,0 +1,12 @@
1
+// Include doxygen-generated documentation
2
+%include "pyupm_doxy2swig.i"
3
+%module pyupm_rhusb
4
+%include "../upm.i"
5
+%include "stdint.i"
6
+
7
+%feature("autodoc", "3");
8
+
9
+%include "rhusb.h"
10
+%{
11
+    #include "rhusb.h"
12
+%}

+ 179
- 0
src/rhusb/rhusb.cxx View File

@@ -0,0 +1,179 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2016 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 <stdlib.h>
27
+#include <iostream>
28
+#include <stdexcept>
29
+#include <string>
30
+
31
+#include "rhusb.h"
32
+
33
+using namespace upm;
34
+using namespace std;
35
+
36
+static const int maxBuffer = 1024;
37
+// baud rate is always 9600
38
+static const int baudRate = 9600;
39
+
40
+// conversion from celcius to fahrenheit 
41
+
42
+static float c2f(float c)
43
+{
44
+  return (c * (9.0 / 5.0) + 32.0);
45
+}
46
+
47
+RHUSB::RHUSB(std::string device) :
48
+  m_uart(device)
49
+{
50
+  m_uart.setBaudRate(baudRate);
51
+
52
+  m_temperature = 0.0;
53
+  m_humidity = 0.0;
54
+}
55
+
56
+RHUSB::~RHUSB()
57
+{
58
+}
59
+
60
+void RHUSB::update()
61
+{
62
+  char *endptr;
63
+  float value;
64
+
65
+  // first get the temp in C
66
+  string resp = sendCommand("C");
67
+
68
+  // convert
69
+  value = strtof(resp.c_str(), &endptr);
70
+
71
+  // check for conversion failure
72
+  if (value == 0.0 && resp.c_str() == endptr)
73
+    {
74
+      throw std::runtime_error(std::string(__FUNCTION__) +
75
+                               ": strtof() temperature conversion failed");
76
+    }
77
+  m_temperature = value;
78
+
79
+  // now humidity
80
+  resp = sendCommand("H");
81
+
82
+  // convert
83
+  value = strtof(resp.c_str(), &endptr);
84
+
85
+  // check for conversion failure
86
+  if (value == 0.0 && resp.c_str() == endptr)
87
+    {
88
+      throw std::runtime_error(std::string(__FUNCTION__) +
89
+                               ": strtof() humidity conversion failed");
90
+    }
91
+  m_humidity = value;
92
+}
93
+
94
+float RHUSB::getTemperature(bool fahrenheit)
95
+{
96
+  if (fahrenheit)
97
+    return c2f(m_temperature);
98
+  else
99
+    return m_temperature;
100
+}
101
+
102
+float RHUSB::getHumidity()
103
+{
104
+  return m_humidity;
105
+}
106
+
107
+bool RHUSB::dataAvailable(unsigned int millis)
108
+{
109
+  return m_uart.dataAvailable(millis);
110
+}
111
+
112
+string RHUSB::readStr(int len)
113
+{
114
+  return m_uart.readStr(len);
115
+}
116
+
117
+int RHUSB::writeStr(string data)
118
+{
119
+  m_uart.flush();
120
+  return m_uart.writeStr(data);
121
+}
122
+
123
+string RHUSB::sendCommand(string cmd)
124
+{
125
+  // make sure we got a command
126
+  if (cmd.empty())
127
+    {
128
+      throw std::invalid_argument(std::string(__FUNCTION__) +
129
+                                  ": cmd is empty!");
130
+      return "";
131
+    }
132
+
133
+  // make sure string is CR terminated
134
+  if (cmd.at(cmd.size() - 1) != '\r')
135
+    cmd.append("\r");
136
+
137
+  writeStr(cmd);
138
+
139
+  string resp;
140
+  // I see random timeouts with wait values below 250ms
141
+  while (dataAvailable(250))
142
+    {
143
+      resp += readStr(maxBuffer);
144
+    }
145
+
146
+  if (resp.empty())
147
+    {
148
+      throw std::runtime_error(std::string(__FUNCTION__) +
149
+                               ": timed out waiting for response");
150
+      return "";
151
+    }
152
+
153
+  // check that the last character is the prompt
154
+  if (resp.at(resp.size() - 1) != '>')
155
+    {
156
+      throw std::runtime_error(std::string(__FUNCTION__) +
157
+                               ": read from device corrupted");
158
+      return "";
159
+    }
160
+
161
+  // delete the last 3 characters, which should be '\r\n>'
162
+  resp.erase(resp.size() - 3, 3);
163
+
164
+  return resp;
165
+}
166
+
167
+string RHUSB::getFirmwareID()
168
+{
169
+  string resp = sendCommand("ENQ");
170
+
171
+  // For readability, replace the intermediate \r\n with a space if found.
172
+
173
+  size_t pos = resp.find("\r\n");
174
+
175
+  if (pos != string::npos)
176
+    resp.replace(pos, 2, " ");
177
+
178
+  return resp;
179
+}

+ 118
- 0
src/rhusb/rhusb.h View File

@@ -0,0 +1,118 @@
1
+/*
2
+ * Author: Jon Trulson <jtrulson@ics.com>
3
+ * Copyright (c) 2016 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/uart.hpp>
28
+
29
+namespace upm {
30
+
31
+  /**
32
+   * @brief Omega RH-USB Temperature and Humidity Sensor
33
+   * @defgroup rhusb libupm-rhusb
34
+   * @ingroup uart temp
35
+   */
36
+
37
+  /**
38
+   * @library rhusb
39
+   * @sensor rhusb
40
+   * @comname UPM API for the Omega RH-USB Temperature and Humidity Sensor
41
+   * @type temp
42
+   * @man omega
43
+   * @con uart
44
+   * @web http://www.omega.com/pptst/RH-USB.html
45
+   *
46
+   * @brief UPM API for the Omega RH-USB Temperature and Humidity Sensor
47
+   *
48
+   * This module implements support for the Omega RH-USB Temperature
49
+   * and Humidity Sensor Probe.  It connects via an integrated USB
50
+   * cable, and is accessed via a serial port.  It is suitable for
51
+   * wall or duct mounting.
52
+   *
53
+   * @snippet rhusb.cxx Interesting
54
+   */
55
+
56
+  class RHUSB {
57
+  public:
58
+    /**
59
+     * RHUSB constructor
60
+     *
61
+     * @param device Path to the serial device
62
+     */
63
+    RHUSB(std::string device);
64
+
65
+    /**
66
+     * RHUSB Destructor
67
+     */
68
+    ~RHUSB();
69
+
70
+    /**
71
+     * Read current values from the sensor and update internal stored
72
+     * values.  This method must be called prior to querying any
73
+     * values, such as temperature or humidity.
74
+     */
75
+    void update();
76
+
77
+    /**
78
+     * Get the current temperature.  update() must have been called
79
+     * prior to calling this method.
80
+     *
81
+     * @param fahrenheit true to return the temperature in degrees
82
+     * fahrenheit, false to return the temperature in degrees celcius.
83
+     * The default is false (degrees Celcius).
84
+     * @return The last temperature reading in Celcius or Fahrenheit
85
+     */
86
+    float getTemperature(bool fahrenheit=false);
87
+
88
+    /**
89
+     * Get the current relative humidity.  update() must have been called
90
+     * prior to calling this method.
91
+     *
92
+     * @return The last humidity reading
93
+     */
94
+    float getHumidity();
95
+
96
+    /**
97
+     * Get the firmware identification string.
98
+     *
99
+     * @return The firmware identification
100
+     */
101
+    std::string getFirmwareID();
102
+
103
+
104
+  protected:
105
+    // serial i/o support
106
+    bool dataAvailable(unsigned int millis);
107
+    int writeStr(std::string data);
108
+    std::string readStr(int len);
109
+    std::string sendCommand(std::string cmd);
110
+
111
+    mraa::Uart m_uart;
112
+
113
+  private:
114
+    // data
115
+    float m_temperature;
116
+    float m_humidity;
117
+  };
118
+}

+ 6
- 0
src/upm.h View File

@@ -326,6 +326,12 @@
326 326
  * @ingroup byman
327 327
  */
328 328
 
329
+/**
330
+ * @brief Omega
331
+ * @defgroup omega Omega
332
+ * @ingroup byman
333
+ */
334
+
329 335
 /**
330 336
  * @brief SeeedStudio - Grove Sensors
331 337
  * @defgroup seeed SeeedStudio