mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
0.1.0 initial version of I2C library for DHT12
This commit is contained in:
parent
d782be9946
commit
a8a1f31100
77
libraries/DHT12/DHT12.cpp
Normal file
77
libraries/DHT12/DHT12.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
//
|
||||
// FILE: DHT12.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: I2C library for DHT12 for Arduino.
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 - 2017-12-11 initial version
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include <DHT12.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// PUBLIC
|
||||
//
|
||||
DHT12::DHT12()
|
||||
{
|
||||
_deviceAddress = DHT12_ADDRESS;
|
||||
}
|
||||
|
||||
int8_t DHT12::read()
|
||||
{
|
||||
// READ SENSOR
|
||||
int status = _readSensor();
|
||||
if (status < 0) return status;
|
||||
|
||||
// CONVERT AND STORE
|
||||
humidity = bits[0] + bits[1] * 0.1;
|
||||
temperature = (bits[2] & 0x7F) + bits[3] * 0.1;
|
||||
if (bits[2] & 0x80)
|
||||
{
|
||||
temperature = -temperature;
|
||||
}
|
||||
|
||||
// TEST CHECKSUM
|
||||
uint8_t checksum = bits[0] + bits[1] + bits[2] + bits[3];
|
||||
if (bits[4] != checksum)
|
||||
{
|
||||
return DHT12_ERROR_CHECKSUM;
|
||||
}
|
||||
|
||||
return DHT12_OK;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
// negative is error in communication
|
||||
// 5 is OK
|
||||
// 0..4 is too few bytes.
|
||||
int DHT12::_readSensor()
|
||||
{
|
||||
// GET CONNECTION
|
||||
Wire.beginTransmission(_deviceAddress);
|
||||
Wire.write(0);
|
||||
int rv = Wire.endTransmission();
|
||||
if (rv < 0) return rv;
|
||||
|
||||
// GET DATA
|
||||
const uint8_t length = 5;
|
||||
int bytes = Wire.requestFrom(_deviceAddress, length);
|
||||
if (bytes == 0) return DHT12_ERROR_CONNECT;
|
||||
if (bytes < length) return DHT12_MISSING_BYTES;
|
||||
|
||||
for (int i = 0; i < bytes; i++)
|
||||
{
|
||||
bits[i] = Wire.read();
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
// END OF FILE
|
43
libraries/DHT12/DHT12.h
Normal file
43
libraries/DHT12/DHT12.h
Normal file
@ -0,0 +1,43 @@
|
||||
#ifndef DHT12_H
|
||||
#define DHT12_H
|
||||
//
|
||||
// FILE: DHT12.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: DHT_I2C library for Arduino .
|
||||
// VERSION: 0.1.0
|
||||
// HISTORY: See DHT12.cpp
|
||||
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries/
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include "Wire.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
#define DHT12_VERSION "0.1.0"
|
||||
|
||||
#define DHT12_OK 0
|
||||
#define DHT12_ERROR_CHECKSUM -10
|
||||
#define DHT12_ERROR_CONNECT -11
|
||||
#define DHT12_MISSING_BYTES -12
|
||||
|
||||
#define DHT12_ADDRESS 0x5C
|
||||
|
||||
class DHT12
|
||||
{
|
||||
public:
|
||||
DHT12();
|
||||
|
||||
int8_t read();
|
||||
|
||||
float humidity;
|
||||
float temperature;
|
||||
|
||||
private:
|
||||
uint8_t _deviceAddress;
|
||||
uint8_t bits[5];
|
||||
int _readSensor();
|
||||
};
|
||||
|
||||
#endif
|
||||
// END OF FILE
|
63
libraries/DHT12/examples/DHT12/DHT12.ino
Normal file
63
libraries/DHT12/examples/DHT12/DHT12.ino
Normal file
@ -0,0 +1,63 @@
|
||||
//
|
||||
// FILE: DHT12.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: Demo for DHT12 I2C humidity & temperature sensor
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 - 2017-12-11 initial version
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include <DHT12.h>
|
||||
|
||||
DHT12 DHT;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Wire.begin();
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("DHT12 LIBRARY VERSION: ");
|
||||
Serial.println(DHT12_VERSION);
|
||||
Serial.println();
|
||||
|
||||
delay(2000);
|
||||
|
||||
Serial.println("Type,\tStatus,\tHumidity (%),\tTemperature (C)");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// READ DATA
|
||||
Serial.print("DHT12, \t");
|
||||
int status = DHT.read();
|
||||
switch (status)
|
||||
{
|
||||
case DHT12_OK:
|
||||
Serial.print("OK,\t");
|
||||
break;
|
||||
case DHT12_ERROR_CHECKSUM:
|
||||
Serial.print("Checksum error,\t");
|
||||
break;
|
||||
case DHT12_ERROR_CONNECT:
|
||||
Serial.print("Connect error,\t");
|
||||
break;
|
||||
case DHT12_MISSING_BYTES:
|
||||
Serial.print("Missing bytes,\t");
|
||||
break;
|
||||
default:
|
||||
Serial.print("Unknown error,\t");
|
||||
break;
|
||||
}
|
||||
// DISPLAY DATA, sensor has only one decimal.
|
||||
Serial.print(DHT.humidity, 1);
|
||||
Serial.print(",\t");
|
||||
Serial.println(DHT.temperature, 1);
|
||||
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
// END OF FILE
|
27
libraries/DHT12/keywords.txt
Normal file
27
libraries/DHT12/keywords.txt
Normal file
@ -0,0 +1,27 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For DHT_I2C
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
DHT12 KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
read KEYWORD2
|
||||
humidity KEYWORD2
|
||||
temperature KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
DHT12_VERSION LITERAL2
|
||||
DHT12_OK LITERAL2
|
||||
DHT12_ERROR_CHECKSUM LITERAL1
|
||||
DHT12_ERROR_CONNECT LITERAL1
|
||||
DHT12_MISSING_BYTES LITERAL1
|
23
libraries/DHT12/library.json
Normal file
23
libraries/DHT12/library.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "DHT12",
|
||||
"keywords": "Temperature Humidity DHT12 I2C",
|
||||
"description": "I2C Library for DHT12",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Rob Tillaart",
|
||||
"email": "Rob.Tillaart@gmail.com",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/Arduino.git"
|
||||
},
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
"export": {
|
||||
"include": "libraries/DHT12"
|
||||
}
|
||||
}
|
9
libraries/DHT12/library.properties
Normal file
9
libraries/DHT12/library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=DHT12
|
||||
version=0.1.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=I2C Library for DHT12 Temperature and Humidity.
|
||||
paragraph=DHT12
|
||||
category=Sensors
|
||||
url=https://github.com/RobTillaart/Arduino/tree/master/libraries/
|
||||
architectures=*
|
Loading…
Reference in New Issue
Block a user