Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include <dht.h>
  2. #include <Wire.h>
  3. #define DHT22_PIN 3
  4. dht DHT;
  5. #define photocellPin 0
  6. #include <avr/sleep.h>
  7. #include <avr/power.h>
  8. #include <avr/wdt.h>
  9. #define DHTPINVCC 2
  10. #define PHOTOCELLVCC 9
  11. #define disk1 0x50
  12. unsigned int address = 0;
  13.  
  14. float temperature;
  15. float humidity;
  16.  
  17.  
  18. uint8_t counter=0;
  19.  
  20. //count number of entering into main loop
  21. uint8_t loopCounter=150; //set to 1 for debug (short sleep) - 150 (20min)
  22.  
  23. //WATCHDOG
  24. volatile int f_wdt=1;
  25.  
  26. ISR(WDT_vect) {
  27. if(f_wdt == 0) {
  28. f_wdt=1;
  29. } else {
  30. //nothing
  31. }
  32. }
  33.  
  34. void enterSleep(void) {
  35. set_sleep_mode(SLEEP_MODE_PWR_DOWN); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
  36. sleep_enable();
  37.  
  38. /* Now enter sleep mode. */
  39. sleep_mode();
  40.  
  41. /* The program will continue from here after the WDT timeout*/
  42. sleep_disable(); /* First thing to do is disable sleep. */
  43.  
  44. /* Re-enable the peripherals. */
  45. power_all_enable();
  46. }
  47.  
  48. void setup()
  49. {
  50. //Serial.println("Initialising...");
  51. Serial.begin(9600);
  52. Wire.begin();
  53. //WATCHDOG
  54. /* Clear the reset flag. */
  55. MCUSR &= ~(1<<WDRF);
  56. /* In order to change WDE or the prescaler, we need to
  57. * set WDCE (This will allow updates for 4 clock cycles).
  58. */
  59. WDTCSR |= (1<<WDCE) | (1<<WDE);
  60. /* set new watchdog timeout prescaler value */
  61. WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
  62. /* Enable the WD interrupt (note no reset). */
  63. WDTCSR |= _BV(WDIE);
  64.  
  65. //CONFIGURE DHT11
  66. pinMode(DHTPINVCC, OUTPUT);
  67. pinMode(PHOTOCELLVCC, OUTPUT);
  68. //make it blink at startup
  69. digitalWrite(DHTPINVCC, HIGH);
  70. digitalWrite(PHOTOCELLVCC, HIGH);
  71. delay(500);
  72. digitalWrite(DHTPINVCC, LOW);
  73. digitalWrite(PHOTOCELLVCC, LOW);
  74. //Serial.println("alive");
  75. //Serial.println("\n");
  76. //Serial.println("dead");
  77. delay(1000);
  78. writeEEPROM(disk1, address, 123);
  79. Serial.print(readEEPROM(disk1, address), DEC);
  80. }
  81. void loop()
  82. {
  83. if(f_wdt == 1) {
  84. loopCounter ++;
  85. if (loopCounter > 6) {
  86. // 110 *8 = 880s = 14.6min @ 16mhtz
  87. readSensor();
  88. loopCounter = 0;
  89. }
  90. f_wdt = 0;
  91. enterSleep();
  92. } else {
  93. //nothing
  94. }
  95. }
  96.  
  97. void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
  98. {
  99. Wire.beginTransmission(deviceaddress);
  100. Wire.write((int)(eeaddress >> 8)); // MSB
  101. Wire.write((int)(eeaddress & 0xFF)); // LSB
  102. Wire.write(data);
  103. Wire.endTransmission();
  104.  
  105. delay(5);
  106. }
  107.  
  108. byte readEEPROM(int deviceaddress, unsigned int eeaddress )
  109. {
  110. byte rdata = 0xFF;
  111.  
  112. Wire.beginTransmission(deviceaddress);
  113. Wire.write((int)(eeaddress >> 8)); // MSB
  114. Wire.write((int)(eeaddress & 0xFF)); // LSB
  115. Wire.endTransmission();
  116.  
  117. Wire.requestFrom(deviceaddress,1);
  118.  
  119. if (Wire.available()) rdata = Wire.read();
  120.  
  121. return rdata;
  122. }
  123.  
  124.  
  125. void readSensor() {
  126. digitalWrite(DHTPINVCC, HIGH);
  127. delay(1500);
  128. digitalWrite(PHOTOCELLVCC, HIGH);
  129. delay(500);
  130. //Serial.println("\n");
  131. int photocellReading = analogRead(photocellPin);
  132. int chk = DHT.read22(DHT22_PIN);
  133. //Serial.print("Read sensor: ");
  134. switch (chk)
  135. {
  136. case DHTLIB_OK:
  137. Serial.print("OK,\t");
  138. break;
  139. case DHTLIB_ERROR_CHECKSUM:
  140. Serial.print("Checksum error,\t");
  141. break;
  142. case DHTLIB_ERROR_TIMEOUT:
  143. Serial.print("Time out error,\t");
  144. break;
  145. default:
  146. Serial.print("Unknown error,\t");
  147. break;
  148. }
  149. digitalWrite(DHTPINVCC, LOW);
  150. address ++;
  151. writeEEPROM(disk1, address, DHT.humidity);
  152. address ++;
  153. writeEEPROM(disk1, address, DHT.temperature);
  154. address ++;
  155. writeEEPROM(disk1, address, photocellReading);
  156. digitalWrite(PHOTOCELLVCC, LOW);
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement