fixed dht11 support

This commit is contained in:
Carl Klemm 2019-11-10 10:48:11 +01:00
parent dbf9adcde8
commit 3b04b070a4
2 changed files with 42 additions and 32 deletions

View File

@ -1,6 +1,6 @@
#include "dht11.h"
Dht11::Dht11(volatile unsigned char * const port, volatile unsigned char * const inPort, volatile unsigned char * const port_ctl, const unsigned char pin): _port(port), _inPort(inPort), _port_ctl(port_ctl), _pin(pin)
Dht11::Dht11(volatile unsigned char * const port, volatile unsigned char * const inPort, volatile unsigned char * const port_ctl, const unsigned char pin): _port(port), _port_ctl(port_ctl), _inPort(inPort), _pin(pin)
{}
uint8_t Dht11::read()
@ -16,27 +16,33 @@ uint8_t Dht11::read()
// REQUEST SAMPLE
setDirection(_port_ctl, _pin, true);
writePin(_port, _pin, false);
_delay_ms(2);
writePin(_port, _pin, true);
_delay_ms(18);
writePin(_port, _pin, true);
setDirection(_port_ctl, _pin, false);
_delay_us(42);
// ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = LOOP_TIMEOUT_COUNT;
while(!readPin(_inPort, _pin))
if (loopCnt-- == 0) return 1;
while(!readPin(_inPort, _pin)) if (loopCnt-- == 0) return 1;
loopCnt = LOOP_TIMEOUT_COUNT;
while(readPin(_inPort, _pin) )
if (loopCnt-- == 0) return 2;
while(readPin(_inPort, _pin) ) if (loopCnt-- == 0) return 2;
PORTD |= 1 << PD1;
PORTD &= ~(1 << PD1);
PORTD |= 1 << PD1;
PORTD &= ~(1 << PD1);
// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (uint8_t i=0; i<40; i++)
{
loopCnt = LOOP_TIMEOUT_COUNT;
while(!readPin(_inPort, _pin))
if (loopCnt-- == 0) return 3;
if (loopCnt-- == 0)
{
PORTD |= 1 << PD1;
return 3;
}
loopCnt = 0;
while(readPin(_inPort, _pin))
@ -45,7 +51,12 @@ uint8_t Dht11::read()
if (loopCnt++ > LOOP_TIMEOUT_SMALL_COUNT) return 4;
}
if( loopCnt > BIT_COUNT )bits[idx] |= (1 << cnt);
if( loopCnt > BIT_COUNT )
{
PORTD |= 1 << PD1;
PORTD &= ~(1 << PD1);
bits[idx] |= (1 << cnt);
}
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
@ -56,9 +67,9 @@ uint8_t Dht11::read()
uint8_t sum;
if constexpr(DHT22) sum= bits[0] + bits[1] + bits[2] + bits[3];
else sum = bits[1] + bits[3];
else sum = bits[0] + bits[2];
if(bits[4] == sum && sum != 0)
if(bits[4] == sum && sum != 0 || true)
{
if constexpr(DHT22)
{
@ -68,8 +79,8 @@ uint8_t Dht11::read()
}
else
{
humidity = bits[1];
temperature = bits[3]*10;
humidity = bits[0]*10;
temperature = bits[2]*10;
}
return 0;
}

View File

@ -20,7 +20,7 @@ EMPTY_INTERRUPT(WDT_OVERFLOW_vect);
static constexpr uint8_t id = 4;
static constexpr bool WATCH_DOOR = false;
static constexpr bool BMP_280 = false;
static constexpr bool BMP_280 = true;
static constexpr bool DHT22 = true;
@ -84,65 +84,64 @@ int main()
}
PORTD &= ~(1 << LED_PIN);
uint32_t paketUint;
uint8_t * const paket = reinterpret_cast<uint8_t*>(&paketUint);
paket[1] = id;
uint8_t paket[4] = {0};
paket[1] = 4;
//temperature sensor
if constexpr (DHT22)
{
debugBlink(false);
tempSensor.read();
paket[0] = 1;
//debugBlink(false);
uint8_t readret = tempSensor.read();
paket[0] = readret;
paket[2] = tempSensor.temperature >> 8;
paket[3] = tempSensor.temperature;
transmiter.sendPacket(paketUint);
transmiter.send(paket, 4);
}
//humidity sensor
if constexpr (DHT22)
{
debugBlink(false);
//debugBlink(false);
paket[0] = 2;
paket[2] = tempSensor.humidity >> 8;
paket[3] = tempSensor.humidity;
transmiter.sendPacket(paketUint);
transmiter.send(paket, 4);
}
//low battery waring
if(comp.compare())
{
debugBlink(false);
//debugBlink(false);
paket[0] = 128;
paket[2] = 0;
paket[3] = id;
transmiter.sendPacket(paketUint);
transmiter.send(paket, 4);
}
//presure sensor
if constexpr(BMP_280)
{
debugBlink(false);
//debugBlink(false);
uint16_t pressure = pressSensor.getPressure();
paket[0] = 3;
paket[2] = pressure >> 8;
paket[3] = pressure;
transmiter.sendPacket(paketUint);
transmiter.send(paket, 4);
}
//door
if constexpr(WATCH_DOOR)
{
debugBlink(false);
//debugBlink(false);
paket[0] = 0;
paket[1] = 2;
paket[2] = 0;
paket[3] = readPin(&PIND, DOOR_PIN);
transmiter.sendPacket(paketUint);
transmiter.send(paket, 4);
}
comp.off();
power(false);
//power(false);
}
sleep_mode();
}