Package: rfcontrolpy
rfcontrolpyrfcontrolpy is a Python library and port of the node.jsrfcontrolpymodule for parsing and constructing 433mhz On-Off Keying (OOK) signals for various devices,
switches and weather stations.It works together with theRFControlArduino library
for receiving the signals.The Python port now contains a working contoller and a dozen of protocols. Not all protocols are
ported yet due to low demand or lack of hardware.You can find a list of all supported protocolshere.The Processing Pipeline1. ReceivingThe arduino is connected via serial bus to the processing computer (for example a raspberry pi)
and waits for rf signal.Mostly all 433mhzw OOK signals from devices are send multiple times directly in row and have a
longer footer pulse in between. They differ by the pulse lengths used to encode the data and footer
and the pulse count.RFControlrunning on the Arduino detects the start of a
signal by its longer footer pulse and verifies it one time by comparing it with the next signal.
It is unaware of the specific protocol, it just uses the stated fact above. Also we are
not interested in it if the pulse was a high or low pulse (presence or absence of a carrier wave),
because the information is decoded in the pulse lengths.We will call the received sequence of pulse lengths nowtimings sequence. For example a timing
sequence in microseconds could look like this:288  972  292  968  292  972  292  968  292  972  920  344  288  976  920  348  
284  976  288  976  284  976  288  976  288  976  916  348  284  980  916  348  
284  976  920  348  284  976  920  348  284  980  280  980  284  980  916  348  
284  9808You can clearly see the two different pulse lengths (around 304 and 959 microseconds) for the data
encoding and the longer footer pulse (9808 microseconds).All observed protocols have less than 8 different pulse length and all pulse length do differ by at
least a factor of 2. This makes a further compression and simplification possible: We map each
pulse length to a number from 0 to 7 (a bucket) and calculate for a better accuracy the average of
all timings mapped to each of the bucket. The result is something like that:buckets: 304, 959, 9808
pulses: 01010101011001100101010101100110011001100101011002To make the representation unique, we choose the buckets in ascending order (respectively we are
sorting it after receiving from the Arduino).We call the sorted bucketspulse lengths, the compressed timingspulse sequenceand the
length of the pulse sequence (inclusive footer)pulse count.2. Protocol MatchingWe detect possible protocols by two criteria. The pulse length must match with a small tolerance
and the pulse count must match.3. Protocol ParsingIf a protocol matches, itsparsefunction is called with the pulse sequence. Most protocols are
parsed almost the same way. First the pulse squence must be converted to a binary representation.In almost all cases there exist a mapping from pulse sequences to a binary0and1. In this
example the pulse sequence0110represents a binary0and0101maps to a binary1:pulses2binary_mapping={["0110":"0"],# Binary 0["0101":"1"],# Binary 1["02":""]# Footer}binary=helpers.pulses2binary(pulses,pulses2binary_mapping)The binary reprsentation now looks like this:110011000010As last step the protocol dependent information must be extracted from the binary representation:decoded={"id":int(binary[:6],2),"unit":int(binary[6:11],2),"state":binary[12]=="1"}DetailsRFControl is more sensitive than needed for most protocols.
So we get sometimes, depending of the accuracy of the sender/remote, different bucket counts.This is by design, to catch up further protocols that maybe need a higher sensitivity. The specific
protocol has not to deal with this issue, becauserfcontrolpyauto merges similar buckets before
calling thedecodePulsesfunction of each protocol.The algorithm is the following:Record the (maybe to many) buckets and compressed pulses withRFControl(Arduino / c++)Sort the buckets inrfcontrolpyprepare_compressed_pulsesTry to find a matching protocol in rfcontrolpydecode_pulsesIf we have more than 3 buckets and two of the buckets are similar (b1*2 < b2) we merge them to just one bucket by averaging and adapting the pulses in rfcontrolpyfix_pulsesGo to step 3Adding a new ProtocolPreparationFork the rfcontrolpy repository and clone your fork into a local directory.We are usingunittestfor automating tests.You should be able to run the tests withpython3 -m unittest discover.Runningpython3 -m buildlet it compile all files and whats for changes.Protocol developmentCreate a new protocol file (like the others) inrfcontrol/protocols/.Add a test case intests/protocolswith the data from the Arduino.Adapt the protocol file, so that the test get passed.