Skip to content

Commit

Permalink
Add specific example for M5Stack ENVIII unit
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Oct 8, 2023
1 parent 1693140 commit 89ede47
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions examples/advanced/m5_env.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion examples/sensor/sht3x.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 89ede47

Please sign in to comment.