1uke_ wrote: ↑Sat Apr 13, 2019 12:57 pm
knute wrote: ↑Fri Apr 05, 2019 1:58 am
The simplest way is to use the dht11 overlay. You just read the data out of a file. It works fine.
Thanks for the reply knute,
Do you have some instructions you could link me to for using the dht11 overlay?
Thank you
Turn off your pi and disconnect the power.
Connect the + lead of your DHT sensor to the 3.3V pin, connect the - lead to a GND pin and the signal/out/data pin to GPIO 4 (you can use others but to keep this simple use GPIO 4 for now). If you are using the sensor on the little PC board you don't need a pull up resistor. If you are using the bare sensor you need a 5.1K ohm resistor between the + and the signal wire. A 4.7K to a 10K will probably work just fine. This overlay works with the DHT11, DHT22 and AM2302 sensors and probably some other similar ones.
Power up your PI
Edit the /boot/config.txt file and add this line:
dtoverlay=dht11
Reboot your Pi
The temperature will be found in the file:
/sys/bus/iio/devices/iio:device0/in_temp_input
and the humidity will be in the file:
/sys/bus/iio/devices/iio:device0/in_humidityrelative_input
There is one caveat with this technique. When you read the temperature file you will occasionally get an error/exception. I think this is because the overlay software is writing to the file but that is only a guess on my part. So you should wrap your reading code in a loop to make sure you successfully read the data. I normally read the temperature first and so if that is successful I rarely get an error reading the humidity but I assume it is possible.
The data you read from the file(s) is a 5 character string that needs to be converted to a decimal value. Convert both strings to a floating point type and divide both the temperature and humidity by 1000.0 to get the actual values. Temperature is in degrees Celsius. If you prefer Kelvin you will have to make the appropriate adjustment.
Even after accounting for the occasional error reading the files I have seen some bad data show up but only every few days of reading the files every few minutes.
I'm not a python guy but here are a Java program and a C program you can use to see how I read the data.
Code: Select all
import java.nio.file.*;
public class dht11 {
public static void main(String... args) {
String dir = "/sys/bus/iio/devices/iio:device0";
String temp = "in_temp_input";
String hum = "in_humidityrelative_input";
boolean done = false;
while (!done) {
try {
String temperature = Files.lines(Paths.get(dir,temp)).
findFirst().orElse("0");
String humidity = Files.lines(Paths.get(dir,hum)).
findFirst().orElse("0");
System.out.printf(
"Temperature: %.1fC %.1fF Humidity: %.1f%%\n",
Double.valueOf(temperature) / 1000.0,
Double.valueOf(temperature) / 1000.0 * 9.0 / 5.0 + 32.0,
Double.valueOf(humidity) / 1000.0);
done = true;
} catch (Exception ioe) {
//System.out.println(ioe);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
FILE *tf;
FILE *hf;
char *temp_file = "/sys/bus/iio/devices/iio:device0/in_temp_input";
char *hum_file = "/sys/bus/iio/devices/iio:device0/in_humidityrelative_input";
char buf[16];
double temp;
double hum;
bool done = false;
int main(int argc,char *argv[]) {
while (!done) {
if ((tf = fopen(temp_file,"r")) != NULL) {
if (fgets(buf,sizeof(buf),tf) != NULL) {
temp = atof(buf) / 1000.0;
if ((hf = fopen(hum_file,"r")) != NULL) {
if (fgets(buf,sizeof(buf),hf) != NULL) {
hum = atof(buf)/ 1000.0;
done = true;
} else {
usleep(500000);
}
fclose(hf);
}
} else {
usleep(500000);
}
fclose(tf);
}
}
printf("Temperature: %.1fC %.1fF Humidity: %.1f%%\n",
temp,temp * 9.0 / 5.0 + 32.0 ,hum);
return 0;
}