Follow pictures are DHT11 and SEN0018. The first one DHT11 can collect humanity and degree information. The second one SEN0018 can collect movement human body using ultra ray.
<Front of SEN0018>
<Back of SEN0018>
<Front of DHT11>
<Back of DHT11>
I connect these sensor's signal line to GPIO G04 (DHT11) and G18(SEN0018). DHT11 needs 3V input and SEN0017 needs 5V input. The connection is done like following pictures.
<Raspberry PI>
<Connection Setup>
Following is DHT11 test code which can easily getting from web site. For example "http://www.uugear.com/portfolio/dht11-humidity-temperature-sensor-module/".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wiringPi.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#define MAXTIMINGS 85 | |
#define DHTPIN 7 | |
int dht11_dat[5] = { 0, 0, 0, 0, 0 }; | |
void read_dht11_dat() | |
{ | |
uint8_t laststate = HIGH; | |
uint8_t counter = 0; | |
uint8_t j = 0, i; | |
float f; /* fahrenheit */ | |
dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0; | |
/* pull pin down for 18 milliseconds */ | |
pinMode( DHTPIN, OUTPUT ); | |
digitalWrite( DHTPIN, LOW ); | |
delay( 18 ); | |
/* then pull it up for 40 microseconds */ | |
digitalWrite( DHTPIN, HIGH ); | |
delayMicroseconds( 40 ); | |
/* prepare to read the pin */ | |
pinMode( DHTPIN, INPUT ); | |
/* detect change and read data */ | |
for ( i = 0; i < MAXTIMINGS; i++ ) | |
{ | |
counter = 0; | |
while ( digitalRead( DHTPIN ) == laststate ) | |
{ | |
counter++; | |
delayMicroseconds( 1 ); | |
if ( counter == 255 ) | |
{ | |
break; | |
} | |
} | |
laststate = digitalRead( DHTPIN ); | |
if ( counter == 255 ) | |
break; | |
/* ignore first 3 transitions */ | |
if ( (i >= 4) && (i % 2 == 0) ) | |
{ /* shove each bit into the storage bytes */ | |
dht11_dat[j / 8] <<= 1; | |
if ( counter > 16 ) | |
dht11_dat[j / 8] |= 1; | |
j++; | |
} | |
} | |
/* | |
* check we read 40 bits (8bit x 5 ) + verify checksum in the last byte | |
* print it out if data is good | |
*/ | |
if ( (j >= 40) && | |
(dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) ) | |
{ | |
f = dht11_dat[2] * 9. / 5. + 32; | |
printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n", | |
dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f ); | |
}else { | |
printf( "Data not good, skip\n" ); | |
} | |
} | |
int main( void ) | |
{ | |
printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" ); | |
if ( wiringPiSetup() == -1 ) | |
exit( 1 ); | |
while ( 1 ) | |
{ | |
read_dht11_dat(); | |
delay( 1000 ); /* wait 1sec to refresh */ | |
} | |
return(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wiringPi.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#define MAXTIMINGS 85 | |
//7 = G04, 0 = G17 , 1 = G18 | |
#define DHTPIN 1 | |
void read_sen0018_dat() | |
{ | |
uint8_t laststate = LOW; | |
uint8_t testState = LOW; | |
int i = 0; | |
/*Writes the value HIGH or LOW (1 or 0) to the given pin which must have been previously set as an output.*/ | |
/* pull pin down for 18 milliseconds */ | |
pinMode( DHTPIN, INPUT ); | |
printf("Sensor start\n"); | |
/* detect change and read data */ | |
while (1){ | |
delay( 1000 ); | |
testState = digitalRead( DHTPIN); | |
if(laststate != testState){ | |
laststate = testState; | |
printf("state = %d\n", laststate); | |
} | |
} | |
} | |
int main( void ) | |
{ | |
//wiringPiSetup :: initialize wiringPi | |
/*This initialises the wiringPi system and assumes that the calling program is going to be using the wiringPi pin numbering scheme. This is a simplified numbering scheme which provides a mapping from virtual pin numbers 0 through 16 to the real underlying Broadcom GPIO pin numbers. See the pins page for a table which maps the wiringPi pin number to the Broadcom GPIO pin number to the physical location on the edge connector.*/ | |
if ( wiringPiSetup() == -1 ) | |
exit( 1 ); | |
read_sen0018_dat(); | |
return(0); | |
} |
Data not good, skip
Humidity = 41.0 % Temperature = 25.0 *C (77.0 *F)
Data not good, skip
Data not good, skip
Humidity = 40.0 % Temperature = 25.0 *C (77.0 *F)
Humidity = 40.0 % Temperature = 25.0 *C (77.0 *F)
Data not good, skip
Data not good, skip
Data not good, skip
Data not good, skip
Humidity = 40.0 % Temperature = 25.0 *C (77.0 *F)
^Cpi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo ./test.o
Sensor start
state = 1
state = 0
state = 1
Next time I will merge these code and save the log data to db in raspberry PI.
댓글 없음:
댓글 쓰기