-
Notifications
You must be signed in to change notification settings - Fork 0
/
BetterWH2.ino
299 lines (259 loc) · 7.31 KB
/
BetterWH2.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
Updated code for receiving data from WH2 weather station
This code implements timeouts to make decoding more robust
Decodes received packets and writes a summary of each packet to the Arduino's
serial port
Created by Luc Small on 19 July 2013.
Released into the public domain.
*/
// Read data from 433MHz receiver on digital pin 2
#define RF_IN 2
// For better efficiency, the port is read directly
// the following two lines should be changed appropriately
// if the line above is changed.
#define RF_IN_RAW PIND2
#define RF_IN_PIN PIND
// Port that is hooked to LED to indicate a packet has been received
#define LED_PACKET 13
#define COUNTER_RATE 3200-1 // 16,000,000Hz / 3200 = 5000 interrupts per second, ie. 200us between interrupts
// 1 is indicated by 500uS pulse
// wh2_accept from 2 = 400us to 3 = 600us
#define IS_HI_PULSE(interval) (interval >= 2 && interval <= 3)
// 0 is indicated by ~1500us pulse
// wh2_accept from 7 = 1400us to 8 = 1600us
#define IS_LOW_PULSE(interval) (interval >= 7 && interval <= 8)
// worst case packet length
// 6 bytes x 8 bits x (1.5 + 1) = 120ms; 120ms = 200us x 600
#define HAS_TIMED_OUT(interval) (interval > 1200)
// we expect 1ms of idle time between pulses
// so if our pulse hasn't arrived by 1.2ms, reset the wh2_packet_state machine
// 6 x 200us = 1.2ms
#define IDLE_HAS_TIMED_OUT(interval) (interval > 6)
// our expected pulse should arrive after 1ms
// we'll wh2_accept it if it arrives after
// 4 x 200us = 800us
#define IDLE_PERIOD_DONE(interval) (interval >= 4)
// Shorthand for tests
//#define RF_HI (digitalRead(RF_IN) == HIGH)
//#define RF_LOW (digitalRead(RF_IN) == LOW)
#define RF_HI (bit_is_set(RF_IN_PIN, RF_IN_RAW))
#define RF_LOW (bit_is_clear(RF_IN_PIN, RF_IN_RAW))
// wh2_flags
#define GOT_PULSE 0x01
#define LOGIC_HI 0x02
volatile byte wh2_flags = 0;
volatile byte wh2_packet_state = 0;
volatile int wh2_timeout = 0;
byte wh2_packet[11];
byte wh2_calculated_crc;
ISR(TIMER1_COMPA_vect)
{
static byte sampling_state = 0;
static byte count;
static boolean was_low = false;
switch(sampling_state) {
case 0: // waiting
wh2_packet_state = 0;
if (RF_HI) {
if (was_low) {
count = 0;
sampling_state = 1;
was_low = false;
}
} else {
was_low = true;
}
break;
case 1: // acquiring first pulse
count++;
// end of first pulse
if (RF_LOW) {
if (IS_HI_PULSE(count)) {
wh2_flags = GOT_PULSE | LOGIC_HI;
sampling_state = 2;
count = 0;
} else if (IS_LOW_PULSE(count)) {
wh2_flags = GOT_PULSE; // logic low
sampling_state = 2;
count = 0;
} else {
sampling_state = 0;
}
}
break;
case 2: // observe 1ms of idle time
count++;
if (RF_HI) {
if (IDLE_HAS_TIMED_OUT(count)) {
sampling_state = 0;
} else if (IDLE_PERIOD_DONE(count)) {
sampling_state = 1;
count = 0;
}
}
break;
}
if (wh2_timeout > 0) {
wh2_timeout++;
if (HAS_TIMED_OUT(wh2_timeout)) {
wh2_packet_state = 0;
wh2_timeout = 0;
}
}
}
void setup() {
Serial.begin(9600);
Serial.println("Much BetterWH2");
delay(100);
pinMode(LED_PACKET, OUTPUT);
pinMode(RF_IN, INPUT);
TCCR1A = 0x00;
TCCR1B = 0x09;
TCCR1C = 0x00;
OCR1A = COUNTER_RATE;
TIMSK1 = 0x02;
// enable interrupts
sei();
}
void loop() {
static unsigned long old = 0, packet_count = 0, bad_count = 0, average_interval;
unsigned long spacing, now;
byte i;
if (wh2_flags) {
if (wh2_accept()) {
// calculate the CRC
wh2_calculate_crc();
now = millis();
spacing = now - old;
old = now;
packet_count++;
average_interval = now / packet_count;
if (!wh2_valid()) {
bad_count++;
}
// flash green led to say got packet
digitalWrite(LED_PACKET, HIGH);
delay(100);
digitalWrite(LED_PACKET, LOW);
Serial.print(packet_count, DEC);
Serial.print(" | ");
Serial.print(bad_count, DEC);
Serial.print(" | ");
Serial.print(spacing, DEC);
Serial.print(" | ");
Serial.print(average_interval, DEC);
Serial.print(" | ");
for(i=1;i<11;i++) {
Serial.print("0x");
Serial.print(wh2_packet[i], HEX);
//Serial.print("/");
//Serial.print(wh2_packet[i], DEC);
Serial.print(" ");
}
Serial.print("| Sensor ID: 0x");
Serial.print(wh2_sensor_id(), HEX);
Serial.print(" | ");
Serial.print(wh2_humidity(), DEC);
Serial.print("% | ");
Serial.print(wh2_temperature(), DEC);
Serial.print(" | ");
Serial.println((wh2_valid() ? "OK" : "BAD"));
}
wh2_flags = 0x00;
}
}
// processes new pulse
boolean wh2_accept()
{
static byte packet_no, bit_no, history;
// reset if in initial wh2_packet_state
if(wh2_packet_state == 0) {
// should history be 0, does it matter?
history = 0xFF;
wh2_packet_state = 1;
// enable wh2_timeout
wh2_timeout = 1;
} // fall thru to wh2_packet_state one
// acquire preamble
if (wh2_packet_state == 1) {
// shift history right and store new value
history <<= 1;
// store a 1 if required (right shift along will store a 0)
if (wh2_flags & LOGIC_HI) {
history |= 0x01;
}
// check if we have a valid start of frame
// xxxxx110
if ((history & B11111111) == B11111111) {
// need to clear packet, and counters
packet_no = 0;
// start at 1 becuase only need to acquire 7 bits for first packet byte.
bit_no = 1;
wh2_packet[0] = wh2_packet[1] = wh2_packet[2] = wh2_packet[3] = wh2_packet[4] = 0xDE;
// we've acquired the preamble
wh2_packet_state = 2;
}
return false;
}
// acquire packet
if (wh2_packet_state == 2) {
wh2_packet[packet_no] <<= 1;
if (wh2_flags & LOGIC_HI) {
wh2_packet[packet_no] |= 0x01;
}
bit_no ++;
if(bit_no > 7) {
bit_no = 0;
packet_no ++;
}
if (packet_no > 10) {
// start the sampling process from scratch
wh2_packet_state = 0;
// clear wh2_timeout
wh2_timeout = 0;
return true;
}
}
return false;
}
void wh2_calculate_crc()
{
wh2_calculated_crc = crc8(wh2_packet+1, 9);
Serial.print("CRC : ");Serial.println(wh2_calculated_crc);
}
bool wh2_valid()
{
return (wh2_calculated_crc == wh2_packet[10]);
}
int wh2_sensor_id()
{
return ((int)wh2_packet[1] << 4) + (wh2_packet[2] >> 4);
}
byte wh2_humidity()
{
return wh2_packet[4];
}
/* Temperature in deci-degrees. e.g. 251 = 25.1 */
int wh2_temperature()
{
int temperature;
temperature = (((int)wh2_packet[2] & 0xF) << 8) + wh2_packet[3];
// make negative
temperature -= 0x190;
return temperature;
}
uint8_t crc8( uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
// Indicated changes are from reference CRC-8 function in OneWire library
while (len--) {
uint8_t inbyte = *addr++;
for (uint8_t i = 8; i; i--) {
uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
crc <<= 1; // changed from right shift
if (mix) crc ^= 0x31;// changed from 0x8C;
inbyte <<= 1; // changed from right shift
}
}
return crc;
}