df9d998eb074d35e965bf531237ddc03104332cd
[weathermon.git] / Temp_DHT.ino
1 /* YourDuino.com Example Software Sketch
2    DHT11 Humidity and Temperature Sensor test
3    Credits: Rob Tillaart
4    http://arduino-direct.com/sunshop/index.php?l=product_detail&p=162
5    terry@yourduino.com */
6    
7 /*-----( Import needed libraries )-----*/
8 #include <dht11.h>
9 #include <Wire.h>
10 #include <BMP085.h>
11 #include <WeatherSensorWH2.h>
12
13 /*-----( Declare objects )-----*/
14 dht11 DHT11;
15 BMP085 bmp;
16
17 /*-----( Declare Constants, Pin Numbers )-----*/
18 #define DHT11PIN 4
19
20 #define RF_IN 3
21
22 #define REDPIN 11
23 #define GREENPIN 12
24
25 volatile byte got_interval = 0;
26 volatile byte interval = 0;
27
28 volatile unsigned long old = 0, packet_count = 0; 
29 volatile unsigned long spacing, now, average_interval;
30
31 WeatherSensorWH2 weather;
32
33 ISR(TIMER1_COMPA_vect)
34 {
35   static byte count = 0;
36   static byte was_hi = 0;
37   
38   if (digitalRead(RF_IN) == HIGH) {
39     count++;
40     was_hi = 1; 
41   } else {
42     if (was_hi) {
43       was_hi = 0;
44       interval = count;
45       got_interval = 1;
46       count = 0;
47     }
48   }
49 }
50
51 void setup()   /*----( SETUP: RUNS ONCE )----*/
52 {
53   Serial.begin(9600);
54   Serial.println("STATUS:START");
55
56   bmp.begin();
57   
58   pinMode(REDPIN,OUTPUT);
59   pinMode(GREENPIN,OUTPUT);  
60
61   pinMode(RF_IN, INPUT);
62   TCCR1A = 0x00;
63   TCCR1B = 0x09;
64   TCCR1C = 0x00;
65   OCR1A = 399;
66   TIMSK1 = 0x02;
67   sei();
68
69 }/*--(end setup )---*/
70
71 unsigned long previousMillis = 0;
72 unsigned long indoor_interval = 60000;
73 unsigned long outdoor_interval = 45000;
74 unsigned long previousIndoor = 0;
75 unsigned long previousOutdoor = 0;
76
77 void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
78 {
79   
80   byte i;
81   byte *packet;
82   
83   if (got_interval) {
84     weather.accept(interval);  
85     if (weather.acquired()) {
86       now = millis();
87       spacing = now - old;
88       old = now;
89       packet_count++;
90       average_interval = now / packet_count;     
91    /*
92       Serial.print("Spacing: ");
93       Serial.println(spacing, DEC);
94       Serial.print("Packet count: ");
95       Serial.println(packet_count, DEC);
96    
97       Serial.print("Average spacing: ");
98       Serial.println(average_interval, DEC);
99   
100       
101       packet = weather.get_packet();
102       for(i=0;i<5;i++) {
103         Serial.print("0x");
104         Serial.print(packet[i], HEX);
105         Serial.print("/");
106         Serial.print(packet[i], DEC);
107         Serial.print(" ");
108       }  
109       
110       Serial.print("crc: ");
111       Serial.print(weather.calculate_crc(), HEX);
112       Serial.println((weather.valid() ? " OK" : " BAD"));
113       
114    */
115    
116       if (weather.valid()) {
117
118         Serial.print("SENSOR:TYPE=OUTDOOR,");
119         
120         Serial.print("ID=");
121         Serial.print(weather.get_sensor_id(), HEX);
122       
123         Serial.print(",HUMIDITY=");
124         Serial.print(weather.get_humidity(), DEC);
125         
126         Serial.print(",TEMPERATURE=");
127         Serial.println(weather.get_temperature_formatted());
128         
129         previousOutdoor = millis();
130         digitalWrite(REDPIN,HIGH);
131         
132       } else {
133       
134         Serial.println("ERROR:OUTDOOR");
135         previousOutdoor = millis();
136         digitalWrite(REDPIN,LOW);
137         
138       }  
139     
140      }
141    
142    got_interval = 0; 
143  
144   }
145   
146   
147   if ((unsigned long)(millis() - previousMillis) >= indoor_interval) {
148
149     previousMillis = millis();
150   
151     int chk = DHT11.read(DHT11PIN);
152
153     if (chk==0) {
154      
155       Serial.print("SENSOR:TYPE=INDOOR,");
156       Serial.print("HUMIDITY=");
157       Serial.println((float)DHT11.humidity, 2);
158
159 //      Serial.print(",TEMPERATURE=");
160 //      Serial.println((float)DHT11.temperature, 2);
161
162       Serial.print("SENSOR:TYPE=BARO,");
163       Serial.print("PRESSURE=");
164       Serial.print(bmp.readPressure());
165       Serial.print(",TEMPERATURE=");
166       Serial.println(bmp.readTemperature());
167
168       previousIndoor = millis();
169       digitalWrite(GREENPIN,HIGH);
170
171
172     } else {
173       
174       Serial.println("ERROR:INDOOR");
175       previousIndoor = millis();
176       digitalWrite(GREENPIN,LOW);
177         
178     } 
179
180   }
181   
182   if ((unsigned long)(millis() - previousIndoor) >= indoor_interval*10) {
183
184       Serial.println("ERROR:INDOOR TIMEOUT");
185       previousIndoor = millis();
186       digitalWrite(GREENPIN,LOW);
187     
188   }
189
190   if ((unsigned long)(millis() - previousOutdoor) >= outdoor_interval*10) {
191
192       Serial.println("ERROR:OUTDOOR TIMEOUT");
193       previousOutdoor = millis();
194       digitalWrite(REDPIN,LOW);
195     
196   }
197   
198   
199 }/* --(end main loop )-- */
200
201 /* ( THE END ) */