Skip to content

Commit

Permalink
initial version (#1)
Browse files Browse the repository at this point in the history
* initial version
  • Loading branch information
RobTillaart authored Jan 11, 2022
1 parent e2adcd3 commit 00fc759
Show file tree
Hide file tree
Showing 17 changed files with 773 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
13 changes: 13 additions & 0 deletions .github/workflows/arduino-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

name: Arduino-lint

on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
compliance: strict
17 changes: 17 additions & 0 deletions .github/workflows/arduino_test_runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Arduino CI

on: [push, pull_request]

jobs:
runTest:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb
18 changes: 18 additions & 0 deletions .github/workflows/jsoncheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: JSON check

on:
push:
paths:
- '**.json'
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

179 changes: 179 additions & 0 deletions DHT20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
//
// FILE: DHT20.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
//
// HISTORY:
// 0.1.0 2022-01-11 initial version (based upon DHT20)



#include "DHT20.h"

const uint8_t DHT20_ADDRESS = 0x38;


DHT20::DHT20(TwoWire *wire)
{
_wire = wire;
// reset() ?
_temperature = 0;
_humidity = 0;
_humOffset = 0;
_tempOffset = 0;
_status = 0;
_lastRequest = 0;
_lastRead = 0;
}


bool DHT20::begin()
{
_wire->begin();
return isConnected();
}


#if defined(ESP8266) || defined(ESP32)
bool DHT20::begin(const uint8_t dataPin, const uint8_t clockPin)
{
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
return isConnected();
}
#endif


bool DHT20::isConnected()
{
_wire->beginTransmission(DHT20_ADDRESS);
int rv = _wire->endTransmission();
return rv == 0;
}


bool DHT20::readyData()
{
return ((millis() - _lastRequest) > 85);
}


int DHT20::read()
{
// READ SENSOR ==> uses the async interface!
int status = _requestData();
if (status < 0) return status;
while (!readyData())
{
yield();
delay(1);
}
_readData();

// CONVERT AND STORE
_status = _bits[0];
uint32_t tmp = _bits[1];
tmp <<= 8;
tmp += _bits[2];
tmp <<= 4;
tmp += (_bits[3] >> 4);
_humidity = tmp * 9.5367431640625e-5; // ==> / 1048576.0 * 100%;

tmp = (_bits[3] & 0x0F);
tmp <<= 8;
tmp += _bits[4];
tmp <<= 8;
tmp += _bits[5];
_temperature = tmp * 1.9073486328125e-4 - 50; // ==> / 1048576.0 * 200 - 50;

// TEST CHECKSUM
uint8_t _crc = _crc8(_bits, 6);
// Serial.print(_crc, HEX);
// Serial.print("\t");
// Serial.println(_bits[6], HEX);
if (_crc != _bits[6]) return DHT20_ERROR_CHECKSUM;

return DHT20_OK;
}


int DHT20::_requestData()
{
// GET CONNECTION
_wire->beginTransmission(DHT20_ADDRESS);
_wire->write(0xAC);
_wire->write(0x33);
_wire->write(0x00);
int rv = _wire->endTransmission();

_lastRequest = millis();
return rv;
}


int DHT20::_readData()
{
// GET DATA
const uint8_t length = 7;
int bytes = _wire->requestFrom(DHT20_ADDRESS, length);

if (bytes == 0) return DHT20_ERROR_CONNECT;
if (bytes < length) return DHT20_MISSING_BYTES;

for (int i = 0; i < bytes; i++)
{
_bits[i] = _wire->read();
// if (_bits[i] < 0x10) Serial.print(0);
// Serial.print(_bits[i], HEX);
// Serial.print(" ");
}
// Serial.println();

_lastRead = millis();
return bytes;
}


int DHT20::_readStatus()
{
// GET CONNECTION
_wire->beginTransmission(DHT20_ADDRESS);
_wire->write(0xAC);
_wire->write(0x71);
_wire->write(0x00);
return _wire->endTransmission();

_wire->requestFrom(DHT20_ADDRESS, (uint8_t)1);
}


uint8_t DHT20::_crc8(uint8_t *ptr, uint8_t len)
{
uint8_t crc = 0xFF;
while(len--)
{
crc ^= *ptr++;
for (uint8_t i = 0; i < 8; i++)
{
if (crc & 0x80)
{
crc <<= 1;
crc ^= 0x31;
}
else
{
crc <<= 1;
}
}
}
return crc;
}


// -- END OF FILE --

77 changes: 77 additions & 0 deletions DHT20.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once
//
// FILE: DHT20.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
// VERSION: 0.1.0
// HISTORY: See DHT20.cpp
// URL: https://github.com/RobTillaart/DHT20
//


#include "Arduino.h"
#include "Wire.h"

#define DHT20_LIB_VERSION (F("0.1.0"))

#define DHT20_OK 0
#define DHT20_ERROR_CHECKSUM -10
#define DHT20_ERROR_CONNECT -11
#define DHT20_MISSING_BYTES -12


class DHT20
{
public:
DHT20(TwoWire *wire = &Wire); // to be tested explicitly

#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t dataPin, const uint8_t clockPin);
#endif
bool begin();
bool isConnected();

// ASYNCHRONUOUS CALL
int requestData() { return _requestData(); };
bool readyData();
int readData() { return _readData(); };


// SYNCHRONUOUS CALL
int read();
float getHumidity() { return _humidity + _humOffset; };
float getTemperature() { return _temperature + _tempOffset; };

// allows 1st order calibration
void setHumOffset(float offset) { _humOffset = offset; };
void setTempOffset(float offset) { _tempOffset = offset; };
float getHumOffset() { return _humOffset; };
float getTempOffset() { return _tempOffset; };

// OTHER
uint32_t lastRead() { return _lastRead; };
uint32_t lastRequest() { return _lastRequest; };
int internalStatus() { return _status; };

private:
float _humidity;
float _temperature;
float _humOffset;
float _tempOffset;
uint8_t _status;
uint32_t _lastRequest;
uint32_t _lastRead;

int _requestData();
int _readData();
int _readStatus();
uint8_t _bits[7];

uint8_t _crc8(uint8_t *ptr, uint8_t len);

TwoWire* _wire;
};


// -- END OF FILE --

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Rob Tillaart
Copyright (c) 2022-2022 Rob Tillaart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit 00fc759

Please sign in to comment.