Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dht.h>
- #include <Wire.h>
- #define DHT22_PIN 3 // what pin the sensor is connected to
- dht DHT;
- #define photocellPin 0 // analog 0
- #include <avr/sleep.h>
- #include <avr/power.h>
- #include <avr/wdt.h>
- #define DHTPINVCC 2
- #define PHOTOCELLVCC 9
- #define disk1 0x50
- float temperature;
- float humidity;
- uint8_t counter=0;
- //count number of entering into main loop
- uint8_t loopCounter=150; //set to 1 for debug (short sleep) - 150 (20min)
- //WATCHDOG
- volatile int f_wdt=1;
- ISR(WDT_vect) {
- if(f_wdt == 0) {
- f_wdt=1;
- } else {
- //nothing
- }
- }
- void enterSleep(void) {
- set_sleep_mode(SLEEP_MODE_PWR_DOWN); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
- sleep_enable();
- /* Now enter sleep mode. */
- sleep_mode();
- /* The program will continue from here after the WDT timeout*/
- sleep_disable(); /* First thing to do is disable sleep. */
- /* Re-enable the peripherals. */
- power_all_enable();
- }
- void setup()
- {
- //Serial.println("Initialising...");
- Serial.begin(9600);
- Wire.begin();
- unsigned int address = 0;
- //WATCHDOG
- /* Clear the reset flag. */
- MCUSR &= ~(1<<WDRF);
- /* In order to change WDE or the prescaler, we need to
- * set WDCE (This will allow updates for 4 clock cycles).
- */
- WDTCSR |= (1<<WDCE) | (1<<WDE);
- /* set new watchdog timeout prescaler value */
- WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
- /* Enable the WD interrupt (note no reset). */
- WDTCSR |= _BV(WDIE);
- //CONFIGURE DHT11
- pinMode(DHTPINVCC, OUTPUT);
- pinMode(PHOTOCELLVCC, OUTPUT);
- //make it blink at startup
- digitalWrite(DHTPINVCC, HIGH);
- digitalWrite(PHOTOCELLVCC, HIGH);
- delay(500);
- digitalWrite(DHTPINVCC, LOW);
- digitalWrite(PHOTOCELLVCC, LOW);
- //Serial.println("alive");
- //Serial.println("\n");
- //Serial.println("dead");
- delay(1000);
- writeEEPROM(disk1, address, 123);
- Serial.print(readEEPROM(disk1, address), DEC);
- }
- void loop()
- {
- if(f_wdt == 1) {
- loopCounter ++;
- if (loopCounter > 6) {
- // 110 *8 = 880s = 14.6min @ 16mhtz
- readSensor();
- loopCounter = 0;
- }
- f_wdt = 0;
- enterSleep();
- } else {
- //nothing
- }
- }
- void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
- {
- Wire.beginTransmission(deviceaddress);
- Wire.write((int)(eeaddress >> 8)); // MSB
- Wire.write((int)(eeaddress & 0xFF)); // LSB
- Wire.write(data);
- Wire.endTransmission();
- delay(5);
- }
- byte readEEPROM(int deviceaddress, unsigned int eeaddress )
- {
- byte rdata = 0xFF;
- Wire.beginTransmission(deviceaddress);
- Wire.write((int)(eeaddress >> 8)); // MSB
- Wire.write((int)(eeaddress & 0xFF)); // LSB
- Wire.endTransmission();
- Wire.requestFrom(deviceaddress,1);
- if (Wire.available()) rdata = Wire.read();
- return rdata;
- }
- void readSensor() {
- digitalWrite(DHTPINVCC, HIGH);
- delay(1500);
- digitalWrite(PHOTOCELLVCC, HIGH);
- delay(500);
- //Serial.println("\n");
- int photocellReading = analogRead(photocellPin);
- int chk = DHT.read22(DHT22_PIN);
- //Serial.print("Read sensor: ");
- switch (chk)
- {
- case DHTLIB_OK:
- Serial.print("OK,\t");
- break;
- case DHTLIB_ERROR_CHECKSUM:
- Serial.print("Checksum error,\t");
- break;
- case DHTLIB_ERROR_TIMEOUT:
- Serial.print("Time out error,\t");
- break;
- default:
- Serial.print("Unknown error,\t");
- break;
- }
- digitalWrite(DHTPINVCC, LOW);
- address ++;
- //Serial.print("Humidity (%): ");
- //Serial.println((float)DHT.humidity, 2);
- //myFile.print("\n");
- //myFile.print("\n");
- //myFile.print("Humidity (%): ");
- //myFile.print((float)DHT.humidity, 2);
- //Serial.print("Temperature (oC): ");
- //Serial.println((float)DHT.temperature, 2);
- //myFile.print("\n");
- //myFile.print(" Temperature (oC): ");
- //myFile.print((float)DHT.temperature, 2);
- //Serial.print(" Lux Reading: ");
- //Serial.print(photocellReading);
- //myFile.print("\n");
- //myFile.print(" Lux Reading: ");
- //myFile.print(photocellReading);
- //myFile.close();
- digitalWrite(PHOTOCELLVCC, LOW);
- //delay(800);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement