pi_sensor 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pi_sensor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Rob Cameron
2
+
3
+ MIT License
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # PiSensor
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pi_sensor'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pi_sensor
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,171 @@
1
+ require 'wiringpi'
2
+
3
+ module PiSensor
4
+ class SHT15
5
+
6
+ HIGH = 1
7
+ LOW = 0
8
+ COMMANDS = { :temperature => 0x03,
9
+ :humidity => 0x05,
10
+ :read_status => 0x07,
11
+ :write_status => 0x06,
12
+ :reset => 0x1E }
13
+
14
+ attr_reader :clock, :data
15
+
16
+ def initialize(pins)
17
+ @clock = pins[:clock]
18
+ @data = pins[:data]
19
+ raise StandardError, "You must assign both data and clock pins. Ex: `sensor = SHT15.new :clock => 0, :data => 1`" unless @clock and @data
20
+ end
21
+
22
+
23
+ # Reads the current temperature from the sensor, defaulting to celcius.
24
+ # Pass in an :f to return fahrenheit instead:
25
+ #
26
+ # sensor = SHT15.new :clock => 0, :data => 1
27
+ # sensor.temperature(:f)
28
+ #
29
+ def temperature(scale=:c)
30
+ send_command(COMMANDS[:temperature])
31
+ temp = read_result
32
+
33
+ if temp.nil?
34
+ return nil
35
+ else
36
+ converted_temp = -39.65 + (0.01 * temp)
37
+ if scale == :f
38
+ return celcius_to_fahrenheit(converted_temp)
39
+ else
40
+ return converted_temp
41
+ end
42
+ end
43
+ end
44
+
45
+
46
+ # Returns the current relative humidity (from 0.0 to 100.0)
47
+ def humidity
48
+ send_command(COMMANDS[:humidity])
49
+ humid = read_result
50
+
51
+ if humid.nil?
52
+ return nil
53
+ else
54
+ return -2.0468 + (0.0367 * humid) + (-1.5955E-6 * humid)
55
+ end
56
+ end
57
+
58
+
59
+ def celcius_to_fahrenheit(temp)
60
+ return temp * 9.0 / 5.0 + 32.0
61
+ end
62
+
63
+
64
+ def fahrenheit_to_celcius(temp)
65
+ return (temp - 32.0) * 5.0 / 9.0
66
+ end
67
+
68
+ private
69
+
70
+ # Keeps an instance of WiringPi::GPIO around so we can talk to our pins
71
+ def io
72
+ @io ||= WiringPi::GPIO.new
73
+ end
74
+
75
+
76
+ # Warms the chip up and sends in a command, then waits until a response is
77
+ # ready before returning
78
+ def send_command(command)
79
+ io.mode(@data, OUTPUT)
80
+ io.mode(@clock, OUTPUT)
81
+
82
+ # Wake up sensor
83
+ io.write @data, HIGH
84
+ io.write @clock, HIGH
85
+ io.write @data, LOW
86
+ io.write @clock, LOW
87
+ io.write @clock, HIGH
88
+ io.write @data, HIGH
89
+ io.write @clock, LOW
90
+
91
+ # Send command
92
+ 7.downto(0) do |i|
93
+ binary = (command & (1 << i))
94
+ bit = binary >= 1 ? HIGH : LOW
95
+ io.write(@data, bit)
96
+ io.write(@clock, HIGH)
97
+ io.write(@clock, LOW)
98
+ end
99
+
100
+ # Make sure the sensor got our request
101
+ io.write(@clock, HIGH)
102
+
103
+ io.mode(@data, INPUT)
104
+ if io.read(@data) != LOW
105
+ raise SensorError, "Sensor should be LOW but isn't"
106
+ end
107
+
108
+ io.write(@clock, LOW)
109
+ if io.read(@data) != HIGH
110
+ raise SensorError, "Sensor should be HIGH but isn't"
111
+ end
112
+
113
+ end
114
+
115
+
116
+ def read_result
117
+ result = 0
118
+
119
+ io.mode(@clock, OUTPUT)
120
+ io.mode(@data, INPUT)
121
+
122
+ # Wait for measurement
123
+ puts "Waiting for sensor to take measurement..."
124
+ 1.upto(10) do |i|
125
+ sleep(0.1)
126
+ if io.read(@data) == LOW
127
+ break
128
+ end
129
+ end
130
+ puts "Data pin LOW, ready to read measurement."
131
+
132
+ # If pin is still high at this point, we've got a problem
133
+ if io.read(@data) == HIGH
134
+ puts "!! Ack Error 3"
135
+ exit 0
136
+ end
137
+
138
+ # Read first 8 bits
139
+ 0.upto(7) do |i|
140
+ io.write(@clock, HIGH)
141
+ result = (result << 1) | io.read(@data)
142
+ io.write(@clock, LOW)
143
+ end
144
+
145
+ # Acknowledge first 8 bits
146
+ io.mode(@data, OUTPUT)
147
+ io.write(@data, HIGH)
148
+ io.write(@data, LOW)
149
+ io.write(@clock, HIGH)
150
+ io.write(@clock, LOW)
151
+
152
+ # Read second 8 bits
153
+ io.mode(@data, INPUT)
154
+ 0.upto(7) do |i|
155
+ io.write(@clock, HIGH)
156
+ result = (result << 1) | io.read(@data)
157
+ io.write(@clock, LOW)
158
+ end
159
+
160
+ # Skip CRC check
161
+ io.mode(@data, OUTPUT)
162
+ io.write(@data, HIGH)
163
+ io.write(@clock, HIGH)
164
+ io.write(@clock, LOW)
165
+
166
+ return result
167
+ end
168
+
169
+
170
+ end
171
+ end
@@ -0,0 +1,3 @@
1
+ module PiSensor
2
+ VERSION = "0.0.1"
3
+ end
data/lib/pi_sensor.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'pi_sensor/version'
2
+ require 'pi_sensor/sht15'
3
+
4
+ module PiSensor
5
+ class SensorError < StandardError; end
6
+ end
data/pi_sensor.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pi_sensor/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "pi_sensor"
8
+ gem.version = PiSensor::VERSION
9
+ gem.authors = ["Rob Cameron"]
10
+ gem.email = ["cannikinn@gmail.com"]
11
+ gem.description = %q{Use Ruby to talk to a bunch of sensors with your Raspberry Pi.}
12
+ gem.summary = %q{Use Ruby to talk to a bunch of sensors with your Raspberry Pi.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'wiringpi'
21
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pi_sensor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Cameron
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: wiringpi
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Use Ruby to talk to a bunch of sensors with your Raspberry Pi.
31
+ email:
32
+ - cannikinn@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/pi_sensor.rb
43
+ - lib/pi_sensor/sht15.rb
44
+ - lib/pi_sensor/version.rb
45
+ - pi_sensor.gemspec
46
+ homepage: ''
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.24
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Use Ruby to talk to a bunch of sensors with your Raspberry Pi.
70
+ test_files: []