| 1 | #include <WProgram.h>␊ |
| 2 | #include <avr/sleep.h>␊ |
| 3 | #include <avr/wdt.h>␊ |
| 4 | #include <avr/io.h>␊ |
| 5 | #include <dht11.h>␊ |
| 6 | ␊ |
| 7 | dht11 DHT11;␊ |
| 8 | #define DHT11PIN 3␊ |
| 9 | ␊ |
| 10 | void setup()␊ |
| 11 | {␊ |
| 12 | ␊ |
| 13 | // By default, all pins (leaving TX/RX alone) set to inputs with␊ |
| 14 | // pullups enabled, to ensure no unwanted power usage.␊ |
| 15 | DDRD &= B00000011;␊ |
| 16 | DDRB = B00000000;␊ |
| 17 | PORTD |= B11111100;␊ |
| 18 | PORTB |= B11111111;␊ |
| 19 | ␊ |
| 20 | // Not using the ADC, so disable to save power.␊ |
| 21 | ADCSRA = 0;␊ |
| 22 | PRR = 1;␊ |
| 23 | ␊ |
| 24 | Serial.begin(9600);␊ |
| 25 | Serial.println("Ciaran's Greenhouse");␊ |
| 26 | Serial.println();␊ |
| 27 | }␊ |
| 28 | ␊ |
| 29 | // watchdog interrupt␊ |
| 30 | ISR (WDT_vect) ␊ |
| 31 | {␊ |
| 32 | wdt_disable();␊ |
| 33 | }␊ |
| 34 | ␊ |
| 35 | // Sleep the device for 8 seconds...␊ |
| 36 | void sleep8secs()␊ |
| 37 | {␊ |
| 38 | MCUSR = 0; ␊ |
| 39 | WDTCSR = _BV(WDCE) | _BV(WDE);␊ |
| 40 | WDTCSR = _BV(WDIE) | _BV(WDP3) | _BV(WDP0);␊ |
| 41 | wdt_reset();␉ ␊ |
| 42 | set_sleep_mode(SLEEP_MODE_PWR_DOWN); ␊ |
| 43 | sleep_enable();␊ |
| 44 | MCUCR = _BV(BODS) | _BV(BODSE);␊ |
| 45 | MCUCR = _BV(BODS); ␊ |
| 46 | sleep_cpu();␊ |
| 47 | sleep_disable();␊ |
| 48 | }␊ |
| 49 | ␊ |
| 50 | void loop()␊ |
| 51 | {␊ |
| 52 | int chk = DHT11.read(DHT11PIN);␊ |
| 53 | ␊ |
| 54 | switch (chk)␊ |
| 55 | {␊ |
| 56 | case 0:␊ |
| 57 | Serial.print("Humidity (%): ");␊ |
| 58 | Serial.println((float)DHT11.humidity, 2);␊ |
| 59 | Serial.print("Temperature (oC): ");␊ |
| 60 | Serial.println((float)DHT11.temperature, 2);␊ |
| 61 | break;␊ |
| 62 | case -1: ␊ |
| 63 | Serial.println("Checksum error");␊ |
| 64 | break;␊ |
| 65 | case -2:␊ |
| 66 | Serial.println("Time out error");␊ |
| 67 | break;␊ |
| 68 | default:␊ |
| 69 | Serial.println("Unknown error");␊ |
| 70 | break;␊ |
| 71 | }␊ |
| 72 | ␊ |
| 73 | // Go to sleep.␊ |
| 74 | for(int i=0;i<2;i++)␊ |
| 75 | ␉ sleep8secs();␊ |
| 76 | ␊ |
| 77 | }␊ |
| 78 | ␊ |
| 79 | ␊ |
| 80 | ␊ |
| 81 | |