raspberry_pi_iot 0.0.8 → 0.1.3
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.
- checksums.yaml +5 -5
- data/doc/IoT.html +8 -6
- data/doc/{Bus.html → RaspberryPiIoT.html} +21 -26
- data/doc/_index.html +84 -77
- data/doc/class_list.html +1 -1
- data/doc/file.LICENSE.html +92 -0
- data/doc/file.README.html +63 -29
- data/doc/file_list.html +5 -0
- data/doc/frames.html +1 -1
- data/doc/index.html +63 -29
- data/doc/method_list.html +170 -202
- data/doc/top-level-namespace.html +4 -6
- data/lib/bus.rb +0 -2
- data/lib/iot.rb +4 -2
- data/lib/iot/actuator.rb +7 -0
- data/lib/iot/bh1750.rb +54 -0
- data/lib/iot/binary_actuator.rb +7 -0
- data/lib/iot/binary_effector.rb +11 -0
- data/lib/iot/binary_receptor.rb +20 -0
- data/lib/iot/binary_sensor.rb +7 -0
- data/lib/iot/bmx_sensor.rb +73 -0
- data/lib/iot/button.rb +100 -0
- data/lib/iot/dht_sensor.rb +41 -0
- data/lib/iot/digital_actuator.rb +7 -0
- data/lib/iot/digital_effector.rb +67 -0
- data/lib/iot/digital_receptor.rb +17 -0
- data/lib/iot/digital_sensor.rb +7 -0
- data/lib/iot/ds18b20.rb +25 -0
- data/lib/iot/effector.rb +32 -0
- data/lib/iot/hcsr501.rb +8 -0
- data/lib/iot/humiture_sensor.rb +27 -0
- data/lib/iot/led.rb +64 -0
- data/lib/iot/led_pwm.rb +8 -0
- data/lib/iot/led_rgb.rb +86 -0
- data/lib/iot/max44009.rb +84 -0
- data/lib/iot/receptor.rb +38 -0
- data/lib/iot/sensor.rb +8 -0
- data/lib/iot/temperature_sensor.rb +51 -0
- data/lib/raspberry_pi.rb +46 -1
- data/lib/raspberry_pi_iot.rb +9 -3
- data/lib/raspberry_pi_iot/version.rb +3 -0
- data/lib/thermal_sensor.rb +2 -3
- data/test/test.sh +17 -0
- data/test/test.txt +173 -0
- data/test/test_Raspberry_Pi_IoT.rb +10 -0
- data/test/test_bh1750.rb +9 -4
- data/test/{standalone_test_bmp280.rb → test_bme280.rb} +24 -19
- data/test/test_bmp280.rb +52 -0
- data/test/test_button.rb +25 -14
- data/test/test_dht11.rb +3 -3
- data/test/test_dht22.rb +35 -0
- data/test/test_ds18b20.rb +4 -3
- data/test/test_helper.rb +6 -0
- data/test/test_info.rb +24 -3
- data/test/test_ky008.rb +41 -0
- data/test/test_led.rb +9 -9
- data/test/test_led_rgb.rb +28 -5
- data/test/test_max44009_gy49.rb +4 -4
- data/test/test_thermal.rb +27 -0
- metadata +103 -59
- data/doc/Actuator.html +0 -145
- data/doc/BH1750.html +0 -541
- data/doc/BME280.html +0 -276
- data/doc/BMP280.html +0 -276
- data/doc/BMxSensor.html +0 -557
- data/doc/BinaryActuator.html +0 -177
- data/doc/BinaryEffector.html +0 -246
- data/doc/BinaryReceptor.html +0 -387
- data/doc/BinarySensor.html +0 -168
- data/doc/Button.html +0 -986
- data/doc/DHT11.html +0 -287
- data/doc/DHT22.html +0 -287
- data/doc/DHTxxSensor.html +0 -361
- data/doc/DS18B20.html +0 -339
- data/doc/DigitalActuator.html +0 -164
- data/doc/DigitalEffector.html +0 -636
- data/doc/DigitalReceptor.html +0 -242
- data/doc/DigitalSensor.html +0 -162
- data/doc/Effector.html +0 -257
- data/doc/HCSR501.html +0 -172
- data/doc/HumitureSensor.html +0 -453
- data/doc/LED.html +0 -784
- data/doc/MAX44009.html +0 -830
- data/doc/RaspberryPi.html +0 -317
- data/doc/Receptor.html +0 -257
- data/doc/Sensor.html +0 -145
- data/doc/TemperatureSensor.html +0 -644
- data/lib/bh1750.rb +0 -46
- data/lib/binary_actuator.rb +0 -5
- data/lib/binary_effector.rb +0 -12
- data/lib/binary_receptor.rb +0 -19
- data/lib/binary_sensor.rb +0 -5
- data/lib/bmx_sensor.rb +0 -85
- data/lib/button.rb +0 -70
- data/lib/dht_sensor.rb +0 -36
- data/lib/digital_actuator.rb +0 -5
- data/lib/digital_effector.rb +0 -69
- data/lib/digital_receptor.rb +0 -16
- data/lib/digital_sensor.rb +0 -5
- data/lib/ds18b20.rb +0 -24
- data/lib/effector.rb +0 -32
- data/lib/hcsr501.rb +0 -7
- data/lib/humiture_sensor.rb +0 -26
- data/lib/led.rb +0 -58
- data/lib/led_pwm.rb +0 -6
- data/lib/led_rgb.rb +0 -101
- data/lib/max44009.rb +0 -77
- data/lib/receptor.rb +0 -33
- data/lib/temperature_sensor.rb +0 -49
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require_relative 'receptor'
|
|
2
|
+
|
|
3
|
+
module IoT
|
|
4
|
+
# DigitalReceptor - receptor with digitized values
|
|
5
|
+
class DigitalReceptor < IoT::Receptor
|
|
6
|
+
@@one_wire_bus = '/sys/bus/w1/devices'
|
|
7
|
+
@sensor_file = nil
|
|
8
|
+
|
|
9
|
+
def self.one_wire_file(prefix='28*', device='w1_slave')
|
|
10
|
+
files = Dir[@@one_wire_bus+'/'+prefix]
|
|
11
|
+
if files.size > 0 then
|
|
12
|
+
@sensor_file = files[0]+'/'+device
|
|
13
|
+
end
|
|
14
|
+
@sensor_file
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/iot/ds18b20.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require_relative 'temperature_sensor'
|
|
2
|
+
|
|
3
|
+
# DS18B20 - high precision temperature sensor with 1-wire interface
|
|
4
|
+
module IoT
|
|
5
|
+
class DS18B20 < IoT::TemperatureSensor
|
|
6
|
+
|
|
7
|
+
def initialize(device='w1_slave')
|
|
8
|
+
self.name = 'DS18B20'
|
|
9
|
+
@sensor_file = DigitalReceptor.one_wire_file('28*', device)
|
|
10
|
+
if @sensor_file
|
|
11
|
+
@temperature = read_data
|
|
12
|
+
else
|
|
13
|
+
raise "#{self.name} - Sensor NOT FOUND!"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def read_data
|
|
18
|
+
File.open(@sensor_file, 'r') do |f|
|
|
19
|
+
value = f.read
|
|
20
|
+
@temperature = value.scan(/t=(\d+)/).flatten[0].to_i/1000.0
|
|
21
|
+
end
|
|
22
|
+
@temperature
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/iot/effector.rb
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# https://github.com/ClockVapor/rpi_gpio
|
|
2
|
+
require 'rpi_gpio'
|
|
3
|
+
|
|
4
|
+
RPi::GPIO.set_warnings(false)
|
|
5
|
+
RPi::GPIO.set_numbering :bcm
|
|
6
|
+
|
|
7
|
+
module IoT
|
|
8
|
+
# Effector - a controlled physical device that performs some (useful) action
|
|
9
|
+
class Effector
|
|
10
|
+
def initialize
|
|
11
|
+
@model = 'N/A'
|
|
12
|
+
@name = 'N/A'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def name
|
|
16
|
+
@name
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def model_name
|
|
20
|
+
@model
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
protected
|
|
24
|
+
def name=(name)
|
|
25
|
+
@name = name
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def model_name=(name)
|
|
29
|
+
@model_name = name
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
data/lib/iot/hcsr501.rb
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require_relative 'temperature_sensor'
|
|
2
|
+
|
|
3
|
+
module IoT
|
|
4
|
+
# HumitureSensor - combined humidity & temperature sensor
|
|
5
|
+
class HumitureSensor < IoT::TemperatureSensor
|
|
6
|
+
@humidity = 0.0
|
|
7
|
+
@temperature = 0.0
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
@humidity, @temperature = read_data
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def read_data
|
|
14
|
+
[@humidity, @temperature]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Humidity in per cents
|
|
18
|
+
def humidity
|
|
19
|
+
@humidity
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Temperature & humidity as a string
|
|
23
|
+
def to_s
|
|
24
|
+
sprintf "%5.2f°, %5.2f %%", @temperature, @humidity
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
data/lib/iot/led.rb
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require_relative 'binary_effector'
|
|
2
|
+
|
|
3
|
+
module IoT
|
|
4
|
+
# LED - Light Emitting Diode
|
|
5
|
+
class LED < IoT::BinaryEffector
|
|
6
|
+
def initialize(pin)
|
|
7
|
+
super(pin)
|
|
8
|
+
self.name = 'LED'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Turn on the LED.
|
|
12
|
+
def on(pin=@pin)
|
|
13
|
+
super(pin)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Turn off the LED.
|
|
17
|
+
def off(pin=@pin)
|
|
18
|
+
super(pin)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Repeat 'repeat' times: light the LED for 'switched_on' secs, then pause for 'switched_off' secs
|
|
22
|
+
def blink(repeat=3, switched_on=0.25, switched_off=0.5)
|
|
23
|
+
repeat.times do |n|
|
|
24
|
+
on
|
|
25
|
+
sleep switched_on
|
|
26
|
+
off
|
|
27
|
+
sleep switched_off
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Turn the LED on for N seconds.
|
|
32
|
+
def on_for(seconds=1)
|
|
33
|
+
on
|
|
34
|
+
sleep seconds
|
|
35
|
+
off
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Turn the LED off for N seconds.
|
|
39
|
+
def off_for(seconds=1)
|
|
40
|
+
off
|
|
41
|
+
sleep seconds
|
|
42
|
+
on
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Experimental methods with names in Russian
|
|
47
|
+
class LED
|
|
48
|
+
def LED.создать(pin = 25)
|
|
49
|
+
LED.new(pin)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def включить
|
|
53
|
+
on
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def выключить
|
|
57
|
+
off
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def мигать(repeat=3, switched_on=0.25, switched_off=0.5)
|
|
61
|
+
blink(repeat, switched_on, switched_off)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/iot/led_pwm.rb
ADDED
data/lib/iot/led_rgb.rb
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
require_relative 'led'
|
|
2
|
+
|
|
3
|
+
module IoT
|
|
4
|
+
class LED
|
|
5
|
+
class RGB < IoT::DigitalEffector
|
|
6
|
+
@red = nil # GPIO pins
|
|
7
|
+
@green = nil #
|
|
8
|
+
@blue = nil #
|
|
9
|
+
|
|
10
|
+
def initialize(red = 17, green = 27, blue = 22)
|
|
11
|
+
super({:red => red, :green => green, :blue => blue}, :low)
|
|
12
|
+
@red = red
|
|
13
|
+
@green = green
|
|
14
|
+
@blue = blue
|
|
15
|
+
self.name = 'RGB LED'
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def on(color=:all)
|
|
19
|
+
color = :aoi if color == :aqua
|
|
20
|
+
case color
|
|
21
|
+
when :red
|
|
22
|
+
super @red
|
|
23
|
+
when :green
|
|
24
|
+
super @green
|
|
25
|
+
when :blue
|
|
26
|
+
super @blue
|
|
27
|
+
when :yellow
|
|
28
|
+
super @red
|
|
29
|
+
super @green
|
|
30
|
+
when :aoi
|
|
31
|
+
super @blue
|
|
32
|
+
super @green
|
|
33
|
+
when :all
|
|
34
|
+
super @red
|
|
35
|
+
super @green
|
|
36
|
+
super @blue
|
|
37
|
+
else
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def off(color=:all)
|
|
42
|
+
color = :aoi if color == :aqua
|
|
43
|
+
case color
|
|
44
|
+
when :red
|
|
45
|
+
super @red
|
|
46
|
+
when :green
|
|
47
|
+
super @green
|
|
48
|
+
when :blue
|
|
49
|
+
super @blue
|
|
50
|
+
when :yellow
|
|
51
|
+
super @red
|
|
52
|
+
super @green
|
|
53
|
+
when :aoi
|
|
54
|
+
super @blue
|
|
55
|
+
super @green
|
|
56
|
+
when :all
|
|
57
|
+
super @red
|
|
58
|
+
super @green
|
|
59
|
+
super @blue
|
|
60
|
+
else
|
|
61
|
+
all_off
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def all_on
|
|
66
|
+
on :all
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def all_off
|
|
70
|
+
off :all
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def blink(list = :all, repeat=3, switched_on=0.25, switched_off=0.5)
|
|
74
|
+
colors = (list == :all) ? [:red, :green, :blue] : (list.class == Array ? list : [list])
|
|
75
|
+
repeat.times do |n|
|
|
76
|
+
colors.each do |color|
|
|
77
|
+
on color
|
|
78
|
+
sleep switched_on
|
|
79
|
+
off color
|
|
80
|
+
sleep switched_off
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
data/lib/iot/max44009.rb
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require 'bus'
|
|
2
|
+
|
|
3
|
+
module IoT
|
|
4
|
+
# MAX44009 - low-power ambient Light Sensor
|
|
5
|
+
# Output: digital, Bus: I²C, Range: 0.045..188,000 Lux, VCC: 1.7..3.6V, Operating Current: 0.65µA
|
|
6
|
+
# https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf
|
|
7
|
+
class MAX44009
|
|
8
|
+
MAX44009_DEFAULT_ADDRESS = 0x4A
|
|
9
|
+
# MAX44009 Register Map
|
|
10
|
+
MAX44009_REG_INTR_STATUS = 0x00 # Interrupt Status Register
|
|
11
|
+
MAX44009_REG_INTR_ENABLE = 0x01 # Interrupt Enable Register
|
|
12
|
+
MAX44009_REG_CONFIG = 0x02 # Configuration Register
|
|
13
|
+
MAX44009_REG_LUX_HIGH = 0x03 # Lux High Byte
|
|
14
|
+
MAX44009_REG_LUX_LOW = 0x04 # Lux Low Byte
|
|
15
|
+
MAX44009_REG_THRES_UP_HI = 0x05 # Upper Threshold High Byte
|
|
16
|
+
MAX44009_REG_THRES_LO_HI = 0x06 # Upper Threshold Low Byte
|
|
17
|
+
MAX44009_REG_THRES_TIMER = 0x07 # Threshold Timer
|
|
18
|
+
# MAX44009 INTERRUPT ENABLE REGISTER
|
|
19
|
+
MAX44009_REG_INTR_ENABLE_NO_ASSRT = 0x00 # The INT pin and the INTS bit are not asserted even if an interrupt event has occurred
|
|
20
|
+
MAX44009_REG_INTR_ENABLE_TRIGGER = 0x01 # Detection of an interrupt event triggers a hardware interrupt
|
|
21
|
+
# MAX44009 CONFIGURATION REGISTER
|
|
22
|
+
MAX44009_REG_CONFIG_CONTMODE_DEFAULT = 0x00 # Default mode
|
|
23
|
+
MAX44009_REG_CONFIG_CONTMODE_CONTIN = 0x80 # Continuous mode
|
|
24
|
+
MAX44009_REG_CONFIG_MANUAL_DEFAULT = 0x00 # Default mode of configuration is used for the IC
|
|
25
|
+
MAX44009_REG_CONFIG_MANUAL_MODEON = 0x40 # Manual mode of configuration is used for the IC
|
|
26
|
+
MAX44009_REG_CONFIG_CDR_NODIVIDED = 0x00 # Current not divided. All of the photodiode current goes to the ADC
|
|
27
|
+
MAX44009_REG_CONFIG_CDR_DIVIDED = 0x08 # Current divided by 8
|
|
28
|
+
MAX44009_REG_CONFIG_INTRTIMER_800 = 0x00 # Integration Time = 800ms, This is a preferred mode for boosting low-light sensitivity
|
|
29
|
+
MAX44009_REG_CONFIG_INTRTIMER_400 = 0x01 # Integration Time = 400ms
|
|
30
|
+
MAX44009_REG_CONFIG_INTRTIMER_200 = 0x02 # Integration Time = 200ms
|
|
31
|
+
MAX44009_REG_CONFIG_INTRTIMER_100 = 0x03 # Integration Time = 100ms, This is a preferred mode for high-brightness applications
|
|
32
|
+
MAX44009_REG_CONFIG_INTRTIMER_50 = 0x04 # Integration Time = 50ms, Manual Mode only
|
|
33
|
+
MAX44009_REG_CONFIG_INTRTIMER_25 = 0x05 # Integration Time = 25ms, Manual Mode only
|
|
34
|
+
MAX44009_REG_CONFIG_INTRTIMER_12_5 = 0x06 # Integration Time = 12.5ms, Manual Mode only
|
|
35
|
+
MAX44009_REG_CONFIG_INTRTIMER_6_25 = 0x07 # Integration Time = 6.25ms, Manual Mode only
|
|
36
|
+
|
|
37
|
+
DEVICE = MAX44009_DEFAULT_ADDRESS # I2C sensor address
|
|
38
|
+
|
|
39
|
+
def initialize(i2c_bus='/dev/i2c-1')
|
|
40
|
+
@sensor_name = 'MAX44009'
|
|
41
|
+
@i2c_bus = i2c_bus
|
|
42
|
+
@device = I2CDevice.new(address: DEVICE, driver: I2CDevice::Driver::I2CDev.new(@i2c_bus))
|
|
43
|
+
write_config()
|
|
44
|
+
@length = 2
|
|
45
|
+
@value = read_sensor
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def name
|
|
49
|
+
@sensor_name
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def write_config()
|
|
53
|
+
# Select the configuration register data from the given provided values
|
|
54
|
+
config = (MAX44009_REG_CONFIG_CONTMODE_CONTIN | MAX44009_REG_CONFIG_MANUAL_MODEON | MAX44009_REG_CONFIG_CDR_NODIVIDED | MAX44009_REG_CONFIG_INTRTIMER_800)
|
|
55
|
+
@device.i2cset(MAX44009_REG_CONFIG, config)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def read_sensor
|
|
59
|
+
# Read data back from MAX44009_REG_LUX_HIGH(0x03), 2 bytes, luminance MSB, luminance LSB
|
|
60
|
+
data = @device.i2cget(MAX44009_REG_LUX_HIGH, 2)
|
|
61
|
+
@value = data_to_lux(data)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def lux
|
|
65
|
+
# Return value in LX
|
|
66
|
+
read_sensor
|
|
67
|
+
@value
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
# Convert the data to lux
|
|
72
|
+
def data_to_lux(data)
|
|
73
|
+
exponent = (data[0].ord & 0xF0) >> 4
|
|
74
|
+
mantissa = ((data[0].ord & 0x0F) << 4) | (data[1].ord & 0x0F)
|
|
75
|
+
luminance = ((2 ** exponent) * mantissa) * 0.045
|
|
76
|
+
luminance
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# convert sensor data to a float number
|
|
80
|
+
def to_f(data)
|
|
81
|
+
return ((data[1].ord + (256 * data[0].ord)) / 1.2)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
data/lib/iot/receptor.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require 'rpi_gpio'
|
|
2
|
+
|
|
3
|
+
RPi::GPIO.set_warnings(false)
|
|
4
|
+
RPi::GPIO.set_numbering :bcm
|
|
5
|
+
|
|
6
|
+
module IoT
|
|
7
|
+
# Receptor - physical device that can provide info about real world
|
|
8
|
+
class Receptor
|
|
9
|
+
def initialize
|
|
10
|
+
@model = 'N/A'
|
|
11
|
+
@name = 'N/A'
|
|
12
|
+
@value = nil
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def name
|
|
16
|
+
@name
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def model_name
|
|
20
|
+
@model
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def read
|
|
24
|
+
@value
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
alias read_sensor read
|
|
28
|
+
|
|
29
|
+
protected
|
|
30
|
+
def name=(name)
|
|
31
|
+
@name = name
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def model_name=(name)
|
|
35
|
+
@model_name = name
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
data/lib/iot/sensor.rb
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require_relative 'digital_sensor'
|
|
2
|
+
|
|
3
|
+
module IoT
|
|
4
|
+
# TemperatureSensor - temperature sensing receptor
|
|
5
|
+
class IoT::TemperatureSensor < IoT::DigitalSensor
|
|
6
|
+
@temperature = 0.0
|
|
7
|
+
|
|
8
|
+
def initialize
|
|
9
|
+
@temperature = read_data
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def read_data
|
|
13
|
+
@temperature
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def celsius
|
|
17
|
+
@temperature
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def fahrenheit
|
|
21
|
+
@temperature * 9.0 / 5.0 + 32
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def kelvin
|
|
25
|
+
@temperature + 273.15
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def reaumur
|
|
29
|
+
@temperature * 0.8
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def to_s
|
|
33
|
+
sprintf "%5.2f°C", @temperature
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def temperature(mode=:celsius)
|
|
37
|
+
case mode
|
|
38
|
+
when :celsius
|
|
39
|
+
celsius
|
|
40
|
+
when :fahrenheit
|
|
41
|
+
fahrenheit
|
|
42
|
+
when :kelvin
|
|
43
|
+
kelvin
|
|
44
|
+
when :reaumur
|
|
45
|
+
reaumur
|
|
46
|
+
else
|
|
47
|
+
celsius
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|