-
Notifications
You must be signed in to change notification settings - Fork 2
/
sniffer_433_RX
419 lines (378 loc) · 14 KB
/
sniffer_433_RX
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
/*
sketch : sniffer_433_RX (sister of "sniffer_433_TX")
Author : Peter Matthews (CurlyWurly) June 2019
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 THAT IF ONLY A SINGLE MESSAGE IS SENT, THIS VERSION WILL NOT SEE IT !!!
THIS VERSION WAITS FOR A LONG ENOUGH LOW PULSE BEFORE TRIGGERING ON THE NEXT "HIGH"
i.e. THIS SKETCH RELIES ON A MESSAGE BEING REPEATED WITH THE 2ND (repeated) MESSAGE BEING DECIPHERED!!!!!!
THE IDEA IS THAT YOU TRIGGER THE TRANSMITTER CLOSE TO THE INPUT AERIAL AND ITS STRONG SIGNAL ENSURES THAT A
NICE CLEAN SIGNAL IS ANALYSED BECAUSE THE "AGC" OF THE RECEIVER WILL HAVE THROTTLED DOWN BY THE TIME THE
2nd MESSAGE IS SEEN (this ensures that any interupting far away signals are nicely filtered out)
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 minStates 24 // Min States (used to calculate a low pulse)
#define lowMultiplier 1.1 // used to help identify the "End pause"
#define maxState 200 // Maximum States (Halve for pulses, will detect "pulse trains" up to 100 pulses long)
#define minBreakLow 1000 // Min break pause
#define maxSmooth 3 // Smoothing factor
#define delayAfter 2000 // Delay after Triggering
bool trig; // recording started
bool pinstate; // State of input pin - Hi(1) or Low(0)
bool newPin; // Smoothed pin state
int stateCount; // State change counter
int pulses; // Number of pulses (half of stateCount)
int i; // Array counter
int cnt; //
int msg_count; // Message 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 firstLow; // First Low length (can replace lastLow)
unsigned long shortestLowPulse; // Shortest LOW pulse length in us
unsigned long shortestHiPulse; // Shortest HI pulse length in us
unsigned long longestLowPulse; // Longest LOW pulse length in us
unsigned long longestHiPulse; // Longest HI pulse length in us
unsigned long pulseLen; // pulse length in us
unsigned long startTime; // Start Time of pulse in us
unsigned long endTime; // End Time of pulse in us
float tempFloat; // General Calculation variable
void data_output();
void decipher_h();
void decipher_l();
//*********************************************************************************************
void setup() {
//*********************************************************************************************
pinMode(rxPin, INPUT); // initialize input pin used for input signal
Serial.begin(115200); // Set up Serial baud rate
// Serial.begin(9600); // Set up Serial baud rate
Serial.println("Scanning for 433Mhz signals (selects 2nd repeated message)");
Serial.print("N.B. After each message has been output, there is a 2 second pause");
Serial.println(", which should ignore any repeated messages from the 3rd Message onwards");
Serial.println(" ");
Serial.print("***");
startTime = micros(); // Store first "Start time"
pinstate = false; // prepare stored version of pin state to start as "low"
trig = false; // initialise
stateCount = 0; // initialise
shortestLowPulse = 999999;
longestLowPulse = 0;
shortestHiPulse = 999999;
longestHiPulse = 0;
cnt = 0;
}
//*********************************************************************************************
void loop() {
//*********************************************************************************************
if ( ( stateCount > minStates ) && ( pinstate == false ) && ( trig == true ) )
{ if ( ( ( micros()-startTime ) > ( longestLowPulse * lowMultiplier ) ) || ( stateCount >= maxState ) )
{
//********************************************************************************
// If you are here, then we have reached the "End Pause". This is signified by
// the current state going HIGH which indicates the endtime of a Low period which
// is considered longer than any other Low period seen since "trig" went high,
// so interpret the array data to decipher the "code" to serial (before trying again)
//********************************************************************************
data_output();
trig = false; // initialise
};
};
// Smooth pinstate
if ( digitalRead(rxPin) == true)
{
if ( newPin == true )
{
cnt = maxSmooth;
}else
{
cnt = cnt + 1;
if ( cnt >= maxSmooth )
{
cnt = maxSmooth;
newPin = true;
};
};
}else
{
if ( newPin == false )
{
cnt = 0;
}else
{
cnt = cnt - 1;
if ( cnt <= 0 )
{
cnt = 0;
newPin = false;
};
};
};
// If a state change, then calculate pulselen
// if (pinstate != digitalRead(rxPin)) {
if ( pinstate != newPin ) {
pinstate = !pinstate;
endTime = micros(); // Record the end time of the read period.
pulseLen = endTime - startTime; // Calculate Pulse length in us
startTime = endTime; // Remember start time for next state change
// If previous pulse Length is less than 100 Us or greater than 50ms, reset
if ( ( pulseLen < 200 ) || ( pulseLen > 100000 ) ) {
trig = false;
} else {
if ( ( trig == false ) && ( pulseLen > minBreakLow) && ( pinstate == true ) ) {
firstLow = pulseLen;
trig = true; // initialise
stateCount = 0; // initialise
shortestLowPulse = 999999;
longestLowPulse = 0;
shortestHiPulse = 999999;
longestHiPulse = 0;
return;
};
};
// Return if not triggered
if ( trig == false ) {
return;
};
// Store previous state time length (N.B. even indexes are for high, odd index is for low)
T_array[stateCount] = pulseLen; // Store Pulse Length
if ( stateCount < minStates )
{
if ( pinstate == true )
{
if ( pulseLen > longestLowPulse )
{
longestLowPulse = pulseLen;
};
if ( pulseLen < shortestLowPulse )
{
shortestLowPulse = pulseLen;
};
}else{
if ( pulseLen > longestHiPulse )
{
longestHiPulse = pulseLen;
};
if ( pulseLen < shortestHiPulse )
{
shortestHiPulse = pulseLen;
};
};
};
stateCount++;
};
}
//*********************************************************************************************
void data_output() {
//*********************************************************************************************
// If necessary, massage time length of "lastlow" to the "firstlow"
// It is assumed that the "HIGH" state will never be long
T_array[stateCount] = firstLow;
// Ignore incorrect end pulse
if ( firstLow < (longestLowPulse * 2) )
{
return;
}
msg_count = msg_count + 1;
// Calculate pulses
pulses = ( (stateCount + 1) / 2);
//********************************************************************************************
// Output deciphered data for TX sketch
Serial.print(" MESSAGE ");
Serial.print(msg_count);
Serial.println(" ***");
Serial.print("int codez[] = {35,");
Serial.print( pulses );
Serial.print(",");
Serial.print(shortestHiPulse);
for(i=0; i<=stateCount; i=i+1){
Serial.print(",");
// Serial.print( (T_array[i] / shortestHiPulse) ); // Alternative round down
Serial.print( ( (T_array[i] * 10) + (5 * shortestHiPulse) ) / (10 * shortestHiPulse) );
};
Serial.println("};");
//********************************************************************************************
// 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[] !!
//**************************************
// if ( (longestHiPulse / shortestHiPulse ) > 1 )
// {
for(i=0; i<=stateCount; i=i+2)
{
if ( ( T_array[i] / shortestHiPulse ) > 1 )
{
B_array[(i/2)] = 0;
} else {
B_array[(i/2)] = 1;
};
};
// HI output of binary string
Serial.print("Hi Binary = ");
for(i=0; i<pulses; i=i+1)
{
Serial.print(B_array[i]);
};
Serial.println(" ");
decipher_h();
// };
//**************************************
// if ( (longestLowPulse / shortestLowPulse ) > 1 )
// {
for(i=1; i<=stateCount; i=i+2)
{
if ( ( T_array[i] / shortestLowPulse ) > 1 )
{
B_array[(i/2)] = 1;
} else {
B_array[(i/2)] = 0;
};
};
// LOW output of binary string
Serial.print("Low Binary = ");
for(i=0; i<pulses; i=i+1)
{
Serial.print(B_array[i]);
};
Serial.println("");
decipher_l();
// };
Serial.println("");
delay(delayAfter);
Serial.print("***");
};
//*********************************************************************************************
void decipher_h() {
//*********************************************************************************************
// Maplin Weather Station 660, pulses = 79 (First 7 pulses are short Hi )
if ( ( shortestHiPulse > 400 )
&& ( shortestHiPulse < 700 )
&& ( pulses == 79 ) )
{
Serial.println("- Maplin Weather Station");
Serial.print("- Outside Temperature = ");
tempFloat = B_array[19]*2048
+ B_array[20]*1024
+ B_array[21]*512
+ B_array[22]*256
+ B_array[23]*128
+ B_array[24]*64
+ B_array[25]*32
+ B_array[26]*16
+ B_array[27]*8
+ B_array[28]*4
+ B_array[29]*2
+ B_array[30]*1;
tempFloat = ( tempFloat - 400 ) / 10;
Serial.print(tempFloat,1);
Serial.println(" Degrees C");
Serial.print("- Outside Humidity = ");
tempFloat = B_array[31]*128
+ B_array[32]*64
+ B_array[33]*32
+ B_array[34]*16
+ B_array[35]*8
+ B_array[36]*4
+ B_array[37]*2
+ B_array[38]*1 ;
Serial.print( tempFloat,0 );
Serial.println( "% RH" );
return;
};
// Maplin Mains Switch
if ( ( shortestHiPulse > 400 )
&& ( shortestHiPulse < 550 )
&& ( pulses == 25 ) )
{
Serial.print("- Maplin Mains Switch");
if ( B_array[9] == true )
{
Serial.print(" - Switch 1");
}else
{
if ( B_array[13] == true )
{
Serial.print(" - Switch 3");
}else
{
if ( B_array[11] == true )
{
Serial.print(" - Switch 2");
}else
{
Serial.print(" - Switch 4");
};
};
};
if ( B_array[23] == true )
{
Serial.println(" - Off ");
}else
{
Serial.println(" - On ");
};
return;
};
};
//*********************************************************************************************
void decipher_l() {
//*********************************************************************************************
// Lidl Temperature Sensor
if ( ( shortestHiPulse > 500 )
&& ( shortestHiPulse < 600 )
&& ( pulses == 34 ) )
{
Serial.println("- Lidl Weather system");
Serial.print("- Outside Temp = ");
tempFloat = ( B_array[16]*128
+ B_array[17]*64
+ B_array[18]*32
+ B_array[19]*16
+ B_array[20]*8
+ B_array[21]*4
+ B_array[22]*2
+ B_array[23]*1 );
tempFloat = tempFloat / 10;
Serial.print(tempFloat,1);
Serial.println(" Degrees C");
return;
};
// 1byOne PIR Driveway Alarm
if ( ( shortestHiPulse > 280 ) // 328
&& ( shortestHiPulse < 400 )
&& ( pulses == 18 ) )
{
Serial.println("- One By One Driveway Alarm");
};
// Byron BY34 Door Bell
if ( ( shortestHiPulse > 450 ) // 328
&& ( shortestHiPulse < 550 )
&& ( pulses == 21 ) )
{
Serial.println("- Byron By34 Door Bell");
return;
};
// IQ PIR Alarm
if ( ( shortestHiPulse > 700 ) // 328
&& ( shortestHiPulse < 950 )
&& ( pulses == 21 ) )
{
Serial.println("- IQ PIR Alarm");
return;
};
}