-
Notifications
You must be signed in to change notification settings - Fork 2
/
sniffer_433_RX_MSG1
307 lines (268 loc) · 12.9 KB
/
sniffer_433_RX_MSG1
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
/*
sketch : sniffer_433_RX (sister of "sniffer_433_TX")
Author : Peter Matthews (CurlyWurly) Sept 2017
Desc : When a static 433 transmission is received, a "int codez[]=.." line is output to the serial bus.
Copy this line and paste it into the the sister sketch "sniffer_433_TX"
INFORMATION
N.B. BE AWARE THIS VERSION IS TRIGGERED WHEN A "HIGH" STATE EXCEEDS "minStartHi"
i.e. THIS VERSION IS TUNED TO TRAP THE FIRST MESSAGE BECAUSE SOME TRANSMITTERS DO NOT REPEAT
THEIR MESSAGE - N.B. IT IS LESS RELIABLE THAN THE "2nd MSG" VERSION
N.B. Connect pin 2 (rxpin) to the RX module output
N.B. Copy the output "int codez[] = ...." from serial bus and copy into the sister sketch "Sniffer_433_TX"
N.B. This sniffer sketch just outputs a set value of 35 in code[0]. This means that you may have to
experiment using a different "repeat" value (usually a value of 15 - 45) when used in the "TX" sketch
N.B. The code structure is contained from code[0] to code[n] and is defined as:
code[0] - No. of times to repeat the code
code[1] - No. of pulses (Hi/low combination) - This means it is half the total number of Hi and Low states
code[2] - Wait unit time (Hi and Low states are constructed with multiples of this)
code[3] - First HIGH state multiplier - multiply with code[2]
code[4] - First LOW state multiplier - multiply with code[2]
....
code[n] - last LOW state multiplier
The first pulse is made up of code[3] and code[4]
and the second pulse is made up of code[5] and code[6]
*/
#define rxPin 2 // Input from receiver
#define maxState 300 // Maximum States (Halve for pulses, will detect "pulse trains" up to 150 pulses long)
#define minStartHi 280 // Min start Hi Length (to indicate that pulses have started
#define minBreakLowMult 10 // Min break Low Length multiplier of unitlen
#define maxBreakLowMult 100 // Max break Low Length multiplier of unitlen
#define pinReadDelay 2 // Delay between pin reads
#define pinCountLimit 5 // the "count" limit at which a "HI" state is determined
bool newPinState; // State of input pin - Hi(1) or Low(0)
bool pinState; // State of input pin - Hi(1) or Low(0)
bool lowExceedTrip; // Flag used to remember that a low exceeded has been tripped
bool lastLowOutput; // Output previous "Last Low" before outputting the latest
int pinCount; //
int stateCount; // State change counter
int pulses; // Number of pulses (half of stateCount)
int i; // Array counter
unsigned long T_array[maxState]; // Array of HiLo pulse lengths (starts with Hi)
bool B_array[(maxState/2)]; // Array of bits translated from the HiLo pulse lengths
unsigned long minlengthHI; // Minimum Hi pulse length in us
unsigned long minlengthLOW; // Minimum Lo pulse length in us
unsigned long minBreakLow; // pulse length in us
unsigned long maxBreakLow; // pulse length in us
unsigned long pulseLen; // pulse length in us
unsigned long pulseLenOld; // pulse length in us
unsigned long previousStartTime; // Start Time of pulse in us
unsigned long startTime; // Start Time of pulse in us
unsigned long endTime; // End Time of pulse in us
unsigned long lowStartTime; // Last low period start time
unsigned long lowEndTime; // Last low period End time
unsigned long unitlen; // lowest AVG pulse length in us (assumed L.C.Denominator)
//*********************************************************************************************
void setup() {
//*********************************************************************************************
pinMode(rxPin, INPUT); // initialize input pin used for input signal
Serial.begin(115200); // Set up Serial baud rate
Serial.println("Scanning 433Mhz signals (for first message)");
startTime = micros(); // Store first "Start time"
pinState = false; // prepare stored version of pin state to start as "low"
newPinState = false; // prepare stored version of pin state to start as "low"
lowExceedTrip = false;
lastLowOutput = false;
minBreakLow = minStartHi * minBreakLowMult;
maxBreakLow = minStartHi * maxBreakLowMult;
pinCount = 0; // initialise
stateCount = 0; // initialise
}
//*********************************************************************************************
void loop() {
//*********************************************************************************************
delayMicroseconds(pinReadDelay);
//*********************************************************************************************
// Output end low pulse info:
// - If recording has NOT been started ("stateCount" == 0)
// - and the current pinstate is still low ("false")
// - and the "lastLowOutput" flags is "true"
// - and the time taken since the last time the offical "pinState" changed is greater than "maxBreakLow"
if ( ( stateCount == 0 ) && ( pinState == false ) && ( lastLowOutput == true ) && ( ( micros() - previousStartTime ) > maxBreakLow ) ) {
lastLowOutput = false;
Serial.print( ",");
Serial.print( ( ( (maxBreakLow)* 10) + (5 * unitlen) ) / (10 * unitlen) );
Serial.println("};");
};
//*********************************************************************************************
// Set "lowExceedTrip" trip flag to "true":
// - If recording has been started ("stateCount" > 0)
// - and the current pinstate is still low ("false")
// - and the "lowExceedTrip" trip flag is currently "false"
// - and the time taken since the last time the offical "pinState" changed is greater than "minBreakLow"
if ( ( stateCount > 0 ) && ( pinState == false ) && ( lowExceedTrip == false ) && ( ( micros() - startTime ) > minBreakLow ) ) {
lowExceedTrip = true;
// If the above logic is not triggered, then read the input pin
}else{
if (digitalRead(rxPin)==true) {
if (pinState == true) {
pinCount = pinCountLimit;
}else{
pinCount++;
};
}else{
if ( pinState == false ) {
pinCount = 0;
}else{
pinCount--;
};
};
if ( pinCount < 0 ){
newPinState = false;
pinCount = 0;
};
if ( pinCount > pinCountLimit ){
newPinState = true;
pinCount = pinCountLimit;
};
};
//*********************************************************************************************
// If a state change in "newPinState" or LowExceedTrip1 has been triggered, then proceed
if ( (pinState != newPinState) || (lowExceedTrip == true ) ) {
endTime = micros(); // Record the end time of the read period.
if (lastLowOutput == true) {
lastLowOutput = false;
Serial.print( ",");
Serial.print( ( ( (endTime - previousStartTime)* 10) + (5 * unitlen) ) / (10 * unitlen) );
Serial.println("};");
};
pulseLen = endTime - startTime; // Calculate Pulse length in us
previousStartTime = startTime; // Remember start of previous pulse
startTime = endTime; // Set start time for next state change
pinState = newPinState; // Align pinstate
lowExceedTrip = false; // Set off trip flag
// Only start on a "High" pulse (a "Hi" Length is measured after pin has gone "low")
if ( ( stateCount == 0 ) && ( pinState == true ) ){
stateCount = 0; // initialise
return;
};
// Only start on an initial "high" greater than "minStartHi "
// If previous pulse Length is less than allowed, reset
if ( ( stateCount == 0 ) && ( pulseLen < minStartHi ) ) {
stateCount = 0; // initialise
return;
};
// Store previous state time length (N.B. even indexes are for high, odd index is for low)
T_array[stateCount] = pulseLen;
// Calculate initial low break values from first pulse (assummed to be short high blip)
if ( stateCount == 0 ){
minBreakLow = pulseLen * minBreakLowMult;
maxBreakLow = pulseLen * maxBreakLowMult;
}else{
if (pulseLen < pulseLenOld){
minBreakLow = pulseLen * minBreakLowMult;
maxBreakLow = pulseLen * maxBreakLowMult;
};
};
pulseLenOld = pulseLen;
// If Length is less than min length then abort
if ( T_array[stateCount] < minStartHi ) {
stateCount = 0; // initialise
return;
};
// If previous state time length is less than minBreakLow, then store pulse length
// and return to keep recording state
if ( ( T_array[stateCount] < minBreakLow ) && ( stateCount < maxState ) ) {
stateCount++;
return;
}
// Calculate pulses
pulses = ( (stateCount + 1) / 2);
// Ignore spurious (?) pulse trains
if ( pulses < 10) {
stateCount = 0; // initialise
return;
};
// Calculate Min lengths
minlengthHI = 9999;
minlengthLOW = 9999;
// Don;t count last low!!
for(i=2; i<stateCount; i=i+1){
if ( ( i % 2) == 0 ) {
if (T_array[i] < minlengthHI) { minlengthHI = T_array[i]; }
} else {
if (T_array[i] < minlengthLOW) { minlengthLOW = T_array[i]; };
};
};
// Calculate Avg unit length of pulses
unitlen = 0;
if ( minlengthLOW < ( minlengthHI / 2 )) {
unitlen = minlengthLOW ;
} else {
unitlen = ( ( minlengthHI + minlengthLOW ) / 2 );
};
// Calculate initial low break values from first pulse (assummed to be short high blip)
minBreakLow = unitlen * minBreakLowMult;
maxBreakLow = unitlen * maxBreakLowMult;
// Ignore unit lengths Greater than 1000
if ( unitlen > 1000) {
stateCount = 0; // initialise
return;
};
// Ignore unit lengths Less than 200
if ( unitlen < 200) {
stateCount = 0; // initialise
return;
};
//********************************************************************************
// If you are here, then we have reached a pause ( should be a Low state) of a
// valid pulse train so interpret the array data to decipher the "code" and output to serial
//********************************************************************************
//********************************************************************************************
// Example Interpretion of message to Binary string which is then stored in B_array[]
// This example uses the length of the "High" state (Even indexes)
// if long, then this is a "0"
// if short, then this is a "1"
// N.B. B_array[] will always have half as many indexes as T_array[] !!
// for(i=0; i<=stateCount; i=i+2){
// if ( ( (T_array[i] * 10) + (5 * minlengthHI) ) / (10 * minlengthHI) == 1 ) {
// B_array[(i/2)] = 1;
// } else {
// B_array[(i/2)] = 0;
// };
// };
// Serial.println(" ");
//********************************************************************************************
// Example check for a particular message ( unitlen = 660, pulses = 79, First 7 pulses are short Hi )
// if ( (unitlen < 600)
// || (unitlen > 700)
// || (pulses != 79)
// || (B_array[0] != 1) || (B_array[1] != 1) || (B_array[2] != 1) || (B_array[3] != 1)
// || (B_array[4] != 1) || (B_array[5] != 1) || (B_array[6] != 1)) {
// return;
// };
//********************************************************************************************
// Example output of binary string
// for(i=0; i<=pulses; i=i+1){
// Serial.print(B_array[i]);
// };
// Serial.println(" ");
//********************************************************************************************
// Output deciphered data for TX sketch
// ( Do not output the last "low" period)
Serial.print("int codez[] = {35,");
Serial.print( pulses );
Serial.print(",");
Serial.print(unitlen);
for(i=0; i<stateCount; i=i+1){
Serial.print(",");
// Serial.print( (T_array[i] / unitlen) ); // Alternative round down
Serial.print( ( (T_array[i] * 10) + (5 * unitlen) ) / (10 * unitlen) );
};
lastLowOutput = true;
//********************************************************************************************
// Print pulse lengths
// for(i=0; i<=stateCount; i=i+1){
// if ( ( i % 2) == 0 ) {
// Serial.print( (i/2 +1) );
// Serial.print( " - ");
// Serial.print(T_array[i] ) ;
// } else {
// Serial.print( " , ");
// Serial.println ( T_array[i] ) ;
// };
// };
// Serial.println(" ");
//********************************************************************************************
stateCount = 0; // initialise
};
};