-
Notifications
You must be signed in to change notification settings - Fork 0
/
telldus-weather-receiver.ino
284 lines (248 loc) · 6.99 KB
/
telldus-weather-receiver.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
/*
Manchester receiver for 433MHz radio sensors.
Pulses are 500us and 1000us
Received data is sent to the host using hex strings to serial port
Each message consists of
* 2 byte length of bits (N)
* 1 space
* (N + 7)/8 bytes data
* 1 space
* 1 byte crc8(polynomial=0x8c, init = 0)
* 2 byte line end "\r\n"
First received bit is the most significant bit in the byte.
For example
0076 5320502C4494015E0D9173FFEFEC28 B4
0x76 = 118 bits = 15 bytes (-2 bits)
data:
01010011 00100000 01010000 00101100 01000100 10010100 00000001 01011110
00001101 10010001 01110011 11111111 11101111 11101100 001010ZZ
The last byte has 2 filler zero bits (Z) as the lowest bits (0x03).
crc is used to detect possible errors in serial communication.
*/
const int txPin = 7;
const int rxPin = 8;
const int ledPin = 9;
#define IS_SHORT(x) ((x >= 300) && (x <= 750))
#define IS_LONG(x) ((x > 750) && (x <= 1150))
#define MAX_PULSE 2000 // us
#define MAX_DATA 256 // bytes
// States
#define NOSYNC0 0 // Have not received a long pulse
#define NOSYNC8 8 // Received 8 short pulses in a row
#define T1 50 // In sync, short received
#define T2 51 // In sync, long received
void setup() {
pinMode(rxPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
Serial.println("Manchester receiver v0.1");
}
static void emit_bit(unsigned char *data, unsigned int *outcount, int level) {
int bitindex = (*outcount) & 7;
int byteindex = (*outcount) / 8;
if (!bitindex) {
data[byteindex] = 0;
}
bitindex = 7 - bitindex;
if (level) {
data[byteindex] |= 1 << bitindex;
//} else {
// data[byteindex] &= ~(1 << bitindex);
}
(*outcount)++;
}
// Return new state
unsigned char manchester_decode(unsigned long pulse, int level, unsigned char *data, unsigned int *outcount, unsigned char state) {
int is_long = IS_LONG(pulse);
int is_short = IS_SHORT(pulse);
switch (state) {
case T2: // In sync
emit_bit(data, outcount, level);
if (is_short) {
return T1;
}
if (!is_long) {
return NOSYNC0; // Error, sync dropped
}
return T2;
case T1: // Previous was short
if (is_long) {
return NOSYNC0; // Sync dropped
}
if (!is_short) {
return NOSYNC0; // Error, sync dropped
}
return T2;
case NOSYNC8:
if (is_short) {
return state; // Go on, wait for long pulse
}
if (!is_long) {
return NOSYNC0; // Error, sync dropped
}
return T2;
default: // NOSYNC < 8
if (is_short) {
return state + 1; // Count 8 short pulses
}
return NOSYNC0; // No sync, need 8 shorts first
}
return NOSYNC0;
}
uint8_t crc8(uint8_t const message[], unsigned nBytes, uint8_t polynomial, uint8_t init)
{
uint8_t crc = init;
unsigned byte, bit;
for (byte = 0; byte < nBytes; ++byte) {
crc ^= message[byte];
for (bit = 0; bit < 8; ++bit) {
if (crc & 0x80) {
crc = (crc << 1) ^ polynomial;
} else {
crc = (crc << 1);
}
}
}
return crc;
}
static void send_bits(uint8_t data, int bitcount) {
for (int j = 0x80; bitcount; bitcount--, j >>= 1) {
if (data & j) {
digitalWrite(txPin, HIGH);
delayMicroseconds(500);
digitalWrite(txPin, LOW);
delayMicroseconds(450);
} else {
digitalWrite(txPin, LOW);
delayMicroseconds(500);
digitalWrite(txPin, HIGH);
delayMicroseconds(450);
}
}
}
static void send_data(uint8_t *data, const unsigned int bitcount) {
int i;
send_bits(0xff, 8); // Send sync
send_bits(0xff, 8);
send_bits(0xff, 8);
send_bits(0xff, 8);
for (i = 0; i < bitcount / 8; i++) {
send_bits(data[i], 8);
}
send_bits(data[i], bitcount & 7);
digitalWrite(txPin, LOW);
delayMicroseconds(2000);
}
static inline uint8_t nibble(char b)
{
if (b >= '0' && b <= '9') return (b - '0');
if (b >= 'A' && b <= 'F') return ((b - 'A') + 10);
if (b >= 'a' && b <= 'f') return ((b - 'a') + 10);
return (0);
}
static void handle_send_data(uint8_t *str, const unsigned int str_length) {
uint8_t data[255];
int i = 0, j = 0;
unsigned bitcount;
if (str_length < 8) {
return;
}
bitcount = (nibble(str[i++]) << 12) | (nibble(str[i++]) << 8) |
(nibble(str[i++]) << 4) | (nibble(str[i++]) << 0);
if (bitcount / 4 > str_length - 8) {
Serial.println("ERROR LENGTH");
return;
}
if (str[i++] != ' ') {
Serial.println("ERROR SPACE 1");
return;
}
int bytecount = (bitcount + 7) / 8;
for (j = 0; j < bytecount; j++) {
data[j] = (nibble(str[i++]) << 4) | (nibble(str[i++]) << 0);
}
if (str[i++] != ' ') {
Serial.println(str[i-1], HEX);
Serial.println("ERROR SPACE 2");
return;
}
uint8_t crc = (nibble(str[i++]) << 4) | (nibble(str[i++]) << 0);
uint8_t crc2 = crc8(data, bytecount, 0x8C, 0);
if (crc != crc2) {
Serial.print("ERROR CRC ");
Serial.println(crc2, HEX);
return;
}
send_data(data, bitcount);
}
const char hexa[] =
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
static void handle_data(uint8_t *data, const unsigned int bitcount)
{
if (bitcount < 16) {
// Not interested in short messages
return;
}
Serial.print(hexa[(bitcount & 0xF000) >> 12]);
Serial.print(hexa[(bitcount & 0x0F00) >> 8]);
Serial.print(hexa[(bitcount & 0x00F0) >> 4]);
Serial.print(hexa[(bitcount & 0x000F) >> 0]);
Serial.print(' ');
int bytecount = (bitcount + 7) / 8;
for (int i = 0; i < bytecount; i++) {
Serial.print(hexa[(data[i] & 0xF0) >> 4]);
Serial.print(hexa[(data[i] & 0x0F) >> 0]);
}
uint8_t crc = crc8(data, bytecount, 0x8C, 0);
Serial.print(' ');
Serial.print(hexa[(crc & 0xF0) >> 4]);
Serial.print(hexa[(crc & 0x0F) >> 0]);
Serial.println("");
}
void loop() {
unsigned int bitcount = 0;
uint8_t data[MAX_DATA];
uint8_t send_data[255];
int send_length = 0;
unsigned char state = NOSYNC0;
unsigned long t = 0;
unsigned long prev_us = micros();
int prev_rx = digitalRead(rxPin);
do
{
int rx;
unsigned long us;
do
{
rx = digitalRead(rxPin);
us = micros();
t = us - prev_us;
} while (prev_rx == rx && t < MAX_PULSE);
prev_us = us;
prev_rx = rx;
if (t >= MAX_PULSE) rx = ~rx;
state = manchester_decode (t, rx, data, &bitcount, state);
digitalWrite(ledPin, state < T1 ? LOW : HIGH);
if ((bitcount >= sizeof(data) * 8) // Data buffer overflow
|| (state < T1 && bitcount)) {
// Sync is lost, handle the data so far
handle_data(data, bitcount);
bitcount = 0;
}
if (!IS_SHORT(t) && !IS_LONG(t)) {
while (Serial.available()) {
// Handle serial when not receiving
int c = Serial.read();
send_data[send_length++] = c;
if (c == '\r' || c == '\n' || send_length >= sizeof(send_data) - 1) {
send_data[send_length] = 0;
handle_send_data(send_data, send_length);
send_length = 0;
}
}
}
} while (1 /*|| IS_SHORT(t) || IS_LONG(t)*/);
}