diff --git a/dht22.c b/dht22.c new file mode 100644 index 0000000..b296007 --- /dev/null +++ b/dht22.c @@ -0,0 +1,105 @@ +/* + * dht22.c: + * Simple test program to test the wiringPi functions + * Based on the existing dht11.c + * Amended by technion@lolware.net + */ + +#include +#include "dht22.h" +#include +#include +#include +#include +#include + + +#define MAXTIMINGS 85 +static int DHTPIN = 7; +static int dht22_dat[5] = {0,0,0,0,0}; + +static uint8_t sizecvt(const int read) +{ + /* digitalRead() and friends from wiringpi are defined as returning a value + *< 256. However, they are returned as int() types. This is a safety function */ + + if (read > 255 || read < 0) + { + printf("Invalid data from wiringPi library\n"); + exit(EXIT_FAILURE); + } + return (uint8_t)read; +} + +static int read_dht22_dat(int pin, sensor *s) +{ + uint8_t laststate = HIGH; + uint8_t counter = 0; + uint8_t j = 0, i; + DHTPIN = pin; + + dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0; + + // pull pin down for 18 milliseconds + pinMode(DHTPIN, OUTPUT); + digitalWrite(DHTPIN, HIGH); + delay(10); + digitalWrite(DHTPIN, LOW); + delay(18); + // then pull it up for 40 microseconds + digitalWrite(DHTPIN, HIGH); + delayMicroseconds(40); + // prepare to read the pin + pinMode(DHTPIN, INPUT); + + // detect change and read data + for ( i=0; i< MAXTIMINGS; i++) { + counter = 0; + while (sizecvt(digitalRead(DHTPIN)) == laststate) { + counter++; + delayMicroseconds(1); + if (counter == 255) { + break; + } + } + laststate = sizecvt(digitalRead(DHTPIN)); + + if (counter == 255) break; + + // ignore first 3 transitions + if ((i >= 4) && (i%2 == 0)) { + // shove each bit into the storage bytes + dht22_dat[j/8] <<= 1; + if (counter > 16) + dht22_dat[j/8] |= 1; + j++; + } + } + + // check we read 40 bits (8bit x 5 ) + verify checksum in the last byte + // print it out if data is good + if ((j >= 40) && + (dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF)) ) { + float t, h; + h = (float)dht22_dat[0] * 256 + (float)dht22_dat[1]; + h /= 10; + t = (float)(dht22_dat[2] & 0x7F)* 256 + (float)dht22_dat[3]; + t /= 10.0; + if ((dht22_dat[2] & 0x80) != 0) t *= -1; + + //printf("Humidity = %.2f %% Temperature = %.2f *C \n", h, t ); + s->humidity = h; + s->temperature = t; + return 1; + } else { + //printf("Data not good, skip\n"); + return 0; + } +} + +void getData(sensor *s) { + int tries = 100; + while (read_dht22_dat(s) == 0 && tries--) { + delay(1000); + } +} diff --git a/dht22.h b/dht22.h new file mode 100644 index 0000000..dee7d86 --- /dev/null +++ b/dht22.h @@ -0,0 +1,9 @@ + + + +typedef struct { + float humidity; + float temperature; +} sensor; + +void getData(int pin, sensor *s); diff --git a/main.c b/main.c new file mode 100644 index 0000000..55085ca --- /dev/null +++ b/main.c @@ -0,0 +1,21 @@ +#include +#include +#include "dht22.h" +#include +#include +int main(void) { + if (wiringPiSetup () == -1) + exit(EXIT_FAILURE) ; + + if (setuid(getuid()) < 0) + { + perror("Dropping privileges failed\n"); + exit(EXIT_FAILURE); + } + + sensor data; + + getData(7, &data); + printf("%.2f, %.2f\n", data.temperature, data.humidity); + return 0; +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..352e807 --- /dev/null +++ b/makefile @@ -0,0 +1,25 @@ +#Infos: http://www.ijon.de/comp/tutorials/makefile.html + + +VERSION = 1.0 +CC = /usr/bin/gcc +CFLAGS = -Wall -g -D_REENTRANT -DVERSION=\"$(VERSION)\" +#LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread +LDFLAGS = -lwiringPi + +OBJ = main.o dht22.o + +dht22: $(OBJ) + $(CC) $(CFLAGS) -o dht22 $(OBJ) $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +.PHONY: clean +clean: + rm -r *.o + +install: + sudo cp -r lcdclock /usr/local/bin + sudo cp -r playsong.sh /usr/local/bin + sudo cp -r settings.cfg /usr/local/bin