-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrf_recv.ino
113 lines (96 loc) · 2.87 KB
/
rf_recv.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
/*
* Sketch to decode signals from wireless sensors of an old
* security system.
*
* See the README.md for more details.
*
* Initially inspired by this forum post:
*
* http://forum.arduino.cc/index.php?topic=242010.msg1738702#msg1738702
*
* Copyright 2017 David M Kent.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define BAUD_RATE 9600
#define NMESS_BYTES 8
typedef struct {
unsigned char code[NMESS_BYTES]; // The 8 byte sensor code
const char* topic; // The topic to identify the sensor
} SENSOR_IDENT;
#include "rf_zeus_rx.h"
#include "rf_config.h"
#ifdef DEBUG
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINTHEX(x) Serial.print(x, HEX)
#else
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINTHEX(x)
#endif
void setup() {
// Set up serial. We will only write resulting data to serial.
Serial.begin(BAUD_RATE);
while (!Serial) {yield();};
// Initialise the decoder.
init_radio(INPUT_PIN);
// Set up LED for recieve status
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
DEBUG_PRINTLN("initialised");
}
void loop() {
byte buf[NMESS_BYTES + 20];
unsigned int nrecv = 0;
// Wait for some data to arrive (actually waits for preamble to finish)
DEBUG_PRINTLN("waiting");
wait_for_data();
// About to receive, turn LED on
digitalWrite(LED_PIN, HIGH);
DEBUG_PRINTLN("got data");
// Get the incoming data, bytes get written to buf.
get_data(&nrecv, buf);
if (nrecv >= NMESS_BYTES) {
// Received a full message (NMESS_BYTES)
decode_message(buf);
}
digitalWrite(LED_PIN, LOW);
}
/*
* Decode message by looking up in configured sensor_codes.
*
* Prints found sensor_code to Serial if found.
*
*/
void decode_message(byte *data){
char topic[50] = "";
#ifdef DEBUG
for (int i=0; i < NMESS_BYTES; i++) {
DEBUG_PRINTHEX(data[i]);
}
DEBUG_PRINTLN("");
#endif
// Search configured sensors to see if it is registered
for (int i=0; i < num_sensors; i++) {
topic[0] = '\0';
if(memcmp(data, sensor_codes[i].code, NMESS_BYTES) == 0) {
// Found a match...
strcat(topic, outTopic);
strcat(topic, "/");
strcat(topic, sensor_codes[i].topic);
Serial.println(topic);
return;
}
}
DEBUG_PRINTLN("No configured sensor found.");
}