Skip to content

Latest commit

 

History

History
36 lines (27 loc) · 1.17 KB

File metadata and controls

36 lines (27 loc) · 1.17 KB

DHT

Read temperature and humidity from DHT11 and DHT22 sensors on a Raspberry Pi in Swift.

Portions adapted from C sample code provided by the nice people at AdaFruit.

License:

DHT is provided under the MIT license. See the LICENSE file for more info.

Linux preconditions:

Requires the package libgpiod-dev (note: this MUST be version 2, the version 1 API is incompatible and will cause compiler errors)

Example:

This reads from GPIO line 4 every five seconds, supplying a moving average after every twelve successful reads:

import DHT

Task {
    let stream = DHT.SampleStream(line: 4, device: .dht22, samplesToAverage: 12, samplePeriod = .seconds(5))
    for try await sample in stream {
        print("humidity: \(Double(sample.humidity)*0.1)%, temperature: \(Double(sample.temperature)*0.1)°C")
    }
}

Use:

To add DHT to your project, declare a dependency in your Package.swift file,

.package(url: "https://github.com/nallick/DHT.git", from: "3.0.0"),

and add the dependency to your target:

.target(name: "MyProjectTarget", dependencies: ["DHT"]),