Portions adapted from C sample code provided by the nice people at AdaFruit.
DHT is provided under the MIT license. See the LICENSE file for more info.
Requires the package libgpiod-dev (note: this MUST be version 2, the version 1 API is incompatible and will cause compiler errors)
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")
}
}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"]),