122 lines
2.2 KiB
C++
122 lines
2.2 KiB
C++
//www.elegoo.com
|
|
//2016.12.9
|
|
|
|
#include <SimpleDHT.h>
|
|
#include <avr/sleep.h>
|
|
// for DHT11,
|
|
// VCC: 5V or 3V
|
|
// GND: GND
|
|
// DATA: 2
|
|
int pinDHT11 = 2;
|
|
SimpleDHT11 dht11;
|
|
|
|
void blink(int delayTime) {
|
|
digitalWrite(13, HIGH);
|
|
delay(delayTime);
|
|
digitalWrite(13, LOW);
|
|
delay(delayTime);
|
|
}
|
|
|
|
void change(){
|
|
delay(500);
|
|
blink(1000);
|
|
delay(500);
|
|
}
|
|
int ct = 0;
|
|
int SerialTransmit = 1;
|
|
int pin2_interrupt_flag = 0;
|
|
|
|
void pin2_isr()
|
|
{
|
|
sleep_disable();
|
|
detachInterrupt(0);
|
|
pin2_interrupt_flag = 1;
|
|
}
|
|
|
|
void setup() {
|
|
if(SerialTransmit){Serial.begin(9600);}
|
|
pinMode(13,OUTPUT);
|
|
}
|
|
|
|
void loop() {
|
|
// start working...
|
|
|
|
// sleep_enable();
|
|
// attachInterrupt(0, pin2_isr, LOW);
|
|
/* 0, 1, or many lines of code here */
|
|
|
|
|
|
if(SerialTransmit){
|
|
Serial.println("=================================");
|
|
Serial.println("Sample DHT11...");
|
|
}
|
|
byte temperature = 0;
|
|
byte humidity = 0;
|
|
byte data[40] = {0};
|
|
// read with raw sample data
|
|
if(ct >= 0){
|
|
dht11.read(pinDHT11, &temperature, &humidity, data);
|
|
ct++;
|
|
|
|
// TEMPERATURE
|
|
for (int i = 0; i < (int)temperature/100; i++){
|
|
blink(50);
|
|
}
|
|
|
|
change();
|
|
for (int i = 0; i < (int)temperature/10; i++){
|
|
blink(50);
|
|
}
|
|
|
|
change();
|
|
for (int i = 0; i < (int)temperature%10; i++){
|
|
blink(50);
|
|
}
|
|
|
|
// HUMIDITY
|
|
change();
|
|
for (int i = 0; i < (int)humidity/10; i++){
|
|
blink(50);
|
|
}
|
|
|
|
change();
|
|
for (int i = 0; i < (int)humidity%10; i++){
|
|
blink(50);
|
|
}
|
|
}
|
|
|
|
delay(1000);
|
|
|
|
|
|
if(SerialTransmit){
|
|
if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
|
|
Serial.print("Read DHT11 failed");
|
|
return;
|
|
}
|
|
|
|
Serial.print("Sample RAW Bits: ");
|
|
for (int i = 0; i < 40; i++) {
|
|
Serial.print((int)data[i]);
|
|
if (i > 0 && ((i + 1) % 4) == 0) {
|
|
Serial.print(' ');
|
|
}
|
|
}
|
|
Serial.println("");
|
|
|
|
Serial.print("Sample OK: ");
|
|
Serial.print((int)temperature); Serial.print(" *C, ");
|
|
Serial.print((int)humidity); Serial.println(" %");
|
|
|
|
// DHT11 sampling rate is 1HZ.
|
|
delay(1000);
|
|
}
|
|
// set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
|
// cli();
|
|
// sleep_bod_disable();
|
|
// sei();
|
|
// sleep_cpu();
|
|
// /* wake up here */
|
|
// sleep_disable();
|
|
// ct = 0;
|
|
}
|