Photon Spark.publish() in RCSWITCH library

Hi all,

I’ve bought a RF receiver and transmitter on ebay. My goal is to receive data from a RF remote control, to eventually switch RF outlets with the transmitter.

I followed this tutorial which uses the RCSWITCH library.
I’ve hooked up the receiver like this:
Photon VIN ==> Receiver VCC
Photon GND ==> Receiver GND
Photon D3 <== Receiver DATA

When I use the RF remote, I can see a change in intensity in led D7, so I guess the receiver works fine?
But, I don’t see any data in the particle logs?

I guess the following piece of code receives the data and publishes it?

    void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {

      if (decimal == 0) {
        Serial.print("Unknown encoding.");
        Spark.publish("No data!");
      } else {
        char* b = mySwitch.dec2binWzerofill(decimal, length);
        char* tristate = bin2tristate(b);
    
        Serial.print("Decimal: ");
        Serial.print(decimal);
        Serial.print(" (");
        Serial.print( length );
        Serial.print("Bit) Binary: ");
        Serial.print( b );
        Serial.print(" Tri-State: ");
        Serial.print( tristate );
        Serial.print(" PulseLength: ");
        Serial.print(delay);
        Serial.print(" microseconds");
        Serial.print(" Protocol: ");
        Serial.println(protocol);
    
        Spark.publish("tristate-received", String(delay) + " " + String(tristate));
        Spark.publish("Data");
      }

I have added the Spark.publish("No data!"); and Spark.publish("Data!"); myself to test.
Still don’t see anything.

When I put Spark.publish("Random..."); in the "void loop() part it publishes it and is visible in the logs.

This is one of my first projects with this kind of things, I hope someone here can help me out with this!
Much thanks in advance!
Robbe

First, try not to used deprecated Spark.publish() but Particle.publish().
But why would D7 change intensity when you only use D3 and your code doesn’t seem to do anything with D7?

And for Particle.publish() you need to keep within the 1/sec rate limit, otherwise your publishes will be ignored after four events.

Are you seeing anything over Serial?

Hi,

I’ve only posted the part that I thought was relevant.
This is the full code (receivedemo-advanced.ino from RCSWITCH library):

    // This #include statement was automatically added by the Particle IDE.
    #include "RCSwitch/RCSwitch.h"
    
    /*
      Example for receiving
    
      https://github.com/suda/RCSwitch
    
      If you want to visualize a telegram copy the raw data and
      paste it into http://test.sui.li/oszi/
    
      Connect receiver to D3 pin and open serial connection @ 9600 bauds or listen
      Spark Cloud for "tristate-received" event.
    */
    
    #include "RCSwitch/RCSwitch.h"
    
    RCSwitch mySwitch = RCSwitch();
    int ledPin = D7;
    int inputPin = D3;
    
    static char *bin2tristate(char *bin) {
      char returnValue[50];
      for (int i=0; i<50; i++) {
        returnValue[i] = '\0';
      }
      int pos = 0;
      int pos2 = 0;
      while (bin[pos]!='\0' && bin[pos+1]!='\0') {
        if (bin[pos]=='0' && bin[pos+1]=='0') {
          returnValue[pos2] = '0';
        } else if (bin[pos]=='1' && bin[pos+1]=='1') {
          returnValue[pos2] = '1';
        } else if (bin[pos]=='0' && bin[pos+1]=='1') {
          returnValue[pos2] = 'F';
        } else {
          return "not applicable";
        }
        pos = pos+2;
        pos2++;
      }
      returnValue[pos2] = '\0';
      return returnValue;
    }
    
    void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {
    
      if (decimal == 0) {
        Serial.print("Unknown encoding.");
        Particle.publish("No data!");
      } else {
        char* b = mySwitch.dec2binWzerofill(decimal, length);
        char* tristate = bin2tristate(b);
    
        Serial.print("Decimal: ");
        Serial.print(decimal);
        Serial.print(" (");
        Serial.print( length );
        Serial.print("Bit) Binary: ");
        Serial.print( b );
        Serial.print(" Tri-State: ");
        Serial.print( tristate );
        Serial.print(" PulseLength: ");
        Serial.print(delay);
        Serial.print(" microseconds");
        Serial.print(" Protocol: ");
        Serial.println(protocol);
    
        Particle.publish("tristate-received", String(delay) + " " + String(tristate));
        Particle.publish("Data");
      }
    
    Serial.print("Raw data: ");
    for (int i=0; i<= length*2; i++) {
    Serial.print(raw[i]);
    Serial.print(",");
    }
    Serial.println();
    Serial.println();
    }
    
    
    
    void setup() {
      Serial.begin(9600);
      Serial.println("Listening");
    
      pinMode(inputPin, INPUT_PULLDOWN);
      mySwitch.enableReceive(inputPin);
    
      pinMode(ledPin, OUTPUT);
    }
    
    void loop() {
      int inputPinState = digitalRead(inputPin);
      digitalWrite(ledPin, inputPinState);
    
      if (mySwitch.available()) {
        output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(), mySwitch.getReceivedProtocol());
        mySwitch.resetAvailable();
      }
    }

At the bottom you can see it maps the input pin D3 to output pin D7.

I have tried Particle.publish() also but didn’t work either.
How should I limit the publish rate here? When I use the delay() function it seems to pause the program and won’t receive anything?

Can the publish rate have anything to do with not showing up in the logs?

No nothing, I’m not sure if I’m doing it correctly though…
I need to put it in listening mode right?

If I don’t put it in listening mode Putty can’t make a connection.
If I put it in listening mode, Putty makes a connection and doesn’t show anything until I send “i” for example, then it shows some information… Is the program still running in listening mode?

Having recently tried that library, I seem to recall it outputs a lot of data, very quickly, thus exceeding the rate limit almost immediately. Have a look with a serial monitor, that ought to help clarify whether or not it’s working as intended.

You shouldn’t be in listening mode. Having the Serial.begin() should make it work as-is.

This is in the code:

void setup() {
  Serial.begin(9600);
  Serial.println("Listening");

  pinMode(inputPin, INPUT_PULLDOWN);
  mySwitch.enableReceive(inputPin);

  pinMode(ledPin, OUTPUT);
}

So I guess that’s fine and I should at least see “Listening”?
When not in listening mode, all I see is a black screen that doesn’t even respond to “i”… Any ideas why?

You should open the connection only after the device has booted. At that point, you’re already too late to see the ‘listening’. Try placing some prints in the loop to see if it works.
You could try a different serial monitor to rule out issues with that. The arduino IDE has one, and so does Particle Dev, as well as the CLI.

When I put Serial.println("Hello"); in the loop it keep showing “Hello” in Putty so that works…
You have used the library before, did you get it working? What did you see in the serial monitor?

Also when I put Serial.println(“Void Output”) in the void output (where it should output the received info), it doesn’t show anything in the serial monitor… I really don’t understand why…

void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {

  **Serial.println("Void Output");**
  if (decimal == 0) {
    Serial.print("Unknown encoding.");
    Particle.publish("No data!");
  } else {
    char* b = mySwitch.dec2binWzerofill(decimal, length);
    char* tristate = bin2tristate(b);

    Serial.print("Decimal: ");
    Serial.print(decimal);
    Serial.print(" (");
    Serial.print( length );
    Serial.print("Bit) Binary: ");
    Serial.print( b );
    Serial.print(" Tri-State: ");
    Serial.print( tristate );
    Serial.print(" PulseLength: ");
    Serial.print(delay);
    Serial.print(" microseconds");
    Serial.print(" Protocol: ");
    Serial.println(protocol);

    Particle.publish("tristate-received", String(delay) + " " + String(tristate));
    Particle.publish("Data");
  }

Serial.print("Raw data: ");
for (int i=0; i<= length*2; i++) {
Serial.print(raw[i]);
Serial.print(",");
}
Serial.println();
Serial.println();
}

Hi Robbe were you able to find the root cause of this issue or get a fix. I have been stuck with the same issue for a few days and it’s been driving me crazy.
I know there is activity as D7 blinks like crazy whenever I operate my RF switch but I don’t get a read on the variable I am publishing to the web IDE.