# HG changeset patch # User Michiel Broek # Date 1399581411 -7200 # Node ID 89dd2b6917019c959adfc58c5206afdd48764e39 # Parent 9c7119ac0455181db45a7bf5c83aea8a1a7b0da4 Added library code to read DHT11 temperature/humidity sensor diff -r 9c7119ac0455 -r 89dd2b691701 .hgignore --- a/.hgignore Thu May 08 15:09:56 2014 +0200 +++ b/.hgignore Thu May 08 22:36:51 2014 +0200 @@ -10,6 +10,7 @@ rc433/send rc433/sniffer coolers/coolers +dht11/dht11 syntax: glob *.o diff -r 9c7119ac0455 -r 89dd2b691701 configure --- a/configure Thu May 08 15:09:56 2014 +0200 +++ b/configure Thu May 08 22:36:51 2014 +0200 @@ -2028,11 +2028,11 @@ ac_config_headers="$ac_config_headers config.h" -SUBDIRS="lib coolers rc433 thermometers" +SUBDIRS="lib coolers dht11 rc433 thermometers" PACKAGE="mbsePi-apps" -VERSION="0.0.4" +VERSION="0.0.5" COPYRIGHT="Copyright (C) 2014 Michiel Broek, All Rights Reserved" CYEARS="2014" diff -r 9c7119ac0455 -r 89dd2b691701 configure.ac --- a/configure.ac Thu May 08 15:09:56 2014 +0200 +++ b/configure.ac Thu May 08 22:36:51 2014 +0200 @@ -2,13 +2,13 @@ AC_INIT(thermometers/main.c) AM_CONFIG_HEADER(config.h) -SUBDIRS="lib coolers rc433 thermometers" +SUBDIRS="lib coolers dht11 rc433 thermometers" AC_SUBST(SUBDIRS) dnl General settings dnl After changeing the version number, run autoconf! PACKAGE="mbsePi-apps" -VERSION="0.0.4" +VERSION="0.0.5" COPYRIGHT="Copyright (C) 2014 Michiel Broek, All Rights Reserved" CYEARS="2014" AC_SUBST(PACKAGE) diff -r 9c7119ac0455 -r 89dd2b691701 dht11/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dht11/Makefile Thu May 08 22:36:51 2014 +0200 @@ -0,0 +1,58 @@ +# Makefile for the mbsePi-apps/thermometers. + +include ../Makefile.global + +SRCS = dht11.c +HDRS = dht11.h +OBJS = dht11.o +SLIBS = ../lib/libmbse.a +TARGET = dht11 +OTHER = Makefile + +############################################################################# + +.c.o: + ${CC} ${CFLAGS} ${INCLUDES} ${DEFINES} -c $< + +all: ${TARGET} + +dht11: dht11.o ${SLIBS} + ${CC} -o dht11 dht11.o ${LDFLAGS} ${LIBS} ${SLIBS} + +clean: + rm -f ${TARGET} *.o *.h~ *.c~ core filelist Makefile.bak + +install: all + ${INSTALL} -c -s -g root -o root -m 0755 ${TARGET} ${BINDIR} + +filelist: Makefile + BASE=`pwd`; \ + BASE=`basename $${BASE}`; \ + (for f in ${SRCS} ${HDRS} ${OTHER} ;do echo ${PACKAGE}-${VERSION}/$${BASE}/$$f; done) >filelist + +depend: + @rm -f Makefile.bak; \ + mv Makefile Makefile.bak; \ + sed -e '/^# DO NOT DELETE/,$$d' Makefile.bak >Makefile; \ + ${ECHO} '# DO NOT DELETE THIS LINE - MAKE DEPEND RELIES ON IT' \ + >>Makefile; \ + ${ECHO} '# Dependencies generated by make depend' >>Makefile; \ + for f in ${SRCS}; \ + do \ + ${ECHO} "Dependencies for $$f:\c"; \ + ${ECHO} "`basename $$f .c`.o:\c" >>Makefile; \ + for h in `sed -n -e \ + 's/^#[ ]*include[ ]*"\([^"]*\)".*/\1/p' $$f`; \ + do \ + ${ECHO} " $$h\c"; \ + ${ECHO} " $$h\c" >>Makefile; \ + done; \ + ${ECHO} " done."; \ + ${ECHO} "" >>Makefile; \ + done; \ + ${ECHO} '# End of generated dependencies' >>Makefile + +# DO NOT DELETE THIS LINE - MAKE DEPEND RELIES ON IT +# Dependencies generated by make depend +dht11.o: ../lib/mbselib.h dht11.h +# End of generated dependencies diff -r 9c7119ac0455 -r 89dd2b691701 dht11/dht11.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dht11/dht11.c Thu May 08 22:36:51 2014 +0200 @@ -0,0 +1,58 @@ +/***************************************************************************** + * Copyright (C) 2014 + * + * Michiel Broek + * + * This file is part of the mbsePi-apps + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * mbsePi-apps is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EC-65K; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "../lib/mbselib.h" +#include "dht11.h" + +#ifdef HAVE_WIRINGPI_H + +extern int dht11_valid; +extern int dht11_temperature; +extern int dht11_humidity; + +int main(int argc, char *argv[]) { + + int PIN = 3; + + if (wiringPiSetup() == -1) + return 0; + + dht11Init(PIN, 0, 6); + dht11Read(); + if (dht11_valid) { + fprintf(stdout, "DHT11: temperature %d degrees, humidity %d%%\n", dht11_temperature, dht11_humidity); + } else { + fprintf(stdout, "DHT11: no valid data or sensor not connected\n"); + } + + exit(0); +} + +#else + +int main(int argc, char *argv[]) { + fprintf(stderr, "This program does nothing without the wiringPi library\n"); + return 0; +} + +#endif + diff -r 9c7119ac0455 -r 89dd2b691701 dht11/dht11.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dht11/dht11.h Thu May 08 22:36:51 2014 +0200 @@ -0,0 +1,9 @@ +#ifndef _DHT11_H +#define _DHT11_H + + +#define TRUE 1 +#define FALSE 0 + + +#endif diff -r 9c7119ac0455 -r 89dd2b691701 lib/Makefile --- a/lib/Makefile Thu May 08 15:09:56 2014 +0200 +++ b/lib/Makefile Thu May 08 22:36:51 2014 +0200 @@ -2,8 +2,8 @@ include ../Makefile.global -SRCS = xutil.c rdconfig.c lcd-pcf8574.c rc-switch.c -OBJS = xutil.o rdconfig.o lcd-pcf8574.o rc-switch.o +SRCS = xutil.c rdconfig.c lcd-pcf8574.c rc-switch.c dht11.c +OBJS = xutil.o rdconfig.o lcd-pcf8574.o rc-switch.o dht11.o HDRS = mbselib.h TARGET = libmbse.a @@ -48,4 +48,5 @@ rdconfig.o: ../config.h mbselib.h lcd-pcf8574.o: ../config.h mbselib.h rc-switch.o: ../config.h mbselib.h +dht11.o: ../config.h mbselib.h # End of generated dependencies diff -r 9c7119ac0455 -r 89dd2b691701 lib/dht11.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/dht11.c Thu May 08 22:36:51 2014 +0200 @@ -0,0 +1,167 @@ +/***************************************************************************** + * Copyright (C) 2008-2014 + * + * Michiel Broek + * + * This file is part of the mbsePi-apps + * + * Parts original by CurlyMo from the pilight project. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * mbsePi-apps is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EC-65K; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "../config.h" +#include "mbselib.h" + +#ifdef HAVE_WIRINGPI_H + +#define MAXTIMINGS 100 + +int dht11_pin = -1; +int dht11_temperature = -1; +int dht11_humidity = -1; +int dht11_valid = false; +int dht11_t_offset = 0; +int dht11_h_offset = 0; + + + +static uint8_t sizecvt(const int read_value) { + /* + * 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_value > 255 || read_value < 0) { + syslog(LOG_NOTICE, "invalid data from wiringPi library"); + } + + return (uint8_t)read_value; +} + + + +/* + * DHT11 sensor read. This should be used in a thread loop. + */ +void dht11Read(void) { + int tries = 5; + unsigned short got_correct_data = 0; + + if (dht11_pin == -1) + return; + + while (tries && !got_correct_data) { + uint8_t laststate = HIGH; + uint8_t counter = 0; + uint8_t j = 0, i = 0; + int dht11_dat[5] = {0,0,0,0,0}; + + /* + * Select output mode to send the start signal. + */ + pinMode(dht11_pin, OUTPUT); + digitalWrite(dht11_pin, HIGH); + usleep(1000); + + /* + * Low for at least 18 milliseconds + */ + digitalWrite(dht11_pin, LOW); + usleep(20000); + digitalWrite(dht11_pin, HIGH); + pinMode(dht11_pin, INPUT); + + /* + * Detect change and read data + */ + for (i=0; i= 4) && (i%2 == 0)) { + + // shove each bit into the storage bytes + dht11_dat[(int)((double)j/8)] <<= 1; + if (counter > 16) + dht11_dat[(int)((double)j/8)] |= 1; + j++; + } + } + + /* + * If there is no sensor, j = 0 + */ + if ((counter == 255) && (j == 0)) { + if (dht11_temperature != -1) { + syslog(LOG_NOTICE, "dht11 sensor disappeared"); + } + dht11_temperature = -1; + dht11_humidity = -1; + dht11_valid = false; + } else { + + /* + * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte + * print it out if data is good + */ + if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) { + got_correct_data = 1; + + int h = dht11_dat[0] + dht11_dat[1]; + int t = (dht11_dat[2] & 0x7F) + dht11_dat[3]; + t += dht11_t_offset; + h += dht11_h_offset; + + if ((dht11_dat[2] & 0x80) != 0) + t *= -1; + + dht11_temperature = t; + dht11_humidity = h; + dht11_valid = true; + } else { + tries--; + if (tries == 0) + syslog(LOG_INFO, "dht11 data checksum was wrong 5 times"); + usleep(100000); + } + } + } +} + + + +void dht11Init(int pin, int t_offset, int h_offset) { + dht11_pin = pin; + dht11_t_offset = t_offset; + dht11_h_offset = h_offset; +} + + +#endif + diff -r 9c7119ac0455 -r 89dd2b691701 lib/mbselib.h --- a/lib/mbselib.h Thu May 08 15:09:56 2014 +0200 +++ b/lib/mbselib.h Thu May 08 22:36:51 2014 +0200 @@ -128,6 +128,12 @@ char *dec2binWzerofill(unsigned long, unsigned int); + +/* dht11.c */ +void dht11Read(void); +void dht11Init(int, int, int); + + #endif