-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsws-th.ino
More file actions
534 lines (476 loc) · 11.5 KB
/
Copy pathsws-th.ino
File metadata and controls
534 lines (476 loc) · 11.5 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* Copyright (c) 2022 Jerome Marchand
*
* Read data from temperature and humidity sensor
* Sencor SWS TH2850-2999-3851-5150 with a RF433 module
*/
#include <ArduinoBLE.h>
#include <limits.h>
#include "sws-th.h"
#undef DEBUG
#ifdef DEBUG
#define SCREEN_WIDTH 128
#define print(...) Serial.print(__VA_ARGS__)
#define println(...) Serial.println(__VA_ARGS__)
unsigned long general_error = 0;
unsigned long dataframe1_error = 0;
unsigned long dataframe1_ok = 0;
unsigned long dataframe2_error = 0;
unsigned long dataframe2_ok = 0;
volatile bool buffer_full = false;
inline void inc_general_error() {
general_error++;
}
inline void inc_dataframe1_error() {
dataframe1_error++;
}
inline void inc_dataframe1_ok() {
dataframe1_ok++;
}
inline void inc_dataframe2_error() {
dataframe2_error++;
}
inline void inc_dataframe2_ok() {
dataframe2_ok++;
}
inline void error_stat() {
print("General error: ");
print(general_error);
print(" Read error type 1: ");
print(dataframe1_error);
print(" / ");
print(dataframe1_ok);
print(" type 2: ");
print(dataframe2_error);
print(" / ");
print(dataframe2_ok);
println("");
}
#define FREERAM_FREQ 60
extern "C" char* sbrk(int incr);
unsigned long last_freeram = 0;
int freeRam() {
char top;
return &top - reinterpret_cast<char*>(sbrk(0));
}
void display_freeram() {
print(F("- SRAM left: "));
println(freeRam());
}
#else
inline void print(...) {}
inline void println(...) {}
inline void inc_general_error() {}
inline void inc_dataframe1_error() {}
inline void inc_dataframe1_ok() {}
inline void inc_dataframe2_error() {}
inline void inc_dataframe2_ok() {}
inline void error_stat() {}
inline void display_freeram() {}
#endif
void switchled() {
if(digitalRead(LED_BUILTIN) == HIGH)
digitalWrite(LED_BUILTIN, LOW);
else
digitalWrite(LED_BUILTIN, HIGH);
}
void blink() {
digitalWrite(LED_BUILTIN, LOW);
delay(150);
digitalWrite(LED_BUILTIN, HIGH);
}
void panic() {
while(true) {
blink();
delay(150);
}
}
#define DATA_PIN 0
#define RING_BUFFER_SIZE 4096
#define DATASZ1 128 /* max value, may vary */
#define MIN_DATASZ1 36
#define DATASZ2 41
#define SYNC_SEQ_LEN 8
#define MAX_DATASZ DATASZ1
#define VALUES 9
#define VALUE0_MIN 200
#define VALUE0_MAX 330
#define VALUE1_MIN 420
#define VALUE1_MAX 560
#define VALUE2_MIN 680
#define VALUE2_MAX 800
#define VALUE3_MIN 950
#define VALUE3_MAX 1200
#define VALUE4_MIN 1900
#define VALUE4_MAX 2100
#define VALUE5_MIN 2400
#define VALUE5_MAX 2500
#define VALUE6_MIN 2900
#define VALUE6_MAX 3100
#define VALUE7_MIN 3300
#define VALUE7_MAX 3500
#define VALUE8_MIN 3800
#define VALUE8_MAX 4200
#define IS_VALUE(B) \
inline bool is_value##B (unsigned long d) { \
return d > VALUE##B##_MIN && d < VALUE##B##_MAX; \
}
IS_VALUE(0);
IS_VALUE(1);
IS_VALUE(2);
IS_VALUE(3);
IS_VALUE(4);
IS_VALUE(5);
IS_VALUE(6);
IS_VALUE(7);
IS_VALUE(8);
struct entry {
short temp;
unsigned char ident;
unsigned char channel;
unsigned char humidity;
unsigned char fahrenheit; /* 0 if celcius, 1 if fahrenheit */
unsigned char low_power; /* 1 if low battery, 0 otherwise */
};
void set_entry(struct entry *e, unsigned char ident, unsigned char channel,
short temp, unsigned char humidity, unsigned char fahrenheit,
unsigned char low_power) {
e->ident = ident;
e->channel = channel;
e->temp = temp;
e->humidity = humidity;
e->fahrenheit = fahrenheit;
e->low_power = low_power;
}
inline short int12toshort(short x) {
return x & 0x800 ? x | 0xf000 : x;
}
unsigned long age(unsigned long old, unsigned long current) {
if (current >= old)
return current - old;
else /* Overflow detected */
return ULONG_MAX / 1000 - old + current;
}
#if DEBUG >= 1
void print_entry(struct entry *e) {
print(e->ident);
print("/");
print(e->channel);
print(":\t");
print(e->temp);
print("dC\t");
print(e->humidity);
print("%");
if(e->low_power)
print(" Low Power");
println("");
}
#else
void print_entry(struct entry *e) {}
#endif
int irq = digitalPinToInterrupt(DATA_PIN);
unsigned long timings[RING_BUFFER_SIZE];
volatile char dataframe[MAX_DATASZ];
volatile int dataframesz;
volatile int data_ready = 0;
unsigned long last_cleanup = 0;
BLEService TempSensor(SVC_TEMPSENSOR_UUID);
BLECharacteristic Meteodata(CHRC_METEODATA_UUID, BLERead | BLEIndicate,
CHRC_METEODATA_LEN, true);
char duration_to_bit(unsigned long d)
{
if (is_value0(d))
return '0';
else if (is_value1(d))
return '1';
else if (is_value2(d))
return '2';
else if (is_value3(d))
return '3';
else if (is_value4(d))
return '4';
else if (is_value5(d))
return '5';
else if (is_value6(d))
return '6';
else if (is_value7(d))
return '7';
else if (is_value8(d))
return '8';
else if (d <= VALUE0_MIN)
return '.';
else if (d >= VALUE8_MAX)
return '_';
else
return '?';
}
inline unsigned int index_sub(unsigned int idx, int i)
{
return (idx + RING_BUFFER_SIZE - i) % RING_BUFFER_SIZE;
}
/* Dataframe1 sync sequence is 81 */
bool is_sync1(unsigned int idx)
{
return is_value1(timings[idx]) &&
is_value8(timings[index_sub(idx, 1)]);
}
/* Sync sequence is 22222222 (x8) */
bool is_sync2(unsigned int idx)
{
for (int i = 0; i < SYNC_SEQ_LEN; i++)
if (!is_value2(timings[index_sub(idx, i)]))
return false;
return true;
}
void handler()
{
static unsigned long duration = 0;
static unsigned long lastTime = 0;
static unsigned int ring_index = 0;
static int datacount = 0;
static int in_dataframe = 0;
/* A bit is coded by two impulsions, the first one is the
* significant bit and the second is for synchronisation */
static bool is_bit;
if (data_ready) {
return;
}
unsigned long time = micros();
duration = time - lastTime;
lastTime = time;
ring_index = (ring_index + 1) % RING_BUFFER_SIZE;
timings[ring_index] = duration;
if (in_dataframe == 1) {
is_bit = !is_bit;
if (is_bit) {
if (is_value3(duration))
dataframe[datacount++] = 0;
else if (is_value4(duration))
dataframe[datacount++] = 1;
else if (is_value8(duration)) {
if (datacount < MIN_DATASZ1)
/* Only consider dataframe1 of some size */
goto dataframe1_error;
/* All dataframe is read */
in_dataframe = 0;
dataframesz = datacount;
datacount = 0;
inc_dataframe1_ok();
data_ready = 1;
} else
goto dataframe1_error;
} else
/* Sync impulsion is always short */
if (!is_value1(duration)) {
dataframe1_error:
/* Error */
in_dataframe = 0;
datacount = 0;
inc_dataframe1_error();
}
} else if (in_dataframe == 2) {
is_bit = !is_bit;
if (is_bit) {
if (is_value0(duration))
dataframe[datacount++] = 0;
else if (is_value1(duration))
dataframe[datacount++] = 1;
else
goto dataframe2_error;
} else {
/* Sync impulsion is always the opposite of the bit one */
if (is_value0(duration)) {
if (dataframe[datacount-1] == 0)
goto dataframe2_error;
} else if (is_value1(duration)) {
if (dataframe[datacount-1] == 1)
goto dataframe2_error;
} else {
dataframe2_error:
in_dataframe = 0;
datacount = 0;
inc_dataframe2_error();
}
}
if (datacount == DATASZ2 && !is_bit) {
/* All dataframe received, including the last sync impuls */
in_dataframe = 0;
dataframesz = datacount;
datacount = 0;
inc_dataframe2_ok();
data_ready = 2;
}
} else if (is_sync1(ring_index)) {
in_dataframe = 1;
is_bit = false; /* Next impulsion is a bit */
} else if (is_sync2(ring_index)) {
in_dataframe = 2;
is_bit = false;
}
#ifdef DEBUG
if (ring_index == RING_BUFFER_SIZE - 1)
buffer_full = true;
#endif
}
void setup()
{
pinMode(DATA_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
blink();
#ifdef DEBUG
Serial.begin(115200);
while (!Serial && millis() < 10000);
if (!Serial)
panic();
println("Started.");
#endif
attachInterrupt(irq, handler, CHANGE);
/* Now activate the BLE device. It will start continuously transmitting BLE
advertising packets and will be visible to remote BLE central devices
until it receives a new connection */
if (!BLE.begin()) {
panic();
}
blink();
// set advertised local name and service UUID:
BLE.setDeviceName(DEVICE_NAME);
BLE.setLocalName(DEVICE_NAME);
BLE.setAdvertisedService(TempSensor);
TempSensor.addCharacteristic(Meteodata);
BLE.addService(TempSensor);
Meteodata.setValue("DEAD");
// Build scan response data packet
//BLEAdvertisingData scanData;
//scanData.setLocalName(DEVICE_NAME);
// Copy set parameters in the actual scan response packet
//BLE.setScanResponseData(scanData);
BLE.advertise();
println("advertising ...");
blink();
}
void loop()
{
unsigned long current_time = millis() / 1000;
BLEDevice central = BLE.central();
if (central) {
static unsigned long last_time = 0;
if (current_time - last_time >= 10) {
print("Connected to central: ");
println(central.address());
last_time = current_time;
}
}
if (data_ready) {
noInterrupts();
switchled();
int humidity, temp_deci, ident, channel;
unsigned char data_u4[MAX_DATASZ / 4 + 1];
bool low_power;
memset(data_u4, 0, dataframesz / 4 + 1);
print("Received ");
print(dataframesz);
print(" bits dataframe of type ");
print(data_ready);
print(" at ");
print(current_time);
println("s.");
error_stat();
for (int i = 0; i < dataframesz; i++) {
data_u4[i / 4] += (dataframe[i] << (3 - (i % 4)));
}
#if DEBUG >= 2
for (int i = 0; i < dataframesz; i++) {
print((int)dataframe[i]);
if ((i % 4) == 3)
print(' ');
}
println("");
for (int i = 0; i < dataframesz / 4; i++) {
print(data_u4[i]);
print(' ');
}
println("");
#endif
if (data_ready == 1) {
if(data_u4[6] != 0xf) {
inc_general_error();
println("Unexpected value for bits 24-27");
}
}
ident = (data_u4[0] << 4) + data_u4[1];
channel = (data_u4[2] & 0x3);
temp_deci = int12toshort((data_u4[3] << 8) +
(data_u4[4] << 4) +
data_u4[5]);
if (data_ready == 1) {
/* Channel number coded from zero */
channel++;
humidity = (data_u4[7] << 4) + data_u4[8];
low_power = !(data_u4[2] & 0x8);
} else {
temp_deci -= 900;
humidity = (data_u4[6] << 4) + data_u4[7];
low_power = data_u4[2] & 0x8;
}
print(ident);
print(": ch");
print(channel);
print("\t");
print(temp_deci / 10);
print(".");
print(abs(temp_deci % 10));
if (data_ready ==1) {
print("C\t");
} else {
float t_celcius = ((float)temp_deci - 320) / 18;
print("F\t");
print(t_celcius);
print("C\t");
}
print(humidity);
println("%");
static struct entry last = {0, 0, 0, 0, 0, 0};
static unsigned long last_timestamp = 0;
struct entry e;
set_entry(&e, ident, channel, temp_deci,
humidity, data_ready == 2, low_power);
print_entry(&e);
/* Only update non duplicate value */
if ((e.ident == last.ident) &&
(e.channel == last.channel) &&
(e.fahrenheit == last.fahrenheit) &&
(age(last_timestamp, current_time) < 2)) {
if ((e.temp != last.temp) ||
(e.humidity != last.humidity)) {
println("Values dont match:");
print_entry(&last);
inc_general_error();
}
} else
Meteodata.setValue((uint8_t*)&e,
CHRC_METEODATA_LEN);
memcpy(&last, &e, sizeof(struct entry));
last_timestamp = current_time;
data_ready = 0;
switchled();
interrupts();
}
#ifdef DEBUG
if (age(last_freeram, current_time) > FREERAM_FREQ) {
display_freeram();
last_freeram = current_time;
}
#endif
#if DEBUG >= 3
if (buffer_full) {
for (int i = 0; i < RING_BUFFER_SIZE; i++) {
unsigned long d = timings[i];
print(duration_to_bit(d));
if ( (i+1) % SCREEN_WIDTH == 0)
println("");
}
buffer_full = false;
}
#endif
}