Skip to content

Commit

Permalink
rename project
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jan 23, 2016
1 parent e230dd8 commit e4a3cf7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 87 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# simple-dht
# SimpleDHT

Simple, Stable and Fast Arduino Temp & Humidity Sensors for [DHT11 etc](http://learn.adafruit.com/dht).
Simple, Stable and Fast Arduino Temp & Humidity Sensors for
[DHT11 etc](http://learn.adafruit.com/dht).

1. Simple: Simple pure C code with lots of comments.
1. Simple: Simple C++ code with lots of comments.
1. Stable: Strictly follow the standard DHT protocol.
1. Fast: Support 0.5HZ or 1HZ sampling rate.

## Usage

To use this library:

1. Download the zip from specified version: https://github.com/winlinvip/simple-dht/releases
1. Download the zip from specified version: https://github.com/winlinvip/SimpleDHT/releases
2. Import to Arduino: Arduino => Sketch => Include Library => Add .ZIP Library...
3. Open example: Arduino => File => Examples => Simple DHT sensor library => DHT11Default
4. Connect the DHT11 and Upload program to Arduino.
Expand Down
66 changes: 16 additions & 50 deletions SimpleDHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@ SOFTWARE.

#include "SimpleDHT.h"

// confirm the OUTPUT is level in us,
// for example, when DHT11 start sample, it will
// 1. PULL LOW 80us, call confirm(pin, 80, LOW)
// 2. PULL HIGH 80us, call confirm(pin, 80, HIGH)
// @return 0 success; oterwise, error.
// @remark should never used to read bits,
// for function call use more time, maybe never got bit0.
// @remark please use simple_dht11_read().
int __simple_dht11_confirm(int pin, int us, byte level) {
int SimpleDHT11::confirm(int pin, int us, byte level) {
// wait one more count to ensure.
int cnt = us / 10 + 1;

Expand All @@ -51,22 +43,15 @@ int __simple_dht11_confirm(int pin, int us, byte level) {
return 0;
}

// @data the bits of a byte.
// @remark please use simple_dht11_read().
byte __simple_dht11_bits2byte(byte data[8]) {
byte SimpleDHT11::bits2byte(byte data[8]) {
byte v = 0;
for (int i = 0; i < 8; i++) {
v += data[i] << (7 - i);
}
return v;
}

// read temperature and humidity from dht11.
// @param pin the pin for DHT11, for example, 2.
// @param data a byte[40] to read bits to 5bytes.
// @return 0 success; otherwise, error.
// @remark please use simple_dht11_read().
int __simple_dht11_sample(int pin, byte data[40]) {
int SimpleDHT11::sample(int pin, byte data[40]) {
// empty output data.
memset(data, 0, 40);

Expand All @@ -84,10 +69,10 @@ int __simple_dht11_sample(int pin, byte data[40]) {
// DHT11 starting:
// 1. PULL LOW 80us
// 2. PULL HIGH 80us
if (__simple_dht11_confirm(pin, 80, LOW)) {
if (confirm(pin, 80, LOW)) {
return 100;
}
if (__simple_dht11_confirm(pin, 80, HIGH)) {
if (confirm(pin, 80, HIGH)) {
return 101;
}

Expand All @@ -96,7 +81,7 @@ int __simple_dht11_sample(int pin, byte data[40]) {
// 2. PULL HIGH 26-28us, bit(0)
// 3. PULL HIGH 70us, bit(1)
for (int j = 0; j < 40; j++) {
if (__simple_dht11_confirm(pin, 50, LOW)) {
if (confirm(pin, 50, LOW)) {
return 102;
}

Expand All @@ -120,21 +105,19 @@ int __simple_dht11_sample(int pin, byte data[40]) {

// DHT11 EOF:
// 1. PULL LOW 50us.
if (__simple_dht11_confirm(pin, 50, LOW)) {
if (confirm(pin, 50, LOW)) {
return 104;
}

return 0;
}

// parse the 40bits data to temperature and humidity.
// @remark please use simple_dht11_read().
int __simple_dht11_parse(byte data[40], byte* ptemperature, byte* phumidity) {
byte humidity = __simple_dht11_bits2byte(data);
byte humidity2 = __simple_dht11_bits2byte(data + 8);
byte temperature = __simple_dht11_bits2byte(data + 16);
byte temperature2 = __simple_dht11_bits2byte(data + 24);
byte check = __simple_dht11_bits2byte(data + 32);
int SimpleDHT11::parse(byte data[40], byte* ptemperature, byte* phumidity) {
byte humidity = bits2byte(data);
byte humidity2 = bits2byte(data + 8);
byte temperature = bits2byte(data + 16);
byte temperature2 = bits2byte(data + 24);
byte check = bits2byte(data + 32);
byte expect = humidity + humidity2 + temperature + temperature2;
if (check != expect) {
return 105;
Expand All @@ -144,34 +127,17 @@ int __simple_dht11_parse(byte data[40], byte* ptemperature, byte* phumidity) {
return 0;
}

// print the raw data of dht11, 5bytes(40bits).
void simple_dht11_serial_print(byte data[40]) {
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
}
Serial.println("");
}

// to read from dht11.
// @param pin the DHT11 pin.
// @param ptemperature output, NULL to igore.
// @param phumidity output, NULL to ignore.
// @param pdata output 40bits sample, NULL to ignore.
// @remark the min delay for this method is 200ms.
int simple_dht11_read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]) {
int SimpleDHT11::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]) {
int ret = 0;

byte data[40] = {0};
if ((ret = __simple_dht11_sample(pin, data)) != 0) {
if ((ret = sample(pin, data)) != 0) {
return ret;
}

byte temperature = 0;
byte humidity = 0;
if ((ret = __simple_dht11_parse(data, &temperature, &humidity)) != 0) {
if ((ret = parse(data, &temperature, &humidity)) != 0) {
return ret;
}

Expand Down
68 changes: 50 additions & 18 deletions SimpleDHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,57 @@
SOFTWARE.
*/

#ifndef SIMPLE_DHT_H
#define SIMPLE_DHT_H

// Arduino only.
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#ifndef __SIMPLE_DHT_H
#define __SIMPLE_DHT_H

#include <Arduino.h>

/*
Simple DHT11
Simple, Stable and Fast DHT11 library.
The circuit:
* VCC: 5V or 3V
* GND: GND
* DATA: Digital ping, for example, 2.
23 Jan 2016 By winlin <[email protected]>
// to read from dht11.
// @param pin the DHT11 pin.
// @param ptemperature output, NULL to igore.
// @param phumidity output, NULL to ignore.
// @param pdata output 40bits sample, NULL to ignore.
// @remark the min delay for this method is 200ms.
extern int simple_dht11_read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]);
https://github.com/winlinvip/SimpleDHT#usage
// print the raw data of dht11, 5bytes(40bits).
extern void simple_dht11_serial_print(byte data[40]);
*/
class SimpleDHT11 {
public:
// to read from dht11.
// @param pin the DHT11 pin.
// @param ptemperature output, NULL to igore.
// @param phumidity output, NULL to ignore.
// @param pdata output 40bits sample, NULL to ignore.
// @remark the min delay for this method is 1s.
int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]);
private:
// confirm the OUTPUT is level in us,
// for example, when DHT11 start sample, it will
// 1. PULL LOW 80us, call confirm(pin, 80, LOW)
// 2. PULL HIGH 80us, call confirm(pin, 80, HIGH)
// @return 0 success; oterwise, error.
// @remark should never used to read bits,
// for function call use more time, maybe never got bit0.
// @remark please use simple_dht11_read().
int confirm(int pin, int us, byte level);
// @data the bits of a byte.
// @remark please use simple_dht11_read().
byte bits2byte(byte data[8]);
// read temperature and humidity from dht11.
// @param pin the pin for DHT11, for example, 2.
// @param data a byte[40] to read bits to 5bytes.
// @return 0 success; otherwise, error.
// @remark please use simple_dht11_read().
int sample(int pin, byte data[40]);
// parse the 40bits data to temperature and humidity.
// @remark please use simple_dht11_read().
int parse(byte data[40], byte* ptemperature, byte* phumidity);
};

#endif
3 changes: 2 additions & 1 deletion examples/DHT11Default/DHT11Default.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// GND: GND
// DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dh11;

void setup() {
Serial.begin(115200);
Expand All @@ -18,7 +19,7 @@ void loop() {
// read without samples.
byte temperature = 0;
byte humidity = 0;
if (simple_dht11_read(pinDHT11, &temperature, &humidity, NULL)) {
if (dh11.read(pinDHT11, &temperature, &humidity, NULL)) {
Serial.print("Read DHT11 failed.");
return;
}
Expand Down
8 changes: 7 additions & 1 deletion examples/DHT11WithRawBits/DHT11WithRawBits.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ void loop() {
}

Serial.print("Sample RAW Bits: ");
simple_dht11_serial_print(data);
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
}
Serial.println("");

Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Expand Down
15 changes: 5 additions & 10 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
###########################################
# Syntax Coloring Map For simple-dht
# Syntax Coloring Map For SimpleDHT
###########################################

###########################################
# Datatypes (KEYWORD1)
###########################################

DHT KEYWORD1
SimpleDHT11 KEYWORD1

###########################################
# Methods and Functions (KEYWORD2)
###########################################

begin KEYWORD2
readTemperature KEYWORD2
convertCtoF KEYWORD2
convertFtoC KEYWORD2
computeHeatIndex KEYWORD2
readHumidity KEYWORD2
read KEYWORD2

###########################################
# Constants (LITERAL1)
###########################################
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Simple DHT sensor library
version=1.0.0
name=SimpleDHT
version=1.0.1
author=Winlin <[email protected]>
maintainer=Winlin <[email protected]>
sentence=Arduino Temp & Humidity Sensors for DHT11 etc.
paragraph=Simple pure C code with lots of comments, strictly follow the standard DHT protocol, supports 0.5HZ or 1HZ sampling rate.
paragraph=Simple C++ code with lots of comments, strictly follow the standard DHT protocol, supports 0.5HZ or 1HZ sampling rate.
category=Sensors
url=https://github.com/winlinvip/simple-dht
architectures=*

0 comments on commit e4a3cf7

Please sign in to comment.