Getting Photon to send string variables to other Photon

I am wanting to send a variable string from one Photon to another. I know there is the Spark.variable that can do that, but it doesn’t send it to the other Photon, you have to access it with a browser. Is my task possible using publish/subscribe? Or is there something else I need to do?

@Leon, that is one of the reasons why Spark.publish() and Spark.subscribe() exist! So yes, your task is actually BEST done with those functions IF you use the Particle Cloud. :smile:

@peekay123, First of all thank you for the quick response, and secondly would you mind giving me a hand?
I have this for my publishing code:

int incomingByte;


void setup() {
  pinMode(D7,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  String content = "";
  char character;
/*  if (Serial.available() > 0) {
    // read the incoming byte:
    //lcd.print("I received: ");
    incomingByte = Serial.read();
    //lcd.println(char(incomingByte));
   // say what you got:
  } */
  while(Serial1.available() > 0) {
      character = Serial1.read();
      content.concat(character);
      delay(10);
  }
  
  Spark.publish("pubdata", content , PRIVATE)
} 

and this for my receiving code:

int i = 0;

void myHandler(const char *event, const char *content)
{
  i++;
  //Serial.print(i);
  //Serial.print(event);
  //Serial.print(", data: ");
  if (content)
    Serial.println(content);
  else
    Serial.println("NULL");
}

void setup()
{
  Spark.subscribe("pub", myHandler, MY_DEVICES);
  Serial.begin(9600);
}

So using this I was unable to get the second one to receive what the first one was sending and I am unsure why.

@Leon, first I need to point out that the cloud only allows 1 publish per second with bursts of 4 per second (only once). You need to consider that in your loop.

As for the receiver, notice that you publish the event “pubdata” but subscribe to the event “pub”. :wink:

@peekay123
Subscribing to “pub” also encompasses “pubdata” since it begins with pub. I have sent information using these variable before and that worked. And ah I forgot about the 1 publish per second. Let me slow down the rate at which it sends.

@Leon, good point on the filter! The issue may be that you swamped the Cloud with Spark.publish() in loop() and the more you send, the bigger the more it gets delayed!

@peekay123 the delay seems to have worked! I must have been flooding the cloud.
Thank you for the help.

2 Likes