diff --git a/examples/advanced/m5_env.rb b/examples/advanced/m5_env.rb new file mode 100644 index 0000000..3e754d6 --- /dev/null +++ b/examples/advanced/m5_env.rb @@ -0,0 +1,41 @@ +# +# This example combines the SHTX and QMP6988 examples. The M5Stack ENV III unit +# contains both sensors, accessible over a single I2C connection. +# +require 'bundler/setup' +require 'denko' + +board = Denko::Board.new(Denko::Connection::Serial.new) +bus = Denko::I2C::Bus.new(board: board, pin: :SDA) +sht = Denko::Sensor::SHT3X.new(bus: bus) # address: 0x44 default +qmp = Denko::Sensor::QMP6988.new(bus: bus) # address: 0x70 default + +# Configure for higher accuracy. +sht.repeatability = :high +qmp.temperature_samples = 2 +qmp.pressure_samples = 16 +qmp.iir_coefficient = 2 + +# Get the shared #print_tph_reading method to print readings neatly. +require_relative 'neat_tph_readings' + +# How many degrees C the two temperature values can differ by before a warning. +TOLERANCE = 0.50 + +loop do + # Read both sensors. + qmp_reading = qmp.read + sht_reading = sht.read + + # Warn if large gap between temperature readings. + difference = (qmp_reading[:temperature] - sht_reading[:temperature]).abs + if (difference > TOLERANCE) + puts "WARNING: temperature values differed by more than #{TOLERANCE}\xC2\xB0C (#{difference.round(4)} \xC2\xB0C actual)" + end + + # Combine values from both sensors, averaging their temperatures. + average_temperature = (qmp_reading[:temperature] + sht_reading[:temperature]) / 2.0 + print_tph_reading(temperature: average_temperature, humidity: sht_reading[:humidity], pressure: qmp_reading[:pressure]) + + sleep 5 +end diff --git a/examples/sensor/sht3x.rb b/examples/sensor/sht3x.rb index e59ed7a..c646593 100644 --- a/examples/sensor/sht3x.rb +++ b/examples/sensor/sht3x.rb @@ -6,7 +6,7 @@ board = Denko::Board.new(Denko::Connection::Serial.new) bus = Denko::I2C::Bus.new(board: board, pin: :SDA) -sensor = Denko::Sensor::SHT3X.new(bus: bus) +sensor = Denko::Sensor::SHT3X.new(bus: bus) # address: 0x44 default # Heater control sensor.heater_on