dht v0.1.0 DHT View Source

You need to specify the GPIO pin number and sensor type when taking a reading. The sensor type can be a string, atom, or integer representation of the target sensor:

iex()> DHT.read(6, :dht22)
{:ok, %{temperature: 22.6, humidity: 50.5}}

iex()> DHT.read(6, "dht22")
{:ok, %{temperature: 22.6, humidity: 50.5}}

iex()> DHT.read(6, 22)
{:ok, %{temperature: 22.6, humidity: 50.5}}

iex()> DHT.read(6, "22")
{:ok, %{temperature: 22.6, humidity: 50.5}}

DHT also supports polling at regular intervals which outputs :telemetry events:

  • [:dht, :read]
    • message is a map with :temperature and :humidity keys
    • metadata is map with :pin and :sensor keys
  • [:dht, :error]
    • message is a map with :error key containing the failure message
    • metadata is map with :pin and :sensor keys

The polling period defaults to 2 seconds, which is the minimum rate allowed for DHT sensors, but you can specify longer:

# Poll DHT22 on GPIO 6 every 30 seconds
iex()> DHT.start_polling(6, :dht22, 30)
{:ok, #PID<0.233.0>}

Once polling, you can attach to the read events view :telemetry.attach/4

defmodule MyWatcher do
  def inspect_it(a, b, c, d) do
    IO.inspect(a)
    IO.inspect(b)
    IO.inspect(c)
    IO.inspect(d)
  end
end

:telemetry.attach("im-attached", [:dht, :read], &MyWatcher.inspect_it/4, nil)

Or do whatever else it is that you cool cats 😸 do with telemetry 😉

Link to this section Summary

Functions

Take a reading on the specified pin for a sensor type

Start polling of readings at specified period intervals that are delivered as telemtry events
  • [:dht, :read]
    • message is a map with :temperature and :humidity keys
    • metadata is map with :pin and :sensor keys
  • [:dht, :error]
    • message is a map with :error key containing the failure message
    • metadata is map with :pin and :sensor keys

The polling period defaults to 2 seconds, which is the minimum rate allowed for DHT sensors, but you can specify longer

Link to this section Types

Specs

period() :: non_neg_integer()

Specs

pin() :: non_neg_integer()

Specs

reading() :: %{temperature: float(), humidity: float()}

Specs

sensor() :: :am2302 | :dht11 | :dht22 | 11 | 22

Link to this section Functions

Specs

read(pin(), sensor()) ::
  {:ok, reading()}
  | {:error, %ArgumentError{__exception__: term(), message: term()}}
  | {:error, integer()}

Take a reading on the specified pin for a sensor type

You need to specify the GPIO pin number and sensor type when taking a reading. The sensor type can be a string, atom, or integer representation of the target sensor:

iex()> DHT.read(6, :dht22)
{:ok, %{temperature: 22.6, humidity: 50.5}}

iex()> DHT.read(6, "dht22")
{:ok, %{temperature: 22.6, humidity: 50.5}}

iex()> DHT.read(6, 22)
{:ok, %{temperature: 22.6, humidity: 50.5}}

iex()> DHT.read(6, "22")
{:ok, %{temperature: 22.6, humidity: 50.5}}
Link to this function

start_polling(pin, sensor, period \\ 2)

View Source

Specs

start_polling(pin(), sensor(), period()) ::
  DynamicSupervisor.on_start_child()
  | {:error, %ArgumentError{__exception__: term(), message: term()}}
Start polling of readings at specified period intervals that are delivered as telemtry events
  • [:dht, :read]
    • message is a map with :temperature and :humidity keys
    • metadata is map with :pin and :sensor keys
  • [:dht, :error]
    • message is a map with :error key containing the failure message
    • metadata is map with :pin and :sensor keys

The polling period defaults to 2 seconds, which is the minimum rate allowed for DHT sensors, but you can specify longer:

# Poll DHT22 on GPIO 6 every 30 seconds
iex()> DHT.start_polling(6, :dht22, 30)
{:ok, #PID<0.233.0>}

Once polling, you can attach to the read events view :telemetry.attach/4

defmodule MyWatcher do
  def inspect_it(a, b, c, d) do
    IO.inspect(a)
    IO.inspect(b)
    IO.inspect(c)
    IO.inspect(d)
  end
end

:telemetry.attach("im-attached", [:dht, :read], &MyWatcher.inspect_it/4, nil)

Or do whatever else it is that you cool cats 😸 do with telemetry 😉

Link to this function

stop_polling(pin, sensor)

View Source

Specs

stop_polling(pin(), sensor()) :: :ok | {:error, :not_found}