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
6 Created by Luc Small on 19 July 2013.
7 Released into the public domain.
14 // DHT11 and BMP085 wired sensors
18 // Humidity sensor at pin 4
27 // Read data from 433MHz receiver on digital pin 3
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
35 // Port that is hooked to LED to indicate a packet has been received
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
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
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))
62 #define GOT_PULSE 0x01
64 volatile byte wh2_flags = 0;
65 volatile byte wh2_packet_state = 0;
66 volatile int wh2_timeout = 0;
68 byte wh2_calculated_crc;
75 ISR(TIMER1_COMPA_vect)
77 static byte sampling_state = 0;
79 static boolean was_low = false;
81 switch(sampling_state) {
94 case 1: // acquiring first pulse
98 if (IS_HI_PULSE(count)) {
99 wh2_flags = GOT_PULSE | LOGIC_HI;
102 } else if (IS_LOW_PULSE(count)) {
103 wh2_flags = GOT_PULSE; // logic low
111 case 2: // observe 1ms of idle time
114 if (IDLE_HAS_TIMED_OUT(count)) {
116 } else if (IDLE_PERIOD_DONE(count)) {
124 if (wh2_timeout > 0) {
126 if (HAS_TIMED_OUT(wh2_timeout)) {
127 wh2_packet_state = 0;
141 Serial1.begin(57600);
143 Serial1.println("STATUS:STARTING");
147 pinMode(REDPIN,OUTPUT);
148 pinMode(GREENPIN,OUTPUT);
150 pinMode(RF_IN, INPUT);
151 digitalWrite(RF_IN,HIGH);
156 OCR1A = COUNTER_RATE;
163 unsigned long previousMillis = 0;
164 unsigned long indoor_interval = 60000;
165 unsigned long outdoor_interval = 45000;
166 unsigned long previousIndoor = 0;
167 unsigned long previousOutdoor = 0;
184 Serial1.print("SENSOR:TYPE=OUTDOOR,");
186 Serial1.print("ID=");
187 Serial1.print(wh2_sensor_id(), HEX);
189 Serial1.print(",HUMIDITY=");
190 Serial1.print(wh2_humidity(), DEC);
192 Serial1.print(",TEMPERATURE=");
193 Serial1.println(format_temp(wh2_temperature()));
195 previousOutdoor = now;
196 digitalWrite(REDPIN,HIGH);
201 Serial1.println("ERROR:OUTDOOR");
202 previousOutdoor = now;
203 digitalWrite(REDPIN,LOW);
211 if ((unsigned long)(now - previousMillis) >= indoor_interval) {
213 previousMillis = now;
215 int chk = DHT.read11(DHT11PIN);
220 Serial1.print("SENSOR:TYPE=INDOOR,");
221 Serial1.print("HUMIDITY=");
222 Serial1.print(DHT.humidity);
223 Serial1.print(",TEMPERATURE=");
224 Serial1.print(DHT.temperature);
227 Serial1.print("SENSOR:TYPE=BARO,");
228 Serial1.print("PRESSURE=");
229 Serial1.print(bmp.readPressure());
230 Serial1.print(",TEMPERATURE=");
231 Serial1.println(bmp.readTemperature());
233 previousIndoor = now;
234 digitalWrite(GREENPIN,HIGH);
240 Serial1.println("ERROR:INDOOR");
241 previousIndoor = now;
242 digitalWrite(GREENPIN,LOW);
248 if ((unsigned long)(now - previousIndoor) > indoor_interval*10) {
251 Serial1.println("ERROR:INDOOR TIMEOUT");
252 previousIndoor = now;
253 digitalWrite(GREENPIN,LOW);
257 if ((unsigned long)(now - previousOutdoor) > outdoor_interval*10) {
260 Serial1.println("ERROR:OUTDOOR TIMEOUT");
261 previousOutdoor = now;
262 digitalWrite(REDPIN,LOW);
270 // processes new pulse
273 static byte packet_no, bit_no, history;
275 // reset if in initial wh2_packet_state
276 if(wh2_packet_state == 0) {
277 // should history be 0, does it matter?
279 wh2_packet_state = 1;
280 // enable wh2_timeout
282 } // fall thru to wh2_packet_state one
285 if (wh2_packet_state == 1) {
286 // shift history right and store new value
288 // store a 1 if required (right shift along will store a 0)
289 if (wh2_flags & LOGIC_HI) {
292 // check if we have a valid start of frame
294 if ((history & B00000111) == B00000110) {
295 // need to clear packet, and counters
297 // start at 1 becuase only need to acquire 7 bits for first packet byte.
299 wh2_packet[0] = wh2_packet[1] = wh2_packet[2] = wh2_packet[3] = wh2_packet[4] = 0;
300 // we've acquired the preamble
301 wh2_packet_state = 2;
306 if (wh2_packet_state == 2) {
308 wh2_packet[packet_no] <<= 1;
309 if (wh2_flags & LOGIC_HI) {
310 wh2_packet[packet_no] |= 0x01;
327 // start the sampling process from scratch
328 wh2_packet_state = 0;
338 void wh2_calculate_crc()
340 wh2_calculated_crc = crc8(wh2_packet, 4);
345 return (wh2_calculated_crc == wh2_packet[4]);
350 return (wh2_packet[0] << 4) + (wh2_packet[1] >> 4);
355 return wh2_packet[3];
358 /* Temperature in deci-degrees. e.g. 251 = 25.1 */
359 int wh2_temperature()
362 temperature = ((wh2_packet[1] & B00000111) << 8) + wh2_packet[2];
364 if (wh2_packet[1] & B00001000) {
365 temperature = -temperature;
370 String format_temp(int temperature)
376 temperature = -temperature;
380 whole = temperature / 10;
381 partial = temperature - (whole*10);
383 s += String(whole, DEC);
385 s += String(partial, DEC);
391 uint8_t crc8( uint8_t *addr, uint8_t len)
395 // Indicated changes are from reference CRC-8 function in OneWire library
397 uint8_t inbyte = *addr++;
398 for (uint8_t i = 8; i; i--) {
399 uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
400 crc <<= 1; // changed from right shift
401 if (mix) crc ^= 0x31;// changed from 0x8C;
402 inbyte <<= 1; // changed from right shift