Skip to content

Add_New_Remote_Part_2

Martin-Laclaustra edited this page Jun 8, 2018 · 10 revisions

Updating the RCSwitch Library

Checklist (Before you start)

  1. Have the results from your completed code sampling and analysis
  2. Have a working 433/315mHz transmitter
  3. A Basic Understanding of C/C++ and Github
  4. Toolchain to edit/debug code working with your device. If in doubt work with an Arduino Uno type unit then port to ESP8266 etc

Updating the code

  1. Create a new Github Repo to host your copy of RCSwitch. This will allow others to review your code and mistakes won't impact others! Use the same license as RCSwitch i.e. Lesser GPL 2.1
  2. I know I can set the pulse length to 605µs so no code change for this.
  3. rc-switch/RCSwitch.cpp added protcol 7 { 605, { 1, 28 }, { 1, 2 }, { 1, 1 }, false }✓ (Usually this is all that you will need to do... now skip to testing)
  4. My remote has a signal that has 1 sync bit + 52 data bits. That is quite a long train of pulses and I will need to modify the source code of the library (cpp + h files) to hold the data (currently RCSwitch uses a 32-bits int, I need a 64-bits int) for both, sending and receiving.
  5. From my sample data I analysed I got: 2 integers (4315972465486165 and 4315972465486182) that represent the 52-bit data to send for Switch ON and OFF. I need to modify the library to use 64-bit int rather than 32-bit. I could create a send64 command so the name does not clash with the existing 32bit send function or I could modify the existing library to work with both. The later seems like a good option, though it will need more memory for the receive / transmit buffer. The larger memory footprint might seem like a deal breaker but its the only way to support the longer data stream that my device needs.
  6. rc-switch/RCSwitch.h "1 sync bit + 52" =53 bits, "You need up+down for each bit." = 2 state changes per bit = Low to high, high to low = 2* 53 =106 #define RCSWITCH_MAX_CHANGES 106
  7. rc-switch/RCSwitch.h "(calculate to accommodate 64 bits)" void send(unsigned long long code, unsigned int length);Guaranteed by the C standard (ISO C99) to be at least 64 bits✓
  8. rc-switch/RCSwitch.cpp void RCSwitch::send(unsigned long long code, unsigned int length) {
  9. rc-switch/RCSwitch.h unsigned long long getReceivedValue();
  10. rc-switch/RCSwitch.h static unsigned long long nReceivedValue;This was line 142 in RCSwitch.h not line 170
  11. rc-switch/RCSwitch.cpp unsigned long long RCSwitch::getReceivedValue() {
  12. rc-switch/RCSwitch.cpp unsigned long long code = 0;
  13. rc-switch/RCSwitch.cpp unsigned long long RCSwitch::nReceivedValue = 0;
  14. When I ran the code I notice that the send code would not compile as in my modified version of the library the signature of the functions had changed and the long long was not supported by Serial.print so I changed my version of the program

@frownbreaker uploaded changes of RCSwitch to work with longer bit-codes at https://github.com/frownbreaker/rc-switch The only changes are rc-switch/RCSwitch.h, and rc-switch/RCSwitch.cpp

Testing

The first test is to be able to control your sockets from rcswitch by sending your codes. Leave reception for subsequent tests after your are sure that you are able to send. Sending "should be" a simpler process than receiving. Use SendDemo with YOUR protocol and YOUR codes previously decoded by hand. If your sockets do not react, inspect where sending does fail by following the procedure in: https://github.com/sui77/rc-switch/wiki/Add_New_Remote_Part_2#if-your-changes-dont-work

Once you have a protocol that works for sending it is possible to try to solve receiving if it does not work directly.

If your changes don't work

The following procedure might help:

  • Run your code to produce your "synthetic" codes and record them with SimpleRcScanner.
  • Compare the recordings of the "native" signal with those of the "synthetic" one.

Testing Reception

NOTE: THE LAST PART OF THIS PAGE NEEDS REVIEWING DUE TO SUB-OPTIMAL PROPOSED CODE MODIFICATIONS (like using division instead of bitshifting ">>")

ReceiveDemo_Advanced

(No Output) When I run the example ReceiveDemo_Advanced it receives protocol1 but not my new protocol 7 (no output)

ReceiveDemo_Simple

(needed minor to print some output only one number matches manual decode) If I compile the example ReceiveDemo_Simple Serial.print conversion is flagged as ambiguous. I tried Serial.print(static_cast< long long >(mySwitch.getReceivedValue()) ); but that would not compile though Serial.print(static_cast< long >(mySwitch.getReceivedValue()) ); does. I put in some code to provide the output.

Sample output

Comments using Remote that I want to capture / transmit with the value output 4315972465486165 matches the manual calculation but the 4315972465486250 does not.

Data from WL101-341/331 receiver 
Received 4315972465486250 / 52bit Protocol: 7 Button 1 ON
Received 4315972465486165 / 52bit Protocol: 7 Button 1 OFF (Matches Manual Calc!)
Received 4315972465432234 / 52bit Protocol: 7 Button 2 ON
Received 4315972465432149 / 52bit Protocol: 7 Button 2 OFF
Received 4315972465239466 / 52bit Protocol: 7 Button 3 ON
Received 4315972465239381 / 52bit Protocol: 7 Button 3 OFF
Received 4315972465157546 / 52bit Protocol: 7 Button 4 ON
Received 4315972465157461 / 52bit Protocol: 7 Button 4 OFF
Data from second cheaper receiver (seems to match the previos reception tests)...
Received 4315972465486250 / 52bit Protocol: 7 Button 1 ON
Received 4315972465486165 / 52bit Protocol: 7 Button 1 OFF (Matches Manual Calc!)
Received 4315972465432234 / 52bit Protocol: 7 Button 2 ON
Received 4315972465432149 / 52bit Protocol: 7 Button 2 OFF
Received 4315972465239466 / 52bit Protocol: 7 Button 3 ON
Received 4315972465239381 / 52bit Protocol: 7 Button 3 OFF
Received 4315972465157546 / 52bit Protocol: 7 Button 4 ON
Received 4315972465157461 / 52bit Protocol: 7 Button 4 OFF

Modifed ReceiveDemo_Simple

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #2
}

void loop() {
  if (mySwitch.available()) {   
    int value = mySwitch.getReceivedValue();   
    if (value == 0) {
      Serial.print("Unknown encoding");
    } else {
/*   Serial.print("Received (Cast long) ");
      Serial.print(static_cast<  long >(mySwitch.getReceivedValue()) );
*/   Serial.print("Received ");
      uint64_t xx = mySwitch.getReceivedValue()/1000000000ULL;
      if (xx >0) Serial.print((long)xx);
      Serial.print((long)(mySwitch.getReceivedValue() - xx*1000000000));
      Serial.print(" / ");
      Serial.print( mySwitch.getReceivedBitlength() );
      Serial.print("bit ");
      Serial.print("Protocol: ");
      Serial.println( mySwitch.getReceivedProtocol() );
    }
    mySwitch.resetAvailable();
  }
}

Code from Send test

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
  Serial.begin(9600);
   // Transmitter is connected to Arduino Pin #10  
  mySwitch.enableTransmit(10); 
}

void loop() {
  mySwitch.setProtocol(1); // Test with working receiver just to validate Transmitter is working
  mySwitch.setPulseLength(175);
  mySwitch.setRepeatTransmit(7);
  mySwitch.send("010001010001010111001100");
  delay(2000);
  // Test code for new protocol 7
  mySwitch.setPulseLength(605);
  mySwitch.setProtocol(7);
  mySwitch.setRepeatTransmit(7);
  mySwitch.send(4315972465486250 ,52);
  delay(20000);
  mySwitch.send(4315972465486165 ,52);   
}

Of course, you will need to test your changes with existing code you have - You change should not break existing code! In my case, my change broke the demo program!