Class: IoT::BMxSensor

Inherits:
HumitureSensor show all
Defined in:
lib/iot/bmx_sensor.rb

Overview

Bosch BMP/BME humidity, temperature & atmospheric pressure sensor family

Direct Known Subclasses

BME280, BMP280

Instance Method Summary collapse

Methods inherited from HumitureSensor

#humidity

Methods inherited from TemperatureSensor

#celsius, #fahrenheit, #kelvin, #reaumur, #temperature

Methods inherited from DigitalReceptor

one_wire_file

Methods inherited from Receptor

#model_name, #read

Constructor Details

#initialize(sensor_name = 'BMx280', bus = 1) ⇒ BMxSensor

Returns a new instance of BMxSensor.



9
10
11
12
13
14
15
# File 'lib/iot/bmx_sensor.rb', line 9

def initialize(sensor_name='BMx280', bus=1)
  @sensor_name = sensor_name

  device = I2C::Dev.create("/dev/i2c-#{bus}")
  @sensor = I2C::Driver::BME280.new(device: device)
  @temperature, @humidity, @pressure = read_data
end

Instance Method Details

#hPaObject

standard atmospheric pressure @ sea level = 1.01325 bar = 1013.25 mbar = 101.325 kPa



30
31
32
# File 'lib/iot/bmx_sensor.rb', line 30

def hPa
  @pressure
end

#nameObject



25
26
27
# File 'lib/iot/bmx_sensor.rb', line 25

def name
  @sensor_name
end

#pressure(mode = :hPa) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/iot/bmx_sensor.rb', line 34

def pressure(mode=:hPa)
  case mode
  when :hPa       # 1 hPa = 100 Pa = 1 mbar = 1 hPa = 0.750062 mmHg [@ 0°C]
    hPa
  when :kPa       # 1 kPa = 1000 Pa = 10 hPa
    hPa / 10.0
  when :Pa        # 1 Pa = 0.01 hPa = 0.001 kPa
    hPa * 100.0
  when :mmHg      # 1 mmHg = 1.3332236842105263 hPa = 133.322387415 Pa = 1.000000142466321... Torr
    hPa / (1013.25 / 760) # 760 mmHg = 101.3250144354 kPa
  when :atm       # 1 atm = 101325 Pa = 101.325 kPa
    hPa / 1013.25
  when :bar       # 1 bar ≡ 100000 Pa = 1000 hPa = 0.987 atm = 750.06 mmHg = 750.06 Torr
    hPa / 1000.0
  when :mbar      # 1 mbar = 0.001 bar
    bar * 1000.0
  when :Torr      # 1 Torr = 1/760 atm = 101325/760 Pa = 0.999999857533699... mmHg
    (hPa / 1013.25) / 760
  else
    hPa
  end
end

#read_dataObject



17
18
19
20
21
22
23
# File 'lib/iot/bmx_sensor.rb', line 17

def read_data
  data = @sensor.all
  @temperature = data[:t]
  @pressure = data[:p]
  @humidity = data[:h]
  [@temperature, @humidity, @pressure]
end

#to_sObject



57
58
59
# File 'lib/iot/bmx_sensor.rb', line 57

def to_s
  sprintf "%5.2f°C, %5.2f %%, %5.2f mmHg", @temperature, @humidity, pressure(:mmHg)
end