Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. #include <dht.h>
  2. #include <Wire.h>
  3. #define DHT22_PIN 3 // what pin the sensor is connected to
  4. dht DHT;
  5. #define photocellPin 0 // analog 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.  
  13. float temperature;
  14. float humidity;
  15.  
  16.  
  17. uint8_t counter=0;
  18.  
  19. //count number of entering into main loop
  20. uint8_t loopCounter=150; //set to 1 for debug (short sleep) - 150 (20min)
  21.  
  22. //WATCHDOG
  23. volatile int f_wdt=1;
  24.  
  25. ISR(WDT_vect) {
  26. if(f_wdt == 0) {
  27. f_wdt=1;
  28. } else {
  29. //nothing
  30. }
  31. }
  32.  
  33. void enterSleep(void) {
  34. set_sleep_mode(SLEEP_MODE_PWR_DOWN); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
  35. sleep_enable();
  36.  
  37. /* Now enter sleep mode. */
  38. sleep_mode();
  39.  
  40. /* The program will continue from here after the WDT timeout*/
  41. sleep_disable(); /* First thing to do is disable sleep. */
  42.  
  43. /* Re-enable the peripherals. */
  44. power_all_enable();
  45. }
  46.  
  47. void setup()
  48. {
  49. //Serial.println("Initialising...");
  50. Serial.begin(9600);
  51. Wire.begin();
  52. unsigned int address = 0;
  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. //Serial.print("Humidity (%): ");
  152. //Serial.println((float)DHT.humidity, 2);
  153. //myFile.print("\n");
  154. //myFile.print("\n");
  155. //myFile.print("Humidity (%): ");
  156. //myFile.print((float)DHT.humidity, 2);
  157. //Serial.print("Temperature (oC): ");
  158. //Serial.println((float)DHT.temperature, 2);
  159. //myFile.print("\n");
  160. //myFile.print(" Temperature (oC): ");
  161. //myFile.print((float)DHT.temperature, 2);
  162. //Serial.print(" Lux Reading: ");
  163. //Serial.print(photocellReading);
  164. //myFile.print("\n");
  165. //myFile.print(" Lux Reading: ");
  166. //myFile.print(photocellReading);
  167. //myFile.close();
  168. digitalWrite(PHOTOCELLVCC, LOW);
  169. //delay(800);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement