Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

HTS221

priv/samples/sensors/hts221.livemd

HTS221

Mix.install([{:hts221, "~> 1.0"}])

Usage

HTS221 is a humidity and temperature sensor that uses an I2C interface.

When controlling the HTS221 there are few setup steps and other checks you may want to do. Also, to keep the transport layer working often times this will call for a GenServer. The HTS221.Server module is meant to provide common functionality around setup and an expose a higher level API for application use:

opts = [transport: {HTS221.Transport.I2C, bus_name: "i2c-1"}]
{:ok, hts_server} = HTS221.Server.start_link(opts)

You can then use the server to read the temperature and humidity on demand.

Temperature is in Celsius by default (ºC).

{:ok, temp} = HTS221.Server.temperature(hts_server)
{:ok, humidity} = HTS221.Server.humidity(hts_server)
temp_f = temp * 9 / 5 + 32

IO.puts("""
Temperature: #{round(temp)}ºC/#{round(temp_f)}ºF
Humidity: #{round(humidity)}%
""")