Raspberry Pi DHT-SwiftyGPIO

10 de March de 2019

Using Swift to display temperature and humidity of a DHT11 module


Raspberry Pi 2+ or Raspberry Pi 3 Model B Model DHT11 or DHT22

Installing Dependencies

To use the provided prebuilt binaries you'll need the install the following dependencies:



sudo apt-get install clang libicu-dev libcurl4-nss-dev




SwiftyGPIO Download last version swift ARM compatible with your board.

See table: Build Swift On ARM prebuilt-binaries and Download




Unzip the files to a folder of your choice: For example: /home/michel/swift-4.2.3/ After unzip, export path:


export PATH=/home/michel/swift-4.2.3/usr/bin:"${PATH}" 


Test if everything went right run command:


swift --version 


And the output should be like this:


Swift version 4.2.3 (swift-4.2.3-RELEASE) Target: armv7-unknown-linux-gnueabihf



Clone and run project


$ git clone 
$ cd DHT-SwiftyGPIO
$ swift run  Waiting output should be like this: 
Temperatura: 25.0 Umidate: 41.0


Reference files

The DHT.swift was based on the project dhtxx But with modifications

DHT class init allows you to select the sensor type and data conversion by default is used Celcius


Enums Types


swift public enum SupportedSensor { 
    case dht11, dht22 

public enum ConversionType { 
    case Fahrenheit, Celsius 

init(pin: GPIO, for sensor: SupportedSensor, conversion: ConversionType = .Celsius) { } 


Example create module DHT11 Object


let dht = DHT(pin: gpios[.P4]!, for: .dht11) 




Initial read errors occur due to initial pulse failure  Usually get a good reading within the past 60s.

See the project on Github DHT-SwiftyGPIO



I hope this project helps start with the creation of IoT projects with Swift



Desenvolvido por Michel Lütz