Переделка веб-интерфейса
[weathermon.git] / Weather_WH2.ino
1 /*
2   Updated code for receiving data from WH2 weather station
3   This code implements timeouts to make decoding more robust
4   Decodes received packets and writes a summary of each packet to the Arduino's
5   serial port
6   Created by Luc Small on 19 July 2013.
7   Released into the public domain.
8 */
9
10 #include <dht.h>
11 #include <Wire.h>
12 #include <BMP085.h>
13
14 // DHT11 and BMP085 wired sensors
15 dht DHT;
16 BMP085 bmp;
17
18 // Humidity sensor at pin 4
19 #define DHT11PIN 5
20
21 #define DEBUG
22
23 // LED pins
24 #define REDPIN 11
25 #define GREENPIN 12
26
27 // Read data from 433MHz receiver on digital pin 3
28 #define RF_IN 4
29 // For better efficiency, the port is read directly
30 // the following two lines should be changed appropriately
31 // if the line above is changed.
32 #define RF_IN_RAW PIND4
33 #define RF_IN_PIN PIND
34
35 // Port that is hooked to LED to indicate a packet has been received
36
37 #define COUNTER_RATE 3200-1 // 16,000,000Hz / 3200 = 5000 interrupts per second, ie. 200us between interrupts
38 // 1 is indicated by 500uS pulse
39 // wh2_accept from 2 = 400us to 3 = 600us
40 #define IS_HI_PULSE(interval)   (interval >= 2 && interval <= 3)
41 // 0 is indicated by ~1500us pulse
42 // wh2_accept from 7 = 1400us to 8 = 1600us
43 #define IS_LOW_PULSE(interval)  (interval >= 7 && interval <= 8)
44 // worst case packet length
45 // 6 bytes x 8 bits x (1.5 + 1) = 120ms; 120ms = 200us x 600
46 #define HAS_TIMED_OUT(interval) (interval > 600)
47 // we expect 1ms of idle time between pulses
48 // so if our pulse hasn't arrived by 1.2ms, reset the wh2_packet_state machine
49 // 6 x 200us = 1.2ms
50 #define IDLE_HAS_TIMED_OUT(interval) (interval > 6)
51 // our expected pulse should arrive after 1ms
52 // we'll wh2_accept it if it arrives after
53 // 4 x 200us = 800us
54 #define IDLE_PERIOD_DONE(interval) (interval >= 4)
55 // Shorthand for tests
56 //#define RF_HI (digitalRead(RF_IN) == HIGH)
57 //#define RF_LOW (digitalRead(RF_IN) == LOW)
58 #define RF_HI (bit_is_set(RF_IN_PIN, RF_IN_RAW))
59 #define RF_LOW (bit_is_clear(RF_IN_PIN, RF_IN_RAW))
60
61 // wh2_flags 
62 #define GOT_PULSE 0x01
63 #define LOGIC_HI  0x02
64 volatile byte wh2_flags = 0;
65 volatile byte wh2_packet_state = 0;
66 volatile int wh2_timeout = 0;
67 byte wh2_packet[5];
68 byte wh2_calculated_crc;
69
70
71 #ifdef DEBUG
72 byte printed = 0;
73 #endif
74
75 ISR(TIMER1_COMPA_vect)
76 {
77   static byte sampling_state = 0;
78   static byte count;   
79   static boolean was_low = false; 
80     
81   switch(sampling_state) {
82     case 0: // waiting
83       wh2_packet_state = 0;
84       if (RF_HI) {
85         if (was_low) {
86           count = 0;
87           sampling_state = 1;
88           was_low = false;
89         }
90       } else {
91         was_low = true;  
92       }
93       break;
94     case 1: // acquiring first pulse
95       count++;
96       // end of first pulse
97       if (RF_LOW) {
98         if (IS_HI_PULSE(count)) {
99           wh2_flags = GOT_PULSE | LOGIC_HI;
100           sampling_state = 2;
101           count = 0;        
102         } else if (IS_LOW_PULSE(count)) {
103           wh2_flags = GOT_PULSE; // logic low
104           sampling_state = 2;
105           count = 0;      
106         } else {
107           sampling_state = 0;
108         }    
109       }   
110       break;
111     case 2: // observe 1ms of idle time
112       count++;
113       if (RF_HI) {
114          if (IDLE_HAS_TIMED_OUT(count)) {
115            sampling_state = 0;
116          } else if (IDLE_PERIOD_DONE(count)) {
117            sampling_state = 1;
118            count = 0;
119          }
120       }     
121       break;     
122   }
123   
124   if (wh2_timeout > 0) {
125     wh2_timeout++; 
126     if (HAS_TIMED_OUT(wh2_timeout)) {
127       wh2_packet_state = 0;
128       wh2_timeout = 0;
129 #ifdef DEBUG
130       if (printed) {
131         Serial1.println();
132         printed=0;
133       }
134 #endif
135     }
136   }
137 }
138
139 void setup() {
140
141   Serial1.begin(115200); // Set the baud.
142  
143    // Wait for U-boot to finish startup.  Consume all bytes until we are done.
144   do {
145      while (Serial1.available() > 0) {
146         Serial1.read();
147      }
148    
149     delay(1000);
150   } while (Serial1.available()>0);  
151
152   Serial1.begin(57600);
153   Serial1.println();
154   Serial1.println("STATUS:STARTING");
155
156   bmp.begin();
157   
158   pinMode(REDPIN,OUTPUT);
159   pinMode(GREENPIN,OUTPUT);  
160
161   pinMode(RF_IN, INPUT);
162   digitalWrite(RF_IN,HIGH);
163   
164   TCCR1A = 0x00;
165   TCCR1B = 0x09;
166   TCCR1C = 0x00;
167   OCR1A = COUNTER_RATE; 
168   TIMSK1 = 0x02;
169   
170   // enable interrupts
171   sei();
172 }
173
174 unsigned long previousMillis = 0;
175 unsigned long indoor_interval = 60000;
176 unsigned long outdoor_interval = 45000;
177 unsigned long previousIndoor = 0;
178 unsigned long previousOutdoor = 0;
179
180 float temperature;
181 float pressure;
182
183 void loop() {
184   unsigned long now;
185   byte i;
186
187   now = millis();
188
189   if (wh2_flags) {
190     if (wh2_accept()) {
191       // calculate the CRC
192       wh2_calculate_crc();
193       
194       if (wh2_valid()) {
195         
196         Serial1.println();
197         Serial1.print("SENSOR:TYPE=OUTDOOR,");
198         
199         Serial1.print("ID=");
200         Serial1.print(wh2_sensor_id(), HEX);
201       
202         Serial1.print(",HUMIDITY=");
203         Serial1.print(wh2_humidity(), DEC);
204         
205         Serial1.print(",TEMPERATURE=");
206         Serial1.println(format_temp(wh2_temperature()));
207         
208         previousOutdoor = now;
209         digitalWrite(REDPIN,HIGH);
210         
211       } else {
212       
213         Serial1.println();
214         Serial1.println("ERROR:OUTDOOR");
215         previousOutdoor = now;
216         digitalWrite(REDPIN,LOW);
217         
218       }  
219
220    }
221    wh2_flags = 0x00; 
222   }
223
224   if ((unsigned long)(now - previousMillis) >= indoor_interval) {
225
226     previousMillis = now;
227  
228     int chk = DHT.read11(DHT11PIN);
229
230     if (chk==0) {
231      
232       Serial1.println();
233       Serial1.print("SENSOR:TYPE=INDOOR,");
234       Serial1.print("HUMIDITY=");
235       Serial1.print(DHT.humidity);
236       Serial1.print(",TEMPERATURE=");
237       Serial1.print(DHT.temperature);
238
239       previousIndoor = now;
240       digitalWrite(GREENPIN,HIGH);
241
242
243     } else {
244         
245       Serial1.println();
246       Serial1.println("ERROR:INDOOR");
247       previousIndoor = now;
248       digitalWrite(GREENPIN,LOW);
249         
250     } 
251
252     pressure=bmp.readPressure();
253     temperature=bmp.readTemperature();
254     Serial1.println();
255     Serial1.print("SENSOR:TYPE=BARO,");
256     Serial1.print("PRESSURE=");
257     Serial1.print(pressure);
258     Serial1.print(",TEMPERATURE=");
259     Serial1.println(temperature);
260
261   }
262   
263   if ((unsigned long)(now - previousIndoor) > indoor_interval*10) {
264
265       Serial1.println();
266       Serial1.println("ERROR:INDOOR TIMEOUT");
267       previousIndoor = now;
268       digitalWrite(GREENPIN,LOW);
269     
270   }
271
272   if ((unsigned long)(now - previousOutdoor) > outdoor_interval*10) {
273
274       Serial1.println();
275       Serial1.println("ERROR:OUTDOOR TIMEOUT");
276       previousOutdoor = now;
277       digitalWrite(REDPIN,LOW);
278     
279   }
280
281
282 }
283
284
285 // processes new pulse
286 boolean wh2_accept()
287 {
288   static byte packet_no, bit_no, history;
289
290   // reset if in initial wh2_packet_state
291   if(wh2_packet_state == 0) {
292      // should history be 0, does it matter?
293     history = 0xFF;
294     wh2_packet_state = 1;
295     // enable wh2_timeout
296     wh2_timeout = 1;
297   } // fall thru to wh2_packet_state one
298  
299   // acquire preamble
300   if (wh2_packet_state == 1) {
301      // shift history right and store new value
302      history <<= 1;
303      // store a 1 if required (right shift along will store a 0)
304      if (wh2_flags & LOGIC_HI) {
305        history |= 0x01;
306      }
307      // check if we have a valid start of frame
308      // xxxxx110
309      if ((history & B00000111) == B00000110) {
310        // need to clear packet, and counters
311        packet_no = 0;
312        // start at 1 becuase only need to acquire 7 bits for first packet byte.
313        bit_no = 1;
314        wh2_packet[0] = wh2_packet[1] = wh2_packet[2] = wh2_packet[3] = wh2_packet[4] = 0;
315        // we've acquired the preamble
316        wh2_packet_state = 2;
317     }
318     return false;
319   }
320   // acquire packet
321   if (wh2_packet_state == 2) {
322
323     wh2_packet[packet_no] <<= 1;
324     if (wh2_flags & LOGIC_HI) {
325       wh2_packet[packet_no] |= 0x01;
326 #ifdef DEBUG
327       Serial1.print('1');
328       printed=1;
329     } else {
330       Serial1.print('0');
331       printed=1; 
332 #endif
333     }
334
335     bit_no ++;
336     if(bit_no > 7) {
337       bit_no = 0;
338       packet_no ++;
339     }
340
341     if (packet_no > 4) {
342       // start the sampling process from scratch
343       wh2_packet_state = 0;
344       // clear wh2_timeout
345       wh2_timeout = 0;
346       return true;
347     }
348   }
349   return false;
350 }
351
352
353 void wh2_calculate_crc()
354 {
355   wh2_calculated_crc = crc8(wh2_packet, 4);
356 }
357
358 bool wh2_valid()
359 {
360   return (wh2_calculated_crc == wh2_packet[4]);
361 }
362
363 int wh2_sensor_id()
364 {
365   return (wh2_packet[0] << 4) + (wh2_packet[1] >> 4);
366 }
367
368 byte wh2_humidity()
369 {
370   return wh2_packet[3];
371 }
372
373 /* Temperature in deci-degrees. e.g. 251 = 25.1 */
374 int wh2_temperature()
375 {
376   int temperature;
377   temperature = ((wh2_packet[1] & B00000111) << 8) + wh2_packet[2];
378   // make negative
379   if (wh2_packet[1] & B00001000) {
380     temperature = -temperature;
381   }
382   return temperature;
383 }
384
385 String format_temp(int temperature)
386 {
387   byte whole, partial;
388   String s;
389   s = String();
390   if (temperature<0) {
391     temperature = -temperature;
392     s += String('-');
393   }
394        
395   whole = temperature / 10;
396   partial = temperature - (whole*10);
397
398   s += String(whole, DEC);
399   s += '.';
400   s += String(partial, DEC);
401
402   return s;
403
404 }
405
406 uint8_t crc8( uint8_t *addr, uint8_t len)
407 {
408   uint8_t crc = 0;
409
410   // Indicated changes are from reference CRC-8 function in OneWire library
411   while (len--) {
412     uint8_t inbyte = *addr++;
413     for (uint8_t i = 8; i; i--) {
414       uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
415       crc <<= 1; // changed from right shift
416       if (mix) crc ^= 0x31;// changed from 0x8C;
417       inbyte <<= 1; // changed from right shift
418     }
419   }
420   return crc;
421 }
422
423
424
425
426
427
428
429
430