-
Notifications
You must be signed in to change notification settings - Fork 11
/
WeatherStation.ino
373 lines (321 loc) · 9.26 KB
/
WeatherStation.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Oregon V2 decoder added - Dominique Pierre
// Oregon V3 decoder revisited - Dominique Pierre
// New code to decode OOK signals from weather sensors, etc.
// 2010-04-11 <[email protected]> http://opensource.org/licenses/mit-license.php
// $Id: ookDecoder.pde 5331 2010-04-17 10:45:17Z jcw $
class DecodeOOK {
protected:
byte total_bits, bits, flip, state, pos, data[25];
virtual char decode (word width) =0;
public:
enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
DecodeOOK () { resetDecoder(); }
bool nextPulse (word width) {
if (state != DONE)
switch (decode(width)) {
case -1: resetDecoder(); break;
case 1: done(); break;
}
return isDone();
}
bool isDone () const { return state == DONE; }
const byte* getData (byte& count) const {
count = pos;
return data;
}
void resetDecoder () {
total_bits = bits = pos = flip = 0;
state = UNKNOWN;
}
// add one bit to the packet data buffer
virtual void gotBit (char value) {
total_bits++;
byte *ptr = data + pos;
*ptr = (*ptr >> 1) | (value << 7);
if (++bits >= 8) {
bits = 0;
if (++pos >= sizeof data) {
resetDecoder();
return;
}
}
state = OK;
}
// store a bit using Manchester encoding
void manchester (char value) {
flip ^= value; // manchester code, long pulse flips the bit
gotBit(flip);
}
// move bits to the front so that all the bits are aligned to the end
void alignTail (byte max =0) {
// align bits
if (bits != 0) {
data[pos] >>= 8 - bits;
for (byte i = 0; i < pos; ++i)
data[i] = (data[i] >> bits) | (data[i+1] << (8 - bits));
bits = 0;
}
// optionally shift bytes down if there are too many of 'em
if (max > 0 && pos > max) {
byte n = pos - max;
pos = max;
for (byte i = 0; i < pos; ++i)
data[i] = data[i+n];
}
}
void reverseBits () {
for (byte i = 0; i < pos; ++i) {
byte b = data[i];
for (byte j = 0; j < 8; ++j) {
data[i] = (data[i] << 1) | (b & 1);
b >>= 1;
}
}
}
void reverseNibbles () {
for (byte i = 0; i < pos; ++i)
data[i] = (data[i] << 4) | (data[i] >> 4);
}
void done () {
while (bits)
gotBit(0); // padding
state = DONE;
}
};
// 433 MHz decoders
class OregonDecoderV2 : public DecodeOOK {
public:
OregonDecoderV2() {}
// add one bit to the packet data buffer
virtual void gotBit (char value) {
if(!(total_bits & 0x01))
{
data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00);
}
total_bits++;
pos = total_bits >> 4;
if (pos >= sizeof data) {
resetDecoder();
return;
}
state = OK;
}
virtual char decode (word width) {
if (200 <= width && width < 1200) {
byte w = width >= 700;
switch (state) {
case UNKNOWN:
if (w != 0) {
// Long pulse
++flip;
} else if (32 <= flip) {
// Short pulse, start bit
flip = 0;
state = T0;
} else {
// Reset decoder
return -1;
}
break;
case OK:
if (w == 0) {
// Short pulse
state = T0;
} else {
// Long pulse
manchester(1);
}
break;
case T0:
if (w == 0) {
// Second short pulse
manchester(0);
} else {
// Reset decoder
return -1;
}
break;
}
} else {
return -1;
}
return total_bits == 160 ? 1: 0;
}
};
class OregonDecoderV3 : public DecodeOOK {
public:
OregonDecoderV3() {}
// add one bit to the packet data buffer
virtual void gotBit (char value) {
data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00);
total_bits++;
pos = total_bits >> 3;
if (pos >= sizeof data) {
resetDecoder();
return;
}
state = OK;
}
virtual char decode (word width) {
if (200 <= width && width < 1200) {
byte w = width >= 700;
switch (state) {
case UNKNOWN:
if (w == 0)
++flip;
else if (32 <= flip) {
flip = 1;
manchester(1);
} else
return -1;
break;
case OK:
if (w == 0)
state = T0;
else
manchester(1);
break;
case T0:
if (w == 0)
manchester(0);
else
return -1;
break;
}
} else {
return -1;
}
return total_bits == 80 ? 1: 0;
}
};
OregonDecoderV2 orscV2;
OregonDecoderV3 orscV3;
#define PORT 2
volatile word pulse;
// Serial activity LED
const int serialLED = 7;
// Sensor activity LED
const int sensorLED = 6;
// Details for my sensor:
const int sensorChannel = 1;
const int sensorCode = 0xEC;
// Serial variables
const int serialLength = 32; // size of the serial buffer
char serialString[serialLength];
byte serialIndex = 0;
char lasttemp[6] = "0.0";
char lasthumid[3] = "0";
long lasttime = 0;
#if defined(__AVR_ATmega1280__)
void ext_int_1(void) {
#else
ISR(ANALOG_COMP_vect) {
#endif
static word last;
// determine the pulse length in microseconds, for either polarity
pulse = micros() - last;
last += pulse;
}
void parseData (const char* s, class DecodeOOK& decoder) {
byte pos;
const byte* data = decoder.getData(pos);
// TODO: something sensible with the sensor code
// if ((int)(data[2] >> 4) == sensorChannel && (int)data[3] == sensorCode) {
if ((int)(data[2] >> 4) == sensorChannel) {
// Get the temperature.
char temp[6];
char *tempptr = &temp[0];
// 14th nibble indicates sign. non-zero for -ve
if ((int)(data[6] & 0x0F) != 0) {
*tempptr = '-';
tempptr++;
}
sprintf(tempptr, "%02x", (int)(data[5]));
tempptr = tempptr + 2;
*tempptr = '.';
tempptr++;
sprintf(tempptr, "%x", (int)(data[4] >> 4));
// Get the humidity.
char humid[3];
char *humidptr = &humid[0];
sprintf(humidptr, "%x", (int)(data[7] & 0x0F));
humidptr++;
sprintf(humidptr, "%x", (int)(data[6] >> 4));
humid[2] = '\0';
strcpy(lasttemp, temp);
strcpy(lasthumid, humid);
lasttime = millis();
}
decoder.resetDecoder();
}
void readSerial() {
while ((Serial.available() > 0) && (serialIndex < serialLength-1)) {
digitalWrite(serialLED, HIGH);
char serialByte = Serial.read();
if (serialByte != ';') {
serialString[serialIndex] = serialByte;
serialIndex++;
}
if (serialByte == ';' or serialIndex == (serialLength-1)) {
parseSerial();
serialIndex = 0;
memset(&serialString, 0, serialLength);
}
}
digitalWrite(serialLED, LOW);
}
void parseSerial() {
if (strcmp(serialString, "get") == 0) {
long age = (millis() - lasttime) / 1000;
// Format is "channel,temp,humidity,age;"
Serial.print("1,");
Serial.print(lasttemp);
Serial.print(",");
Serial.print(lasthumid);
Serial.print(",");
Serial.print(age);
Serial.print(";\n");
}
}
void setup () {
Serial.begin(115200);
pinMode(serialLED, OUTPUT);
digitalWrite(serialLED, LOW);
pinMode(sensorLED, OUTPUT);
digitalWrite(serialLED, LOW);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
#if !defined(__AVR_ATmega1280__)
pinMode(13 + PORT, INPUT); // use the AIO pin
digitalWrite(13 + PORT, 1); // enable pull-up
// use analog comparator to switch at 1.1V bandgap transition
ACSR = _BV(ACBG) | _BV(ACI) | _BV(ACIE);
// set ADC mux to the proper port
ADCSRA &= ~ bit(ADEN);
ADCSRB |= bit(ACME);
ADMUX = PORT - 1;
#else
attachInterrupt(1, ext_int_1, CHANGE);
DDRE &= ~_BV(PE5);
PORTE &= ~_BV(PE5);
#endif
// Serial.println("\n[weatherstation initialised]");
}
void loop () {
static int i = 0;
cli();
word p = pulse;
pulse = 0;
sei();
//if (p != 0){ Serial.print(++i); Serial.print('\n');}
if (p != 0) {
digitalWrite(sensorLED, HIGH);
if (orscV2.nextPulse(p)) {
digitalWrite(sensorLED, HIGH);
parseData("OSV2", orscV2);
digitalWrite(sensorLED, LOW);
}
digitalWrite(sensorLED, LOW);
}
readSerial();
}