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(115200); // Set the baud.
143 // Wait for U-boot to finish startup. Consume all bytes until we are done.
145 while (Serial1.available() > 0) {
150 } while (Serial1.available()>0);
152 Serial1.begin(57600);
154 Serial1.println("STATUS:STARTING");
158 pinMode(REDPIN,OUTPUT);
159 pinMode(GREENPIN,OUTPUT);
161 pinMode(RF_IN, INPUT);
162 digitalWrite(RF_IN,HIGH);
167 OCR1A = COUNTER_RATE;
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;
197 Serial1.print("SENSOR:TYPE=OUTDOOR,");
199 Serial1.print("ID=");
200 Serial1.print(wh2_sensor_id(), HEX);
202 Serial1.print(",HUMIDITY=");
203 Serial1.print(wh2_humidity(), DEC);
205 Serial1.print(",TEMPERATURE=");
206 Serial1.println(format_temp(wh2_temperature()));
208 previousOutdoor = now;
209 digitalWrite(REDPIN,HIGH);
214 Serial1.println("ERROR:OUTDOOR");
215 previousOutdoor = now;
216 digitalWrite(REDPIN,LOW);
224 if ((unsigned long)(now - previousMillis) >= indoor_interval) {
226 previousMillis = now;
228 int chk = DHT.read11(DHT11PIN);
233 Serial1.print("SENSOR:TYPE=INDOOR,");
234 Serial1.print("HUMIDITY=");
235 Serial1.print(DHT.humidity);
236 Serial1.print(",TEMPERATURE=");
237 Serial1.print(DHT.temperature);
239 previousIndoor = now;
240 digitalWrite(GREENPIN,HIGH);
246 Serial1.println("ERROR:INDOOR");
247 previousIndoor = now;
248 digitalWrite(GREENPIN,LOW);
252 pressure=bmp.readPressure();
253 temperature=bmp.readTemperature();
255 Serial1.print("SENSOR:TYPE=BARO,");
256 Serial1.print("PRESSURE=");
257 Serial1.print(pressure);
258 Serial1.print(",TEMPERATURE=");
259 Serial1.println(temperature);
263 if ((unsigned long)(now - previousIndoor) > indoor_interval*10) {
266 Serial1.println("ERROR:INDOOR TIMEOUT");
267 previousIndoor = now;
268 digitalWrite(GREENPIN,LOW);
272 if ((unsigned long)(now - previousOutdoor) > outdoor_interval*10) {
275 Serial1.println("ERROR:OUTDOOR TIMEOUT");
276 previousOutdoor = now;
277 digitalWrite(REDPIN,LOW);
285 // processes new pulse
288 static byte packet_no, bit_no, history;
290 // reset if in initial wh2_packet_state
291 if(wh2_packet_state == 0) {
292 // should history be 0, does it matter?
294 wh2_packet_state = 1;
295 // enable wh2_timeout
297 } // fall thru to wh2_packet_state one
300 if (wh2_packet_state == 1) {
301 // shift history right and store new value
303 // store a 1 if required (right shift along will store a 0)
304 if (wh2_flags & LOGIC_HI) {
307 // check if we have a valid start of frame
309 if ((history & B00000111) == B00000110) {
310 // need to clear packet, and counters
312 // start at 1 becuase only need to acquire 7 bits for first packet byte.
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;
321 if (wh2_packet_state == 2) {
323 wh2_packet[packet_no] <<= 1;
324 if (wh2_flags & LOGIC_HI) {
325 wh2_packet[packet_no] |= 0x01;
342 // start the sampling process from scratch
343 wh2_packet_state = 0;
353 void wh2_calculate_crc()
355 wh2_calculated_crc = crc8(wh2_packet, 4);
360 return (wh2_calculated_crc == wh2_packet[4]);
365 return (wh2_packet[0] << 4) + (wh2_packet[1] >> 4);
370 return wh2_packet[3];
373 /* Temperature in deci-degrees. e.g. 251 = 25.1 */
374 int wh2_temperature()
377 temperature = ((wh2_packet[1] & B00000111) << 8) + wh2_packet[2];
379 if (wh2_packet[1] & B00001000) {
380 temperature = -temperature;
385 String format_temp(int temperature)
391 temperature = -temperature;
395 whole = temperature / 10;
396 partial = temperature - (whole*10);
398 s += String(whole, DEC);
400 s += String(partial, DEC);
406 uint8_t crc8( uint8_t *addr, uint8_t len)
410 // Indicated changes are from reference CRC-8 function in OneWire library
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