Publish/Subscribe on single Photon?

Just received my first Photon.
Wanting to test subscribe and publish on single Photon.
What happens:
in setup() I define the subscribe:
success = Particle.subscribe(“dark”, myHandler, MY_DEVICES);
in loop() I publish:
success = Particle.publish(“dark”);

The subscription is executed correctly.
The publish command is executed correctly.
I expected function “myHandler” to be called. But that does not happen.

Is it not allowed to subscribe on your own published events?
Do I really need two Photons to achieve that?

Thanks in advance for your help!

Could you show us the entire code you’ve got? It’s a bit hard to tell what’s going on without being able to see it.

Also, why would you want to use that on a single device? Unless it’s for testing purposes, there are better ways to do whatever it is that you want that to do :wink:

1 Like

Here is the code based on the lightmeter demo from the Make book:

int reading = 0;
double volts = 0.0;
int analogPin = A0;
int led3 = D3; 
int led4 = D4; 
int led7 = D7; 
boolean highlight = false;
boolean lowlight = false;
boolean led3On = false;
boolean success;

int i = 0;

// Date: 03-01-2016
// Goal: test publish / subscibe mechanism using just one Photon
// Base program: light sensor test from p-09-lightmeter.ino from book
// Make: Getting Started with the Photon by Simon Monk
// Photon connected via USB to PC
// Use PuTTY to check Serial output
// Use 3 LEDs
void setup() {
    pinMode(led3, OUTPUT); // led 3 senses voltage differences between 1.1 and 2.8 Volt
    pinMode(led4, OUTPUT); // led 4 will lit if a subscribe or publish fails
    pinMode(led7, OUTPUT); // led 7 should react on received subscribe messages, but does not!!!!
    Serial.begin(9600);  // open serial over USB
    while (!Serial.available())  Particle.process(); // Start PuTTY and press any key
    Serial.println("Hello Computer!");
    RGB.control(true); //"RGB" stands for Red Green Blue
    Particle.variable("analog", &reading, INT);
    Particle.variable("volts", &volts, DOUBLE);
    success = Particle.subscribe("dark", lightD7, MY_DEVICES);
    if (!success) { 
            digitalWrite(led4, HIGH);  // led 4 will lit if subscribe fails
            Serial.printlnf("subscribe error dark = %f", success); 
    }
    else { 
        Serial.printlnf("subscribe OK dark = %d", success);  // returns: subscribe OK dark = 1
    }
    success = Particle.subscribe("light", dimD7, MY_DEVICES);
    if (!success) { 
            digitalWrite(led4, HIGH);  // led 4 will lit if subscribe fails
            Serial.printlnf("subscribe error light = %f", success); 
    }
    else {
        Serial.printlnf("subscribe OK light = %d", success);   // returns: subscribe OK light = 1
    }
}

void loop() {
  reading = analogRead(analogPin);
  volts = reading * 3.3 / 4096.0;
  if (volts 3.0 && highlight == false) {
      success = Particle.publish("dark");
      if (!success) { 
            digitalWrite(led4, HIGH); // led 4 will lit if publish fails
      }
      Serial.printlnf("Volts 3.0 = %f",volts,4); // typical return:  Volts 3.0 = 3.000293
      highlight = true;
      lowlight = false;
      RGB.color(255, 0, 0); //red
  }
  if (volts < 1.0 && lowlight == false) {
      success = Particle.publish("light");
      if (!success) { 
            digitalWrite(led4, HIGH); // led 4 will lit if publish fails
      }
      Serial.printlnf("Volts < 1.0 = %f", volts,4);  // typical return: Volts < 1.0 = 0.991772

      lowlight = true;
      highlight = false;
      RGB.color(0, 255, 0); // green
  }
  if (volts 1.1 && volts < 2.8) {
      RGB.color(0, 0, 255); // blue
      if (led3On == true) {
          digitalWrite(led3, HIGH); // results in Led 3 reacting on changing voltage
          led3On = false;
      }    
      else {
          digitalWrite(led3, LOW); 
          led3On = true;
      }
  }
  if (Serial.available()) {      
      int inByte = Serial.read();   // show key pressed on PuTTY
      Serial.write(inByte);
  }
}

// Subscribe functions
// Code copied from Particle docs
void lightD7(const char *event, const char *data)
{
  i++;
  Serial.print(i);
  Serial.print(event);
  Serial.print(", data: ");
  if (data)
    Serial.println(data);
  else
    Serial.println("NULL");
  digitalWrite(led7, HIGH); 
}

void dimD7(const char *event, const char *data) {
 digitalWrite(led7, LOW); 
}

// https://api.spark.io/v1/devices/44002e000347343337373739/analog?access_token=...
// https://api.spark.io/v1/devices/44002e000347343337373739/volts?access_token=fd949343...

//https://community.particle.io/t/publish-subscribe-on-single-photon/18730

Just received my first Photon.
Wanting to test subscribe and publish on single Photon.
What happens:
in setup() I define the subscribe:
success = Particle.subscribe("dark", myHandler, MY_DEVICES);
in loop() I publish:
success = Particle.publish("dark");

The subscription is executed correctly.
The publish command is executed correctly.
I expected function “myHandler” to be called. But that does not happen.

Is it not allowed to subscribe on your own published events?
Do I really need two Photons to achieve that?

Thanks in advance for your help!

Output from curl cmd window:

event: spark/status
data: {"data":"online","ttl":"60","published_at":"2016-01-03T08:44:53.298Z","coreid":"44002e000347343337373739"}

event: spark/device/app-hash
data: {"data":"B1C91686C9CDD1F53B80A530C6A591820636D8F98C3431F77B951E6AA697B778","ttl":"60","published_at":"2016-01-03T08:44:54.668Z","coreid":"44002e000347343337373739"}

event: dark
data: {"data":"null","ttl":"60","published_at":"2016-01-03T08:45:40.405Z","coreid":"44002e000347343337373739"}

event: light
data: {"data":"null","ttl":"60","published_at":"2016-01-03T08:45:46.478Z","coreid":"44002e000347343337373739"}

*/

If you subscribe with MY_DEVICES you have to publish PRIVATE.
The way you publish you’d have to subscribe without MY_DEVICES.

Thank you ScruffR: removing MY_DEVICES solved the problem.
I now have a means to check the mechanism in itself.
Thanks!