Current section

Files

Jump to
nerves_livebook priv samples sensors hts221.livemd
Raw

priv/samples/sensors/hts221.livemd

# HTS221
```elixir
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:
```elixir
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`).
```elixir
{: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)}%
""")
```