fixed dht11 support
This commit is contained in:
		
							parent
							
								
									dbf9adcde8
								
							
						
					
					
						commit
						3b04b070a4
					
				
					 2 changed files with 42 additions and 32 deletions
				
			
		
							
								
								
									
										41
									
								
								dht11.cpp
									
										
									
									
									
								
							
							
						
						
									
										41
									
								
								dht11.cpp
									
										
									
									
									
								
							|  | @ -1,6 +1,6 @@ | ||||||
| #include "dht11.h" | #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() | uint8_t Dht11::read() | ||||||
|  | @ -16,27 +16,33 @@ uint8_t Dht11::read() | ||||||
| 	// REQUEST SAMPLE
 | 	// REQUEST SAMPLE
 | ||||||
| 	setDirection(_port_ctl, _pin, true); | 	setDirection(_port_ctl, _pin, true); | ||||||
| 	writePin(_port, _pin, false); | 	writePin(_port, _pin, false); | ||||||
| 	_delay_ms(2); | 	_delay_ms(18); | ||||||
| 	writePin(_port, _pin, true); |     writePin(_port, _pin, true); | ||||||
| 	setDirection(_port_ctl, _pin, false); | 	setDirection(_port_ctl, _pin, false); | ||||||
| 	_delay_us(42); | 	_delay_us(42); | ||||||
| 	 |  | ||||||
| 
 | 
 | ||||||
| 	// ACKNOWLEDGE or TIMEOUT
 | 	// ACKNOWLEDGE or TIMEOUT
 | ||||||
| 	unsigned int loopCnt = LOOP_TIMEOUT_COUNT; | 	unsigned int loopCnt = LOOP_TIMEOUT_COUNT; | ||||||
| 	while(!readPin(_inPort, _pin)) | 	while(!readPin(_inPort, _pin)) if (loopCnt-- == 0) return 1; | ||||||
| 		if (loopCnt-- == 0) return 1; |          | ||||||
| 
 |  | ||||||
| 	loopCnt = LOOP_TIMEOUT_COUNT; | 	loopCnt = LOOP_TIMEOUT_COUNT; | ||||||
| 	while(readPin(_inPort, _pin) ) | 	while(readPin(_inPort, _pin) ) if (loopCnt-- == 0)  return 2; | ||||||
| 		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
 | 	// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
 | ||||||
| 	for (uint8_t i=0; i<40; i++) | 	for (uint8_t i=0; i<40; i++) | ||||||
| 	{ | 	{ | ||||||
| 		loopCnt = LOOP_TIMEOUT_COUNT; | 		loopCnt = LOOP_TIMEOUT_COUNT; | ||||||
| 		while(!readPin(_inPort, _pin)) | 		while(!readPin(_inPort, _pin)) | ||||||
| 			if (loopCnt-- == 0) return 3; | 			if (loopCnt-- == 0)  | ||||||
|  |             { | ||||||
|  |                 PORTD |= 1 << PD1; | ||||||
|  |                 return 3; | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
| 		loopCnt = 0; | 		loopCnt = 0; | ||||||
| 		while(readPin(_inPort, _pin)) | 		while(readPin(_inPort, _pin)) | ||||||
|  | @ -45,7 +51,12 @@ uint8_t Dht11::read() | ||||||
| 			if (loopCnt++ > LOOP_TIMEOUT_SMALL_COUNT) return 4; | 			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?
 | 		if (cnt == 0)   // next byte?
 | ||||||
| 		{ | 		{ | ||||||
| 			cnt = 7;    // restart at MSB
 | 			cnt = 7;    // restart at MSB
 | ||||||
|  | @ -56,9 +67,9 @@ uint8_t Dht11::read() | ||||||
| 	 | 	 | ||||||
| 	uint8_t sum; | 	uint8_t sum; | ||||||
| 	if constexpr(DHT22) sum= bits[0] + bits[1] + bits[2] + bits[3]; | 	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) | 		if constexpr(DHT22) | ||||||
| 		{ | 		{ | ||||||
|  | @ -68,8 +79,8 @@ uint8_t Dht11::read() | ||||||
| 		} | 		} | ||||||
| 		else | 		else | ||||||
| 		{ | 		{ | ||||||
| 			humidity = bits[1]; | 			humidity = bits[0]*10; | ||||||
| 			temperature = bits[3]*10; | 			temperature = bits[2]*10; | ||||||
| 		} | 		} | ||||||
| 		return 0; | 		return 0; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
							
								
								
									
										33
									
								
								main.cpp
									
										
									
									
									
								
							
							
						
						
									
										33
									
								
								main.cpp
									
										
									
									
									
								
							|  | @ -20,7 +20,7 @@ EMPTY_INTERRUPT(WDT_OVERFLOW_vect); | ||||||
| 
 | 
 | ||||||
| static constexpr uint8_t id = 4; | static constexpr uint8_t id = 4; | ||||||
| static constexpr bool WATCH_DOOR = false; | static constexpr bool WATCH_DOOR = false; | ||||||
| static constexpr bool BMP_280 = false; | static constexpr bool BMP_280 = true; | ||||||
| static constexpr bool DHT22   = true; | static constexpr bool DHT22   = true; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -84,65 +84,64 @@ int main() | ||||||
| 			} | 			} | ||||||
| 			PORTD &= ~(1 << LED_PIN); | 			PORTD &= ~(1 << LED_PIN); | ||||||
| 			 | 			 | ||||||
| 			uint32_t paketUint; | 			uint8_t paket[4] = {0}; | ||||||
| 			uint8_t * const paket = reinterpret_cast<uint8_t*>(&paketUint); |             paket[1] = 4; | ||||||
| 			paket[1] = id; |  | ||||||
| 
 | 
 | ||||||
| 			 | 			 | ||||||
| 			//temperature sensor
 | 			//temperature sensor
 | ||||||
| 			if constexpr (DHT22) | 			if constexpr (DHT22) | ||||||
| 			{ | 			{ | ||||||
| 				debugBlink(false); | 				//debugBlink(false);
 | ||||||
| 				tempSensor.read();  |                 uint8_t readret = tempSensor.read();  | ||||||
| 				paket[0] = 1; |                 paket[0] = readret; | ||||||
| 				paket[2] = tempSensor.temperature >> 8; | 				paket[2] = tempSensor.temperature >> 8; | ||||||
| 				paket[3] = tempSensor.temperature; | 				paket[3] = tempSensor.temperature; | ||||||
| 				transmiter.sendPacket(paketUint); | 				transmiter.send(paket, 4); | ||||||
| 			} | 			} | ||||||
| 			 | 			 | ||||||
| 			//humidity sensor
 | 			//humidity sensor
 | ||||||
| 			if constexpr (DHT22) | 			if constexpr (DHT22) | ||||||
| 			{ | 			{ | ||||||
| 				debugBlink(false); | 				//debugBlink(false);
 | ||||||
| 				paket[0] = 2; | 				paket[0] = 2; | ||||||
| 				paket[2] = tempSensor.humidity >> 8; | 				paket[2] = tempSensor.humidity >> 8; | ||||||
| 				paket[3] = tempSensor.humidity; | 				paket[3] = tempSensor.humidity; | ||||||
| 				transmiter.sendPacket(paketUint); | 				transmiter.send(paket, 4); | ||||||
| 			} | 			} | ||||||
| 			 | 			 | ||||||
| 			//low battery waring
 | 			//low battery waring
 | ||||||
| 			if(comp.compare()) | 			if(comp.compare()) | ||||||
| 			{ | 			{ | ||||||
| 				debugBlink(false); | 				//debugBlink(false);
 | ||||||
| 				paket[0] = 128; | 				paket[0] = 128; | ||||||
| 				paket[2] = 0; | 				paket[2] = 0; | ||||||
| 				paket[3] = id; | 				paket[3] = id; | ||||||
| 				transmiter.sendPacket(paketUint); | 				transmiter.send(paket, 4); | ||||||
| 			} | 			} | ||||||
| 			 | 			 | ||||||
| 			//presure sensor
 | 			//presure sensor
 | ||||||
| 			if constexpr(BMP_280) | 			if constexpr(BMP_280) | ||||||
| 			{ | 			{ | ||||||
| 				debugBlink(false); | 				//debugBlink(false);
 | ||||||
| 				uint16_t pressure = pressSensor.getPressure(); | 				uint16_t pressure = pressSensor.getPressure(); | ||||||
| 				paket[0] = 3; | 				paket[0] = 3; | ||||||
| 				paket[2] = pressure >> 8; | 				paket[2] = pressure >> 8; | ||||||
| 				paket[3] = pressure; | 				paket[3] = pressure; | ||||||
| 				transmiter.sendPacket(paketUint); | 				transmiter.send(paket, 4); | ||||||
| 			} | 			} | ||||||
| 			 | 			 | ||||||
| 			//door
 | 			//door
 | ||||||
| 			if constexpr(WATCH_DOOR) | 			if constexpr(WATCH_DOOR) | ||||||
| 			{ | 			{ | ||||||
| 				debugBlink(false); | 				//debugBlink(false);
 | ||||||
| 				paket[0] = 0; | 				paket[0] = 0; | ||||||
| 				paket[1] = 2; | 				paket[1] = 2; | ||||||
| 				paket[2] = 0; | 				paket[2] = 0; | ||||||
| 				paket[3] = readPin(&PIND, DOOR_PIN);  | 				paket[3] = readPin(&PIND, DOOR_PIN);  | ||||||
| 				transmiter.sendPacket(paketUint); | 				transmiter.send(paket, 4); | ||||||
| 			} | 			} | ||||||
| 			comp.off(); | 			comp.off(); | ||||||
| 			power(false); | 			//power(false);
 | ||||||
| 		} | 		} | ||||||
| 		sleep_mode(); | 		sleep_mode(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue