dht-sensor-ffi 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +20 -0
- data/README.md +44 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/bin/dht_sensor +5 -0
- data/dht-sensor-ffi.gemspec +73 -0
- data/ext/dhtreader.cpp +215 -0
- data/ext/extconf.rb +40 -0
- data/lib/dht-sensor-ffi.rb +34 -0
- data/lib/dht-sensor/app.rb +142 -0
- data/lib/dht-sensor/reading.rb +20 -0
- data/test/helper.rb +35 -0
- data/test/test_dht-sensor-ffi.rb +7 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 440ac4f0fa63f665fd65c876720da6e961d04002
|
4
|
+
data.tar.gz: bc1fa0e8d23542c67f79a5cb43f2a53f13e000ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 073bec16616ac7d1ebfe3e9183dddbd359709ba780479b19e76017cb989a46e8ada35ab63cc9ae851cd8f2b088e2a9df328908180861cfd367f57cdbef700512
|
7
|
+
data.tar.gz: e570525556086446bcc1676700223050de28a26605f1fcd149a31857cee55cb81cbe5de938cd10190044f1144bff2eff53d8927580b50b8829bea51519605ac2
|
data/.document
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Chetan Sarva
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# DHT Sensor FFI
|
2
|
+
|
3
|
+
A Ruby library for reading from a DHT-11 or DHT-22 type sensor connected to a Raspberry Pi
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
First install the bcm2835 lib, available at:
|
8
|
+
http://www.airspayce.com/mikem/bcm2835
|
9
|
+
|
10
|
+
Then either install manually
|
11
|
+
|
12
|
+
```
|
13
|
+
gem install dht-sensor-ffi
|
14
|
+
```
|
15
|
+
|
16
|
+
or add to your Gemfile
|
17
|
+
|
18
|
+
```
|
19
|
+
gem "dht-sensor-ffi"
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require "dht-sensor-ffi"
|
26
|
+
val = DhtSensor.read(4, 22) # pin=4, sensor type=DHT-22
|
27
|
+
puts val.temp # => 21.899999618530273 (temp in C)
|
28
|
+
puts val.temp_f # => 71.4199993133545 (temp in F)
|
29
|
+
puts val.humidity # => 22.700000762939453 (relative humidity %)
|
30
|
+
```
|
31
|
+
|
32
|
+
## Contributing to dht-sensor-ffi
|
33
|
+
|
34
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
35
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
36
|
+
* Fork the project.
|
37
|
+
* Start a feature/bugfix branch.
|
38
|
+
* Commit and push until you are happy with your contribution.
|
39
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
40
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
41
|
+
|
42
|
+
## Copyright
|
43
|
+
|
44
|
+
Copyright (c) 2014 Chetan Sarva. Licensed under the MIT license. See LICENSE.txt for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = "dht-sensor-ffi"
|
18
|
+
gem.homepage = "http://github.com/chetan/dht-sensor-ffi"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A library to use the DHT-XX type humidity/temperature sensors}
|
21
|
+
gem.description = %Q{A library to use the DHT-XX type humidity/temperature sensors}
|
22
|
+
gem.email = "chetan@pixelcop.net"
|
23
|
+
gem.authors = ["Chetan Sarva"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Code coverage detail"
|
36
|
+
task :simplecov do
|
37
|
+
ENV['COVERAGE'] = "true"
|
38
|
+
Rake::Task['test'].execute
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'yard'
|
44
|
+
YARD::Rake::YardocTask.new
|
45
|
+
|
46
|
+
require "rake/extensiontask"
|
47
|
+
Rake::ExtensionTask.new 'dht_sensor' do |ext|
|
48
|
+
ext.ext_dir = "ext"
|
49
|
+
ext.lib_dir = "lib/dht-sensor"
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/bin/dht_sensor
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: dht-sensor-ffi 0.1.0 ruby lib
|
6
|
+
# stub: ext/extconf.rb
|
7
|
+
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = "dht-sensor-ffi"
|
10
|
+
s.version = "0.1.0"
|
11
|
+
|
12
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
13
|
+
s.require_paths = ["lib"]
|
14
|
+
s.authors = ["Chetan Sarva"]
|
15
|
+
s.date = "2014-11-20"
|
16
|
+
s.description = "A library to use the DHT-XX type humidity/temperature sensors"
|
17
|
+
s.email = "chetan@pixelcop.net"
|
18
|
+
s.executables = ["dht_sensor"]
|
19
|
+
s.extensions = ["ext/extconf.rb"]
|
20
|
+
s.extra_rdoc_files = [
|
21
|
+
"LICENSE.txt",
|
22
|
+
"README.md"
|
23
|
+
]
|
24
|
+
s.files = [
|
25
|
+
".document",
|
26
|
+
"Gemfile",
|
27
|
+
"LICENSE.txt",
|
28
|
+
"README.md",
|
29
|
+
"Rakefile",
|
30
|
+
"VERSION",
|
31
|
+
"bin/dht_sensor",
|
32
|
+
"dht-sensor-ffi.gemspec",
|
33
|
+
"ext/dhtreader.cpp",
|
34
|
+
"ext/extconf.rb",
|
35
|
+
"lib/dht-sensor-ffi.rb",
|
36
|
+
"lib/dht-sensor/app.rb",
|
37
|
+
"lib/dht-sensor/reading.rb",
|
38
|
+
"test/helper.rb",
|
39
|
+
"test/test_dht-sensor-ffi.rb"
|
40
|
+
]
|
41
|
+
s.homepage = "http://github.com/chetan/dht-sensor-ffi"
|
42
|
+
s.licenses = ["MIT"]
|
43
|
+
s.rubygems_version = "2.4.2"
|
44
|
+
s.summary = "A library to use the DHT-XX type humidity/temperature sensors"
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
s.specification_version = 4
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_runtime_dependency(%q<ffi>, ["~> 1.9"])
|
51
|
+
s.add_development_dependency(%q<rake>, ["~> 10.3"])
|
52
|
+
s.add_development_dependency(%q<rake-compiler>, ["~> 0.9"])
|
53
|
+
s.add_development_dependency(%q<yard>, ["~> 0.8"])
|
54
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.7"])
|
55
|
+
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<ffi>, ["~> 1.9"])
|
58
|
+
s.add_dependency(%q<rake>, ["~> 10.3"])
|
59
|
+
s.add_dependency(%q<rake-compiler>, ["~> 0.9"])
|
60
|
+
s.add_dependency(%q<yard>, ["~> 0.8"])
|
61
|
+
s.add_dependency(%q<bundler>, ["~> 1.7"])
|
62
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
63
|
+
end
|
64
|
+
else
|
65
|
+
s.add_dependency(%q<ffi>, ["~> 1.9"])
|
66
|
+
s.add_dependency(%q<rake>, ["~> 10.3"])
|
67
|
+
s.add_dependency(%q<rake-compiler>, ["~> 0.9"])
|
68
|
+
s.add_dependency(%q<yard>, ["~> 0.8"])
|
69
|
+
s.add_dependency(%q<bundler>, ["~> 1.7"])
|
70
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
data/ext/dhtreader.cpp
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
/**
|
2
|
+
* Adapted from node-dht-sensor
|
3
|
+
* https://github.com/momenso/node-dht-sensor
|
4
|
+
*
|
5
|
+
* The contents of this file are licensed under the LGPL
|
6
|
+
*/
|
7
|
+
|
8
|
+
#include <cstdlib>
|
9
|
+
#include <ctime>
|
10
|
+
|
11
|
+
// Access from ARM running linux
|
12
|
+
#define BCM2708_PERI_BASE 0x20000000
|
13
|
+
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)
|
14
|
+
|
15
|
+
#include <stdio.h>
|
16
|
+
#include <string.h>
|
17
|
+
#include <stdlib.h>
|
18
|
+
#include <dirent.h>
|
19
|
+
#include <fcntl.h>
|
20
|
+
#include <assert.h>
|
21
|
+
#include <unistd.h>
|
22
|
+
#include <sys/mman.h>
|
23
|
+
#include <sys/types.h>
|
24
|
+
#include <sys/stat.h>
|
25
|
+
#include <sys/time.h>
|
26
|
+
#include <bcm2835.h>
|
27
|
+
#include <unistd.h>
|
28
|
+
#include <sched.h>
|
29
|
+
|
30
|
+
extern "C" {
|
31
|
+
long readDHT(int type, int pin, float &temperature, float &humidity);
|
32
|
+
int initDHT();
|
33
|
+
}
|
34
|
+
|
35
|
+
extern "C" void Init_dht_sensor() {
|
36
|
+
initDHT();
|
37
|
+
}
|
38
|
+
|
39
|
+
// uncomment to enable debug output
|
40
|
+
// #define VERBOSE 1;
|
41
|
+
|
42
|
+
#define MAXTIMINGS 100
|
43
|
+
|
44
|
+
#define DHT11 11
|
45
|
+
#define DHT22 22
|
46
|
+
#define AM2302 22
|
47
|
+
|
48
|
+
#ifdef VERBOSE
|
49
|
+
int bits[1000];
|
50
|
+
int bitidx = 0;
|
51
|
+
#endif
|
52
|
+
int initialized = 0;
|
53
|
+
int data[100];
|
54
|
+
unsigned long long last_read[32];
|
55
|
+
float last_temperature[32] = {};
|
56
|
+
float last_humidity[32] = {};
|
57
|
+
|
58
|
+
unsigned long long getTime()
|
59
|
+
{
|
60
|
+
struct timeval tv;
|
61
|
+
gettimeofday(&tv, NULL);
|
62
|
+
unsigned long long time = (unsigned long long)(tv.tv_sec)*1000 +
|
63
|
+
(unsigned long long)(tv.tv_usec)/1000;
|
64
|
+
return time;
|
65
|
+
}
|
66
|
+
|
67
|
+
long readDHT(int type, int pin, float &temperature, float &humidity)
|
68
|
+
{
|
69
|
+
int counter = 0;
|
70
|
+
int laststate = HIGH;
|
71
|
+
int j=0;
|
72
|
+
#ifdef VERBOSE
|
73
|
+
bitidx = 0;
|
74
|
+
#endif
|
75
|
+
|
76
|
+
unsigned long long now = getTime();
|
77
|
+
if (now - last_read[pin] < 2000 && last_temperature[pin] != 0 && last_humidity[pin] != 0) {
|
78
|
+
#ifdef VERBOSE
|
79
|
+
printf("Too early to read again pin %d: %llu\n", pin, now - last_read[pin]);
|
80
|
+
#endif
|
81
|
+
temperature = last_temperature[pin];
|
82
|
+
humidity = last_humidity[pin];
|
83
|
+
return 0;
|
84
|
+
} else {
|
85
|
+
last_read[pin] = now + 420;
|
86
|
+
}
|
87
|
+
|
88
|
+
// Set GPIO pin to output
|
89
|
+
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
|
90
|
+
|
91
|
+
bcm2835_gpio_write(pin, HIGH);
|
92
|
+
bcm2835_delay(400);
|
93
|
+
bcm2835_gpio_write(pin, LOW);
|
94
|
+
bcm2835_delay(20);
|
95
|
+
|
96
|
+
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
|
97
|
+
|
98
|
+
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
|
99
|
+
|
100
|
+
// wait for pin to drop?
|
101
|
+
int timeout = 100000;
|
102
|
+
while (bcm2835_gpio_lev(pin) == 1) {
|
103
|
+
if (--timeout < 0) {
|
104
|
+
#ifdef VERBOSE
|
105
|
+
printf("Sensor timeout.\n");
|
106
|
+
#endif
|
107
|
+
return -3;
|
108
|
+
}
|
109
|
+
bcm2835_delayMicroseconds(1); //usleep(1);
|
110
|
+
}
|
111
|
+
|
112
|
+
// read data!
|
113
|
+
for (int i = 0; i < MAXTIMINGS; i++) {
|
114
|
+
counter = 0;
|
115
|
+
while (bcm2835_gpio_lev(pin) == laststate) {
|
116
|
+
counter++;
|
117
|
+
if (counter == 1000)
|
118
|
+
break;
|
119
|
+
}
|
120
|
+
laststate = bcm2835_gpio_lev(pin);
|
121
|
+
if (counter == 1000) break;
|
122
|
+
#ifdef VERBOSE
|
123
|
+
if (bitidx < 1000) {
|
124
|
+
bits[bitidx++] = counter;
|
125
|
+
} else {
|
126
|
+
printf("WARNING: bits buffer blew up!\n");
|
127
|
+
}
|
128
|
+
#endif
|
129
|
+
|
130
|
+
if ((i>3) && (i%2 == 0)) {
|
131
|
+
// shove each bit into the storage bytes
|
132
|
+
data[j/8] <<= 1;
|
133
|
+
if (counter > 200)
|
134
|
+
data[j/8] |= 1;
|
135
|
+
j++;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
if ((j >= 39) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xff)))
|
140
|
+
{
|
141
|
+
#ifdef VERBOSE
|
142
|
+
printf("[Sensor type = %d] ", type);
|
143
|
+
#endif
|
144
|
+
|
145
|
+
if (type == DHT11) {
|
146
|
+
#ifdef VERBOSE
|
147
|
+
printf("Temp = %d C, Hum = %d %%\n", data[2], data[0]);
|
148
|
+
#endif
|
149
|
+
temperature = data[2];
|
150
|
+
humidity = data[0];
|
151
|
+
}
|
152
|
+
else if (type == DHT22)
|
153
|
+
{
|
154
|
+
float f, h;
|
155
|
+
h = data[0] * 256 + data[1];
|
156
|
+
h /= 10;
|
157
|
+
|
158
|
+
f = (data[2] & 0x7F) * 256 + data[3];
|
159
|
+
f /= 10.0;
|
160
|
+
if (data[2] & 0x80) f *= -1;
|
161
|
+
|
162
|
+
#ifdef VERBOSE
|
163
|
+
printf("Temp = %.1f C, Hum = %.1f %%\n", f, h);
|
164
|
+
#endif
|
165
|
+
temperature = f;
|
166
|
+
humidity = h;
|
167
|
+
}
|
168
|
+
else
|
169
|
+
{
|
170
|
+
return -2;
|
171
|
+
}
|
172
|
+
}
|
173
|
+
else
|
174
|
+
{
|
175
|
+
#ifdef VERBOSE
|
176
|
+
printf("Unexpected data: j=%d: %d != %d + %d + %d + %d\n",
|
177
|
+
j, data[4], data[0], data[1], data[2], data[3]);
|
178
|
+
#endif
|
179
|
+
return -1;
|
180
|
+
}
|
181
|
+
|
182
|
+
#ifdef VERBOSE
|
183
|
+
printf("Obtained readout successfully.\n");
|
184
|
+
#endif
|
185
|
+
|
186
|
+
// update last readout
|
187
|
+
last_temperature[pin] = temperature;
|
188
|
+
last_humidity[pin] = humidity;
|
189
|
+
return 0;
|
190
|
+
}
|
191
|
+
|
192
|
+
int initDHT()
|
193
|
+
{
|
194
|
+
// set up real-time scheduling
|
195
|
+
struct sched_param schedp;
|
196
|
+
schedp.sched_priority = 1;
|
197
|
+
sched_setscheduler(0, SCHED_FIFO, &schedp);
|
198
|
+
|
199
|
+
if (!bcm2835_init())
|
200
|
+
{
|
201
|
+
#ifdef VERBOSE
|
202
|
+
printf("BCM2835 initialization failed.\n");
|
203
|
+
#endif
|
204
|
+
return 1;
|
205
|
+
}
|
206
|
+
else
|
207
|
+
{
|
208
|
+
#ifdef VERBOSE
|
209
|
+
printf("BCM2835 initialized.\n");
|
210
|
+
#endif
|
211
|
+
initialized = 1;
|
212
|
+
memset(last_read, 0, sizeof(unsigned long long)*32);
|
213
|
+
return 0;
|
214
|
+
}
|
215
|
+
}
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
require 'mkmf'
|
3
|
+
|
4
|
+
LIBDIR = RbConfig::CONFIG['libdir']
|
5
|
+
INCLUDEDIR = RbConfig::CONFIG['includedir']
|
6
|
+
|
7
|
+
HEADER_DIRS = [
|
8
|
+
'/opt/local/include',
|
9
|
+
'/usr/local/include',
|
10
|
+
INCLUDEDIR,
|
11
|
+
'/usr/include',
|
12
|
+
]
|
13
|
+
|
14
|
+
LIB_DIRS = [
|
15
|
+
'/opt/local/lib',
|
16
|
+
'/usr/local/lib',
|
17
|
+
LIBDIR,
|
18
|
+
'/usr/lib',
|
19
|
+
]
|
20
|
+
|
21
|
+
unless find_header('bcm2835.h')
|
22
|
+
abort 'bcm2835 is missing, for production you will need to install http://www.airspayce.com/mikem/bcm2835/'
|
23
|
+
end
|
24
|
+
|
25
|
+
unless have_library('bcm2835') && append_library($libs, 'bcm2835')
|
26
|
+
abort "Can't Appended Library bcm2835! for production you will need to install http://www.airspayce.com/mikem/bcm2835/"
|
27
|
+
end
|
28
|
+
|
29
|
+
dir_config('bcm2835', HEADER_DIRS, LIB_DIRS)
|
30
|
+
|
31
|
+
# $CFLAGS << ' -std=c99'
|
32
|
+
|
33
|
+
# Give it a name
|
34
|
+
#extension_name = 'dht_sensor'
|
35
|
+
|
36
|
+
# The destination
|
37
|
+
#dir_config(extension_name)
|
38
|
+
|
39
|
+
# Do the work
|
40
|
+
create_makefile("dht-sensor/dht_sensor")
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
require "dht-sensor/dht_sensor" # load ext
|
3
|
+
require "dht-sensor/reading"
|
4
|
+
|
5
|
+
require "ffi"
|
6
|
+
|
7
|
+
module DhtSensor
|
8
|
+
extend FFI::Library
|
9
|
+
|
10
|
+
ffi_lib File.expand_path("../dht-sensor/dht_sensor.so", __FILE__)
|
11
|
+
|
12
|
+
attach_function :readDHT, [:int, :int, :pointer, :pointer], :long
|
13
|
+
|
14
|
+
def self.read(pin, type=22)
|
15
|
+
|
16
|
+
temperature = FFI::MemoryPointer.new(:float)
|
17
|
+
humidity = FFI::MemoryPointer.new(:float)
|
18
|
+
|
19
|
+
tries = 50
|
20
|
+
ret = 0
|
21
|
+
while tries > 0 do
|
22
|
+
tries -= 1
|
23
|
+
ret = DhtSensor.readDHT(type, pin, temperature, humidity)
|
24
|
+
break if ret == 0 && !(temperature.read_float == 0.0 && humidity.read_float == 0.0)
|
25
|
+
sleep 0.1
|
26
|
+
end
|
27
|
+
|
28
|
+
if ret != 0 then
|
29
|
+
raise "Failed to read from sensor (read call returned #{ret})"
|
30
|
+
end
|
31
|
+
|
32
|
+
Reading.new(temperature.read_float, humidity.read_float)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
|
2
|
+
require "optparse"
|
3
|
+
require "optparse/time"
|
4
|
+
|
5
|
+
module DhtSensor
|
6
|
+
class App
|
7
|
+
|
8
|
+
def run!
|
9
|
+
@options = parse_opts()
|
10
|
+
validate_opts(@options)
|
11
|
+
|
12
|
+
case @options[:command]
|
13
|
+
when :read
|
14
|
+
do_read()
|
15
|
+
when :json
|
16
|
+
do_json()
|
17
|
+
when :loop
|
18
|
+
do_loop()
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def do_read
|
27
|
+
print(read())
|
28
|
+
exit
|
29
|
+
end
|
30
|
+
|
31
|
+
def do_loop
|
32
|
+
while true do
|
33
|
+
begin
|
34
|
+
print(read())
|
35
|
+
sleep 2
|
36
|
+
rescue InterruptedException => ex
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def do_json
|
42
|
+
require "json"
|
43
|
+
puts JSON.dump(to_hash(read()))
|
44
|
+
end
|
45
|
+
|
46
|
+
# Convert to sensor reading to hash
|
47
|
+
def to_hash(val)
|
48
|
+
if @options[:humidity] then
|
49
|
+
return {"humidity" => val.humidity}
|
50
|
+
end
|
51
|
+
|
52
|
+
if @options[:unit] == :c then
|
53
|
+
if @options[:temperature] then
|
54
|
+
return {"temperature" => val.temp}
|
55
|
+
else
|
56
|
+
return {"temperature" => val.temp, "humidity" => val.humidity}
|
57
|
+
end
|
58
|
+
else
|
59
|
+
if @options[:temperature] then
|
60
|
+
return {"temperature" => val.temp_f}
|
61
|
+
else
|
62
|
+
return {"temperature" => val.temp_f, "humidity" => val.humidity}
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Print to stdout, taking the various output options into account
|
68
|
+
def print(val)
|
69
|
+
if @options[:humidity] then
|
70
|
+
puts sprintf("Humidity: %.2f%%", val.humidity)
|
71
|
+
return
|
72
|
+
end
|
73
|
+
|
74
|
+
if @options[:unit] == :c then
|
75
|
+
if @options[:temperature] then
|
76
|
+
puts sprintf("Temperature: %.2f C", val.temp)
|
77
|
+
else
|
78
|
+
puts sprintf("Temperature: %.2f C Humidity: %.2f%%", val.temp, val.humidity)
|
79
|
+
end
|
80
|
+
else
|
81
|
+
if @options[:temperature] then
|
82
|
+
puts sprintf("Temperature: %.2f F", val.temp_f, val.humidity)
|
83
|
+
else
|
84
|
+
puts sprintf("Temperature: %.2f F Humidity: %.2f%%", val.temp_f, val.humidity)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def read
|
90
|
+
DhtSensor.read(@options[:pin], @options[:type])
|
91
|
+
end
|
92
|
+
|
93
|
+
def validate_opts(opts)
|
94
|
+
|
95
|
+
if opts[:type] && ![11, 22].include?(opts[:type]) then
|
96
|
+
puts "error: invalid sensor type '#{opts[:type]}'; must be either 11 or 22"
|
97
|
+
puts
|
98
|
+
puts @opt_parser
|
99
|
+
exit 1
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
def parse_opts
|
105
|
+
options = {
|
106
|
+
:command => :read,
|
107
|
+
:pin => 4,
|
108
|
+
:type => 22,
|
109
|
+
:unit => :c
|
110
|
+
}
|
111
|
+
|
112
|
+
@opt_parser = OptionParser.new do |opts|
|
113
|
+
opts.banner = "Usage: dht_sensor [command]"
|
114
|
+
opts.separator ""
|
115
|
+
opts.separator "Options:"
|
116
|
+
|
117
|
+
opts.on("-r", "--read", "Read the current sensor values and print to STDOUT (default)") { options[:command] = :read }
|
118
|
+
opts.on("-j", "--json", "Read the current sensor values and output as JSON") { options[:command] = :json }
|
119
|
+
opts.on("-l", "--loop", "Continuously read from the sensor and print to STDOUT") { options[:command] = :loop }
|
120
|
+
opts.on("-t", "--temp", "Temperature only") { options[:temp] = true }
|
121
|
+
opts.on("-h", "--humidity", "Humidity only") { options[:humidity] = true }
|
122
|
+
|
123
|
+
opts.on("-c", "--celsius", "Display temperature in Celsius (default)") { options[:unit] = :c }
|
124
|
+
opts.on("-f", "--fahrenheit", "Display temperature in Fahrenheit") { options[:unit] = :f }
|
125
|
+
|
126
|
+
opts.on("-p", "--pin PIN", "Pin number which sensor is connected to (default: 4)") { |t| options[:pin] = t.to_i }
|
127
|
+
opts.on("-T", "--type TYPE", "DHT sensor type, either 11 or 22 (default: 22)") { |t| options[:type] = t.to_i }
|
128
|
+
|
129
|
+
opts.on_tail("--help", "Show this message") do
|
130
|
+
puts opts
|
131
|
+
exit
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
@opt_parser.parse!(ARGV)
|
136
|
+
options
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
DhtSensor::App.new.run!
|
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
module DhtSensor
|
3
|
+
class Reading
|
4
|
+
|
5
|
+
attr_reader :temperature, :humidity
|
6
|
+
|
7
|
+
def initialize(temp, hum)
|
8
|
+
@temperature = temp
|
9
|
+
@humidity = hum
|
10
|
+
end
|
11
|
+
|
12
|
+
def temperature_f
|
13
|
+
return 0 if @temperature == 0
|
14
|
+
(@temperature * 9/5) + 32
|
15
|
+
end
|
16
|
+
alias_method :temp_f, :temperature_f
|
17
|
+
alias_method :temp, :temperature
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
module SimpleCov::Configuration
|
4
|
+
def clean_filters
|
5
|
+
@filters = []
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
SimpleCov.configure do
|
10
|
+
clean_filters
|
11
|
+
load_adapter 'test_frameworks'
|
12
|
+
end
|
13
|
+
|
14
|
+
ENV["COVERAGE"] && SimpleCov.start do
|
15
|
+
add_filter "/.rvm/"
|
16
|
+
end
|
17
|
+
require 'rubygems'
|
18
|
+
require 'bundler'
|
19
|
+
begin
|
20
|
+
Bundler.setup(:default, :development)
|
21
|
+
rescue Bundler::BundlerError => e
|
22
|
+
$stderr.puts e.message
|
23
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
24
|
+
exit e.status_code
|
25
|
+
end
|
26
|
+
require 'minitest/unit'
|
27
|
+
|
28
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
29
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
30
|
+
require 'dht-sensor-ffi'
|
31
|
+
|
32
|
+
class MiniTest::Unit::TestCase
|
33
|
+
end
|
34
|
+
|
35
|
+
MiniTest::Unit.autorun
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dht-sensor-ffi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chetan Sarva
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.7'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.7'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: jeweler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.0.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.0.1
|
97
|
+
description: A library to use the DHT-XX type humidity/temperature sensors
|
98
|
+
email: chetan@pixelcop.net
|
99
|
+
executables:
|
100
|
+
- dht_sensor
|
101
|
+
extensions:
|
102
|
+
- ext/extconf.rb
|
103
|
+
extra_rdoc_files:
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
files:
|
107
|
+
- ".document"
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- VERSION
|
113
|
+
- bin/dht_sensor
|
114
|
+
- dht-sensor-ffi.gemspec
|
115
|
+
- ext/dhtreader.cpp
|
116
|
+
- ext/extconf.rb
|
117
|
+
- lib/dht-sensor-ffi.rb
|
118
|
+
- lib/dht-sensor/app.rb
|
119
|
+
- lib/dht-sensor/reading.rb
|
120
|
+
- test/helper.rb
|
121
|
+
- test/test_dht-sensor-ffi.rb
|
122
|
+
homepage: http://github.com/chetan/dht-sensor-ffi
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.4.2
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: A library to use the DHT-XX type humidity/temperature sensors
|
146
|
+
test_files: []
|